---
title: "Flex Attention — paper by Dong et al. (2024)"
description: "Its hand-built design, however, covers only a few popular variants. 23 references, cited by 52 papers here."
authors: ["Juechu Dong", "Boyuan Feng", "Driss Guessous", "Yanbo Liang", "Horace He"]
year: 2024
venue: "arXiv:2412.05496"
source: "https://arxiv.org/abs/2412.05496"
doi: "10.48550/arxiv.2412.05496"
arxiv: "2412.05496"
licence: "CC BY 4.0 (arXiv)"
licence_url: "http://creativecommons.org/licenses/by/4.0/"
summary_licence: "Elamur Cosmic License"
canonical: "https://research.elamur.ai/free/en/papers/dong-2024-flex-attention-programming-model-generating/"
---
[Elamur Research](https://md.research.elamur.ai/free/en/) · [this page in HTML](https://research.elamur.ai/free/en/papers/dong-2024-flex-attention-programming-model-generating/)

[home](https://md.research.elamur.ai/free/en/) / [papers](https://md.research.elamur.ai/free/en/papers/) / [2024](https://md.research.elamur.ai/free/en/papers/2024/page/7/) / Flex Attention: A Programming Model for Generating Optimized Attention Kernels

# Flex Attention: A Programming Model for Generating Optimized Attention Kernels

**Authors:** Juechu Dong, Boyuan Feng, Driss Guessous, Yanbo Liang and [Horace He](https://md.research.elamur.ai/free/en/authors/horace-he/) · **Year:** 2024 · **Venue:** arXiv:2412.05496 · **Licence:** [CC BY 4.0 (arXiv)](http://creativecommons.org/licenses/by/4.0/)

**Links:** [Publisher's page](https://arxiv.org/abs/2412.05496) · [PDF](https://arxiv.org/pdf/2412.05496) · [DOI 10.48550/arxiv.2412.05496](https://doi.org/10.48550/arxiv.2412.05496) · [arXiv 2412.05496](https://arxiv.org/abs/2412.05496) · [OpenAlex](https://openalex.org/W4405252387)

**Citations:** 3 citations (OpenAlex, 2026-10-14) · [52](https://md.research.elamur.ai/free/en/papers/dong-2024-flex-attention-programming-model-generating/cited-by/) in our library, of the 149,439 papers whose bibliographies we read

## Summary
FlashAttention made attention fast and memory-efficient by fusing it into a single kernel. Its hand-built design, however, covers only a few popular variants. Researchers who want something different, or a combination of variants, are stuck with slow, memory-hungry code; the authors call this a "software lottery." FlexAttention, from Meta and the University of Michigan, addresses this with a compiler-driven programming model in PyTorch. A user describes a variant with two small Python callables: one alters attention scores before softmax (score_mod), and one returns a boolean mask from positional indices (mask_mod). During torch.compile, these functions are traced and fused into the main loop of hand-tuned attention kernel templates. The backward pass is generated automatically. A precomputed BlockMask lets the kernel skip fully masked tiles. On variants that FlashAttention already supports, FlexAttention comes close to or beats it, and it also speeds up real training and inference pipelines. This lowers the cost of trying new attention designs and makes it easy to combine them.

## Key points
- **API:** users write `score_mod` and `mask_mod` in ordinary PyTorch. Variants combine by nesting score functions or joining masks with boolean logic, which avoids writing a separate kernel for every combination.
- **Supported variants:** examples include ALiBi, Softcapping, Sliding Window, Document Masking, PagedAttention and Neighborhood Attention. Tiled and Morton-curve versions of Neighborhood Attention take fewer than 10 lines.
- **BlockMask:** `create_block_mask` builds a block-level sparsity map from a mask function using `torch.vmap`. The same index structure also serves as the page mapping for PagedAttention, with negligible overhead.
- **Kernel performance:** across 7 variants, it reaches 0.68x–1.43x the speed of FAv2 and 0.93x–1.45x of FAKV in decoding. It was also compared against PyTorch SDPA and FAv3.
- **End-to-end gains:** inference in gpt-fast is 2.04x faster at 16k context, and training in torchtune is 2.4x faster.

*Our summary of the paper, in our own words — not its text. The work belongs to its authors; read it at the links above.*

📚 Shelves (6): [🖥️ Computer Science](https://md.research.elamur.ai/free/en/shelves/computer-science/) · 18,614 works · [🤖 Artificial Intelligence](https://md.research.elamur.ai/free/en/shelves/computer-science/artificial-intelligence/) · 17,965 works · [🎭 Arts and Humanities](https://md.research.elamur.ai/free/en/shelves/arts-and-humanities/) · 17,450 works · [🗣️ Language and Linguistics](https://md.research.elamur.ai/free/en/shelves/arts-and-humanities/language-and-linguistics/) · 17,296 works · [👁️ Computer Vision and Pattern Recognition](https://md.research.elamur.ai/free/en/shelves/computer-science/computer-vision-and-pattern-recognition/) · 3,486 works · [🔌 Hardware and Architecture](https://md.research.elamur.ai/free/en/shelves/computer-science/hardware-and-architecture/) · 167 works

## Cite

Cite as: Juechu Dong, Boyuan Feng, Driss Guessous, Yanbo Liang and Horace He. 2024. Flex Attention: A Programming Model for Generating Optimized Attention Kernels. arXiv:2412.05496.

The paper itself, not this page. BibTeX from the library's record of the work.

```bibtex
@misc{dong2024flex,
  title = {{Flex Attention: A Programming Model for Generating Optimized Attention Kernels}},
  author = {Juechu Dong, Boyuan Feng, Driss Guessous, Yanbo Liang and Horace He},
  year = {2024},
  doi = {10.48550/arxiv.2412.05496},
  eprint = {2412.05496},
  archivePrefix = {arXiv},
  url = {https://doi.org/10.48550/arxiv.2412.05496}
}
```

- [← previous DiscoveryBench: Towards Data-Driven Discovery with Large Language Models](https://md.research.elamur.ai/free/en/papers/majumder-2024-discoverybench-data-driven-discovery/)
- [next → Hopping Too Late: Exploring the Limitations of Large Language Models on Multi-Hop Queries](https://md.research.elamur.ai/free/en/papers/biran-2024-hopping-too-late-exploring-limitations/)
- [↑ back to papers of 2024](https://md.research.elamur.ai/free/en/papers/2024/page/7/)

## References

23 references, 12 of them with a card of ours. ↗ leads to the work, «find» searches for one we have no id for yet.

1. Ainslie, J., Lee-Thorp, J., de Jong, M., Zemlyanskiy, Y., Lebrón, F., and Sanghai, S. [Gqa: Training generalized multi-query transformer models from multi-head checkpoints](https://md.research.elamur.ai/free/en/papers/ainslie-2023-gqa-training-generalized-multi-query/), 2023. URL https://arxiv.org/abs/ 2305.13245. [↗](https://arxiv.org/abs/)
2. Ansel, J., Yang, E., He, H., Gimelshein, N., Jain, A., Voznesensky, M., Bao, B., Bell, P., and et al. [Pytorch 2: Faster machine learning through dynamic python bytecode transformation and graph compilation](https://md.research.elamur.ai/free/en/papers/ansel-2024-pytorch-2-faster-machine-learning/). In Proceedings of the 29th ACM International Conference on Architectural Support for Programm…
3. Beltagy, I., Peters, M. E., and Cohan, A. [Longformer: The long-document transformer](https://md.research.elamur.ai/free/en/papers/beltagy-2020-longformer-long-document-transformer/), 2020a. URL https://arxiv.org/abs/2004.05150. [↗](https://arxiv.org/abs/2004.05150)
4. Beltagy, I., Peters, M. E., and Cohan, A. [Longformer: The long-document transformer](https://md.research.elamur.ai/free/en/papers/beltagy-2020-longformer-long-document-transformer/), 2020b. URL https://arxiv.org/abs/2004.05150. [↗](https://arxiv.org/abs/2004.05150)
5. Chen, T., Moreau, T., Jiang, Z., Zheng, L., Yan, E., Cowan, M., Shen, H., Wang, L., Hu, Y., Ceze, L., Guestrin, C., and Krishnamurthy, A. [Tvm: an automated end-to-end optimizing compiler for deep learning](https://md.research.elamur.ai/free/en/papers/chen-2018-tvm-automated-end-end-optimizing/). In Proceedings of the 13th USENIX Conference on Operating Systems Design and Implementation, O…
6. Hassani, A. and Shi, H. Dilated neighborhood attention transformer, 2022. URL [https://arxiv.org/](https://arxiv.org/) abs/2209.15001. — [arXiv:2209.15001](https://arxiv.org/abs/2209.15001) [↗](https://arxiv.org/)
7. Hassani, A., Walton, S., Li, J., Li, S., and Shi, H. Neighborhood attention transformer. In IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), 2023. [find](https://scholar.google.com/scholar?q=Hassani%2C+A%2E%2C+Walton%2C+S%2E%2C+Li%2C+J%2E%2C+Li%2C+S%2E%2C+and+Shi%2C+H%2E+Neighborhood+attention+transformer%2E+In+IEEE%2FCVF+Conference+on+Computer+Vision+and+Pattern+Recognition+%28CVPR%29%2C+2023%2E)
8. Hassani, A., Hwu, W.-M., and Shi, H. Faster neighborhood attention: Reducing the o(n 2 ) cost of self attention at the threadblock level, 2024. URL https://arxiv.org/abs/2403.04690. [↗](https://arxiv.org/abs/2403.04690)
9. Kwon, W., Li, Z., Zhuang, S., Sheng, Y., Zheng, L., Yu, C. H., Gonzalez, J. E., Zhang, H., and Stoica, I. [Efficient memory management for large language model serving with pagedattention](https://md.research.elamur.ai/free/en/papers/kwon-2023-efficient-memory-management-large-language/). In Proceedings of the ACM SIGOPS 29th Symposium on Operating Systems Principles, 2023a.
10. Kwon, W., Li, Z., Zhuang, S., Sheng, Y., Zheng, L., Yu, C. H., Gonzalez, J. E., Zhang, H., and Stoica, I. [Efficient memory management for large language model serving with pagedattention](https://md.research.elamur.ai/free/en/papers/kwon-2023-efficient-memory-management-large-language/). In Proceedings of the ACM SIGOPS 29th Symposium on Operating Systems Principles, 2023b.
11. Press, O., Smith, N. A., and Lewis, M. Train short, test long: Attention with linear biases enables input length extrapolation, 2022. URL https://arxiv.org/abs/ 2108.12409. [↗](https://arxiv.org/abs/)
12. Rabe, M. N. and Staats, C. Self-attention does not need o(n 2 ) memory, 2022. URL [https://arxiv.org/](https://arxiv.org/) abs/2112.05682. [↗](https://arxiv.org/)
13. Dao, T. [Flashattention-2: Faster attention with better parallelism and work partitioning](https://md.research.elamur.ai/free/en/papers/dao-2023-flashattention-2-faster-attention-better/). In International Conference on Learning Representations (ICLR), 2024.
14. Raffel, C., Shazeer, N., Roberts, A., Lee, K., Narang, S., Matena, M., Zhou, Y., Li, W., and Liu, P. J. [Exploring the limits of transfer learning with a unified text-to-text transformer](https://md.research.elamur.ai/free/en/papers/raffel-2019-exploring-limits-transfer-learning-unified/), 2023. URL [https://arxiv.org/](https://arxiv.org/) abs/1910.10683. [↗](https://arxiv.org/)
15. Dao, T., Fu, D. Y., Ermon, S., Rudra, A., and Ré, C. Flashattention: Fast and memory-efficient exact attention with IO-awareness. In Advances in Neural Information Processing Systems (NeurIPS), 2022. [find](https://scholar.google.com/scholar?q=Dao%2C+T%2E%2C+Fu%2C+D%2E+Y%2E%2C+Ermon%2C+S%2E%2C+Rudra%2C+A%2E%2C+and+Re%CC%81%2C+C%2E+Flashattention%3A+Fast+and+memory%2Defficient+exact+attention+with+IO%2Dawareness%2E+In+Advances+in+Neural+Information+Processing+Systems+%28NeurIPS%29%2C+2022%2E)
16. Shah, J., Bikshandi, G., Zhang, Y., Thakkar, V., Ramani, P., and Dao, T. [Flashattention-3: Fast and accurate attention with asynchrony and low-precision](https://md.research.elamur.ai/free/en/papers/shah-2024-flashattention-3-fast-accurate-attention/), 2024. URL https://arxiv.org/abs/2407.08608. [↗](https://arxiv.org/abs/2407.08608)
17. Dao, T., Haziza, D., Massa, F., and Sizov, G. Flashdecoding for long-context inference, 2023. URL [https://crfm.stanford.edu/2023/10/12/](https://crfm.stanford.edu/2023/10/12/) flashdecoding.html. Accessed: 2024-09-15. [↗](https://crfm.stanford.edu/2023/10/12/)
18. Taori, R., Gulrajani, I., Zhang, T., Dubois, Y., Li, X., Guestrin, C., Liang, P., and Hashimoto, T. B. Stanford alpaca: An instruction-following llama model. [https://github.com/tatsu-lab/](https://github.com/tatsu-lab/) stanford_alpaca, 2023. [↗](https://github.com/tatsu-lab/)
19. Dubey, A., Jauhri, A., Pandey, A., Kadian, A., Al-Dahle, A., Letman, A., and Akhil Mathur, e. a. [The llama 3 herd of models](https://md.research.elamur.ai/free/en/papers/grattafiori-2024-llama-3-herd-models/), 2024. URL https://arxiv.org/abs/ 2407.21783. gpt-fast maintainers and contributors. Accelerating generative ai with pytorch ii: Gpt, fast, November 2023. URL https://github.co… [↗](https://arxiv.org/abs/)
20. Team, G., Riviere, M., Pathak, S., Sessa, P. G., Hardin, C., Bhupatiraju, S., Hussenot, L., and et al., T. M. Gemma 2: Improving open language models at a practical size, 2024. URL https://arxiv.org/abs/2408.00118. torchtune maintainers and contributors. torchtune: Pytorch’s finetuning library, Apri… [↗](https://arxiv.org/abs/2408.00118)
21. Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, L. u., and Polosukhin, I. [Attention is all you need](https://md.research.elamur.ai/free/en/papers/vaswani-2017-attention-all-need/). In Guyon, I., Luxburg, U. V., Bengio, S., Wallach, H., Fergus, R., Vishwanathan, S., and Garnett, R. (eds.), Advances in Neural Information Processing Systems, v…
22. Wang, G., Zeng, J., Xiao, X., Wu, S., Yang, J., Zheng, L., Chen, Z., Bian, J., Yu, D., and Wang, H. Flashmask: Efficient and rich mask extension of flashattention. arXiv preprint arXiv:2410.01359, 2024. [find](https://scholar.google.com/scholar?q=Wang%2C+G%2E%2C+Zeng%2C+J%2E%2C+Xiao%2C+X%2E%2C+Wu%2C+S%2E%2C+Yang%2C+J%2E%2C+Zheng%2C+L%2E%2C+Chen%2C+Z%2E%2C+Bian%2C+J%2E%2C+Yu%2C+D%2E%2C+and+Wang%2C+H%2E+Flashmask%3A+Efficient+and+rich+mask+extension+of+flashattention%2E+arXiv+preprint+arXiv%3A2410%2E01359%2C+202)
23. Wu, M., Cheng, X., Padon, O., and Jia, Z. A multi-level superoptimizer for tensor programs, 2024. URL https://arxiv.org/abs/2405.05751. [↗](https://arxiv.org/abs/2405.05751)

*Updated 2 November 2026*
