Is a tiled attention kernel an approximation

L3SeniorConcept10 minML depthDesign review

RolesInference EngineeringLLM Product Engineering

The question

A colleague refuses to enable Flash Attention on a quality-sensitive model because "approximate attention changes the outputs". Is it an approximation? Explain what the kernel changes and what it keeps, with the memory argument that motivated it.

Answer it first, aloud or on paper. Then open the answer.

The answer

Show short answer

It is exact. The kernel tiles the computation so the full N by N score matrix never lands in the GPU's main memory; each tile is scored, exponentiated and accumulated in on-chip memory with a running maximum and a running sum, and the final normalization matches the single-pass softmax up to floating-point reordering. What changes is the memory traffic: three round trips to main memory become one write per output tile, and the footprint drops from N squared to N. The numbers that leave the kernel are the same as standard attention.

Show full answer

Why standard attention is slow

output
S = Q @ K^T       read Q and K, write an N x N matrix
P = softmax(S)    read N x N, write N x N
O = P @ V         read N x N and V, write N x d

Three round trips through high-bandwidth memory. On the reference accelerator that memory moves about 3 TB/s while the on-chip memory moves about 30 TB/s, so every round trip is a factor-of-ten slowdown against keeping the data on chip. From about 2K tokens, attention becomes memory-bound before it becomes compute-bound, and classic kernels underuse the hardware by 4 to 10 times.

The tiling

output
for each block of Q (about 128 x 128):
    load the Q tile on chip
    for each block of K, V:
        load the K and V tiles on chip
        scores for the tile
        running softmax aggregation
        accumulate into the output tile
    write the output tile once

The score matrix exists only one tile at a time. The backward pass recomputes some values instead of storing them, which is a second memory win.

Why the result is exact

The softmax needs the global maximum and the global sum over a row, and the kernel only sees one tile at a time. It keeps a running pair, maximum and sum, and whenever a new tile raises the maximum it rescales the earlier sum and the earlier partial output by the exponential of the old maximum minus the new one. At the end every term is expressed against the same global maximum and divided by the same global sum. The lesson's description is "bit-identical output to standard attention", with the caveat that 16-bit arithmetic is not associative, so reordering can move the last bits.

Versions

VersionYearChangeSpeedup on the reference hardware
Flash 12022tiled on-chip kernel2 times on the earlier accelerator
Flash 22023better parallelism, causal-first ordering3 times on the same
Flash 32024asynchrony and FP8 on the newer accelerator1.5 to 2 times
Flash 42026five-stage pipeline for the latest generationinference-first, forward pass only at launch

What to tell the colleague

Run the two kernels on the same inputs and compare the outputs. The difference is at the level of 16-bit rounding, the same noise you get from a different batch size, not a change to the model.

Follow-ups

  1. L3SeniorWhy does the kernel keep a running maximum and not just a running sum?

    Show expected answer

    numerical stability; exponentials of raw scores overflow, so every exponential is taken relative to the maximum seen so far and rescaled when it changes.

  2. L3SeniorWhy recompute values during training instead of storing the softmax output?

    Show expected answer

    storing it would mean materializing the N by N matrix, which is the thing the kernel avoids.

  3. L4StaffThe newest version is forward-only at launch. What does that mean for your stack?

    Show expected answer

    serving can use it, training keeps the previous version until the backward kernel ships.

  4. L4StaffIs the output identical across batch sizes?

    Show expected answer

    not bit for bit, because the reduction order changes in 16-bit arithmetic; that is a determinism question, not an approximation.

Wrong answers

Show the wrong answers that interviewers hear
  • “It drops small attention weights to save work.”

    Nothing is dropped; every key contributes through the running sum.

  • “It only helps at very long context.”

    It helps whenever attention is memory-bound, and the lesson reports 2 to 4 times on the earlier accelerator for ordinary lengths.

  • “It reduces the FLOPs of attention.”

    The arithmetic is the same; the saving is in memory traffic.

Taught in

The KV cache and Flash Attention lesson explains the tiling, the running softmax and the version table.