flash-attention
Use when the user asks about FlashAttention, flash_attn, SDPA, scaled_dot_product_attention backends, online softmax, FA2/FA3/FA4 or attention OOM at long…
от j4flmao★ 47 звёзд на GitHub
Установить flash-attention в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
FlashAttention
Purpose
Compute exact softmax attention without materializing the N x N score matrix in GPU HBM. FlashAttention is IO-aware: it tiles Q, K, V into on-chip SRAM, fuses matmul + softmax + matmul into one kernel, and recomputes scores in the backward pass. FLOPs are unchanged (still O(N²·d)); memory for attention drops from O(N²) to O(N), and wall-clock time drops because HBM traffic drops.
Agent Protocol
Trigger
"flashattention", "flash attention", "flash_attn", "sdpa", "scaled_dot_product_attention", "online softmax", "attention OOM", "long context training memory".
Input Context
- GPU (Ampere/Ada/Hopper/Blackwell), dtype, head dimension, sequence lengths, masking (causal, padding, sliding window).
- Framework (PyTorch, HF transformers, JAX) and whether training or inference.
Output Artifact
Configuration/code to enable the fastest exact attention path + verification steps. No file unless requested.
Response Format
Path: {SDPA flash backend|flash-attn FA2|FA3|FA4|cuDNN|memory-efficient}
Requirements met: dtype={fp16/bf16} head_dim={d} mask={...}
Change: {code/config}
Verify: {profiler kernel name / benchmark}
Completion Criteria
- Confirmed the flash kernel actually runs (profiler trace or forced backend), not a silent fallback.
- Numerical parity checked against a reference attention on small inputs.
- Padding handled with varlen/packed sequences rather than dense masks where possible.
Why It Works (A100 numbers from the FlashAttention paper)
| Memory | Capacity | Bandwidth |
|---|---|---|
| HBM | 40-80 GB | ~1.5-2.0 TB/s |
| On-chip SRAM (shared memory/L1) | 192 KB per SM, ~20 MB across 108 SMs | ~19 TB/s |
Standard attention writes S = QKᵀ and P = softmax(S) (each N x N) to HBM and reads them back. FlashAttention keeps tiles of S and P in SRAM, so HBM traffic falls from Θ(Nd + N²) to Θ(N²d²/M) for SRAM size M.
Online Softmax (per query-row block, iterating over key/value blocks j)
m_new = max(m, rowmax(S_j)) # S_j = Q_i K_jᵀ · scale
P_j = exp(S_j - m_new)
l_new = exp(m - m_new) · l + rowsum(P_j)
O = exp(m - m_new) · O + P_j V_j # rescale previous partial output
m, l = m_new, l_new
# after the last block: O = O / l ; store logsumexp L = m + log(l) for backward
The rescaling of the accumulated output O is essential; tracking only m and l is not enough. The backward pass recomputes S and P from Q, K, V and L instead of storing them.
Versions
| Version | Target | Key ideas |
|---|---|---|
| FlashAttention (2022) | Ampere | Tiling + recomputation, IO-aware analysis |
| FlashAttention-2 (2023) | Ampere/Ada/Hopper | Parallelism over sequence length, fewer non-matmul FLOPs, better warp work split |
| FlashAttention-3 (2024) | Hopper (sm_90) | Asynchronous WGMMA + TMA, warp specialization, overlapping softmax with matmul, FP8 |
| FlashAttention-4 (2025-26) | Blackwell (sm_100) | Fully asynchronous MMA with tensor memory, software-emulated exponentials, conditional softmax rescaling |
Using It
PyTorch SDPA (default choice)
import torch
import torch.nn.functional as F
from torch.nn.attention import sdpa_kernel, SDPBackend
q = torch.randn(4, 16, 4096, 128, device="cuda", dtype=torch.bfloat16) # (B, H, L, D)
k = torch.randn_like(q)
v = torch.randn_like(q)
out = F.scaled_dot_product_attention(q, k, v, is_causal=True) # auto-selects a backend
with sdpa_kernel(SDPBackend.FLASH_ATTENTION): # force flash; raises if inputs unsupported
out = F.scaled_dot_product_attention(q, k, v, is_causal=True)
SDPA backends: FLASH_ATTENTION, EFFICIENT_ATTENTION (memory-efficient, supports arbitrary masks), CUDNN_ATTENTION, MATH (reference). An arbitrary attn_mask tensor generally disqualifies the flash backend.
flash-attn package
from flash_attn import flash_attn_func, flash_attn_varlen_func
# layout (B, L, H, D), fp16/bf16 only
out = flash_attn_func(q, k, v, dropout_p=0.0, causal=True)
# packed variable-length batch: no padding FLOPs
# q: (total_tokens, H, D); cu_seqlens: int32 cumulative lengths [0, l1, l1+l2, ...]
out = flash_attn_varlen_func(q, k, v, cu_seqlens_q=cu, cu_seqlens_k=cu,
max_seqlen_q=max_len, max_seqlen_k=max_len, causal=True)
Supports grouped/multi-query attention (fewer K/V heads than Q heads) and sliding window via window_size=(left, right).
Hugging Face transformers
model = AutoModelForCausalLM.from_pretrained(
model_id, dtype=torch.bfloat16, attn_implementation="flash_attention_2" # or "sdpa"
)
Requirements / Common Fallback Causes
- dtype must be FP16 or BF16 (FP8 for FA3 forward). FP32 inputs fall back to slower kernels.
- Head dimension limits (flash-attn supports up to 256; check the installed version).
- Unsupported GPU architecture for the installed wheel (FA3/FA4 are architecture-specific builds).
- Dense additive masks instead of
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл flash-attention?
Use when the user asks about FlashAttention, flash_attn, SDPA, scaled_dot_product_attention backends, online softmax, FA2/FA3/FA4 or attention OOM at long context. Produces the right exact-attention path, config and a check that the flash kernel really runs. Not for KV-cache paging in serving (use paged-attention) or custom kernels (use triton-kernels).
Как установить скилл flash-attention?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл flash-attention запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
AnthropicToolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
access
AnthropicManage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
configure
AnthropicSet up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
access
AnthropicManage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…