๐ŸŽฏ DAM: Dynamic Attention Mask for Long-Context LLM Inference Acceleration

June 16, 2025 ยท View on GitHub

A Novel Framework for Efficient Long-Context Inference in Large Language Models

GitHub


๐Ÿ“– Abstract

DAM (Dynamic Attention Mask) introduces a breakthrough approach to long-context inference in large language models. Unlike traditional sparse attention methods that rely on static, predefined patterns, DAM dynamically learns adaptive attention masks at the granularity of individual attention maps. This preserves the heterogeneous patterns across different layers and heads while significantly reducing computational overhead.

Key Innovation: DAM eliminates the need for fine-tuning by learning context-aware attention structures from frozen pretrained models, making it immediately applicable to existing LLMs without modification.

Authors: Hanzhi Zhang, Heng Fan, Kewei Sha, Yan Huang, Yunhe Feng

Attention Patterns Comparison
Comparison of attention patterns: (a) full attention, (b) static sparse attention, (c) predefined patterns, and (d) DAM's dynamic heterogeneous patterns.

๐Ÿ”ฌ Methodology Overview

DAM Framework Architecture
Two-stage DAM framework: Pattern extraction and transformation (Stage 1) followed by efficient sparse inference (Stage 2).

DAM operates through a two-stage framework that dynamically learns sparse attention patterns:

๐Ÿ” Stage 1: Pattern Extraction

DAM first extracts attention patterns from a frozen pretrained model processing sequences up to Pattern Capture Length (PCL). The baseline attention computation follows:

S = QK^T / โˆšd_k

where Q โˆˆ โ„^(nร—d_k) and K โˆˆ โ„^(mร—d_k) are query and key matrices.

Feature Amplification via Box-Cox Transformation: To enhance pattern visibility, we apply Box-Cox transformation to mean attention scores:

B_โ„“,h,i,j = { (X_โ„“,h,i,j^ฮป - 1) / ฮป,  if ฮป โ‰  0
            { ln(X_โ„“,h,i,j),           if ฮป = 0

where X_โ„“,h,i,j = max(ฤ€_โ„“,h,i,j, ฮต) are the stabilized mean attention values.

True Mask Generation: Binary masks are generated through thresholding:

m_i,j = { 1, if รƒ_โ„“,h,i,j โ‰ฅ ฯ„
        { 0, if รƒ_โ„“,h,i,j < ฯ„

where ฯ„ is the threshold parameter and รƒ_โ„“,h,i,j are the normalized attention values.

Dynamic Pattern Visualization
Box-Cox transformation (bottom) enhances pattern visibility compared to averaging (top), revealing heterogeneous structures in attention maps.

โšก Stage 2: Sparse Inference

Dynamic Mask Generation via Pattern Matching: For sequences longer than PCL, we use structural pattern matching. Each pattern P_k is compared against true masks M_โ„“,h using similarity scores:

ฮณ_k = (ฮฃ_i,j M_โ„“,h(i,j) ยท P_k(i,j)) / (ฮฃ_i,j P_k(i,j))

A pattern is matched if ฮณ_k โ‰ฅ ฮผ, where ฮผ is the matching threshold.

Extended Mask Construction: The final extended mask combines all matched patterns:

Mฬƒ_โ„“,h = ฮฃ_{P_k โˆˆ P, ฮณ_k โ‰ฅ ฮผ} P_k

Sparse Attention Application: The sparse attention is computed as:

A'_โ„“,h = (Q_โ„“,h K_โ„“,h^T / โˆšd_k) โŠ™ Mฬƒ_โ„“,h

where โŠ™ denotes element-wise multiplication, effectively setting masked positions to -โˆž before softmax normalization.


โœจ Key Features

FeatureDescription
๐ŸŽฏ Dynamic Sparse AttentionLearns adaptive, context-aware sparse masks for each attention map
๐Ÿš€ Zero Fine-TuningWorks with frozen pretrained models; no retraining required
๐Ÿ“ˆ Scalable ArchitectureEfficiently extends to long contexts beyond hardware memory limits
๐ŸŽฏ High AccuracyMaintains performance close to full attention on benchmarks
โšก Optimized KernelsCustom Triton kernels for efficient sparse computation

๐Ÿ“Š Key Results

DAM demonstrates superior performance across multiple benchmarks and model sizes:

  • ๐ŸŽฏ Accuracy: Maintains 79.66% average accuracy on LongEval (vs. 80.11% for full attention)
  • โšก Efficiency: Enables 8K token inference where full attention fails (OOM)
  • ๐Ÿ“ˆ Scalability: Processes sequences up to 64K tokens with minimal degradation
  • ๐Ÿ”ง Compatibility: Works across different model sizes (1B, 3B, 7B parameters)

Long-Context Performance

Long-Context Performance
Retrieval accuracy on LongEval benchmark (3.1k to 38.7k tokens). DAM maintains consistent performance while baselines degrade.

Model Comparison

Model Comparison Results
Detailed comparison for LLaMA 3.2 models. DAM closely matches dense attention across various positions and sequence lengths.

Benchmark Evaluation

Benchmark Evaluation
LV-Eval scores on long-context QA tasks. DAM achieves 18.61 at 64K tokens, significantly outperforming alternatives.

๐Ÿ“ Citation

If you find DAM useful in your research, please cite our work:

@misc{zhang2025damdynamicattentionmask,
      title={DAM: Dynamic Attention Mask for Long-Context Large Language Model Inference Acceleration}, 
      author={Hanzhi Zhang and Heng Fan and Kewei Sha and Yan Huang and Yunhe Feng},
      year={2025},
      eprint={2506.11104},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2506.11104}, 
}

๐Ÿ™ Acknowledgements

Responsible AI Lab, University of North Texas

Built with ๐Ÿค— HuggingFace Transformers โ€ข Triton โ€ข PyTorch


For more details, see our project paper