Unit 2 of 12 · about 4 hours · explain, derive

Prefill and decode attention shapes and IO-aware attention

Attention shapes, prefill and decode reuse, and materialized traffic already supplied the square-attention ledger. Serving adds cached suffixes, mixed request lengths and a shape distribution that can invalidate a single-kernel choice.

You'll be able to

  • Derive rectangular attention shapes for uncached prefill, cached chunks and ordinary decode.
  • Separate query-head arithmetic from KV-head traffic and identify potential GQA reuse.
  • Predict when query tiling or split-KV parallelism is worth testing without assuming a bandwidth bottleneck.

Covers

Prefill attention tensor shapesDecode attention tensor shapesIO-aware attention and FlashAttention
1

Read

In this order, only the parts listed

0 of 2 done

  1. Serving intensity and tile selection, arXiv:2501.01005v1FlashInfer: Efficient and Customizable Attention Engine for LLM Inference Serving

    Section 2.1, FlashAttention; Sections 3.2.1 and 3.2.2, data movement and microkernel tile sizes; Appendix A, Head Group Fusion for Grouped-Query Attention

    Use the paper's intensity as an attention-only model. Its statement about batching does not apply to weight reuse in the rest of a transformer.

  2. Forward attention state and workCS336 Assignment 2 - FlashAttention and Triton Sections

    Section 4.2.2, Details of the FlashAttention forward pass and Algorithm 1, printed pp. 24 to 25

    Reuse your completed forward implementation. Skip backward and the training benchmark sweep; extend the shape ledger to rectangular cached attention.

2

Focus on

  • For one request with query tokens, visible KV tokens, query heads and head width , scores have shape . Uncached prefill has equal lengths; a cached chunk has more keys than queries; ordinary decode has one query.
  • Dense score and value products cost FLOPs, counting a multiply-add as two. This excludes scaling, masking and softmax; useful causal pairs and executed padded tiles need separate counts.
  • An ideal single KV read costs bytes for equal key/value widths and bytes per element. Reusing these bytes across grouped heads is a schedule property, not a consequence of fewer stored heads alone.
  • IO-aware attention removes global score/probability materialization. Short queries still offer little query-row reuse; split-KV adds parallelism but needs partial-state storage and reduction.
Common mistakes · 3
  • Using a square or upper-left causal mask for a cached suffix. Its query positions start after the cached prefix.
  • Reducing arithmetic by the GQA group size. Query heads still compute distinct scores and outputs.
  • Calling a low-intensity attention shape bandwidth-bound without timings and profiler evidence. Launch supply or insufficient parallelism may dominate.
3

Try it yourself

Closed-book, before checking anything

Closed-book, draw a request with 5 cached tokens and a 3-token chunk, four query heads, two KV heads and width 8. Scores are ; the three query rows allow 6, 7 and 8 keys respectively. Derive 3072 dense-product FLOPs versus 2688 useful causal-product FLOPs, and 512 bytes for one ideal KV read with two-byte storage. Keep query/output bytes, softmax work, metadata and actual reloads outside that KV-only count.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Does batching unrelated decode requests create reuse of each request's KV across query tokens?Answer

    No. Ordinary decode still has one query per request. Batching adds parallel work and can improve weight reuse elsewhere, but unrelated requests have different KV.

  2. 2.Why distinguish useful causal FLOPs from executed FLOPs?Answer

    A masked tile can still execute dense instructions or padded lanes. A mathematical pair count is not a hardware instruction count.

  3. 3.Why can FlashAttention reduce traffic without reducing the attention pair count?Answer

    It streams tiles and retains row state on chip instead of writing quadratic intermediate matrices. It still evaluates the permitted query-key interactions.

You're done when

You derive the cached-chunk ledger and mask from absolute positions, then explain a different request mix without confusing useful work, modeled bytes and measured traffic.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 12 more in the section

Also in this section