Maps to: Stanford CS 217 Hardware Accelerators for Machine Learning (Winter 2026), Lecture 9 · Tue 02/03/2026 · Compilers and Software Stack (Part 1). Reading (from the schedule): H. Kwon, P. Chatarasi, M. Pellauer, A. Parashar, V. Sarkar, T. Krishna, Understanding reuse, performance, and hardware cost of DNN dataflows: a data-centric approach, MICRO 2019 (MAESTRO). The schedule also lists FlashAttention, PagedAttention and PyTorch 2 for this unit; they are used in Lecture 10. Basis: The slides are not public. This lecture follows the session title, the MAESTRO paper, and related public work on analytical mapping models (Timeloop, Parashar et al., ISPASS 2019; Interstellar, Yang et al., ASPLOS 2020; Sze et al.’s textbook). The experiments use an analytical mapping model written for these notes (
mapper.py). In the spirit of MAESTRO and Timeloop, it computes reuse, traffic, energy and latency from a mapping without simulating. It is validated exactly against Lecture 5’s cycle-accurate systolic array and Lecture 7’s tiling counts. The experiments do not reflect what was said in class. Code:code/lecture09/(4 scripts + 1 module; 1–12 s each) Reading time: 3–4 hours including running the code
0. How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- Write a convolution or GEMM as a 7-dimensional loop nest, say which dimensions each tensor depends on, and compute the reuse available in each tensor.
- Describe a mapping — tiling at each memory level, loop order at each level, spatial unrolling over the PE array — and explain why weight-, output-, input- and row-stationary dataflows are families of mappings.
- Apply the reuse rule to count how often each tile is fetched. Tell multicast from spatial reduction.
- Read and write MAESTRO’s data-centric directives (TemporalMap, SpatialMap, Cluster).
- Build an analytical cost model: accesses per level, energy, latency, validity. Explain how it is validated.
- Explain why the mapspace is astronomically large, why its mappings differ 100× in energy and latency, and how it is searched.
- Explain how the best mapping depends on the layer and on the buffer sizes, and what a fixed dataflow costs over a network.
0.2 Prerequisites and scope
| Assumed | Introduced here | Developed later |
|---|---|---|
| Lecture 3: dataflows as loop orders, Eyeriss energy accounting | the 7-D loop nest, relevance, the reuse rule | operator fusion across layers (L10) |
| Lecture 5: systolic WS/OS/IS | mappings over a memory hierarchy and a PE array | FlashAttention’s tiling (L10) |
| Lecture 7: tiling, loop orders, the √S law | analytical cost models; MAESTRO directives | GPU kernels as mappings (L11) |
| Lecture 8: multicast and reduction networks | mapspace search; co-design of buffers and mappings | spatial dataflow compilers (L12–13) |
0.3 Map
§1 a layer is a loop nest: relevance, reuse ← code 01 (Part 1)
§2 a mapping: tiling, order, spatial unrolling; dataflows; MAESTRO directives ← code 01 (Parts 2-4)
§3 the analytical cost model, validated ← code 02
§4 the mapspace and how to search it ← code 03
§5 the best mapping of each layer; buffers; fixed vs flexible ← code 04
§6 from framework to hardware: where mapping sits in the compiler
§7 summary §8 pitfalls §9 exercises §10 further reading §11 next lecture
0.4 Notation
| Symbol | Meaning |
|---|---|
| N, K, C, P, Q, R, S | batch, output channels, input channels, output rows/cols, filter rows/cols |
| W, I, O | weights , inputs , outputs |
| GEMM | P = M, C = K (reduction), K = N (stride 1, R = S = 1) |
| DRAM / GB / RF | off-chip memory / global buffer / a PE’s register file |
| tile | the part of a tensor a level holds at once |
| mapspace | the set of all mappings of a layer onto an architecture |
| EDP | energy-delay product (energy × latency) |
1. A layer is a loop nest
1.1 The 7-D loop nest
for n in range(N):
for k in range(K):
for c in range(C):
for p in range(P):
for q in range(Q):
for r in range(R):
for s in range(S):
O[n][k][p][q] += W[k][c][r][s] * I[n][c][p + r][q + s]
Script 01 runs exactly this nest on a tiny layer and matches numpy to 5e-15. A GEMM is the special case P = M, C = K, K = N with the other loops of size 1. The 7 loops can run in any order and be split at will: addition is associative (up to rounding, Lecture 2), so every ordering computes the same result. That freedom is what a mapping exploits.
1.2 Relevance and reuse
Each tensor depends on a subset of the dimensions:
| Tensor | Depends on | Irrelevant (a loop over these REUSES the tensor) |
|---|---|---|
| W | K, C, R, S | N, P, Q |
| I | N, C, P, Q, R, S (P with R, Q with S: sliding windows) | K |
| O | N, K, P, Q | C, R, S |
The reuse of a tensor is MACs ÷ its distinct elements. Script 01, Part 1:
| Layer | Uses per weight | per input | MACs per output |
|---|---|---|---|
| ResNet-50 3×3, 56×56, 64→64 | 3,136 | ~537 | 576 |
| ResNet-50 1×1, 56×56, 256→64 | 3,136 | 64 | 256 |
| ResNet-50 3×3, 14×14, 256→256 | 196 | 1,764 | 2,304 |
| Llama q_proj, 512 tokens | 512 | 4,096 | 4,096 |
| Llama MLP up, decode batch 1 | 1 | 14,336 | 4,096 |
Every MAC needs one W, one I and one O. If each came from DRAM, a MAC would cost ~600 MAC-equivalents of energy (Lecture 3). Reuse is the budget a dataflow spends: capture it close to the MAC (register, PE array) and the expensive levels are touched rarely. A decode GEMV uses each weight once — no dataflow can save its weight traffic (Lectures 1, 7).
Three kinds of reuse:
- temporal — the same element stays in a buffer while loops over its irrelevant dimensions run;
- spatial multicast — the same element is sent to several PEs at once, which compute different irrelevant indices;
- spatial reduction — several PEs produce partial sums of the same output (over C, R or S) that are added on the way.
2. Mappings
2.1 Tiling, order, spatial unrolling
The accelerator is a hierarchy: DRAM → global buffer → PE array → RF → MAC (Eyeriss-like here: 16 × 16 PEs, 512 B RF per PE, a 128 KiB GB). A mapping splits every dimension into factors, one per level, and fixes an order of loops at each temporal level:
for ... DRAM-level loops (each iteration brings a new GB tile)
for ... GB-level loops (each iteration brings a new PE-array tile)
parallel_for ... spatial loops over the PE array (x, y)
for ... RF-level loops (inside each PE)
MAC
It also says which tensors each PE’s RF keeps. A systolic weight-stationary PE, for example, keeps only its weight; its inputs and partial sums pass through. Three decisions, three effects:
- tiling sets how much of each tensor each level holds, which bounds the reuse it can capture (Lecture 7’s √S law);
- order sets which tensor stays put while which changes: the stationarity;
- spatial unrolling sets which tensor is multicast or reduced across PEs, and how many PEs are busy.
2.2 The reuse rule
Flatten all temporal loops above a level, outer to inner. A tensor’s tile stays resident while the innermost run of loops irrelevant to it iterates; every other iteration requires a new tile. So fetches = product of the trip counts above, minus that innermost irrelevant run.
Script 01, Part 2 applies it to a 1024³ GEMM tiled 128³ into the GB:
| DRAM loop order | A fetches | B fetches | C visits | Total (M elements) |
|---|---|---|---|---|
| m n k (k innermost: C stays) | 512 | 512 | 64 | 17.8 |
| m k n (n innermost: A stays) | 64 | 512 | 512 | 25.2 |
| n k m (m innermost: B stays) | 512 | 64 | 512 | 25.2 |
This is Lecture 7’s rule, now applicable at every level and for every tensor. Across the PE array, a spatial loop over a dimension irrelevant to a tensor sends the same element to all those PEs (multicast). For the output, such a loop is a reduction.
2.3 Dataflows as mappings
The classic dataflows fix what stays in the PE and what is unrolled spatially; the tiling is still free. Script 01, Part 3 prints them for the ResNet 3×3 layer. Weight-stationary, fitted into the 128 KiB GB:
for q_dram in range(8):
for p_dram in range(4):
for k_gb in range(4):
for c_gb in range(4):
for r_gb in range(3):
for s_gb in range(3):
parallel_for k_s in range(16) [x], c_s in range(16) [y]:
for p_rf in range(14):
for q_rf in range(7):
O[n][k][p][q] += W[k][c][r][s] * I[n][c][p+r][q+s] # RF keeps W
Each PE holds one weight while 14 × 7 output pixels stream past. Script 01, Part 4 lists what each dataflow asks of the network:
| Dataflow | Spatial unrolling | PEs | W multicast to | I multicast to | O reduced over | RF keeps |
|---|---|---|---|---|---|---|
| weight-stationary | K16 × C16 | 256 | 1 | 16 | 16 | W |
| output-stationary | P14 × K16 | 224 | 14 | 16 | 1 | O |
| input-stationary | C16 × P14 | 224 | 14 | 1 | 16 | I |
| row-stationary (Eyeriss) | P14 × R3 × K4 | 168 | 14 | 4 | 3 | W, I, O |
A dataflow is therefore also a network demand: WS needs input multicast and output reduction trees; OS needs weight and input multicast and no reduction. Lecture 8’s fabrics and this lecture’s mappings must be designed together.
2.4 MAESTRO’s data-centric directives
MAESTRO (Kwon et al.) describes a mapping by what data each PE holds and how it moves, instead of by loops:
TemporalMap(size, offset) D— at each time step, a chunk ofsizeindices of dimension D, advancing byoffset;SpatialMap(size, offset) D— the same, across PEs (each PE gets a different chunk);Cluster(n)— group PEs into clusters of n, for multi-level mappings.
The weight-stationary mapping above, rendered by mapper.maestro_text:
TemporalMap(7,7) Q // DRAM -> GB
TemporalMap(14,14) P // DRAM -> GB
TemporalMap(16,16) K // GB -> array
TemporalMap(16,16) C // GB -> array
TemporalMap(1,1) R ; TemporalMap(1,1) S
SpatialMap(1,1) K // across PEs (x)
SpatialMap(1,1) C // across PEs (y)
Cluster(256)
TemporalMap(1,1) P ; TemporalMap(1,1) Q // inside a PE
The data-centric form makes reuse explicit. MAESTRO computes directly from the directives:
- which PEs share a tensor element (multicast);
- how much of each tensor changes between consecutive time steps (the new data to fetch);
- which PEs hold partial sums of the same output (reduction).
For sliding windows, offset < size describes the overlap between consecutive input tiles — something the simple loop view misses (our model does not credit it either; Exercise 12).
3. The analytical cost model
3.1 What it computes
For each tensor, mapper.evaluate:
- RF level: if the RF keeps the tensor, its tile is the extent of the RF loops, refilled per the reuse rule over the DRAM + GB loops. If not, the PE holds one element in its operand register, refetched whenever the loops change it.
- Array: a fill of all used PEs costs one network delivery per PE, but only (PEs ÷ multicast) GB reads. For outputs, partial sums are reduced in the network: PEs ÷ reduction writes.
- GB level: the GB tile (extent of GB + spatial + RF loops) is refilled from DRAM per the reuse rule over the DRAM loops.
- Outputs: each visit of an output tile writes partial sums; all visits but the first also read them back.
- Energy = Σ accesses × cost (Eyeriss’s normalized costs: MAC 1, RF 1, array 2, GB 6, DRAM 200 per 16-bit access).
- Latency = max(compute cycles = product of all temporal trip counts, GB accesses ÷ GB bandwidth, DRAM bytes ÷ DRAM bandwidth).
- Validity: factors multiply to each dimension; the spatial loops fit the array; the RF and GB tiles fit.
3.2 Validation
A model is only useful if it is right. Script 02 checks it against the course’s simulators.
Against Lecture 5’s systolic array. Lecture 5’s array maps to: K over the rows, N over the columns, M streamed through, only W kept in the PE. The model’s buffer traffic equals the cycle-accurate simulator’s exactly:
| GEMM on array | W reads (model / systolic) | I reads | O reads + writes | Model cycles | Systolic cycles |
|---|---|---|---|---|---|
| 96×64×48 on 16×16 | 3,072 / 3,072 | 18,432 / 18,432 | 32,256 / 32,256 | 1,152 | 1,198 (sim 1,198) |
| 37×64×32 on 8×8 | 2,048 / 2,048 | 9,472 / 9,472 | 17,760 / 17,760 | 1,184 | 1,206 (sim 1,206) |
The cycles differ by the array’s fill and drain, which the analytical model ignores. The difference is small for long streams and larger for short ones.
Against Lecture 7’s tiling.gemm_traffic. The model’s DRAM traffic for GB-level tilings equals the exact counts in 9 of 9 cases (three shapes × three loop orders).
3.3 Where the energy goes
Script 02, Part 3 (MAC units per MAC; 128 KiB GB):
| Layer | Mapping | MAC | RF | Array | Buffer | DRAM | Total | Utilization |
|---|---|---|---|---|---|---|---|---|
| ResNet 3×3, 56×56 | weight-stationary | 1.00 | 1.00 | 5.96 | 1.20 | 0.92 | 10.09 | 1.00 |
| output-stationary | 1.00 | 2.00 | 4.05 | 1.13 | 5.68 | 13.87 | 0.88 | |
| row-stationary | 1.00 | 4.00 | 2.12 | 2.07 | 5.68 | 14.88 | 0.66 | |
| best (energy) | 1.00 | 2.00 | 0.33 | 0.27 | 1.49 | 5.09 | 0.06 | |
| best (EDP) | 1.00 | 2.00 | 0.43 | 0.20 | 2.02 | 5.65 | 0.88 | |
| Llama q_proj | weight-stationary | 1.00 | 1.00 | 6.24 | 2.72 | 28.12 | 39.08 | 0.44 |
| best (EDP) | 1.00 | 3.00 | 1.03 | 0.21 | 3.56 | 8.81 | 1.00 |
Three lessons:
- A dataflow’s name says little; its tiling says a lot. The greedily fitted templates cost 2–4× more than searched mappings, mostly in the array network and in DRAM.
- Energy alone is the wrong objective. The energy-optimal mapping uses 6% of the PEs: small spatial factors let each PE reuse more but leave the array idle. Energy × latency gives almost the same energy at 88% utilization.
- Mappings cannot remove compulsory traffic, but a bad one multiplies it. q_proj’s hand-written dataflows spend 26–51 MAC units per MAC on DRAM; the best mapping 2.4–3.6.
4. The mapspace and how to search it
4.1 How big?
Each dimension splits over 5 levels (DRAM, GB, spatial x, spatial y, RF). The number of ordered factorizations of into 5 factors is . Multiply over the 7 dimensions, then by the loop orders of three temporal levels (). For the ResNet 3×3 layer that gives ~4 × 10²¹ mappings (script 03, Part 1). Most are invalid or equivalent, but the rest are still far beyond enumeration.
4.2 How different?
Script 03, Part 2 samples 20,000 random mappings; 84% are valid:
| Best | 10th pct | Median | 90th pct | Worst | |
|---|---|---|---|---|---|
| energy per MAC | 5.6 | 14.6 | 33.2 | 120.7 | 646.8 |
| latency (cycles) | 451,584 | 1.37 M | 3.80 M | 38.5 M | 115.6 M |
Valid mappings of one layer on one chip differ 116× in energy and 256× in latency. The compiler’s mapper matters as much as most hardware decisions.
4.3 Search strategies
- Exhaustive with pruning (Timeloop’s mapper): enumerate a constrained space, skipping equivalent orders and pruning invalid tilings.
- Random sampling: simple and embarrassingly parallel, but blind.
- Local search / heuristics: hill climbing, simulated annealing, genetic algorithms (GAMMA), learned or gradient-based surrogates (Mind Mappings). These exploit the fact that the mapspace is locally smooth: moving one factor between levels or swapping two loops changes the cost a little.
Script 03, Part 3 compares random sampling with “sample-then-climb” (30% of the budget sampling, then hill climbing from the best four). EDP ÷ 10⁶, mean of 3 seeds:
| Layer | Best hand-written | Random 100 | Local 100 | Random 1,000 | Local 1,000 | Random 10,000 | Local 10,000 |
|---|---|---|---|---|---|---|---|
| ResNet 3×3, 56×56 | 4.6 | 5.2 | 5.9 | 4.1 | 2.4 | 3.1 | 2.4 |
| ResNet 1×1, 56×56 | 2.8 | 2.8 | 2.7 | 2.1 | 1.8 | 1.8 | 1.7 |
| ResNet 3×3, 14×14 | 5.2 | 4.9 | 5.0 | 4.2 | 3.3 | 3.3 | 2.5 |
With 100 evaluations there is nothing good to climb from. From ~1,000 on, climbing from good samples beats sampling alone and the hand-written dataflows. (A first version that climbed from a single random start lost badly to sampling: the mapspace is rugged at large scale.)
4.4 Energy vs latency
The Pareto front of the random sample (script 03, Part 4) runs from 5.6 MAC units per MAC at 1.8 M cycles (25% utilization) to 7.0 at the ideal 452 k cycles. The hand-written dataflows lie well above it: weight-stationary is fast but costs 10.1, row-stationary 14.9 at 688 k cycles. A dataflow is a family; the mapper picks a point.
5. The best mapping of each layer
5.1 It changes from layer to layer
Script 04, Part 1 (best by EDP, 16 × 16 PEs, 512 B RF, 128 KiB GB):
| Layer | Best mapping | Energy/MAC | DRAM share | Utilization |
|---|---|---|---|---|
| ResNet conv1-like 7×7, 3→64 | spatial K16 × P16; RF keeps I, O | 5.87 | 27% | 1.00 |
| ResNet 3×3, 56×56, 64→64 | C16 × K16; W, I | 4.75 | 27% | 1.00 |
| ResNet 1×1, 56×56, 256→64 | C16 × C16; W, I | 9.24 | 43% | 1.00 |
| ResNet 3×3, 14×14, 256→256 | C16 × Q14; W, I | 5.08 | 31% | 0.88 |
| Llama q_proj, 512 tokens | P16 × C16; I | 8.30 | 42% | 1.00 |
| Llama QKᵀ, one head | P16 × P16; I | 6.92 | 40% | 1.00 |
| Llama MLP up, decode batch 8 | C16 × P8; I | 30.57 | 82% | 0.50 |
Eight layers use seven different spatial unrollings. The 7×7 layer has 3 input channels and cannot fill the array with C. Decode is DRAM-bound whatever the mapping. At batch 8 each weight is used 8 times, so its DRAM energy is at least 200/8 = 25 per MAC — 82% of the best mapping’s 30.6.
5.2 Buffers and mappings are co-designed
Script 04, Part 2 (best energy per MAC; the DRAM share in parentheses):
| ResNet 3×3 | GB 16 KiB | 64 KiB | 256 KiB | 1 MiB | 4 MiB |
|---|---|---|---|---|---|
| RF 64 B | 9.69 (35%) | 6.52 | 5.63 | 5.53 | 5.53 |
| RF 1 KiB | 7.46 (48%) | 5.73 | 5.17 | 5.10 | 5.10 |
| Llama q_proj | GB 16 KiB | 64 KiB | 256 KiB | 1 MiB | 4 MiB |
|---|---|---|---|---|---|
| RF 64 B | 12.46 (51%) | 9.85 | 6.68 | 5.53 | 5.18 (12%) |
| RF 1 KiB | 11.68 (54%) | 7.41 | 6.31 | 4.67 | 4.45 (14%) |
The ResNet layer gains most from the first GB doublings and then saturates: its whole working set fits. The q_proj GEMM keeps gaining up to 4 MiB, following the √S law. A larger RF helps both, by letting each PE keep more of its operands. Every entry also tries the best mappings of smaller configurations (still valid with more storage). Without that, search noise made some bigger buffers look worse. The best buffer size depends on the mappings the compiler can find, and vice versa.
5.3 What does a fixed dataflow cost?
A rigid accelerator implements one dataflow: fixed spatial dimensions and what the PE keeps. Script 04, Part 3 gives each fixed dataflow its own tile and loop-order search, so the comparison is fair. The flexible design may use any mapping, the templates included:
| Fixed dataflow vs flexible | ResNet layers: energy | latency | All layers: energy | Worst single layer (EDP) |
|---|---|---|---|---|
| weight-stationary | 1.14× | 2.06× | 1.14× | 5.44× |
| output-stationary | 1.27× | 1.19× | 1.06× | 2.69× |
| row-stationary | 1.30× | 1.42× | 1.03× | 2.64× |
With its tiling searched, a fixed dataflow costs 1.1–1.3× over the ResNet layers. That is less than its 5× worst case on a mismatched layer (weight-stationary on the 3-channel 7×7 layer) suggests. Over all layers the big q_proj GEMM dominates, and the costs shrink to 1.03–1.14×. Interstellar (Yang et al., ASPLOS 2020), which used Halide schedules to explore accelerator designs, reached a similar conclusion: many dataflows reach similar efficiency once their tiling and memory hierarchy are chosen well. Flexibility (MAERI, SIGMA, Lecture 8) pays on irregular layers. Mappers pay everywhere.
6. From framework to hardware
Mapping is one stage of an accelerator compiler:
PyTorch / JAX program
-> graph capture (TorchDynamo + FX graphs, jax.jit tracing) Lecture 10
-> graph optimizations: fusion, layout, quantization Lectures 4, 10
-> lowering of each operator to a loop nest / tensor IR (TVM, MLIR linalg, Halide)
-> MAPPING of each loop nest onto the hardware (this lecture)
-> code generation: DMA descriptors, buffer allocation and placement (L7),
array configuration, vector-unit programs and VLIW bundles (L6), synchronization
-> runtime: scheduling layers, managing memory (PagedAttention, Lecture 10)
The mapper needs three inputs:
- a model of the hardware, like
mapper.Archor MAESTRO’s and Timeloop’s architecture descriptions; - a cost model that is cheap and validated;
- a search strategy.
Its output must also be legal for the code generator: buffer placement without bank conflicts (Lecture 7), network patterns the NoC supports (Lecture 8). Lecture 10 moves up the stack: fusing operators so that intermediate tensors never reach DRAM at all.
7. Summary
- A layer is a 7-D loop nest. Each tensor ignores some dimensions, and loops over those dimensions reuse it: 3,136 uses per weight in a ResNet 3×3 layer, but 1 in a decode GEMV.
- A mapping = tiling per level + loop order per level + spatial unrolling + what the PE keeps. Dataflows (WS, OS, IS, RS) are families of mappings.
- The reuse rule: a tile stays while the innermost loops irrelevant to it iterate; spatial loops over irrelevant dimensions multicast inputs and reduce outputs.
- MAESTRO’s TemporalMap / SpatialMap / Cluster directives express the same thing data-centrically and make reuse explicit.
- An analytical model turns a mapping into accesses, energy and latency in microseconds of compute. Ours matches the systolic simulator’s buffer traffic and Lecture 7’s DRAM counts exactly.
- Hand-written dataflows cost 2–4× more energy than searched mappings on the same hardware. The energy-only optimum is slow (6% utilization), so optimize energy × latency.
- The mapspace of one layer has ~10²¹ points whose valid members span 116× in energy and 256× in latency. Sample-then-climb search beats random sampling and the templates from ~1,000 evaluations.
- The best mapping changes per layer (7 unrollings for 8 layers) and with buffer sizes. Hardware and mapper must be co-designed.
- A fixed dataflow with a good tiling search costs only 1.1–1.3× over the ResNet layers, but up to 5× on mismatched layers. Tiling and the memory hierarchy matter more than the dataflow’s name.
8. Common pitfalls
- “Weight-stationary is a hardware property.” It is a family of mappings; its tiling still decides most of the energy (§3.3).
- “Minimize energy.” The energy optimum can idle 94% of the PEs (§3.3).
- “A cost model is fine if it looks plausible.” Validate it against a simulator or exact counts; ours exposed a double-counting bug that way.
- “Random search is enough.” It is a good baseline, but local search from good samples wins from ~1,000 evaluations (§4.3).
- “A bigger buffer always helps.” Only until the working set fits (ResNet 3×3 stops gaining at 1 MiB, §5.2) — and only if the mapper exploits it.
- “Decode can be fixed with a better mapping.” Its weight traffic is compulsory: 200/batch per MAC (§5.1).
- “Flexible hardware is always worth it.” Over CNNs it saved 10–30% vs a fixed dataflow with good tiling (§5.3).
9. Exercises
Exercise 1 (reuse). For a layer with K = 128, C = 64, P = Q = 28, R = S = 3 (batch 1), compute the uses per weight, per input (approximately) and the MACs per output.
Answer
MACs = 128 × 64 × 28 × 28 × 9 = 57.8 M. Weights 128 × 64 × 9 = 73,728 → 784 uses each (P × Q). Inputs 64 × 30 × 30 = 57,600 → ~1,003 uses (≈ K × R × S = 1,152, less at the borders). Outputs 128 × 784 = 100,352 → 576 MACs each (C × R × S).
Exercise 2 (the reuse rule). Above the RF, the loops are (outer to inner) K 4, C 8, P 7. How many times are the RF tiles of W, I and O fetched?
Answer
W depends on K, C: the innermost run of irrelevant loops is P (7), so W is fetched 4 × 8 = 32 times. I depends on C, P: P is relevant, nothing is dropped: 4 × 8 × 7 = 224. O depends on K, P: the innermost loop P is relevant: 224 fetches over 4 × 7 = 28 distinct tiles — 8 visits each (one per C chunk): written 8 times, read back 7.
Exercise 3 (multicast and reduction). A mapping unrolls K16 × C16 across 256 PEs. For one fill of the array, how many GB reads of W, I and O does it need per element each PE receives, and what does the network do?
Answer
W depends on both K and C: 256 different elements, 256 GB reads (unicast). I does not depend on K: the 16 PEs along x share each input — 16 GB reads, each multicast to 16 PEs. O does not depend on C: the 16 PEs along y produce partial sums of the same output — the network reduces them, and 16 values are written. The fabric needs multicast along x and adder trees along y.
Exercise 4 (MAESTRO). Translate into a loop nest and name the dataflow: SpatialMap(1,1) K; TemporalMap(64,64) C; TemporalMap(Sz(R),1) Y'; TemporalMap(Sz(S),1) X'; Cluster(64); SpatialMap(1,1) C; TemporalMap(Sz(R),Sz(R)) R; ....
Answer
Outer: each PE cluster gets one output channel k (spatial over K). Over time, chunks of 64 input channels, and output rows/columns slide by one (Y’, X’ are output coordinates; size = filter, offset = 1 = a sliding window). Inside a cluster of 64 PEs, each PE takes one input channel (spatial over C) and runs the filter window over time. Weights W[k][c][r][s] are fixed per PE while the window slides: a weight-stationary dataflow in the NVDLA style (K across clusters, C across PEs, outputs reduced over C in the cluster).
Exercise 5 (validation by hand). Use the reuse rule to derive Lecture 5’s weight-stationary traffic .
Answer
Above the PE: GB loops (n tiles, k tiles), then the RF loop over M. I is not kept in the PE, so it is fetched once per MAC-slot: loops n, k, m; the innermost m is relevant, so nothing is dropped: fetches per PE. With R × C PEs and multicast factor C (the K dimension across columns is irrelevant to I), the GB reads are .
Exercise 6 (reading an energy breakdown). The best (energy) ResNet 3×3 mapping spends 2.00 on RF, 0.33 on the array, 0.27 on the buffer and 1.49 on DRAM per MAC. How many accesses per MAC is that at each level, and how many MACs does each DRAM element serve?
Answer
RF: 2 accesses/MAC (cost 1). Array: 0.33/2 = 0.165 deliveries/MAC. Buffer: 0.27/6 = 0.045 accesses/MAC. DRAM: 1.49/200 = 0.0075 accesses/MAC — each DRAM element serves ~134 MACs. Compulsory traffic is (W + I + O) / MACs = 452,864 / 115.6 M ≈ 0.0039/MAC, so this mapping moves about 1.9× the compulsory DRAM traffic.
Exercise 7 (mapspace size). Compute the number of tilings (5-way factor splits) of the ResNet 3×3 layer and check script 03’s 3.38 × 10¹⁰.
Answer
64 = 2⁶: ; 56 = 2³·7: ; 3: 5; 1: 1. Product for (N, K, C, P, Q, R, S) = 1 × 210 × 210 × 175 × 175 × 5 × 5 = 3.38 × 10¹⁰. Times orders: 4.3 × 10²¹.
Exercise 8 (why the energy optimum is slow). Explain why the energy-optimal ResNet mapping uses only 6% of the PEs, and propose two ways to keep the energy while restoring speed.
Answer
With few PEs, each PE (and the GB) holds larger temporal tiles in which more of the reuse is captured close to the MAC; the array network carries less (0.33 vs 5.96 for WS). But 94% of the PEs idle. Fixes: optimize energy × latency (5.65 at 88%, §3.3), or constrain latency (e.g. ≥ 80% utilization) and minimize energy among valid mappings — and give the hardware larger RFs so that a fully parallel mapping can keep more data per PE (§5.2).
Exercise 9 (decode’s floor). Show that no mapping of the batch-8 decode GEMM can spend less than 25 MAC units per MAC on DRAM, and compare with script 04.
Answer
Every weight must be read from DRAM at least once (it does not fit on chip) and is used in exactly 8 MACs (batch 8): DRAM energy ≥ 200 / 8 = 25 per MAC. Script 04: 30.57 × 82% = 25.1 — the mapper reached the floor; only a larger batch, a smaller weight format (Lecture 4) or computing in memory (Lecture 14) can lower it.
Exercise 10 (buffer co-design). From §5.2, what does growing the GB from 256 KiB to 1 MiB buy for the ResNet 3×3 layer and for q_proj (RF 1 KiB)? Explain the difference.
Answer
ResNet: 5.17 → 5.10 (1%). q_proj: 6.31 → 4.67 (26%). The ResNet layer’s working set (37 k weights, 215 k inputs, 201 k outputs ≈ 0.9 MB in 16-bit) mostly fits in 256 KiB tiles with good reuse; q_proj’s 33 MB of weights never fit, so it follows the √S law, and each doubling of the GB keeps cutting its DRAM traffic.
Exercise 11 (programming: a pruned exhaustive search). Fix the spatial mapping to K16 × C16 and the RF to keep W and I. Enumerate all splits of P and Q over (DRAM, GB, RF) and the 6 orders of {K, C, P} at the GB level, and compare the best EDP with local_search on the same layer.
Answer
56 = 2³ · 7 has ordered 3-way splits, so P and Q give 900 combinations, × 6 orders = 5,400 evaluations (a fraction of a second). In the author’s run (remaining K and C factors of 4 at the GB level, R and S in the RF) the best mapping reaches 2.06 × 10⁶ EDP — 4.56 MAC units per MAC at 100% utilization — better than local search’s 2.4 × 10⁶ with 10,000 evaluations. Fixing the decision with the largest effect (the spatial unrolling) by rule and enumerating the rest beats a generic search here: this is how real mappers prune.
Exercise 12 (programming: sliding-window reuse). The model refetches a whole input tile, halo included, whenever P or Q advances. Credit the overlap: when the innermost loop above the RF is P with tile , only of the input rows are new. Implement it and measure the change in input traffic for the row-stationary mapping of the 3×3 layer.
Answer
New rows per step: instead of ; the input fetch volume falls by the factor for those loops. For small (row-stationary uses 1 output row per PE row: , R = 3) this cuts input traffic up to 3×. That is why Eyeriss’s row-stationary dataflow passes input rows diagonally between PEs, and why MAESTRO’s directives carry an explicit offset (size 3, offset 1).
10. Further reading
- H. Kwon et al., “Understanding reuse, performance, and hardware cost of DNN dataflows: a data-centric approach”, MICRO 2019 (MAESTRO).
- A. Parashar et al., “Timeloop: a systematic approach to DNN accelerator evaluation”, ISPASS 2019; Y. N. Wu et al., “Accelergy”, ICCAD 2019.
- X. Yang et al., “Interstellar: using Halide’s scheduling language to analyze DNN accelerators”, ASPLOS 2020.
- Y.-H. Chen, J. Emer, V. Sze, “Eyeriss: a spatial architecture for energy-efficient dataflow for CNNs”, ISCA 2016.
- S.-C. Kao, T. Krishna, “GAMMA: automating the HW mapping of DNN models on accelerators via genetic algorithm”, ICCAD 2020; K. Hegde et al., “Mind Mappings: enabling efficient algorithm-accelerator mapping space search”, ASPLOS 2021.
- T. Chen et al., “TVM: an automated end-to-end optimizing compiler for deep learning”, OSDI 2018.
- V. Sze et al., Efficient Processing of Deep Neural Networks, chapters 5–6.
11. Next lecture
Lecture 10, Compilers and software stack (Part 2), moves from one layer to the graph. Every unfused element-wise operator writes its output to DRAM and reads it back, so we will build a small tensor-graph IR with a fusion pass and price it with the roofline. We will then implement FlashAttention — tiling attention with an online softmax so that the score matrix of §4.4 (Lecture 7) never reaches DRAM — and check that it is exact. PagedAttention treats the KV cache like virtual memory. Finally, we will see how PyTorch 2 captures and compiles graphs.