Reduction is GPU compression
A reduction collapses many parallel values into one (or a small handful). Sum, max, mean, norm, dot product, softmax denominator — all reductions. The reason it deserves its own track lesson: every neural network is full of them, and getting reduction wrong wastes 50%+ of your hot-path time.
The winning recipe is hierarchical:
- Registers — each thread accumulates its own partial sum in a register (no memory traffic).
- Warp-level shuffle — combine 32 partials within a warp using
__shfl_down_sync. Zero shared-memory traffic. - Shared-memory tree — each warp writes its sum to one shared-memory slot, then a tree reduction across slots gives the block sum.
- Block result — either an atomic add to a global accumulator, or a second kernel launch that reduces block sums.
Where LLMs spend their reduction budget:
- Attention softmax — find row max + sum of exp(x - max) per row. Two reductions per attention head.
- LayerNorm / RMSNorm — mean and (root-)mean-square per token, every layer.
- Loss computation — billions of logits → one scalar.
- Gradient accumulation — micro-batch gradients summed before optimizer step.