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

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

arXiv:2502.20766 · 2026-07-27 공개 · arXiv · PDF

llm-inference sparse-attention efficiency long-sequence context-aware query-aware jensen-shannon-divergence flexprefill

Abstract

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.

한국어 요약

한 줄 요약

FlexPrefill은 입력에 따라 실시간으로 스파스 어텐션 패턴을 조정하여, 긴 시퀀스 추론의 효율성을 향상시키는 새로운 메커니즘이다.

핵심 기여도

핵심 아이디어

기존 스파스 어텐션 메커니즘은 고정된 패턴을 사용하거나 제한된 상황에서만 스파스 패턴을 결정하여, 다양한 입력에 대한 유연한 대응이 어려웠다. FlexPrefill은 입력에 따라 어텐션 헤드별로 최적의 스파스 패턴을 실시간으로 결정하는 두 가지 핵심 모듈을 도입한다.

첫 번째, **Query-Aware Sparse Pattern Determination**은 Jensen-Shannon divergence를 사용하여 예측된 어텐션 분포와 실제 분포 간의 차이를 측정하고, 이에 따라 어텐션 헤드가 'Diverse' 또는 'Structured' 패턴 중 하나를 선택한다.

두 번째, **Cumulative-Attention Based Index Selection**은 어텐션 점수의 누적 합이 사전 정의된 임계값에 도달할 때까지 키-쿼리 인덱스를 동적으로 선택함으로써, 계산 예산을 효율적으로 할당한다. 이는 어텐션 헤드별로 스파스 비율을 조절하면서도 모델 성능을 유지하는 데 기여한다.

기술적 접근법

주요 결과

의의 및 한계

FlexPrefill은 고정된 스파스 패턴이 아닌, 입력에 따라 실시간으로 어텐션 계산을 최적화함으로써, 긴 시퀀스 추론의 효율성을 획기적으로 향상시킨다. 특히, 어텐션 헤드별로 다른 스파스 비율을 적용할 수 있어, 모델 성능과 계산 효율성 사이의 균형을 유지하는 데 기여한다.

그러나, FlexPrefill은 입력에 따라 실시간으로 스파스 패턴을 결정하는 과정에서 추가적인 계산 비용이 발생할 수 있으며, 이는 일부 실시간 응용에서는 한계가 될 수 있다. 또한, 현재 실험은 특정 LLM과 데이터셋에만 적용되었기 때문에, 다양한 모델 아키텍처와 상황에서의 일반화 가능성은 추가 연구가 필요하다.

실용적 활용

FlexPrefill은 긴 시퀀스를 다루는 대형 언어 모델의 추론 과정에서 유용하게 활용될 수 있다. 특히, 문서 요약, 대화형 챗봇, 코드 생성 등에서 실시간 성능 향상과 정확도 유지가 중요한 상황에 적합하다. 또한, 컴퓨팅 자원이 제한된 모바일 또는 임베디드 환경에서도 유연한 스파스 계산을 통해 효율적인 추론이 가능하다.