Skip to content

Rooflines and arithmetic intensity

25 min read

Prerequisites: TPU hardware for PyTorch engineers

A TPU step that takes 40 ms is either doing 40 ms of arithmetic or waiting for bytes, and the fix for one is useless for the other. Batching more tokens does nothing for an operation that is already saturating the MXU; a faster kernel does nothing for one that is starved by HBM. This chapter gives you the test that tells the two apart before you write any code: count an operation's FLOPs and bytes, divide by the chip's peak rates, and compare. The test is called a roofline, and every performance argument in Parts III to V of this book is built from it. The ideas follow Part 1 of the scaling book (How to Scale Your Model), All About Rooflines, applied to PyTorch shapes and the v6e and v7x numbers from chapter 1.

What this chapter covers

  • Why an operation's time lies between the larger of its compute and memory times and their sum, and what overlap has to do with it.
  • Arithmetic intensity of a matmul and of an elementwise operation, from tensor shapes and dtype.
  • The ridge point of a v6e or v7x device, and classifying an operation as compute-bound or memory-bound.
  • How the token batch moves a matmul across the ridge point, and why some layers never reach it.
  • Estimating the run time of an operation or a block before profiling, and comparing it with a measurement.
  • The same reasoning applied to bytes sent between chips over ICI.

Where the time goes

An operation on a TPU does two kinds of work. The MXU (or the VPU) does arithmetic, and the DMA engines move bytes between HBM and VMEM. Each has a peak rate, so each has a minimum time:

\[ T_\text{math} = \frac{\text{FLOPs}}{\text{peak FLOP/s}} \qquad T_\text{mem} = \frac{\text{bytes moved to and from HBM}}{\text{HBM bandwidth}} \]

The two kinds of work can run at the same time. While the MXU multiplies one tile, the next tile is already being copied in. If the overlap is perfect, the operation takes as long as the slower of the two; if there is no overlap at all, it takes their sum. That gives a lower and an upper bound:

\[ \max(T_\text{math}, T_\text{mem}) \;\le\; T \;\le\; T_\text{math} + T_\text{mem} \]

The two bounds are never more than a factor of 2 apart, because a sum of two numbers is at most twice the larger one. The scaling book's advice is to plan against the lower bound: copies are pipelined with compute, and a well-overlapped program usually comes close to it.

Three timelines, each with an HBM to VMEM lane and an MXU lane, drawn tile by tile over four tiles. Top: compute-bound with copies overlapped; the MXU lane is busy almost all the time and the total is 4.6 units against T_math 4.0. Middle: memory-bound with copies overlapped; the copy lane is busy all the time, the MXU works in short bursts with gaps, and the total is 4.5 units against T_mem 4.0. Bottom: the same compute-bound work with no overlap; copy and MXU alternate and the total is 6.4, the sum of 4.0 and 2.4.

Watch which lane is never idle. In the top row it is the MXU, so the operation finishes near \(T_\text{math}\); in the middle row it is the copy lane, so the MXU sits in gaps and the operation finishes near \(T_\text{mem}\). The bottom row does the same work as the top but waits for each copy, and pays the sum. The time units are illustrative.

When \(T_\text{math} > T_\text{mem}\) the operation is compute-bound: the MXU is the busy resource and the copies hide behind it. When \(T_\text{mem} > T_\text{math}\) it is memory-bound (also called bandwidth-bound): the MXU finishes each tile early and waits for the next one, so part of the chip's FLOP/s is wasted. This book uses "memory-bound" for HBM and "communication-bound" when the slow link is between chips.

Arithmetic intensity

You do not need both times to know which one is larger. Divide the condition \(T_\text{math} > T_\text{mem}\) through and it becomes a comparison between one property of the operation and one property of the chip:

\[ \frac{\text{FLOPs}}{\text{peak FLOP/s}} > \frac{\text{bytes}}{\text{bandwidth}} \iff \underbrace{\frac{\text{FLOPs}}{\text{bytes}}}_{\text{intensity of the op}} > \underbrace{\frac{\text{peak FLOP/s}}{\text{bandwidth}}}_{\text{ridge point of the chip}} \]

The left side is the operation's arithmetic intensity: FLOPs per byte moved. The right side is the chip's ridge point (the scaling book calls it the hardware's peak arithmetic intensity): the intensity an operation needs to keep the MXU at its peak. If the operation's intensity is above the ridge point, it is compute-bound; below, memory-bound.

A matmul. For x @ w with x of shape [M, K] and w of shape [K, N], the MXU does one multiply and one add for every triple of indices, so \(2MKN\) FLOPs. In bf16 it reads \(2MK + 2KN\) bytes and writes \(2MN\) bytes, assuming each tensor crosses HBM once:

\[ \text{intensity}(\text{matmul}) = \frac{2MKN}{2(MK + KN + MN)} = \frac{MKN}{MK + KN + MN} \]

The MXU accumulates in fp32, but the scaling book notes that the result is usually cast to bf16 before it is written back to HBM, which is why the output counts 2 bytes per element. If your matmul writes fp32 results, count 4.

Take a square case, \(M = K = N = 4096\):

\[ \frac{2 \times 4096^3}{2 \times 3 \times 4096^2} = \frac{4096}{3} \approx 1365.3 \ \text{FLOPs per byte} \]

That is above the ridge point of both v6e (about 560) and v7x (about 313), so this matmul is compute-bound on both. The companion function reproduces it:

from roofline import arithmetic_intensity, bound, CHIPS   # code/ch02_rooflines/roofline.py

ai = arithmetic_intensity(4096, 4096, 4096)               # 1365.3
v7x = CHIPS["v7x"]
bound(ai, v7x.bf16_flops_per_device, v7x.hbm_bw_per_device)   # "compute"

An elementwise operation. a + b on two bf16 tensors of \(n\) elements does \(n\) FLOPs, reads \(4n\) bytes and writes \(2n\) bytes: an intensity of \(1/6\), whatever \(n\) is. Every element is used once, so there is no reuse to amortize the byte. The scaling book's dot product example makes the same point: its intensity tends to \(1/2\) as the vectors grow, and no length makes it compute-bound.

That contrast is the core of the chapter. A matmul's intensity grows with its dimensions, because each byte loaded is reused across a whole row or column of the other operand. An elementwise operation's intensity is a small constant, fixed by how many FLOPs it does per element.

The ridge point of each chip

The ridge point is peak FLOP/s divided by HBM bandwidth. With the numbers from chapter 1's generations table (bf16 peak from TorchTPU's tpu_hardware.cc, HBM bandwidth from Google's published specifications):

v5e v5p v6e v7x per chip v7x per device
Peak bf16 FLOP/s 197 T 459 T 918 T 2,307 T 1,153.5 T
HBM bandwidth 819 GB/s 2,765 GB/s 1,638 GB/s 7,380 GB/s 3,690 GB/s
Ridge point, FLOPs per byte 240 166 560 313 313

The v5e figure is the scaling book's worked example (it rounds to 240). For v7x the division gives 312.6, which this book rounds to 313. Two things in the table are worth stopping on.

Per chip and per device give the same ridge, because a v7x device gets half the chip's FLOP/s and half its bandwidth. The danger is in mixing them: 2,307 TFLOP/s per chip divided by 3,690 GB/s per device gives 625, twice the true ridge. Always divide numbers that describe the same thing.

v7x's ridge is lower than v6e's, although v7x has more than twice the peak FLOP/s per chip. The reason is that Ironwood's bandwidth grew faster than its compute: 4.5 times v6e's HBM bandwidth against 2.5 times its FLOP/s. A lower ridge means that an operation needs less reuse per byte to keep the MXU busy, so more of a model is compute-bound on v7x than on v6e.

Log-log roofline chart, attainable bf16 TFLOP/s per device against arithmetic intensity. Two rooflines: v6e (dashed teal) rises along the bandwidth slope to a flat ceiling of 918 TFLOP/s at a ridge of 560 FLOPs per byte; v7x (solid blue) rises along a steeper bandwidth line to a ceiling of 1,153.5 TFLOP/s at a ridge of 313. Four kernels are marked on both lines: a bf16 add at 0.17 FLOPs per byte, a decode GEMV of shape 8 by 8192 times 8192 by 8192 at 8.0, a 256 by 8192 times 8192 by 8192 matmul at 240.9, and a square GEMM of shape 4096 by 4096 times 4096 by 4096 at 1,365.3.

Read the chart from left to right. On the sloped part, attainable FLOP/s is intensity times bandwidth, so v7x's line sits higher because each device has more bandwidth; on the flat part it is the peak. Only the 4096 GEMM reaches the flat roof on both chips. The 256-token matmul sits just left of both ridges: it reaches about 77% of peak on v7x and about 43% on v6e. The add and the GEMV use a few percent of the MXU or less.

The chart is the whole roofline idea in one picture: the attainable rate of an operation is \(\min(\text{peak}, \ \text{intensity} \times \text{bandwidth})\). Moving an operation up the sloped part needs more intensity (more reuse per byte) or more bandwidth. Moving it along the flat part changes nothing; it is already at the peak.

Peak bandwidth is a ceiling, not a forecast

The ridge points above use spec-sheet figures. For a v7x chiplet, the TPU Agent Suite's chip constants give 3,686 GB/s theoretical HBM bandwidth and 3,207 GB/s practical. With the practical figure the v7x ridge moves from 313 to about 360 FLOPs per byte. Use the peak to classify an operation, and say which figure you used whenever you turn the classification into a predicted time.

Matmuls: intensity grows with the batch

Most of a transformer's FLOPs are linear layers: activations x of shape [B, D] times weights w of shape [D, F], where \(B\) is the number of tokens the layer sees at once on this device. Weights are large (\(D\) and \(F\) in the thousands) and \(B\) is often smaller. When \(B\) is much smaller than \(D\) and \(F\), the weight bytes dominate the denominator:

\[ \frac{BDF}{BD + DF + BF} \approx \frac{BDF}{DF} = B \]

The intensity of a linear layer is roughly its token batch. That gives the most useful rule in this chapter: a bf16 linear layer is compute-bound once the token batch per device exceeds the chip's ridge point, roughly 560 tokens on v6e and 313 on v7x. The scaling book stresses that \(B\) here counts tokens, not sequences: 4 sequences of 128 tokens are 512 tokens for this purpose.

An animation over eleven frames. A bf16 matmul of shape B by 8192 times 8192 by 8192 is placed on the v6e and v7x rooflines as B grows from 8 to 4,096 tokens. Each frame states B, the intensity and, for each chip, whether the matmul is memory-bound or compute-bound and what fraction of peak it reaches. At 339 tokens v7x turns compute-bound while v6e is still memory-bound at about 56 percent of peak; at 650 tokens both are compute-bound.

Follow the two markers. They climb the sloped part together, the v7x marker reaches its roof first at 339 tokens, and the v6e marker needs 650. Past both ridges, more tokens add work without adding speed per token.

The rule \(B > \text{ridge}\) is an approximation. With exact byte counts, the critical batch (the smallest whole number of tokens at which the layer is compute-bound) for \(D = F = 8192\) is 650 tokens on v6e and 339 on v7x, a little above the ridge because the activation and output bytes are not quite negligible. For smaller layers the gap grows, and for small enough layers the crossing never comes: as \(B\) grows without limit, the intensity approaches \(DF/(D + F)\), which for \(D = F = 1024\) is 512. That is below v6e's ridge of 560, so a 1024-wide bf16 layer is memory-bound on v6e at every batch size. critical_batch() in the companion code solves the exact equation and returns None in that case; worked problem 5 uses it.

Four refinements change the threshold in predictable directions:

  • Lower-precision weights lower it. If weights are stored in int8 or fp8 but the arithmetic stays in bf16, the weight bytes halve and the intensity becomes about \(2B\), so the threshold halves (about 156 tokens on v7x). The scaling book works the int8-weight case; the numbers here are for v7x.
  • Lower-precision arithmetic raises the ridge. v7x does fp8 matmuls at twice its bf16 rate, so its fp8 ridge is twice as high, about 625 FLOPs per byte. If both weights and activations are 8-bit, the intensity doubles too and the token threshold stays about where it was.
  • Tiling lowers the real intensity. A large matmul is computed in tiles that fit in VMEM, and operands are re-read for each tile they meet. The scaling book shows that a tiled matmul with tile sizes \(b_m \times b_n\) has an intensity of about \(b_m b_n / (b_m + b_n)\), so tile shape matters. XLA picks tiles for you; in a Pallas kernel you pick them (chapter 12).
  • Operands that stay in VMEM need far less intensity. On v7x, VMEM-to-MXU bandwidth is about 7 times HBM bandwidth, so an operation that reads from VMEM needs about one seventh of the intensity to reach peak. VMEM is small, so this mostly matters inside custom kernels.

Elementwise operations and fusion

Every elementwise operation has an intensity well under 1 FLOP per byte: about \(1/6\) for a bf16 add, a little more for a GELU or an RMSNorm that does several FLOPs per element. Against ridge points in the hundreds, they are deep in memory-bound territory on every TPU. Their cost is the HBM round trip, not the arithmetic.

The VPU, not the MXU, runs these operations, and its peak is far lower: the scaling book estimates about \(1.8 \times 10^{13}\) bf16 FLOP/s per v7x TensorCore. The TPU Agent Suite's roofline model uses a higher figure, 55.8 TFLOP/s bf16 per chiplet. Either way the VPU's ridge against HBM is in the single or low double digits (about 5 or about 15 FLOPs per byte per device), still far above the intensity of an add. Elementwise work is memory-bound whichever peak you use.

Since the bytes are the cost, the only lever is moving fewer of them. Take three elementwise operations in a row on a bf16 [8192, 8192] activation (134 MB per tensor): a residual add, a scale by a scalar, and an activation function. Run one at a time, each reads its inputs from HBM and writes its result back: 3 + 2 + 2 = 7 tensor transfers, about 940 MB, about 255 µs on one v7x device. Fused into one kernel, the chain reads the two inputs and writes one output: 3 transfers, about 403 MB, about 109 µs. The arithmetic did not change; the bytes fell by more than half, and so did the time. This is why TorchTPU's DeferAndFuse mode and torch.compile matter so much on a TPU (chapter 4): fusion is the fix for a memory-bound chain of small operations.

Communication rooflines: a preview of ICI

So far the slow link has been HBM. The same arithmetic works for any link, and when a model is split across chips the link that matters is often ICI. Intensity is then FLOPs per byte sent between chips, and the "ridge" is peak FLOP/s divided by the link's bandwidth.

The scaling book's example splits a matmul x @ w, with x of shape [B, D] and w of shape [D, F], across two chips along \(D\). Each chip multiplies its half (x[:, :D//2] @ w[:D//2, :] on one, the other half on the other), producing a partial [B, F] result that must be added to the other chip's. So:

\[ T_\text{math} = \frac{BDF}{\text{peak FLOP/s per chip}} \qquad T_\text{comms} = \frac{2BF}{\text{link bandwidth}} \]

The intensity with respect to the link is \(BDF / 2BF = D/2\). The batch cancels: whether this sharded matmul hides its communication depends on \(D\), the dimension that was split, not on \(B\). For two v6e chips joined by one ICI link at 90 GB/s each way, compute-bound needs \(D/2 > 918 \times 10^{12} / 90 \times 10^{9} = 10{,}200\), so \(D > 20{,}400\). A layer with \(D = 8192\) split this way spends more time sending partial sums than computing them.

That single example already explains why the choice of which dimension to shard, and over which link, decides whether a parallel layout scales. On v7x there is an extra wrinkle from chapter 1: the two devices on a chip share the chip's external ICI links, and the TPU Agent Suite's v7x reference halves each device's bandwidth when both drive the same link. Chapter 13 builds the full set of collective rooflines.

Estimating before you profile

A roofline estimate takes a few minutes and tells you what a profile should show. Make it before you open XProf, so that the profile answers a question instead of posing one.

A six-step procedure in gray boxes on three columns, with one decision. Top row, left to right: step 1, List one step's large ops, per device, after sharding; an arrow to step 2, Count FLOPs, 2·M·K·N per matmul, a few per element otherwise; an arrow to step 3, Count HBM bytes, inputs and outputs at their stored dtype. From step 3 an arrow turns back to the start of the second row: step 4, Two bounds per op, T_math = FLOPs / peak and T_mem = bytes / bandwidth. An arrow leads right to a white question, Intensity above the ridge point? Yes leads right to a blue card, Compute-bound, check MXU alignment and padding. No leads down to a blue card, Memory-bound, fuse, batch more tokens, or shrink bytes. Both cards lead down into step 5 on the bottom row, Sum the bounds, from Σ max(T_math, T_mem) to Σ (T_math + T_mem), which leads right to a shorter box on the same centre line, step 6, Measure, then compare.

Steps 2 to 4 and the question are done for each large op; step 5 adds them up into a range for the step time, whose low end assumes compute and memory traffic overlap and whose high end assumes they do not. The two cards classify each op and say where a fix would come from.

Worked problem 8 applies the procedure to a SwiGLU MLP block on one v7x device, with 4,096 tokens, hidden size 4,096 and intermediate size 14,336. The three matmuls have an intensity of 1,792 and are compute-bound at about 417 µs each; the elementwise silu(gate) * up in between is memory-bound at about 95 µs. The block's estimate is 1,347 µs to 1,578 µs, and the unfused elementwise step is about 7% of the lower bound: worth fusing, not worth a custom kernel.

Six ways an estimate goes wrong

  • Per chip against per device. On v7x, mixing them is a factor of 2 in the ridge and in every time.
  • Logical shapes instead of padded ones. The MXU pads small dimensions up to 256 on v6e and v7x (chapter 1). A layer with an output dimension of 100 costs the MXU time of 256.
  • The wrong output dtype. An fp32 result writes 4 bytes per element, not 2.
  • Assuming each tensor crosses HBM once. Tiled matmuls re-read operands, and an unfused chain writes and re-reads every intermediate.
  • Peak where you meant practical. Spec-sheet bandwidth is a ceiling; say which figure you used.
  • Device time where you meant step time. The roofline covers device work only. Compilation on the first call (chapter 7), host gaps such as .item() (chapter 4) and input loading are outside it.

Verifying the result

A roofline estimate is right when the machine's measurement lands within its bounds, and when you can explain any disagreement.

  1. Reproduce the arithmetic. Run python code/ch02_rooflines/roofline.py --cpu-smoke (source). It prints the square 4096 GEMM (intensity 1365.3, compute-bound on v6e and v7x), a decode-sized GEMV, an elementwise add, the critical batches and the ICI threshold. Work at least one of them on paper first and check you land on the same number.
  2. Predict, then time one operation on a TPU. On a TPU VM with TorchTPU, run the script without --cpu-smoke, once with the default 4096 GEMM and once with --m 8 --k 8192 --n 8192. Before each run, write down the bound and the lower-bound time you expect. The script reads the peak per device from torch.tpu.get_device_properties(0) (an experimental API), warms up so the first-call compilation is excluded, times 20 calls, calls torch.accelerator.synchronize() so device time is included rather than only dispatch, and prints the measured time as a multiple of the lower bound and as a fraction of peak FLOP/s.
  3. Read the ratio. The measured time should never be below the lower bound; if it is, the chip figures are wrong (per chip against per device is the usual cause). The scaling book notes that TPUs typically reach close to 95% of their claimed peak FLOP/s in normal use, so a large compute-bound GEMM should land close to its bound, and a memory-bound GEMV close to its bandwidth bound. A ratio far above 1 means something outside the roofline is costing time, and that is what to look for next.
  4. Find the operation in XProf. Profile the same code with torch.profiler and TorchTPU's profiler config, as in TorchTPU performance: execution modes, XProf and Pallas kernels:

    import torch
    from torch.tpu.profiler import TpuProfilerConfig
    
    config = TpuProfilerConfig(host_tracer_level=2, device_tracer_level=1, run_dir=log_dir)
    with torch.profiler.profile(
        activities=[torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.TPU],
        experimental_config=config,
        on_trace_ready=torch.profiler.tensorboard_trace_handler(str(log_dir)),
    ) as prof:
        for _ in range(10):
            out = a @ b
            torch.accelerator.synchronize()
            prof.step()
    

    Open the trace with xprof --logdir=<log_dir> --port=6006 and compare the matmul's device time with your estimate. For a whole step, compare the step time with the sum of your per-op bounds. Large gaps between TPU ops are host time the roofline does not model; that post describes a healthy profile as long continuous TPU blocks with MXU utilization above 60%. Chapter 10 covers reading XProf in depth.

Worked problems

Problem 1. Explain why a + b on two bf16 tensors is memory-bound on every TPU in this chapter, however large the tensors are.

Answer

Each element costs 1 FLOP and 6 bytes (two 2-byte reads and one 2-byte write), an intensity of \(1/6\) that does not depend on the tensor size because no element is reused. The lowest ridge point in the table is v5p's 166 FLOPs per byte, three orders of magnitude higher. Even against the VPU's much lower peak (a ridge of roughly 5 to 15 FLOPs per byte per v7x device, depending on which source's VPU rate you use) it is far below. Only reducing the bytes, by fusing the add with its neighbours, makes it faster.

Problem 2. A v7x chip has about 2.5 times the bf16 peak of a v6e chip, yet its ridge point is lower (313 against 560). Explain how, and what it means for the token batch a linear layer needs.

Answer

The ridge is peak FLOP/s divided by HBM bandwidth. From v6e to v7x the peak grew by \(2{,}307 / 918 \approx 2.5\) times per chip, but HBM bandwidth grew by \(7{,}380 / 1{,}638 \approx 4.5\) times, so the ratio fell. A linear layer's intensity is about its token batch, so on v7x it becomes compute-bound at about 313 tokens per device instead of about 560 on v6e: fewer tokens are enough to keep the MXU busy.

Problem 3. A colleague computes the v7x ridge point as 2,307 TFLOP/s divided by 3,690 GB/s and gets 625 FLOPs per byte. What went wrong, and why might the number look familiar?

Answer

The numerator is per chip and the denominator is per device; on v7x a device is half a chip, so the result is twice the true ridge. Per chip it is \(2{,}307 / 7{,}380 \approx 313\) TFLOP per TB, and per device \(1{,}153.5 / 3{,}690 \approx 313\). The 625 looks familiar because it is close to v7x's fp8 ridge (fp8 runs at twice the bf16 rate), which makes the mistake easy to miss.

Problem 4. For a bf16 GEMM, [4096, 4096] @ [4096, 4096], compute the FLOPs, the HBM bytes, the intensity, and the lower and upper time bounds on one v6e device and one v7x device.

Answer

FLOPs: \(2 \times 4096^3 \approx 1.374 \times 10^{11}\). Bytes: \(2 \times 3 \times 4096^2 = 100{,}663{,}296 \approx 1.007 \times 10^{8}\). Intensity: \(4096 / 3 \approx 1365.3\) FLOPs per byte, above both ridges, so compute-bound on both.

\(T_\text{math} = 1.374 \times 10^{11} / 918 \times 10^{12} \approx 149.7\) µs. \(T_\text{mem} = 1.007 \times 10^{8} / 1.638 \times 10^{12} \approx 61.5\) µs. The estimate is 149.7 µs (perfect overlap) to 211.2 µs (none).

Per device: \(T_\text{math} = 1.374 \times 10^{11} / 1{,}153.5 \times 10^{12} \approx 119.1\) µs. \(T_\text{mem} = 1.007 \times 10^{8} / 3.69 \times 10^{12} \approx 27.3\) µs. The estimate is 119.1 µs to 146.4 µs.

python code/ch02_rooflines/roofline.py --cpu-smoke prints the same numbers.

Problem 5. For square bf16 linear layers [B, D] @ [D, D] with \(D\) = 1,024, 4,096 and 8,192, find the smallest token batch \(B\) that makes the layer compute-bound on a v6e device and on a v7x device, using exact byte counts. Write a short script that prints the table.

Answer

Set the exact intensity equal to the ridge \(r\) and solve: \(B D^2 / (2BD + D^2) = r\) gives \(B = r D^2 / (D^2 - 2rD)\), which only has a positive solution when \(D/2 > r\).

\(D = F\) v6e (ridge 560.4) v7x (ridge 312.6)
1,024 never: intensity caps at 512 803 tokens
4,096 772 tokens 369 tokens
8,192 650 tokens 339 tokens

Larger layers get closer to the "batch equals ridge" rule. A 1,024-wide layer on v6e is memory-bound at any batch, because even with infinitely many tokens each weight byte is reused at most \(D \cdot F / (D + F) = 512\) times. The solution script is code/solutions/ch02/critical_batch.py; it calls critical_batch() from the chapter's companion module.

Problem 6. During decode, 8 tokens pass through a bf16 [8192, 8192] weight on one v7x device. Estimate the time and the fraction of peak FLOP/s it reaches. Then store the weight in fp8 while keeping bf16 arithmetic and estimate again. Roughly how many tokens would the fp8-weight layer need to become compute-bound?

Answer

bf16: bytes \(= 2(8 \cdot 8192 + 8192^2 + 8 \cdot 8192) \approx 1.345 \times 10^{8}\), so \(T_\text{mem} \approx 36.4\) µs; FLOPs \(= 2 \cdot 8 \cdot 8192^2 \approx 1.07 \times 10^{9}\), so \(T_\text{math} \approx 0.9\) µs. The intensity is about 8, which reaches \(8 \times 3.69 \times 10^{12} \approx 29.5\) TFLOP/s, about 2.6% of the device's 1,153.5 TFLOP/s.

fp8 weights: the weight bytes halve, \(2 \cdot 8 \cdot 8192 + 8192^2 + 2 \cdot 8 \cdot 8192 \approx 6.74 \times 10^{7}\) bytes, so \(T_\text{mem} \approx 18.3\) µs, almost twice as fast. The intensity becomes about \(2B\), so the layer becomes compute-bound at about half the bf16 threshold: the exact solution of \(2BD^2 / (4BD + D^2) = 312.6\) for \(D = 8192\) is about 169 tokens, against 339 in bf16. Chapter 17 builds decode performance on this reasoning.

Problem 7. Two v6e chips split x @ w along \(D\) as in the ICI section, with \(B = 1024\) tokens and \(D = F = 8192\), over one ICI link at 90 GB/s each way. Compute \(T_\text{math}\) and \(T_\text{comms}\), name the bound, and give the smallest \(D\) that would hide the communication.

Answer

\(T_\text{math} = BDF / 918 \times 10^{12} = 1024 \cdot 8192^2 / 9.18 \times 10^{14} \approx 74.9\) µs. \(T_\text{comms} = 2BF / 9 \times 10^{10} = 2 \cdot 1024 \cdot 8192 / 9 \times 10^{10} \approx 186\) µs. The layer is communication-bound: the chips spend more than twice as long exchanging partial sums as computing them. Hiding the transfer needs \(D/2 > 10{,}200\), so \(D > 20{,}400\); changing \(B\) does not help, because it scales both times equally.

Problem 8. Estimate a SwiGLU MLP block on one v7x device, with \(T = 4096\) tokens, \(D = 4096\), \(F = 14336\), all bf16: gate = x @ W_gate, up = x @ W_up, h = silu(gate) * up, y = h @ W_down. Treat each op separately, as if nothing were fused; assume the elementwise op costs about 4 FLOPs per element on the VPU at \(1.8 \times 10^{13}\) FLOP/s. Give per-op bounds, the block's range, and the share of the lower bound taken by the elementwise op. Write it as a script.

Answer

Each matmul does \(2 \cdot 4096 \cdot 4096 \cdot 14336 \approx 4.81 \times 10^{11}\) FLOPs and moves \(2(4096 \cdot 4096 + 4096 \cdot 14336 + 4096 \cdot 14336) \approx 2.68 \times 10^{8}\) bytes, an intensity of 1,792: compute-bound, \(T_\text{math} \approx 417.0\) µs, \(T_\text{mem} \approx 72.7\) µs. The elementwise op reads two and writes one [4096, 14336] tensor, \(\approx 3.52 \times 10^{8}\) bytes, intensity about 0.7: memory-bound, \(T_\text{mem} \approx 95.5\) µs against \(T_\text{math} \approx 13\) µs on the VPU.

Block: \(3 \times 417.0 + 95.5 \approx 1{,}347\) µs with overlap, about 1,578 µs without. The elementwise op is about 7.1% of the lower bound. Fusing it into its neighbours would remove most of those 95 µs; the matmuls are already at the MXU's roof, so the remaining lever there is alignment, not intensity. The solution script is code/solutions/ch02/mlp_estimate.py.

Quick questions

  1. In one sentence each, what do \(\max(T_\text{math}, T_\text{mem})\) and \(T_\text{math} + T_\text{mem}\) assume about overlap?
  2. Why does a matmul's intensity grow with its dimensions while an elementwise operation's does not?
  3. What is the ridge point of a v6e device and of a v7x device, and why is it the same per chip and per device on v7x?
  4. Why is the token batch, and not the number of sequences, the quantity to compare with the ridge point?
  5. In the two-chip sharded matmul, why does the compute-bound condition depend on \(D\) and not on \(B\)?
  6. Name three reasons a measured time can sit well above the roofline lower bound.

Key takeaways

  • An operation's time lies between \(\max(T_\text{math}, T_\text{mem})\) and \(T_\text{math} + T_\text{mem}\). Plan against the maximum; copies overlap with compute, and a well-overlapped program gets close to it.
  • Arithmetic intensity is FLOPs per byte moved. Compare it with the chip's ridge point, peak FLOP/s divided by bandwidth: above is compute-bound, below is memory-bound.
  • With chapter 1's numbers the bf16 ridge is about 560 FLOPs per byte on v6e and about 313 on v7x, per chip and per device alike. Never divide a per-chip number by a per-device one.
  • A bf16 linear layer's intensity is roughly its token batch per device, so it needs about 560 tokens on v6e and 313 on v7x to be compute-bound; small layers may never get there.
  • Elementwise operations sit far below every ridge. Their cost is HBM traffic, and fusion is the fix.
  • The same test applies to ICI: for a matmul sharded along \(D\), the intensity against the link is \(D/2\), independent of the batch.
  • Estimate before you profile, then compare. A measurement below the lower bound means your numbers are wrong; one far above it means time is going somewhere the roofline does not model.

References