Unit 2 of 10 · about 4 hours · derive

Operation counts for projections, MLPs, output and attention

Operation counts are the numerator of every intensity and roofline in this section. A count that mixes prefill with decode, or drops the attention term at long context, corrupts all of them.

You'll be able to

  • Derive the per-token dense operations rule from the qkv, output and MLP walks.
  • Derive prefill and decode attention operations separately and state when attention rivals the dense count.
  • State exactly what a FLOP count omits (softmax, norms, sampling, unembedding) and when the omission stops being safe.

Covers

Useful operations for projections, MLPs and output layersPrefill and decode attention operations by sequence and head shape
1

Read

In this order, only the parts listed

0 of 3 done

  1. Dense walk: 2P operations per tokenTransformer Inference Arithmetic

    flops counting, with the How many flops in a matmul? box in kv cache

    The count covers the dense weights once per token, in prefill and decode alike. Attention comes on top, and in prefill it grows with the square of prompt length.

  2. Attention operations: prefill versus decodeHow to Scale Your Model - Inference

    What about attention? (the intensity derivation) and Theoretical estimates for LLM latency and throughput (Min Step Time with the small-batch quiz)

  3. Key-value cache FLOP savings grow with sequenceTransformer Inference Arithmetic

    kv cache: the one-sixth saving paragraph and the no-cache quadratic paragraph

2

Focus on

  • One matmul through all parameters costs operations by the rule; the layer walk (qkv, output, MLP) sums to the same within 2%.
  • Decode attention per token scales with cached length ( per layer for scores plus values); prefill attention scales with prompt length squared.
  • Softmax, norms, sampling and the unembedding are per layer against dense work; they matter only when the dense term shrinks or a profile says otherwise.
  • Use head count times head dimension, not model width, wherever key-value heads differ from query heads.
Common mistakes · 3
  • Taking per token as the whole prefill cost. Prefill pays the dense term per prompt token plus attention that grows with prompt length squared, so at long prompts the dense count understates the work.
  • Writing the attention scale as division by head dimension. The implemented scale is ; the count is unchanged but a kernel you write later must use the root.
  • Calling attention free in decode because 2P dominates. At 8192 cached tokens the invented model's attention is nearly half the dense count; recompute the share before dropping it.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive decode operations per token for the invented 18.4e9-parameter model at cached length 8192: dense , attention , total about .
4

Quiz yourself

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

0 got · 0 missed

  1. 1.How many dense operations does one decode step take for the invented 18.4e9-parameter model?Answer

    About , from .

  2. 2.At cached length 8192, what is its decode attention count, and what share is that?Answer

    , nearly half the dense count, so it cannot be dropped there.

  3. 3.Why is per token not the whole cost of a prefill?Answer

    counts the dense weights once per token. Prefill also computes attention across all prompt tokens, which grows with prompt length squared, so at long prompts the dense count understates the work.

  4. 4.Without a cache, how does per-step work grow with sequence length ?Answer

    Each step recomputes keys and values for all tokens: operations, or of a one-token forward pass, so totals grow quadratically.

5

Practice

You're done when

You derive the 5.4e10 decode count at length 8192 without notes and reproduce Question 1 exactly.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 7 more in the section
  • How to Scale Your Model - InferenceJAX Scaling Book · primary

    Work through the single-request, batching, key-value memory and parallelism arithmetic. Recompute every quantity for the small model and workload used by the track project, keeping symbols, units and assumptions visible.

  • Transformer Inference ArithmeticKipply · required

    Reproduce the FLOP, parameter-byte, key-value-byte and communication estimates for dense transformer inference. Compare the formulas with an instrumented framework trace and explain model residuals instead of fitting constants after measurement.

Also in this section

  • AI Performance Engineering - Start HereWafer · reference

    A curated, dated index. Its Start Here list names the readings this section already assigns; use the later sections only to find a primary source for a later section, never as the reading itself.

  • Efficiently Scaling Transformer InferenceMLSys · required

    Study the latency, memory and parallelism model. Use the paper to derive a batch and parallelism crossover for your workload, while reserving distributed execution for GPU-9.

  • EtalonEtalon Authors · support

    Read the latency metric definitions, their pitfalls and fluidity-index. Add TTFT and TPOT SLOs to a small load generator, and show why peak token throughput can rank two systems differently from an SLO-qualified rate.

  • GPU Microbenchmark and Kernel SuiteRoadmap-Final · support

    Adopt the project harness schema, architecture constraints and hardware tiers now. Implement only the environment manifest, independent references, raw-trial storage and the initial operation and byte models in this stage.

  • How to Scale Your Model - TrainingJAX Scaling Book · required

    Work Some Problems to Work Questions 1 to 3 for training-step parameters, Adam and activation bytes and step time; read Data Parallelism and Fully-Sharded Data Parallelism for the comms-bound batch rule.

  • Amdahl's lawWikipedia · reference

    Read Definition and Derivation for the overall-speedup formula with the worked share examples; apply it to kernel time shares at fixed workload.

  • DistServe: Disaggregating Prefill and Decoding for Goodput-optimized LLM ServingZhong et al. (OSDI 2024) · support

    Read Section 1 with Figure 1 for per-GPU goodput: the highest request rate served while a target share of requests meets the TTFT and TPOT SLOs.