一键导入
flash-attention
Optimize transformer attention with Flash Attention — 2-4x speedup, 10-20x memory reduction for long sequences on CUDA GPUs.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Optimize transformer attention with Flash Attention — 2-4x speedup, 10-20x memory reduction for long sequences on CUDA GPUs.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Compress conversation context — summarize the current session, extract key decisions and facts, then compact history to free up context window.
Generate insights about your Claude Code usage — what topics you work on most, common patterns, productivity trends.
Route Claude Code work by complexity, risk, and tool needs. Use when deciding how much reasoning depth a task needs, whether to read project memory first, whether the task should be decomposed, and whether the work is lightweight, standard, or investigation-heavy.
Query Polymarket prediction markets for probability data and research insights on real-world events.
Search and retrieve academic papers from arXiv using their free REST API. No API key needed.
Query Base (Ethereum L2) blockchain data — wallet balances, token info, transactions, gas analysis, contract inspection. No API key required.
| name | flash-attention |
| description | Optimize transformer attention with Flash Attention — 2-4x speedup, 10-20x memory reduction for long sequences on CUDA GPUs. |
| version | 1.0.0 |
| author | hermes-CCC (ported from Hermes Agent by NousResearch) |
| license | MIT |
| metadata | {"hermes":{"tags":["Optimization","Flash-Attention","Memory-Efficiency","PyTorch","Transformers","CUDA","Long-Context"],"related_skills":["grpo-rl-training","vllm"]}} |
Flash Attention provides 2-4x training speedup and 10-20x memory reduction by replacing the standard O(N²) attention with an IO-aware tiling algorithm.
import torch
import torch.nn.functional as F
# PyTorch automatically uses Flash Attention if available
q = torch.randn(2, 8, 512, 64, device="cuda", dtype=torch.float16)
k = torch.randn(2, 8, 512, 64, device="cuda", dtype=torch.float16)
v = torch.randn(2, 8, 512, 64, device="cuda", dtype=torch.float16)
# This automatically dispatches to Flash Attention on compatible hardware
output = F.scaled_dot_product_attention(q, k, v, dropout_p=0.0, is_causal=True)
# Check which kernel is being used
with torch.backends.cuda.sdp_kernel(
enable_flash=True, enable_math=False, enable_mem_efficient=False
):
output = F.scaled_dot_product_attention(q, k, v, is_causal=True)
# Requires: CUDA toolkit, torch, ninja
pip install flash-attn --no-build-isolation
# If build fails:
pip install packaging ninja
pip install flash-attn --no-build-isolation --no-cache-dir
from flash_attn import flash_attn_func, flash_attn_varlen_func
# Basic usage: q,k,v shape: (batch, seqlen, nheads, headdim)
q = torch.randn(2, 512, 8, 64, device="cuda", dtype=torch.float16)
k = torch.randn(2, 512, 8, 64, device="cuda", dtype=torch.float16)
v = torch.randn(2, 512, 8, 64, device="cuda", dtype=torch.float16)
output = flash_attn_func(q, k, v, dropout_p=0.0, causal=True)
from transformers import AutoModelForCausalLM
# Automatic Flash Attention 2 (recommended)
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-3.1-8B-Instruct",
attn_implementation="flash_attention_2",
torch_dtype=torch.bfloat16,
device_map="auto",
)
# Or: eager (standard), sdpa (PyTorch SDPA)
model = AutoModelForCausalLM.from_pretrained(
"...",
attn_implementation="sdpa", # no install needed
)
| GPU | FP16 | BF16 | FP8 |
|---|---|---|---|
| A100 | ✅ | ✅ | ❌ |
| H100 | ✅ | ✅ | ✅ |
| A10/A30 | ✅ | ✅ | ❌ |
| RTX 3090/4090 | ✅ | ✅ | ❌ |
| V100 | ✅ | ❌ | ❌ |
| T4 | ✅ | ❌ | ❌ |
Requirements:
from flash_attn.flash_attn_interface import flash_attn_varlen_func
# For sequences longer than GPU memory allows
# Use sliding window to limit attention span
output = flash_attn_func(
q, k, v,
causal=True,
window_size=(512, 0) # attend to last 512 tokens only
)
Standard attention: O(N²) memory Flash Attention: O(N) memory (recomputes on backward pass)
Sequence length 4096: ~6x memory savings
Sequence length 8192: ~15x memory savings
Sequence length 32768: ~50x+ memory savings
import time, torch
def bench(fn, warmup=3, reps=10):
for _ in range(warmup):
fn()
torch.cuda.synchronize()
t0 = time.time()
for _ in range(reps):
fn()
torch.cuda.synchronize()
return (time.time() - t0) / reps * 1000 # ms
q = torch.randn(4, 2048, 16, 64, device="cuda", dtype=torch.float16)
k, v = q.clone(), q.clone()
standard_ms = bench(lambda: F.scaled_dot_product_attention(q, k, v))
flash_ms = bench(lambda: flash_attn_func(q, k, v, causal=True))
print(f"Standard: {standard_ms:.1f}ms | Flash: {flash_ms:.1f}ms | Speedup: {standard_ms/flash_ms:.1f}x")
Build fails: Ensure CUDA toolkit matches PyTorch CUDA version (python -c "import torch; print(torch.version.cuda)")
Wrong dtype: Flash Attention requires FP16 or BF16, not FP32
OOM despite Flash Attention: Use gradient checkpointing additionally: model.gradient_checkpointing_enable()
Not faster on short sequences: Flash Attention shines at >512 tokens; below that overhead can dominate