Efficient LLM Inference over Long Sequences
See the codeThis repository contains code for the paper Star Attention: Efficient LLM Inference over Long Sequences. Star Attention is a novel block-sparse attention mechanism designed to enable efficient inference on long sequences in transformer-based LLMs. The method operates in two phases:
Star Attention improves the inference time by up to 11x while preserving 97-100% of accuracy. The method is compatible with most Transformer-based LLMs trained with global attention, operating seamlessly out-of-the-box without additional training/finetuning. Furthermore, Star Attention is orthogonal to other optimization methods, including Flash Attention and KV cache compression techniques, allowing for potential combined enhancements.
This codebase contains the implementation of Star Attention in PyTorch using the HuggingFace Transformers library, along with the code for launching inference with Star Attention on two benchmarks: RULER and BABILong.
| Model | Seq. Len. (K) | Block Size (K) | Ring-Attn Acc. (%) | Star-Attn | |
|---|---|---|---|---|---|
| Δ Acc. | Δ Speedup | ||||
| meta-llama Llama3.1-8B-Instruct | 16 | 4 | 92.22 | -0.94% | 1.1x |
| 32 | 8 | 87.53 | +1.17% | 1.2x | |
| 64 | 16 | 84.79 | -1.42% | 1.8x | |
| 128 | 32 | 76.31 | -1.90% | 2.7x | |
| meta-llama Llama-3.1-70B-Instruct | 16 | 4 | 95.09 | -2.71% | 1.7x |
| 32 | 8 | 94.61 | -2.55% | 2.0x | |
| 64 | 16 | 88.54 | -1.44% | 4.7x | |
Table 1: Accuracy and relative inference speedup of Star Attention compared to Ring Attention on RULER across sequence lengths from 16K to 128K. Accuracy is reported as the absolute difference from Ring Attention and speedup reflects relative improvements in inference efficiency. Star Attention significantly accelerates inference with minimal accuracy loss.
Figure 1: Accuracy comparison of Star Attention and Global Attention on RULER and BABILong from 16K to 128K sequence lengths using various models. All runs use a block and anchor block size set to one-quarter of the total sequence length. Star Attention maintains 97-100% of the accuracy of global attention, and in some cases, even outperform it.
Install all the project dependencies with
$ pip install -r requirements.txt
In a python shell, download the punkt tokenizer from the nltk library:
import nltk
nltk.download('punkt_tab')
To generate synthetic data for RULER, you need to download:
To download these data, run:
$ bash ruler/download_data.sh
To download a model from HuggingFace, use the script: scripts/download_hf_model.py.
NOTE: For certain models, you might need to input the huggingface hub token from your account settings via the --token flag.
This repository contains code for launching inference with Star Attention on two benchmarks: RULER and BABILong. The instructions to run each of those benchmarks are shared in the following subsections.
To run inference on RULER, use the script: run_ruler.py.
Usage:
$ python run_ruler.py \
-n <experiment_name> \
-p <path_to_model> \
-pc <prompt_template_type> \
-a star \
-bs <context_block_size> \
-l <list_of_sequence_lengths_to_run_inference> \
-np <num_parallel_processes_per_node> \
--output_dir <output_directory>
After running the evaluations, you can display all the results together using the script:
$ python ruler/gather_results_ruler.py \
-e <path_to_results_directory>
-np flagThe -np flag specifies the number of parallel processes (hosts) to use for running inference. For example, if your machine has 8 GPUs:
-np 8: Launch 8 hosts, each host assigned a single GPU.-np 4: Launch 4 hosts, each host assigned 2 GPUs.This is useful when you want to run star attention with bigger context block sizes or with bigger models where assigning a single GPU per host leads to out-of-memory errors.
To see an example of how to run this script with different configurations, check launch.sh.
-nn flag for Multi-Node InferenceIf you have a multi-node setup (such as a slurm cluster), then you can add the -nn <num_nodes> for running multi-node inference. The script will launch a total of nn * np processes (hosts) for inference.
For example, in a system with 8 GPUs on each node, if -nn 1 and -np 4 are specified, the script will launch 4 processes (hosts) on a single node. This means that each host is allocated 2 GPUs to load the model. If the model or the block size is too large, you can scale the nn and the np parameters accordingly. If -nn 2 and -np 2, then the script will launch a total of 4 processes (hosts) across 2 nodes, with each host containing 4 GPUs.
To run inference on BABILong, use the script: run_babilong.py.
The script takes in the same set of arguments as the RULER script described above. To see an example of how to run this script with different configurations, check launch.sh.
After running the evaluations, you can display all the results together using the script:
$ python babilong/gather_results_babilong.py \
-e <path_to_results_directory>
To run inference on your custom data, use the script: run_star_attn_inference.py. The scripts takes in the input data in .jsonl format in which each line of jsonl should look like:
{
"index": "<sample index>", # optional
"input_context": "<long context portion of the input sample>",
"input_query": "<query portion of the input sample>",
"output": "<expected output response>",
}
Script usage:
$ python run_star_attn_inference.py \
--model_path <path_to_model> \
--attn_type star \
--block_size <context_block_size> \
--tokens_to_generate <num_tokens_to_generate> \
--stop_words <end_of_sequence_tokens> \
--input_path <path_to_input_jsonl> \
--output_path <path_to_output_jsonl>
For more details on the script arguments, run:
$ python run_star_attn_inference.py --help
Given a system with $H$ hosts and an input sample with context $c$ followed by query $q$, Star Attention operates in two phases:
This method ensures that attention scores are correctly normalized across all hosts, requiring only the communication of a single scalar (the sum of exponents, $s_h$) and a vector (the local attention output, $A_h$) per token.
@inproceedings{
acharya2025starattention,
title={Star Attention: Efficient {LLM} Inference over Long Sequences},
author={Shantanu Acharya and Fei Jia and Boris Ginsburg},
booktitle={Forty-second International Conference on Machine Learning (ICML)},
year={2025},
}
If you need any help or want to report a bug, feel free to raise an issue in the repo.
Python
98.5%
Shell
1.5%
Efficient LLM Inference over Long Sequences
See the codeThis repository contains code for the paper Star Attention: Efficient LLM Inference over Long Sequences. Star Attention is a novel block-sparse attention mechanism designed to enable efficient inference on long sequences in transformer-based LLMs. The method operates in two phases:
Star Attention improves the inference time by up to 11x while preserving 97-100% of accuracy. The method is compatible with most Transformer-based LLMs trained with global attention, operating seamlessly out-of-the-box without additional training/finetuning. Furthermore, Star Attention is orthogonal to other optimization methods, including Flash Attention and KV cache compression techniques, allowing for potential combined enhancements.
This codebase contains the implementation of Star Attention in PyTorch using the HuggingFace Transformers library, along with the code for launching inference with Star Attention on two benchmarks: RULER and BABILong.
| Model | Seq. Len. (K) | Block Size (K) | Ring-Attn Acc. (%) | Star-Attn | |
|---|---|---|---|---|---|
| Δ Acc. | Δ Speedup | ||||
| meta-llama Llama3.1-8B-Instruct | 16 | 4 | 92.22 | -0.94% | 1.1x |
| 32 | 8 | 87.53 | +1.17% | 1.2x | |
| 64 | 16 | 84.79 | -1.42% | 1.8x | |
| 128 | 32 | 76.31 | -1.90% | 2.7x | |
| meta-llama Llama-3.1-70B-Instruct | 16 | 4 | 95.09 | -2.71% | 1.7x |
| 32 | 8 | 94.61 | -2.55% | 2.0x | |
| 64 | 16 | 88.54 | -1.44% | 4.7x | |
Table 1: Accuracy and relative inference speedup of Star Attention compared to Ring Attention on RULER across sequence lengths from 16K to 128K. Accuracy is reported as the absolute difference from Ring Attention and speedup reflects relative improvements in inference efficiency. Star Attention significantly accelerates inference with minimal accuracy loss.
Figure 1: Accuracy comparison of Star Attention and Global Attention on RULER and BABILong from 16K to 128K sequence lengths using various models. All runs use a block and anchor block size set to one-quarter of the total sequence length. Star Attention maintains 97-100% of the accuracy of global attention, and in some cases, even outperform it.
Install all the project dependencies with
$ pip install -r requirements.txt
In a python shell, download the punkt tokenizer from the nltk library:
import nltk
nltk.download('punkt_tab')
To generate synthetic data for RULER, you need to download:
To download these data, run:
$ bash ruler/download_data.sh
To download a model from HuggingFace, use the script: scripts/download_hf_model.py.
NOTE: For certain models, you might need to input the huggingface hub token from your account settings via the --token flag.
This repository contains code for launching inference with Star Attention on two benchmarks: RULER and BABILong. The instructions to run each of those benchmarks are shared in the following subsections.
To run inference on RULER, use the script: run_ruler.py.
Usage:
$ python run_ruler.py \
-n <experiment_name> \
-p <path_to_model> \
-pc <prompt_template_type> \
-a star \
-bs <context_block_size> \
-l <list_of_sequence_lengths_to_run_inference> \
-np <num_parallel_processes_per_node> \
--output_dir <output_directory>
After running the evaluations, you can display all the results together using the script:
$ python ruler/gather_results_ruler.py \
-e <path_to_results_directory>
-np flagThe -np flag specifies the number of parallel processes (hosts) to use for running inference. For example, if your machine has 8 GPUs:
-np 8: Launch 8 hosts, each host assigned a single GPU.-np 4: Launch 4 hosts, each host assigned 2 GPUs.This is useful when you want to run star attention with bigger context block sizes or with bigger models where assigning a single GPU per host leads to out-of-memory errors.
To see an example of how to run this script with different configurations, check launch.sh.
-nn flag for Multi-Node InferenceIf you have a multi-node setup (such as a slurm cluster), then you can add the -nn <num_nodes> for running multi-node inference. The script will launch a total of nn * np processes (hosts) for inference.
For example, in a system with 8 GPUs on each node, if -nn 1 and -np 4 are specified, the script will launch 4 processes (hosts) on a single node. This means that each host is allocated 2 GPUs to load the model. If the model or the block size is too large, you can scale the nn and the np parameters accordingly. If -nn 2 and -np 2, then the script will launch a total of 4 processes (hosts) across 2 nodes, with each host containing 4 GPUs.
To run inference on BABILong, use the script: run_babilong.py.
The script takes in the same set of arguments as the RULER script described above. To see an example of how to run this script with different configurations, check launch.sh.
After running the evaluations, you can display all the results together using the script:
$ python babilong/gather_results_babilong.py \
-e <path_to_results_directory>
To run inference on your custom data, use the script: run_star_attn_inference.py. The scripts takes in the input data in .jsonl format in which each line of jsonl should look like:
{
"index": "<sample index>", # optional
"input_context": "<long context portion of the input sample>",
"input_query": "<query portion of the input sample>",
"output": "<expected output response>",
}
Script usage:
$ python run_star_attn_inference.py \
--model_path <path_to_model> \
--attn_type star \
--block_size <context_block_size> \
--tokens_to_generate <num_tokens_to_generate> \
--stop_words <end_of_sequence_tokens> \
--input_path <path_to_input_jsonl> \
--output_path <path_to_output_jsonl>
For more details on the script arguments, run:
$ python run_star_attn_inference.py --help
Given a system with $H$ hosts and an input sample with context $c$ followed by query $q$, Star Attention operates in two phases:
This method ensures that attention scores are correctly normalized across all hosts, requiring only the communication of a single scalar (the sum of exponents, $s_h$) and a vector (the local attention output, $A_h$) per token.
@inproceedings{
acharya2025starattention,
title={Star Attention: Efficient {LLM} Inference over Long Sequences},
author={Shantanu Acharya and Fei Jia and Boris Ginsburg},
booktitle={Forty-second International Conference on Machine Learning (ICML)},
year={2025},
}
If you need any help or want to report a bug, feel free to raise an issue in the repo.
Python
98.5%
Shell
1.5%