Module 4 · Lesson 3 of 5

FlashAttention: tiles, fusion and IO

Keep the attention result; change where intermediates live and how often inputs move.

Terms from Module 3 you need

This lesson uses these GPU terms; each link opens the Module 3 lesson that teaches it.

Global memory
The GPU’s large main memory (HBM on data-centre GPUs), reachable by every thread. It is the slow boundary: K/V, weights and any full score matrix normally live here. Module 3.2 →
Shared memory
Small, fast memory shared by the threads of one thread block; a kernel stages a tile of operands here on purpose so the block can reuse them. Module 3.2 →
Register
Per-thread working storage, the fastest and smallest level; running sums (accumulators) live here. Too many per thread can spill to slower memory. Module 3.2 →
Tile reuse
Load a tile of operands once into fast memory and use each value for many outputs, so fewer bytes cross the slow boundary. Module 3.3 →
Logical vs actual bytes
Logical bytes are what the algorithm asks to load in the model; actual bytes are what the hardware moves, which caches, padding and spills can change. Name the boundary you count. Module 3.4 →
Bandwidth
Bytes per second a memory path can deliver. When a step needs more bytes than the path supplies, memory, not arithmetic, sets its speed. Module 3.4 →

Why tiling needs online normalization

A tile is a small matrix patch. It lets us compute and consume temporary scores while they are in the active working set, then reuse their storage.

What you will be able to explain

Trace tile residency; account for transfers; explain larger-tile resource trade-offs without promising speedup.

In lesson 3.3, operand reuse reduced logical loads for matrix multiplication. Attention adds normalization across all allowed keys: online summaries from lesson 4.2 let us finish a row without keeping every score in global memory.

Input memoryQ, K and V persist until consumed.
Temporary tileSmall score/exponential patch; consumed, then reused.
Running stateQuery, maximum, denominator and weighted numerator retained.
Output memoryCompleted O stored after normalization.

“On chip” is an algorithmic working-set description. Optimized kernels split values across registers/shared memory and architecture-specific resources; this schematic is not a physical die map.

Follow one query through two K/V tiles

This focused trace uses the same four-position arithmetic fixture as 4.2. Storage reads/writes use two bytes per element; resource sizing uses float32 accumulators.

The teaching loop keeps one Q row and its summary active while loading successive K/V tiles. Observe which operands are ready, which temporary storage is reusable, and when output becomes complete.

Q read: 4 bytes; two K/V tiles: 32 bytes; O write: 4 bytes. Total 40 logical bytes in this one-query example.

These are dependency steps, not a GPU timing trace. The full four-position score/probability arrays are not written to global memory by this schedule.

Open focused tile walkthrough · Revisit shared-memory multiplication barriers

Count the chosen loop, then discuss its limits

More query rows per tile can reuse each K/V load across more queries. Larger tiles also require more active state.

For query-tile outer loop, key/value-tile inner loop: Q reads Tq × dk elements; K/V reads ceil(Tq / Br) × Tk × (dk + dv); O writes Tq × dv. This noncausal count assumes query and summary remain resident. Br means query rows per tile; Bc means key rows per tile.

Worked number (invented, one head, one layer, one sequence, noncausal, two bytes per element). Take Tq = Tk = 1,024 and dk = dv = 128. With Br = 64, ceil(1,024 / 64) = 16 passes over K/V, so K/V reads = 16 × 1,024 × (128 + 128) = 4,194,304 elements = 8 MiB. With Br = 128 the factor is ceil(1,024 / 128) = 8, so reads = 8 × 1,024 × 256 = 2,097,152 elements = 4 MiB. Doubling the query tile halves the K/V loads. For comparison, Q is read once (1,024 × 128 = 131,072 elements) and O written once. These are logical loads, not measured memory traffic.

Explore in the tile walkthrough computes a separate 8 × 8 sizing example. All totals come from the same event ledger as its read/write counts. Peak modeled storage includes Q/K/V tiles, float32 score scratch, numerator and row statistics. No occupancy percentage or speedup is inferred.

Predict: A bigger query tile reduces K/V loads. Must runtime fall?

for query_tile in Q:
    load(query_tile)
    summary = empty_rows()
    for key_tile, value_tile in KV:
        load(key_tile, value_tile)
        scores = scaled_masked_dot(query_tile, key_tile)
        summary = merge(summary, scores, value_tile)
    store(normalize(summary))
Causal tiles, versions and backward pass

Skip tiles wholly above the causal diagonal. A diagonal tile can contain masked cells; loaded slots and valid score pairs are separate counts. Ragged last tiles use valid dimensions in this analytical model. Real kernels may pad loads.

This query-outer schedule is a teaching choice, not a transcription of FlashAttention Algorithm 1. FlashAttention-2 changes work partitioning and reduces non-matmul overhead. Larger FlashAttention versions bring further hardware-specific techniques; learning the invariant does not require replaying each version.

Training backward can recompute score/probability tiles from saved inputs and normalization statistics, trading extra computation for less stored activation traffic. This module’s main path is inference forward; no backward kernel implemented here.

FlashAttention §§3.1–3.2 · FlashAttention-2

Takeaway: Tiling cuts the bytes that cross global memory by keeping score tiles in the working set; it does not remove the dense query–key work, and fewer bytes is not by itself a speedup.