Part introduction
Part IV - CUDA and Accelerator Programming
Part III located work on the serving path: matrix projections, attention over cached state, normalization, and token selection. To improve one of those operations, we must now explain how its arrays move through the device and how threads cooperate on the result.
This part starts with execution and memory, then develops matrix multiplication, reductions, attention, and serving kernels. Each schedule must compute the intended operation before its speed is useful. We retain the same model geometry as Part III so that a tile or memory-layout choice can be connected to an actual prefill or decode shape. Part V will add communication between devices to this local execution model.
Running example used throughout¶
Every major section returns to one illustrative workload so estimates stay comparable. The hidden and head dimensions match Part III's running model; the 2,048-token prefill and 4,096-token decode contexts below are separate kernel benchmark shapes, not a change to its request-level workload. Treat the hardware numbers as a teaching machine class, not a product claim.
| Symbol | Meaning | Value in this part |
|---|---|---|
D |
model hidden width | 4096 |
H |
query heads | 32 |
H_kv |
key/value heads (GQA) | 8 |
d |
head dimension (D / H) |
128 |
| Prefill | one prompt | B = 1, S_q = 2048 |
| Decode | concurrent sequences | B = 8, each with context S = 4096 |
| Device | illustrative datacenter GPU | ~3 TB/s HBM, high tensor-core peak |
Two regimes matter:
- Prefill / training-like attention: many query rows attend over keys and values. Arithmetic intensity can be high when tiles reuse well.
- Decode: each sequence adds one new query token and rereads a long KV cache. Bytes per useful FLOP rise sharply; launch and memory latency matter as much as peak math.
The same attention equations appear in both regimes. Prefill wants large tiled GEMM reuse. Decode wants efficient paged KV traffic and low per-token overhead.
Prerequisites and reader contract¶
You need matrix multiplication, reductions, and the ability to read short CUDA C++ kernels. The chapters progress from memory access and thread coordination to tiled GEMM, attention, serving kernels, profiling, and accelerator ports. Exercises and worked solutions use the running shapes above.
Map of this part¶
| Section | Role in the story |
|---|---|
| Execution and memory | Build the resource ledger the rest of the part uses |
| Host orchestration | Show when kernel speed fails to reach the user |
| Hierarchical GEMM | Construct the reusable engine behind linear layers |
| Reductions, scans, histograms | Obtain the collective primitives softmax and routing need |
| Softmax, norm, top-k | Compose those primitives into transformer epilogues |
| FlashAttention | Fuse score, softmax, and value reduction without quadratic HBM |
| Blackwell pipelines | Track asynchronous buffer ownership, tensor memory, and attention dependencies |
| LLM inference kernels | Specialize the schedule for decode, paging, and quantization |
| Profiling and correctness | Close the loop on the running example with evidence |
| Beyond CUDA and NVIDIA | Separate portable model semantics from target-specific execution |
Chapters in this part
- 27GPU Execution, Memory, and Resource Accounting
- 28Host-Device Orchestration, Streams, and CUDA Graphs
- 29Hierarchical Matrix Multiplication
- 30Reductions, Prefix Scans, and Histograms
- 31Softmax, Normalization, Top-K, and Sampling
- 32FlashAttention and IO-Aware Exact Attention
- 33Blackwell Pipelines and Modern Attention Kernels
- 34CUDA Kernels for LLM Inference
- 35Kernel Engineering, Profiling, and Correctness
- 36Accelerator Ecosystems Beyond CUDA and NVIDIA
- 37Model Portability and Compiled State