P

Flash Attention: Definition and Examples

Flash Attention is an optimized algorithm for computing the attention mechanism in Transformers, drastically reducing memory consumption and accelerating the training and inference of large language models.

Full definition

Flash Attention is an optimized implementation of the attention mechanism (self-attention) used in Transformer architectures. Developed by Tri Dao and his collaborators at Stanford in 2022, this algorithm solves a fundamental problem: classical attention computation has quadratic memory complexity with respect to sequence length, which severely limits the context sizes models can handle.

The central idea of Flash Attention relies on the concept of "tiling" (dividing into blocks) and intelligent management of the GPU memory hierarchy. Instead of materializing the full attention matrix in high-bandwidth memory (HBM), the algorithm performs calculations block by block directly in the GPU's fast memory (SRAM), then merges the results. This avoids costly round trips between different memory levels, which constitute the true bottleneck.

Flash Attention 2, released in 2023, further improved performance by optimizing parallelism and reducing non-essential operations. This version achieves approximately 50 to 70% of the theoretical maximum utilization of modern GPUs, compared to only 25 to 40% for standard attention. Flash Attention 3, released in 2024, pushes these optimizations further for Hopper GPUs (H100).

The impact of Flash Attention on the AI ecosystem is substantial: it has made possible the training of models with context windows of 100,000 tokens and beyond, where classical implementations were limited to a few thousand. Today, virtually all major language models (GPT-4, Claude, Llama, Mistral) use a variant of Flash Attention.

Etymology

The term "Flash" refers to the algorithm's execution speed and its use of SRAM memory (sometimes called "flash memory" by analogy with its speed), as opposed to slower HBM memory. The name also evokes the idea of a "lightning" calculation that avoids storing large intermediate results.

Concrete examples

Training an LLM with a long context

You are an assistant that can process documents of 200,000 tokens. Analyze this complete annual report and identify the 5 major strategic risks mentioned.

Production inference optimization

Configure the vLLM inference server with Flash Attention 2 enabled to serve a Llama 3 70B model on 4 A100 GPUs with maximum throughput.

Research and fine-tuning on limited GPU

Fine-tune this 7B model on a single 24GB GPU using Flash Attention and gradient checkpointing to maximize the supported sequence length.

Practical usage

In prompt engineering, Flash Attention is not a concept used directly in prompts, but it conditions what is possible: thanks to it, modern models can accept very long contexts. Concretely, this means you can provide entire documents, long conversations, or many few-shot examples in a single prompt without major performance degradation. Understanding Flash Attention also helps in choosing the right inference parameters and sizing GPU infrastructure for deploying models in production.

Related concepts

Attention mechanism (Self-Attention)TransformerContext windowQuadratic complexityKV CacheMulti-Head Attention

FAQ

Does Flash Attention change model outputs?
No. Flash Attention computes exactly the same mathematical result as standard attention. It is a purely algorithmic and hardware optimization: only the way calculations are ordered and stored in memory changes, not the final result. The outputs are identical up to numerical precision.
Why is Flash Attention faster even though it does the same computation?
The gain comes from reducing memory transfers, not from reducing the number of operations. Standard attention writes a huge intermediate matrix (N×N) to slow memory (HBM), then reads it back to compute the result. Flash Attention processes everything in small blocks within the GPU's fast memory (SRAM), eliminating these costly transfers. On modern GPUs, memory bandwidth—not compute power—is the limiting factor.
Do I need to enable Flash Attention manually?
It depends on the framework used. In Hugging Face Transformers, it can be enabled with the parameter attn_implementation='flash_attention_2' when loading the model. In PyTorch 2.0+, the function scaled_dot_product_attention automatically uses Flash Attention when conditions are met (compatible GPU, correctly formatted tensors). Inference frameworks like vLLM and TensorRT-LLM enable it by default.

See also

How to use this prompt

  1. Copy the prompt with the button above.
  2. Paste it into ChatGPT, Claude or your favorite AI assistant.
  3. Replace the bracketed variables with your details, then refine the result.

About Prompt Guide

Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.

More definitions

Get new prompts every week

Join our newsletter.