Tensor Shapes — Align to the Hardware Tile, or Pay in Padding
The most baffling operator failure is the mysterious OOM — a job that "should fit" dies anyway. The cause is almost always shape, not size. Accelerators process matrices in fixed-size tiles and silently zero-pad any dimension that isn't a multiple, so a dimension just over a tile boundary rounds up to the next one — paying compute and memory for a wall of zeros. The exact tile size is hardware-specific (commonly 128). For infra engineers, no calculus.
100Math for the Metal (arithmetic & shapes)Lesson 4 of 4ShowHide
Chapter 1 of 2 in Math for ML/AI Workloads
- Tensor Shapes — Align to the Hardware Tile, or Pay in PaddingYou're here
Prerequisites: What Is a Tensor? — A Multidimensional Array
The one-line summary
The most baffling failure you'll hit operating ML jobs is the mysterious OOM: a run that "should fit" by your back-of-envelope math dies anyway. The cause is almost always shape, not size. Accelerators are rigid — the matrix unit works in fixed-size tiles (commonly 128×128), and the compiler silently zero-pads any dimension that isn't a multiple of the tile. A 129-wide matrix quietly becomes 256-wide: you pay compute and memory for a wall of zeros. Knowing this turns "why did it OOM?" into a ten-second diagnosis.
Shape is what gets allocated
You already know a is just a multidimensional array of numbers (see What Is a Tensor?). What matters here is its shape — the size of each dimension. Shape, not a vague "size," sets the memory footprint and decides how the tensor maps onto the hardware.
The hardware tile, and the zeros you didn't ask for
An accelerator's processes the world in fixed-size tiles —
most commonly 128×128 (some use 256). We'll use 128 throughout; your accelerator may use a
different size, but the rule is identical. When a matmul dimension isn't a multiple of that tile, the
compiler it up to the next multiple. So a 129-wide
dimension spills past the first 128-tile into a second — rounding up to 2×128 = 256, and that whole
second tile is almost entirely zeros the hardware still stores and still multiplies. (The vector
unit's lanes — its parallel slots, like SIMD lanes — also want filling in multiples of 8, a
smaller version of the same tax; but the headline rule is the matrix tile.)
Drag the dimensions of a matmul and watch the real matrix round up to the tile grid — the red overhang is pure waste:
Predict before you drag: a 129 × 129 matrix is laid out on 128-wide tiles. Roughly what fraction of the padded matrix's slots hold zeros?
Padding costs compute and memory — and the memory is the OOM
Two bills come due. The compute bill: the matrix unit does real FLOPs on the padded zeros (a 129→256 dimension nearly doubles that axis's work). The memory bill: those zeros are stored in — and that is the surprise OOM. You sized the job by its real element count; the device allocated the padded shape, which is bigger. The job that fit on your napkin doesn't fit on the device. Put bluntly: at a near-tile dimension you're renting a device you're only half-using.
That's why the same byte-width-and-intensity from the precision lesson is sensitive to shape too — padding inflates the bytes moved. Set a clean dimension and note the FLOP/byte; now bump one just over a tile boundary and watch the bytes-moved climb and the operating point slide toward memory-bound:
Why shapes are rigid (and why they recompile)
One more thing that trips up engineers coming from dynamic Python: accelerator shapes are static, fixed at compile time. The device can't allocate on the fly — it needs the exact buffer layout planned before it runs. So shapes freeze, and changing one re-triggers compilation (the "why did my step suddenly get slow after I changed the batch size?" gotcha).
Why this matters
Shape is one of the few performance-and-capacity levers you can reason about without touching the model. When a data scientist files "my job OOMs and it shouldn't," your first move is now "are the batch and feature dims aligned to the hardware tile?" — assume 128 unless the accelerator's docs or the compiler's padding warnings say otherwise. And when someone's MFU is mysteriously low, padding on a near-tile dimension is a prime suspect. You won't pick the model's dimensions — but you can spot the ones that quietly cost double, flag them to the team that owns the model, and turn a guessed capacity plan into a sized one.
Put it together
Make the waste appear
In the tiler, set every dimension to 512 (zero waste), then nudge one to 513. Read off how much compute and memory just went to padding, and what shape it rounded up to.
Fix a surprise OOM
A job with a
129-wide feature dimension OOMs a device it should fit. Round it to the nearest multiple of the tile and say, in one sentence, why the footprint dropped.Connect it to the roofline
Explain how padding a near-tile dimension can push a workload the wrong way on the roofline — more bytes moved for the same useful math.
Review
What is a tensor, and why does its shape matter to an operator?
Check yourself
A matmul has a 129-wide dimension on hardware with a 128-wide tile. What does the compiler do?