← All notes

Attention backends: sdpa, FlashAttention, flex, paged

Answer: All attention implementations perform the same computation; backends differ in how they do it. Use the attn_implementation argument in from_pretrained to set an attention function.

Why backends matter

Basic attention materializes the full attention matrix in memory, which slows down inference. Optimized implementations rearrange the math to reduce memory traffic.

Options

Switching and kernels

Switch backends at runtime without reloading the model using set_attn_implementation. The Kernels library downloads compiled kernels from the Hub at runtime, avoiding mismatched PyTorch or CUDA versions.

Multimodal models can map each backbone to a different attention function with a dict keyed by sub-config names.

Artifacts

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Hugging Face docs (huggingface/transformers, huggingface/huggingface_hub, huggingface/diffusers on GitHub), Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0). Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Hugging Face docs, Attention backends, Apache-2.0 (huggingface docs repos); Hugging Face docs, How caching works, Apache-2.0 (huggingface docs repos).

Cite this

Permanent ID: iil:note:v4cdvdhd

iinventedlight. 2026. "Attention backends: sdpa, FlashAttention, flex, paged." iinventedlight Library, iil:note:v4cdvdhd, version of 2026-11-09. https://iinventedlight.com/library/notes/hf-transformers-attention-backends/. License: CC-BY-4.0.

@misc{iil_note_v4cdvdhd,
  author = {{iinventedlight}},
  title = {{Attention backends: sdpa, FlashAttention, flex, paged}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-09},
  url = {https://iinventedlight.com/library/notes/hf-transformers-attention-backends/},
  note = {Permanent ID iil:note:v4cdvdhd. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/hf-transformers-attention-backends.md.