Papers › FlexPrefill: A Context-Aware Sparse Attention Mechanism for Efficient Long-Sequence Inference

FlexPrefill: A Context-Aware Sparse Attention Mechanism for Efficient Long-Sequence Inference

28 Feb 2025arXiv:2502.20766archive 2025-07-28

Xunhao Lai, Jianqiao Lu, Yao Luo, Yiyuan Ma, Xun Zhou

Large language models (LLMs) encounter computational challenges during long-sequence inference, especially in the attention pre-filling phase, where the complexity grows quadratically with the prompt length. Previous efforts to mitigate these challenges have relied on fixed sparse attention patterns or identifying sparse attention patterns based on limited cases. However, these methods lacked the flexibility to efficiently adapt to varying input demands. In this paper, we introduce FlexPrefill, a Flexible sparse Pre-filling mechanism that dynamically adjusts sparse attention patterns and computational budget in real-time to meet the specific requirements of each input and attention head. The flexibility of our method is demonstrated through two key innovations: 1) Query-Aware Sparse Pattern Determination: By measuring Jensen-Shannon divergence, this component adaptively switches between query-specific diverse attention patterns and predefined attention patterns. 2) Cumulative-Attention Based Index Selection: This component dynamically selects query-key indexes to be computed based on different attention patterns, ensuring the sum of attention scores meets a predefined threshold. FlexPrefill adaptively optimizes the sparse pattern and sparse ratio of each attention head based on the prompt, enhancing efficiency in long-sequence inference tasks. Experimental results show significant improvements in both speed and accuracy over prior methods, providing a more flexible and efficient solution for LLM inference.

PaperPDFCodeCode Syntology ran

In Syntology Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.

For agents, Syntology's MCP tool lists every function and class Syntology harvested from this paper and whether it ran (how to connect): get_harvested_code_for_paper(arxiv_id="2502.20766")

Code

Syntology Ran 1 of 11 code samples harvested from 2 repositories linked to this paper; 10 have no recorded run. Of those that ran: 1 ran · honoured contract.

By repository: official repository: 10 samples from 1 repository, 0 ran; community: 1 sample from 1 repository, 1 ran. The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

bytedance/FlexPrefill officialmentioned in papermentioned on GitHubpytorchApache-2.0 report

Repository list and official/mentioned flags are the archive's, frozen 2025-07-28. Reachability, where shown, is from one Syntology probe window (2026-09-16 to 2026-09-18); repositories not probed show nothing. GitHub stars are not tracked.

Code Syntology ran Syntology

11 samples harvested; 1 ran; 1 honoured the contract we drafted; 10 have no recorded run. Read from Syntology's graph 2026-09-24; that is when this build read the record, not when the samples ran.

1ran · honoured contract
10unverified

Licence: 0 of the 11 samples are pointer only, meaning Syntology does not serve that copy's text. This page shows no code text for any sample; each one links to its file in the repository.

Harvested from 2 repositories linked to this paper, official or community; each sample names its own and says which. “Ran” means the sample executed on a synthesized input. It does not mean the output is correct, and nothing here reproduces the paper's results. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself; “our draft was wrong” and “fixture could not drive it” are failures of Syntology's instrument, not of the code.

Each sample ends with its code_sha256, Syntology's identity for that exact code. An agent fetches the stored sample with Syntology's MCP tool get_code(code_sha256="…") (how to connect); click an identity to copy that call.

Repository labels, per sample. official repository: The archive marks this repository official for the paper. named in the paper: The archive records that the paper mentions this repository; it is not marked official. community (archive-listed): In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper. found in paper text by Syntology: Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted. community: Not in the archive's code links for this paper; a community repository Syntology harvested. Samples from a repository marked official are listed first. Licence labels name the repository's licence as recorded at harvest. “Pointer only” means Syntology does not serve that copy's text, for one of four reasons: no licence file was found; the licence was not identified; the licence is recorded as permissive but that copy's record is not marked cleared; or the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar). Some licences outside that list permit redistribution, such as WTFPL, and GPL-3.0 under its conditions; they are simply not on the list. Hover a licence label for the reason. File links open the file on GitHub at the default branch, which may have changed since the harvest.

apply_rotary_pos_emb bytedance/FlexPrefill/flex_prefill/modules/glm/glm_self_attention_foward.py official repository unverified Apache-2.0 (permissive) · 059a805ecbeca6af · report
get_config_example bytedance/FlexPrefill/flex_prefill/modules/patch.py official repository unverified Apache-2.0 (permissive) · 78e17a79809e571b · report
get_dataloader bytedance/FlexPrefill/experiments/benchmark/run_ruler.py official repository unverified Apache-2.0 (permissive) · 6904e1292ef34ff1 · report
get_tasks bytedance/FlexPrefill/experiments/benchmark/run_ruler.py official repository unverified Apache-2.0 (permissive) · 0ff1542f9a950e79 · report
glm_mlp_forward bytedance/FlexPrefill/flex_prefill/modules/glm/glm_mlp_forward.py official repository unverified Apache-2.0 (permissive) · 5f120e3472fb5048 · report
glm_self_attention_forward bytedance/FlexPrefill/flex_prefill/modules/glm/glm_self_attention_foward.py official repository unverified Apache-2.0 (permissive) · 1c80690cb1a8743d · report
mid_trunc_and_pad bytedance/FlexPrefill/experiments/benchmark/utils.py official repository unverified Apache-2.0 (permissive) · 1bd20ca4f6cfb4eb · report
remove_duplicates_by_index bytedance/FlexPrefill/experiments/benchmark/run_ruler.py official repository unverified Apache-2.0 (permissive) · 2742a6153e7ec429 · report
split_tensor_along_last_dim bytedance/FlexPrefill/flex_prefill/modules/glm/glm_self_attention_foward.py official repository unverified Apache-2.0 (permissive) · 08b693a1068fa690 · report
str_to_dict bytedance/FlexPrefill/experiments/benchmark/utils.py official repository unverified Apache-2.0 (permissive) · 35b094727218cc43 · report
get_num_warps_stages ByteDance-Seed/FlexPrefill/flex_prefill/ops/flex_prefill_attention.py community ran · honoured contract fingerprinted Apache-2.0 (permissive) · c7bbee93a299753b · report

Results from the paper archive 2025-07-28

No leaderboard rows for this paper in the archive.

Methods

AttentionSPEEDSoftmax

Report a problem or propose a change · a person checks every report against the paper or source before anything changes; decisions are listed on /corrections