跳到主要内容

优化 Flash Attention

通过 Flash Attention 优化 Transformer 注意力机制,实现 2-4 倍的速度提升和 10-20 倍的内存减少。当训练或运行具有长序列(>512 个 token)的 Transformer、遇到注意力机制导致的 GPU 内存问题,或需要更快的推理速度时使用。支持 PyTorch 原生 SDPA、flash-attn 库、H100 FP8 以及滑动窗口注意力。

技能元数据

来源可选 — 使用 hermes skills install official/mlops/flash-attention 安装
路径optional-skills/mlops/flash-attention
版本1.0.0
作者Orchestra Research
许可证MIT
依赖项flash-attn, torch, transformers
标签Optimization, Flash Attention, Attention Optimization, Memory Efficiency, Speed Optimization, Long Context, PyTorch, SDPA, H100, FP8, Transformers

参考:完整 SKILL.md

信息

以下是 Hermes 在触发此技能时加载的完整技能定义。这是技能激活时代理看到的指令。

Flash Attention - 快速且内存高效的注意力机制

快速开始

Flash Attention 通过 IO 感知的分块和重计算,为 Transformer 注意力机制提供 2-4 倍的速度提升和 10-20 倍的内存减少。

PyTorch 原生(最简单,PyTorch 2.2+)

import torch
import torch.nn.functional as F

q = torch.randn(2, 8, 512, 64, device='cuda', dtype=torch.float16) # [batch, heads, seq, dim]
k = torch.randn(2, 8, 512, 64, device='cuda', dtype=torch.float16)
v = torch.randn(2, 8, 512, 64, device='cuda', dtype=torch.float16)

# Automatically uses Flash Attention if available
out = F.scaled_dot_product_attention(q, k, v)

flash-attn 库(更多功能)

pip install flash-attn --no-build-isolation
from flash_attn import flash_attn_func

# q, k, v: [batch, seqlen, nheads, headdim]
out = flash_attn_func(q, k, v, dropout_p=0.0, causal=True)

常见工作流

工作流 1:在现有 PyTorch 模型中启用

复制此检查清单:

Flash Attention Integration:
- [ ] Step 1: Check PyTorch version (≥2.2)
- [ ] Step 2: Enable Flash Attention backend
- [ ] Step 3: Verify speedup with profiling
- [ ] Step 4: Test accuracy matches baseline

步骤 1:检查 PyTorch 版本

python -c "import torch; print(torch.__version__)"
# Should be ≥2.2.0

如果 <2.2,请升级:

pip install --upgrade torch

步骤 2:启用 Flash Attention 后端

替换标准注意力:

# Before (standard attention)
attn_weights = torch.softmax(q @ k.transpose(-2, -1) / math.sqrt(d_k), dim=-1)
out = attn_weights @ v

# After (Flash Attention)
import torch.nn.functional as F
out = F.scaled_dot_product_attention(q, k, v, attn_mask=mask)

强制使用 Flash Attention 后端:

with torch.backends.cuda.sdp_kernel(
enable_flash=True,
enable_math=False,
enable_mem_efficient=False
):
out = F.scaled_dot_product_attention(q, k, v)

步骤 3:通过性能分析验证加速效果

import torch.utils.benchmark as benchmark

def test_attention(use_flash):
q, k, v = [torch.randn(2, 8, 2048, 64, device='cuda', dtype=torch.float16) for _ in range(3)]

if use_flash:
with torch.backends.cuda.sdp_kernel(enable_flash=True):
return F.scaled_dot_product_attention(q, k, v)
else:
attn = (q @ k.transpose(-2, -1) / 8.0).softmax(dim=-1)
return attn @ v

# Benchmark
t_flash = benchmark.Timer(stmt='test_attention(True)', globals=globals())
t_standard = benchmark.Timer(stmt='test_attention(False)', globals=globals())

print(f"Flash: {t_flash.timeit(100).mean:.3f}s")
print(f"Standard: {t_standard.timeit(100).mean:.3f}s")

预期:对于超过 512 个 token 的序列,速度提升 2-4 倍。

步骤 4:测试准确率是否与基线一致

# Compare outputs
q, k, v = [torch.randn(1, 8, 512, 64, device='cuda', dtype=torch.float16) for _ in range(3)]

# Flash Attention
out_flash = F.scaled_dot_product_attention(q, k, v)

# Standard attention
attn_weights = torch.softmax(q @ k.transpose(-2, -1) / 8.0, dim=-1)
out_standard = attn_weights @ v

# Check difference
diff = (out_flash - out_standard).abs().max()
print(f"Max difference: {diff:.6f}")
# Should be <1e-3 for float16

工作流 2:使用 flash-attn 库获取高级功能

适用于多查询注意力、滑动窗口或 H100 FP8。

复制此检查清单:

flash-attn Library Setup:
- [ ] Step 1: Install flash-attn library
- [ ] Step 2: Modify attention code
- [ ] Step 3: Enable advanced features
- [ ] Step 4: Benchmark performance

步骤 1:安装 flash-attn 库

# NVIDIA GPUs (CUDA 12.0+)
pip install flash-attn --no-build-isolation

# Verify installation
python -c "from flash_attn import flash_attn_func; print('Success')"

步骤 2:修改注意力代码

from flash_attn import flash_attn_func

# Input: [batch_size, seq_len, num_heads, head_dim]
# Transpose from [batch, heads, seq, dim] if needed
q = q.transpose(1, 2) # [batch, seq, heads, dim]
k = k.transpose(1, 2)
v = v.transpose(1, 2)

out = flash_attn_func(
q, k, v,
dropout_p=0.1,
causal=True, # For autoregressive models
window_size=(-1, -1), # No sliding window
softmax_scale=None # Auto-scale
)

out = out.transpose(1, 2) # Back to [batch, heads, seq, dim]

步骤 3:启用高级功能

多查询注意力(跨头共享 K/V):

from flash_attn import flash_attn_func

# q: [batch, seq, num_q_heads, dim]
# k, v: [batch, seq, num_kv_heads, dim] # Fewer KV heads
out = flash_attn_func(q, k, v) # Automatically handles MQA

滑动窗口注意力(局部注意力):

# Only attend to window of 256 tokens before/after
out = flash_attn_func(
q, k, v,
window_size=(256, 256), # (left, right) window
causal=True
)

步骤 4:基准测试性能

import torch
from flash_attn import flash_attn_func
import time

q, k, v = [torch.randn(4, 4096, 32, 64, device='cuda', dtype=torch.float16) for _ in range(3)]

# Warmup
for _ in range(10):
_ = flash_attn_func(q, k, v)

# Benchmark
torch.cuda.synchronize()
start = time.time()
for _ in range(100):
out = flash_attn_func(q, k, v)
torch.cuda.synchronize()
end = time.time()

print(f"Time per iteration: {(end-start)/100*1000:.2f}ms")
print(f"Memory allocated: {torch.cuda.max_memory_allocated()/1e9:.2f}GB")

工作流 3:H100 FP8 优化(FlashAttention-3)

适用于在 H100 GPU 上获得最大性能。

FP8 Setup:
- [ ] Step 1: Verify H100 GPU available
- [ ] Step 2: Install flash-attn with FP8 support
- [ ] Step 3: Convert inputs to FP8
- [ ] Step 4: Run with FP8 attention

步骤 1:验证 H100 GPU

nvidia-smi --query-gpu=name --format=csv
# Should show "H100" or "H800"

步骤 2:安装支持 FP8 的 flash-attn

pip install flash-attn --no-build-isolation
# FP8 support included for H100

步骤 3:将输入转换为 FP8

import torch

q = torch.randn(2, 4096, 32, 64, device='cuda', dtype=torch.float16)
k = torch.randn(2, 4096, 32, 64, device='cuda', dtype=torch.float16)
v = torch.randn(2, 4096, 32, 64, device='cuda', dtype=torch.float16)

# Convert to float8_e4m3 (FP8)
q_fp8 = q.to(torch.float8_e4m3fn)
k_fp8 = k.to(torch.float8_e4m3fn)
v_fp8 = v.to(torch.float8_e4m3fn)

步骤 4:运行 FP8 注意力

from flash_attn import flash_attn_func

# FlashAttention-3 automatically uses FP8 kernels on H100
out = flash_attn_func(q_fp8, k_fp8, v_fp8)
# Result: ~1.2 PFLOPS, 1.5-2x faster than FP16

何时使用与替代方案对比

在以下情况使用 Flash Attention:

  • 训练序列长度 >512 个 token 的 Transformer
  • 运行具有长上下文(>2K 个 token)的推理
  • GPU 内存受限(标准注意力导致 OOM)
  • 需要在不损失准确率的情况下获得 2-4 倍的速度提升
  • 使用 PyTorch 2.2+ 或可以安装 flash-attn

在以下情况使用替代方案:

  • 标准注意力:序列长度 <256 个 token(开销不值得)
  • xFormers:需要更多注意力变体(不仅仅是速度)
  • 内存高效注意力:CPU 推理(Flash Attention 需要 GPU)

常见问题

问题:ImportError: cannot import flash_attn

使用 no-build-isolation 标志安装:

pip install flash-attn --no-build-isolation

或者先安装 CUDA toolkit:

conda install cuda -c nvidia
pip install flash-attn --no-build-isolation

问题:比预期慢(无加速效果)

Flash Attention 的优势随序列长度增加而增加:

  • <512 个 token:最小加速(10-20%)
  • 512-2K 个 token:2-3 倍加速
  • 2K 个 token:3-4 倍加速

检查序列长度是否足够。

问题:RuntimeError: CUDA error

验证 GPU 是否支持 Flash Attention:

import torch
print(torch.cuda.get_device_capability())
# Should be ≥(7, 5) for Turing+

Flash Attention 要求:

  • Ampere (A100, A10):✅ 完全支持
  • Turing (T4):✅ 支持
  • Volta (V100):❌ 不支持

问题:准确率下降

检查 dtype 是否为 float16 或 bfloat16(而非 float32):

q = q.to(torch.float16)  # Or torch.bfloat16

Flash Attention 使用 float16/bfloat16 以提高速度。不支持 Float32。

高级主题

与 HuggingFace Transformers 集成:参见 references/transformers-integration.md 以了解如何在 BERT、GPT、Llama 模型中启用 Flash Attention。

性能基准测试:参见 references/benchmarks.md 以获取跨 GPU 和序列长度的详细速度和内存比较。

算法细节:请参阅 references/algorithm.md 了解分块策略、重计算以及 I/O 复杂度分析。

高级特性:请参阅 references/advanced-features.md 了解旋转位置编码(rotary embeddings)、ALiBi、分页 KV 缓存和自定义注意力掩码。

硬件要求

  • GPU:NVIDIA Ampere+(A100、A10、A30)或 AMD MI200+
  • VRAM:与标准注意力机制相同(Flash Attention 不会增加内存占用)
  • CUDA:12.0+(最低 11.8)
  • PyTorch:2.2+ 以获得原生支持

不支持:V100(Volta)、CPU 推理

资源