FlashAttention is an exact attention-computation technique for scaling transformers to longer inputs. It changes the implementation rather than the attention result, reducing memory traffic without approximation error. section 9.4.1
Standard attention materializes the full n × n matrix of QKᵀ, applies softmax, and multiplies by V. FlashAttention instead streams tiles of queries and keys through fast on-chip SRAM, computes partial products, performs numerically stable incremental normalization, and accumulates outputs in HBM. section 9.4.1 section 9.4.1
The implementation improves attention efficiency by:
- Avoiding materialization of the full attention matrix and reducing memory complexity from O(n²) to O(n). section 9.4.1
- Reducing redundant reads and writes between high-bandwidth memory and on-chip memory. section 9.4.1
- Delivering reported 2× to 4× speedups over naïve kernels while retaining exactness, unlike sparse or linear attention methods. section 9.4.1
FlashAttention-3 deployment
A Transformers model can enable FlashAttention-3 through the attention implementation setting `kernels-community/vllm-flash-attn3`. The variant targets NVIDIA Hopper hardware and uses features such as Warpgroup Matrix Multiply-Accumulate and the Tensor Memory Accelerator. section 9.4.1
Figure 9.8 Blockwise computation in FlashAttention. Rather than materializing the full n × n attention matrix, the algorithm streams tiles of Q and K into SRAM, applies softmax locally, rescales for global normalization, and accumulates outputs. This avoids expensive HBM traffic.
Use the blockwise-computation figure to trace how Q and K tiles move through SRAM, receive local softmax treatment, undergo global rescaling, and accumulate outputs without full-matrix materialization. section 9.4.1
Listing 9.3 Enabling FlashAttention-3
model_id = "<your model id>"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
torch_dtype="auto",
attn_implementation="kernels-community/vllm-flash-attn3", #1
)
Use the configuration listing to inspect the model-loading change that enables the FlashAttention-3 implementation. section 9.4.1
