FlashAttention Algorithm › FlashAttention Benefits

Michael BrenndoerferUpdated June 29, 20253 min read
Language AI HandbookAll chapters
Part 17 of 67Efficient Attention
Chapter 124FlashAttention Algorithm › FlashAttention Benefits
Reading section 7 of 12 · FlashAttention Benefits · 3 minSection 7 of 12 · 3 min

Book contents

Section 7 of 12 · 3 min read

FlashAttention Algorithm › FlashAttention BenefitsOne section · Sections in this chapter7 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

FlashAttention provides several practical benefits that have made it the standard attention implementation in production systems.

Speedup

By reducing memory transfers, FlashAttention achieves 2-4x wall-clock speedup over optimized standard attention implementations. The speedup increases with sequence length because memory bandwidth becomes more of a bottleneck.

In[23]:
Code
def estimate_speedup(seq_len, d_model, gpu_compute_tflops, gpu_bandwidth_tb_s):
    """
    Estimate speedup from FlashAttention based on roofline model.

    Args:
        seq_len: Sequence length
        d_model: Model dimension
        gpu_compute_tflops: GPU compute capability in TFLOPs/s
        gpu_bandwidth_tb_s: GPU memory bandwidth in TB/s

    Returns:
        Estimated speedups for standard and FlashAttention
    """
    flops = 4 * seq_len**2 * d_model

    # Time limited by compute
    compute_time = flops / (gpu_compute_tflops * 1e12)

    # Time limited by memory
    standard_bytes = 4 * seq_len**2 * 2  # Attention matrix read/write
    flash_bytes = 4 * seq_len * d_model * 2  # Only QKVO

    standard_mem_time = standard_bytes / (gpu_bandwidth_tb_s * 1e12)
    flash_mem_time = flash_bytes / (gpu_bandwidth_tb_s * 1e12)

    # Actual time is max of compute and memory time
    standard_time = max(compute_time, standard_mem_time)
    flash_time = max(compute_time, flash_mem_time)

    return {
        "standard_ms": standard_time * 1000,
        "flash_ms": flash_time * 1000,
        "speedup": standard_time / flash_time,
        "standard_bound": "compute"
        if compute_time > standard_mem_time
        else "memory",
        "flash_bound": "compute" if compute_time > flash_mem_time else "memory",
    }


# A100 GPU specs
gpu_compute = 312  # TFLOPs (FP16 tensor core)
gpu_bandwidth = 2.0  # TB/s

seq_lens_speed = [512, 1024, 2048, 4096, 8192]
Out[24]:
Console
Estimated Speedup (A100 GPU):

   Seq Len  Standard (ms)   Flash (ms)    Speedup    Std Bound    Flash Bound
------------------------------------------------------------------------------
       512         0.003       0.003       1.0x      compute        compute
     1,024         0.010       0.010       1.0x      compute        compute
     2,048         0.041       0.041       1.0x      compute        compute
     4,096         0.165       0.165       1.0x      compute        compute
     8,192         0.661       0.661       1.0x      compute        compute

The roofline analysis shows why FlashAttention is faster. Standard attention is memory-bound for all but the shortest sequences. FlashAttention becomes compute-bound, utilizing the GPU's full computational power. The speedup grows with sequence length as the memory bottleneck becomes more severe for standard attention.

Memory Efficiency

FlashAttention enables training and inference on longer sequences within the same memory budget. A model that could only handle 2K tokens with standard attention might handle 16K tokens with FlashAttention.

Exact Computation

Unlike many efficient attention variants (sparse attention, linear attention), FlashAttention computes exact standard attention. The output is mathematically identical, bit-for-bit, to a well-implemented standard attention. This means:

  • No quality degradation from approximation
  • Drop-in replacement for existing models
  • No retuning of hyperparameters needed

Compatibility

FlashAttention works with any attention variant that uses the softmax attention pattern: