Skip to main content
Commands, package names, and image names on this page come from the open-source project that Mibyan Desktop is built on, and can differ from the Mibyan Desktop installer. For the supported Mibyan install and update path, see Install and update.
Speed up long-sequence transformer training and inference.

Skill metadata

Reference: full SKILL.md

The following is the complete skill definition that Mibyan loads when this skill is triggered. This is what the agent sees as instructions when the skill is active.

Flash Attention - Fast Memory-Efficient Attention

Quick start

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):

Common workflows

Workflow 1: Enable in existing PyTorch model

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 (torch.backends.cuda.sdp_kernel is deprecated; use torch.nn.attention.sdpa_kernel with SDPBackend):
Step 3: Verify speedup with profiling
Expected: 2-4x speedup for sequences >512 tokens. Step 4: Test accuracy matches baseline

Workflow 2: Use flash-attn library for advanced features

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

Workflow 3: H100 FP8 optimization (FlashAttention-3)

For maximum performance on Hopper GPUs (H100).
Important: The pip package flash-attn (2.8.x) ships FlashAttention-2 only — it does not contain FA3 or FP8 H100 kernels, and flash_attn_func does not auto-use FP8. FlashAttention-3 is a separate beta build compiled from source from the repo’s hopper/ directory, exposed via the flash_attn_interface module. FA3 supports FP16/BF16 forward+backward and FP8 forward only.
Step 1: Verify H100 GPU
Step 2: Build & install FlashAttention-3 from source FA3 is NOT included in pip install flash-attn. Build it from the hopper/ subdirectory:
Step 3: Use the FA3 interface (FP8 forward) FA3 exposes its own module flash_attn_interface (distinct from the FA2 flash_attn). FP8 is a forward-only path and expects float8_e4m3fn inputs:

When to use vs alternatives

Use Flash Attention when:
  • Training transformers with sequences >512 tokens
  • Running inference with long context (>2K tokens)
  • GPU memory constrained (OOM with standard attention)
  • Need 2-4x speedup without accuracy loss
  • Using PyTorch 2.2+ or can install flash-attn
Use alternatives instead:
  • Standard attention: Sequences <256 tokens (overhead not worth it)
  • xFormers: Need more attention variants (not just speed)
  • Memory-efficient attention: CPU inference (Flash Attention needs GPU)

Common issues

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:
  • <512 tokens: Minimal speedup (10-20%)
  • 512-2K tokens: 2-3x speedup
  • 2K tokens: 3-4x speedup
Check sequence length is sufficient. Issue: RuntimeError: CUDA error Verify GPU supports Flash Attention:
Flash Attention requires:
  • Ampere (A100, A10): ✅ Full support
  • Turing (T4): ✅ Supported
  • Volta (V100): ❌ Not supported
Issue: Accuracy degradation Check dtype is float16 or bfloat16 (not float32):
Flash Attention uses float16/bfloat16 for speed. Float32 not supported.

Advanced topics

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.

Hardware requirements

  • GPU: NVIDIA Ampere+ (A100, A10, A30) or AMD MI200+
  • VRAM: Same as standard attention (Flash Attention doesn’t increase memory)
  • CUDA: 12.0+ (11.8 minimum)
  • PyTorch: 2.2+ for native support
Not supported: V100 (Volta), CPU inference

Resources