optimizing-attention-flash

Enable Flash Attention in PyTorch transformers to reduce latency and GPU memory usage.

1|Updated May 16, 2026
One-click install
npx skills add https://github.com/devMoez/titan --skill optimizing-attention-flash-devmoez
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-attention-flash
Source: https://github.com/devMoez/titan/tree/main/optional-skills/mlops/flash-attention
Command: npx skills add https://github.com/devMoez/titan --skill optimizing-attention-flash-devmoez

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you reduce transformer attention compute time and GPU memory pressure by switching attention implementations to Flash Attention, especially when sequence lengths are long or you hit CUDA out-of-memory errors.

Core Features & Use Cases

  • Faster, memory-efficient attention: Achieves 2–4x speedups and 10–20x memory reductions by avoiding materializing the full attention matrix through IO-aware tiling and recomputation.
  • Multiple integration paths: Uses PyTorch native scaled_dot_product_attention (SDPA) for easy enablement, or the flash-attn library for advanced features and more control.
  • Long-context and GPU optimization: Supports scenarios like training/inference with >512 tokens, sliding-window attention, multi-query attention, and FlashAttention-3 optimizations such as H100 FP8 acceleration.

Example Use Case

If your training or inference run OOMs when pushing context to thousands of tokens, enable Flash Attention to fit larger batches or longer sequences while improving throughput.

Quick Start

Use the optimizing-attention-flash skill to enable Flash Attention in your PyTorch model (or switch to flash-attn) for transformer attention on long sequences and then verify performance with a short benchmark.

Frequently Asked Questions about optimizing-attention-flash

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

FAQPage Schema
How do I fix CUDA out-of-memory errors when training transformers with long context?

To fix CUDA out-of-memory errors during long-context transformer training, enable Flash Attention to reduce GPU memory pressure by avoiding the materialization of the full attention matrix. This approach can achieve 10–20x memory reductions.

How do I enable Flash Attention in PyTorch for faster long-sequence inference?

To enable Flash Attention in PyTorch for faster long-sequence inference, use the native scaled_dot_product_attention (SDPA) for easy enablement or integrate the flash-attn library for advanced features, achieving 2–4x speedups.

Does Flash Attention work with FP8 on H100 GPUs for maximum throughput?

Yes, Flash Attention works with FP8 on H100 GPUs for maximum throughput by utilizing FlashAttention-3 optimizations. You must use appropriate dtypes and compatible PyTorch/flash-attn versions while maintaining attention correctness.

Can I use PyTorch SDPA for sliding-window and multi-query attention?

Yes, you can use PyTorch SDPA for sliding-window and multi-query attention. Flash Attention supports these scenarios to optimize transformer attention compute time and GPU memory usage during long-sequence training and inference.

What is the best way to optimize transformer attention beyond 512 tokens?

The best way to optimize transformer attention beyond 512 tokens is enabling Flash Attention via IO-aware tiling and recomputation. This prevents materializing the full attention matrix, significantly reducing latency and fitting larger batches.