npx skills add ...
npx skills add firecrawl/ai-research-skills --skill optimizing-attention-flash
Optimizes transformer attention with Flash Attention for 2-4x speedup and 10-20x memory reduction. Use when training/running transformers with long sequences (>512 tokens), encountering GPU memory issues with attention, or need faster inference. Supports PyTorch native SDPA, flash-attn library, H100 FP8, and sliding window attention.
npx skills add firecrawl/ai-research-skills --skill optimizing-attention-flash
Flash Attention provides 2-4x speedup and 10-20x memory reduction for transformer attention through IO-aware tiling and recomputation.
PyTorch native (easiest, PyTorch 2.2+):
flash-attn library (more features):
Copy this checklist:
Step 1: Check PyTorch version
If <2.2, upgrade:
Step 2: Enable Flash Attention backend
Replace standard attention:
Force Flash Attention backend:
Step 3: Verify speedup with profiling
Expected: 2-4x speedup for sequences >512 tokens.
Step 4: Test accuracy matches baseline
For multi-query attention, sliding window, or H100 FP8.
Copy this checklist:
Step 1: Install flash-attn library
Step 2: Modify attention code
Step 3: Enable advanced features
Multi-query attention (shared K/V across heads):
Sliding window attention (local attention):
Step 4: Benchmark performance
For maximum performance on H100 GPUs.
Step 1: Verify H100 GPU
Step 2: Install flash-attn with FP8 support
Step 3: Convert inputs to FP8
Step 4: Run with FP8 attention
Use Flash Attention when:
Use alternatives instead:
Issue: ImportError: cannot import flash_attn
Install with no-build-isolation flag:
Or install CUDA toolkit first:
Issue: Slower than expected (no speedup)
Flash Attention benefits increase with sequence length:
Check sequence length is sufficient.
Issue: RuntimeError: CUDA error
Verify GPU supports Flash Attention:
Flash Attention requires:
Issue: Accuracy degradation
Check dtype is float16 or bfloat16 (not float32):
Flash Attention uses float16/bfloat16 for speed. Float32 not supported.
Integration with HuggingFace Transformers: See references/transformers-integration.md for enabling Flash Attention in BERT, GPT, Llama models.
Performance benchmarks: See references/benchmarks.md for detailed speed and memory comparisons across GPUs and sequence lengths.
Algorithm details: See references/algorithm.md for tiling strategy, recomputation, and IO complexity analysis.
Advanced features: See references/advanced-features.md for rotary embeddings, ALiBi, paged KV cache, and custom attention masks.
Not supported: V100 (Volta), CPU inference