๐ฏ DAM: Dynamic Attention Mask for Long-Context LLM Inference Acceleration
June 16, 2025 ยท View on 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
Comparison of attention patterns: (a) full attention, (b) static sparse attention, (c) predefined patterns, and (d) DAM's dynamic heterogeneous patterns.
๐ฌ Methodology Overview
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.
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
| Feature | Description |
|---|---|
| ๐ฏ Dynamic Sparse Attention | Learns adaptive, context-aware sparse masks for each attention map |
| ๐ Zero Fine-Tuning | Works with frozen pretrained models; no retraining required |
| ๐ Scalable Architecture | Efficiently extends to long contexts beyond hardware memory limits |
| ๐ฏ High Accuracy | Maintains performance close to full attention on benchmarks |
| โก Optimized Kernels | Custom 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
Retrieval accuracy on LongEval benchmark (3.1k to 38.7k tokens). DAM maintains consistent performance while baselines degrade.
Model Comparison
Detailed comparison for LLaMA 3.2 models. DAM closely matches dense attention across various positions and sequence lengths.
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