The book
You know GPUs from the CUDA side, and you have an Apple Silicon machine in front of you. This book replaces the hardware half of what you know with the Apple version, one difference at a time, and it does that around one piece of work. A matrix multiply starts as a library call at 11.7 TFLOPS. By the end of Part 4 it is a 144-line kernel at 13.5, the practical ceiling of the chip. Part 5 finds the same design inside MLX's production kernels. Part 6 turns it into attention and asks why decoding a language model is slow. Part 7 is about where real wins come from and what the failures look like, and Part 8 is the M5's matrix unit, which did not exist when the kernel was written.
Read it in order; every chapter assumes the ones before it. Each one opens with where you are, derives its numbers once, and ends with three questions to review what you read, with the answers covered until you hover over or tap them. If you want one topic rather than the whole track, the glossary holds the same material as standalone entries.
Part 1. The machine
What is different about this chip?
- 01Two hierarchies: what you launch and what runs itApple uses one word, 'core', for a different level than NVIDIA does, and that one word causes a lot of the early confusion.
- 02The core's memories: 208 KB of registers, 32 KB of scratch208 KB of registers per core and 32 KB of threadgroup memory, which is why Apple kernels keep their working set in registers and why a spill costs 10 times, not 10 percent.
- 03One memory pool and the one law: bandwidthOne memory pool, no transfers, and one law: most kernels are decided by bandwidth before they are written.
- 04Keeping the core busy: occupancy, stalls, and F16Occupancy here is resource arithmetic, and F16 wins for stall and register reasons, not for throughput.
- 05The two asymmetries: fast exp2, emulated float atomicsA fast exp2 and float atomics that are emulated: two hardware paths that reshape kernels written for NVIDIA.
Part 2. Running a kernel
What is the least Metal you need?
- 06From source to pipeline stateThe final compile happens on the device at pipeline creation, so the machine code is made on your machine for your chip.
- 07Launching, batching, and waitingA dispatch is cheap and a wait is not; inference converged on one command buffer per forward pass for a reason.
- 08One kernel, many shapesFunction constants turn one shipped kernel source into a variant per case, specialized at run time for the shape at hand.
Part 3. Measuring before optimizing
How do you know a kernel is slow?
- 09The baseline and the stopwatchMPS multiplies 4096 by 4096 at 11.7 TFLOPS on an M5 Max; the stopwatch is GPU timestamps, and the first runs are thrown away.
- 10Build your own rooflineThe compute roof is measured, not quoted from a spec sheet: 13.5 TFLOPS and a ridge of about 26 FLOPs per byte on the M5 Max.
Part 4. One matrix multiply
How does a 144-line kernel beat the library?
- 11Tiling and the cooperative loadA tile sized for 32 KB of threadgroup memory and a coalesced cooperative load: the first two techniques of the 144-line kernel.
- 12simdgroup_matrix and register blockingThe kernel beats MPS by about 15 percent, and the whole win is keeping the accumulator in registers across the K loop.
- 13Reading the whole kernelsync_copy.metal, read end to end, with every one of its lines accounted for.
- 14Double buffering, and the copy engine that diedThe copy intrinsic exists in the headers and is dead on the hardware, so double buffering is yours to do, and it does not always win.
- 15Fusion and epiloguesThe bias, the activation and the scaling around a matmul fold into its one store, and each one folded is a pass over memory you no longer pay.
Part 5. The same GEMM in production
What does MLX do with your matmul?
- 16Arrays that record: MLX and lazy evaluationAn MLX array records work instead of doing it, and one mx.eval per step is the difference between a fast loop and a slow one.
- 17How an op becomes a kernelmx.matmul walks a dispatch tree to a precompiled steel kernel; on an M5 Max in 32-bit it lands on a NAX tile with TF32 on by default.
- 18Steel: the GEMM as componentsThe hand kernel's three parts, loader, multiply and epilogue, are steel's three components: BlockLoader, BlockMMA and the epilogue.
- 19mx.compile: fusing the long tailmx.compile traces the lazy graph once and fuses the elementwise chains around the matmul; the matmul itself it leaves alone.
Part 6. Attention and inference
Why is decode slow, and what can be done?
- 20Online softmax and flash attentionThe score tile never touches memory: online softmax corrects a running maximum so attention runs in one pass over the keys.
- 21Three attention kernels, one algorithmThree production attention kernels, one algorithm, three design bets: steel's, metal-flash-attention's generated kernels, and llama.cpp's enumerated ones.
- 22The KV cache, decode, and prefillA 32K-token cache is about 4.29 GB, and reading it once per token at 495 GB/s is what makes decode a byte-streaming problem.
- 23Quantization as a bandwidth optimizationFour-bit weights stream about 3.5 times fewer bytes per token, and the QMV kernel multiplies against them without expanding the matrix.
Part 7. Working on real models
Where do wins come from?
- 24The three questionsThree questions, in order, before any custom kernel: can you delete work, can you unlock an existing fast path, can you cut dispatch and synchronization overhead.
- 25Wins and failures, measuredA ledger of 14 custom kernels: 2 isolated wins, 11 losses or neutral, 1 with no lever, and the loss modes the failures share.
- 26When it is still slow: reading the machine codeApple ships no disassembler; applegpu is how you read the machine code, and register spills are the first thing you look for.
- 27Scaling out across machinesTwo Mac Studios hold a trillion-parameter model over Thunderbolt 5 RDMA, and whether a split ever wins is one inequality.
Part 8. The new hardware
What changes on M5?
- 28Three matrix engines on one dieThree matrix engines on one die, AMX, the GPU and the Neural Engine, and Metal talks to only one of them.
- 29Tensor ops and the NAX GEMMA matmul2d descriptor hands the multiply to the M5 matrix unit; the tile, the accumulator grid and the lane layout are still yours.