Unit 4 of 10 · about 4 hours · derive

FlashAttention IO complexity and FlashAttention-2 work partitioning

IO-aware attention is an algorithm plus a schedule. The second paper improves utilization without making dense attention a different mathematical operation.

You'll be able to

  • Derive the FlashAttention IO bound under its stated two-level memory assumptions.
  • Explain query-block parallelism and split-query warp ownership in FlashAttention-2.
  • Predict tile resource limits and distinguish grid parallelism from resident occupancy.

Covers

FlashAttention IO complexity and memory hierarchy assumptionsFlashAttention-2 work partitioning and occupancy
1

Read

In this order, only the parts listed

0 of 2 done

  1. FlashAttention IO analysis, arXiv:2205.14135v2FlashAttention

    Section 3.1, Algorithm 1; Section 3.2, Theorem 2 and Proposition 3; Appendix C, Proof of Theorem 2 (pp. 23 to 24)

    Skip block-sparse attention. Keep the scope of Proposition 3: it rules out an asymptotic improvement for every memory size in the stated range, not optimality at each fixed size.

  2. FlashAttention-2 schedule, arXiv:2307.08691v1FlashAttention-2

    Sections 3.1.1, 3.2 and 3.3; Algorithm 1 and Figures 2 and 3 (pp. 5 to 9)

    Algorithm 1 line 10 on the rendered v1 page incorrectly inverts the exponential rescaling. Multiply the old numerator by , without an inverse. Use CS336 Algorithm 1 for implementation. Stop before Section 4's hardware-specific results.

2

Focus on

  • In the original square-attention model, is fast-memory capacity in elements and . Key/value tiles permit passes over query/output state, giving slow-memory element accesses.
  • Standard materialization costs accesses in this model. Convert elements to bytes using each buffer's dtype; do not insert a shared-memory byte count directly for . At fixed and , the FlashAttention IO expression is still quadratic in .
  • FlashAttention-2 assigns independent query blocks to thread blocks and keeps their state on chip. Within a block, split-query warps own different output rows, avoiding the split-key partial-output combination described in Figure 3.
  • A tile needs query, key, value, score and output storage, row statistics and compiler temporaries. Larger tiles can reduce reloads but increase registers, spills and shared memory. More blocks in the grid do not guarantee more resident warps.
Common mistakes · 3
  • Reading 'exact attention' as bitwise equality. The algorithm avoids sparse or low-rank approximation; floating-point order and precision still change outputs.
  • Treating the IO theorem as a latency prediction. It omits launch overhead, cache hierarchy, exponentials and hardware scheduling.
  • Confusing split-key warp partitioning with decode's split-KV across thread blocks. They split related dimensions but have different owners, workspaces and merge costs.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive the IO expression by counting key/value tiles and query/output passes. Draw original key-outer and FlashAttention-2 query-outer schedules, marking every slow-memory read/write and the owner of each output row. For square query tiles of 64 rows with sequence length 1024, batch size 1 and eight heads, derive 128 forward thread blocks. Explain why this grid count is not an occupancy measurement.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Why does increasing fast-memory capacity lower the modeled IO?Answer

    Larger key/value tiles fit, so fewer outer tiles require fewer passes over query/output state. This says nothing by itself about the best resident tile on real hardware.

  2. 2.What must split-key workers combine that split-query workers need not combine?Answer

    Contributions to the same output rows. Split-query workers own different rows; split-key workers own different pieces of each row's reduction.

  3. 3.Why might extra query blocks fail to help one-row decode?Answer

    There is only one query row per head, so query tiling creates no substantial new parallelism. Splitting the KV sequence can create work, but needs a stable state merge and extra workspace.

You're done when

You derive the IO bound with its assumptions, explain both ownership diagrams, and identify a resource constraint that prevents unlimited tile growth.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 11 more in the section
  • FlashAttentionTri Dao and collaborators · primary

    Read the IO-awareness argument, exact attention algorithm and complexity analysis. Derive the online state and block dataflow, then implement a reduced forward kernel or trace a trusted implementation across at least causal and noncausal cases.

  • FlashAttention-2Tri Dao · required

    Study reduced non-matmul work, sequence parallelism and warp work partitioning. Compare the schedule with the first paper and predict which shapes benefit before checking measurements.

Also in this section

  • Online Normalizer Calculation for SoftmaxOnline Softmax Authors · required

    Re-derive the recurrence inside tiled attention and prove rescaling correctness. Add extreme-logit, all-masked, odd-head and partial-tile tests before any performance comparison.

  • CS336 Assignment 2 - Section 4 FlashAttention-2 in TritonStanford University · required

    Do the weighted-sum warm-up, then flash_forward (a PyTorch tiled version and the Triton kernel) and flash_backward against tests/test_attention.py. Run flash_benchmarking on sizes that fit your GPU instead of the handout's B200 sweep.

  • TensorTonic Language Modeling from Scratch - Blockwise Online SoftmaxTensorTonic · support

    Solve it before the tiled attention kernel as a check on the rescaling recurrence.

  • LeetGPU - Attention and Transformer ChallengesAlphaGPU · required

    Solve 6 Softmax Attention and 12 Multi-Head Attention or 53 Causal Attention. Then choose two of 50 RMS Normalization, 54 SwiGLU, 61 RoPE, 80 Grouped Query Attention, 83 Fused Residual Add RMS Norm and 84 SwiGLU MLP Block. Keep correctness tests beyond the hosted score.

  • FlashInferFlashInfer Project · support

    Inspect the attention, sampling and serving-kernel interfaces. Reconstruct one dispatch path and compare its layout and workspace contracts with your reduced kernel instead of copying the optimized implementation.

  • GPU MODE - Attention and Fused KernelsGPU MODE · support

    Use Lectures 12, 13, 18, 36 and 40 for FlashAttention, Ring Attention, fusion, FlashAttention-3 and FlashInfer. Reproduce one schedule or fusion idea under the stage shape matrix and a strong baseline.

  • FlashAttention-3FlashAttention Authors · depth

    Study Hopper-specific asynchronous execution and low-precision design after the portable kernel. Implement only with the required architecture, and separate algorithmic from hardware-specific gains.

  • Modern GPU Programming for MLSys - Flash Attention 4MLC · depth

    Read the complete Blackwell attention chapter, including two MMAs, online softmax, causal masking and GQA. Run it only on compatible sm_100a hardware with the pinned TIRx stack; otherwise use the code for architecture reconstruction.

  • PyTorch SDPA contracts and integration tutorialPyTorch · reference

    The PyTorch 2.14 SDPA API page gives mask polarity, rectangular causal alignment, GQA and backend limitations. The SDPA tutorial (https://docs.pytorch.org/tutorials/intermediate/scaled_dot_product_attention_tutorial.html, read 2026-10-13) gives Explicit Dispatcher Control, Causal Self Attention, Using SDPA with torch.compile and Using SDPA with attn_bias subclasses. Correct the tutorial's evaluation-time causal flag before integration.

  • Triton 3.8.0 Layer NormalizationTriton Project · required

    Motivations and _layer_norm_fwd_fused in tutorial 05, pinned to v3.8.0. Study centered variance, float32 reductions, valid-column masks and the forward wrapper's supported-width guard. Defer backward and the benchmark sweep.

  • vLLM serving latency and goodput definitionsvLLM Project · reference

    vLLM v0.31.0: Benchmark CLI, Understanding the Latency Metrics and Load Pattern Configuration; serving benchmark CLI, goodput service-level objectives. Use definitions to translate kernel evidence into request metrics, not as a required serving-engine installation.