optimizing-attention-flash

Optimizes transformer attention with Flash Attention for faster training and lower GPU memory usage.

Updated Jun 5, 2026
One-click install
npx skills add https://github.com/xu1713/openhorse --skill optimizing-attention-flash-xu1713
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-attention-flash
Source: https://github.com/xu1713/openhorse/tree/main/openhorse/openhorse/optional-skills/mlops/flash-attention
Command: npx skills add https://github.com/xu1713/openhorse --skill optimizing-attention-flash-xu1713

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires flash-attn, torch, transformers, and includes references (resource) components.

What problem does it solve? Standard transformer attention consumes quadratic GPU memory and becomes slow or impossible to run on long sequences, causing out-of-memory errors and slow training or inference. ## Core Features & Use Cases - PyTorch Native Integration: Enable Flash Attention through torch.nn.functional.scaled_dot_product_attention in PyTorch 2.2+ with backend selection and profiling. - flash-attn Library Workflows: Use flash_attn_func for causal masking, multi-query attention, sliding window attention, and H100 FP8 acceleration. - Verification and Benchmarking: Compare outputs against standard attention and measure speedups across sequence lengths and GPUs. - Use Case: When fine-tuning a Llama 2 7B model with 8K context runs out of memory on an A100, switch attention to Flash Attention to fit the batch and gain roughly 3x training throughput. ## Quick Start Enable Flash Attention in my PyTorch transformer model and verify the speedup and accuracy against standard attention.

Frequently Asked Questions about optimizing-attention-flash

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enable Flash Attention in PyTorch?

Use torch.nn.functional.scaled_dot_product_attention in PyTorch 2.2+, which automatically dispatches to Flash Attention when available. You can force the backend with torch.backends.cuda.sdp_kernel(enable_flash=True) and pass float16 tensors on CUDA.

How do I enable Flash Attention in HuggingFace Transformers?

Pass attn_implementation="flash_attention_2" to AutoModel.from_pretrained along with torch_dtype=torch.float16. This works for supported architectures like Llama, Mistral, Falcon, and Qwen with transformers 4.36+ and flash-attn installed.

What is the difference between PyTorch SDPA and the flash-attn library?

PyTorch SDPA is the easiest option built into PyTorch 2.2+ and covers standard attention. The flash-attn library adds advanced features like multi-query attention, sliding window attention, and FP8 support on H100 GPUs.

Which GPUs support Flash Attention?

Flash Attention requires NVIDIA Ampere or newer GPUs such as A100, A10, and H100, plus Turing GPUs like T4. V100 (Volta) and CPU inference are not supported, and CUDA 11.8 or higher is required.

Why is Flash Attention not giving me a speedup?

Speedups depend on sequence length: under 512 tokens gains are only 10-20%, while 2K+ tokens yield 3-4x. Also confirm inputs are float16 or bfloat16, since float32 is not supported by Flash Attention kernels.

When should I not use Flash Attention?

Avoid it for sequences under 256 tokens where overhead outweighs gains, for CPU inference, or when you need attention variants beyond speed optimization, where xFormers or memory-efficient attention may fit better.