Reduction은 GPU 압축
Reduction은 병렬 값들을 하나 (또는 작은 한 줌)로 collapse. Sum, max, mean, norm, dot product, softmax 분모 — 다 reduction. 자기 트랙 레슨 받을 자격 있는 이유: 모든 신경망에 가득하고, reduction 잘못하면 hot-path 시간 50%+ 낭비.
이기는 레시피는 계층적:
- Register — 각 thread가 자기 partial sum을 register에 누적 (메모리 traffic 0).
- Warp-level shuffle —
__shfl_down_sync로 warp 안 partial 32개 결합. shared-memory traffic 0. - Shared-memory tree — 각 warp가 자기 sum을 shared-memory slot에 write, 그 다음 slot 가로질러 tree reduction으로 block sum.
- Block 결과 — global 누적기에 atomic add 또는 block sum reduce하는 두 번째 커널 launch.
LLM이 reduction 예산 쓰는 곳:
- Attention softmax — row max + exp(x - max) sum per row. attention head당 reduction 둘.
- LayerNorm / RMSNorm — token당 mean과 (root-)mean-square, 모든 layer.
- Loss 계산 — logit 수십억 → scalar 하나.
- Gradient accumulation — micro-batch gradient를 optimizer step 전에 합산.