Warp shuffles and reductions: softmax and RMSNorm kernels
How threads in a warp exchange registers directly with shuffle instructions, how to build a warp and a block reduction from them, and how to use those to write the two reductions every transformer runs constantly: RMSNorm and softmax, including the online softmax that makes FlashAttention possible.
By Sandip Maity, Applied AI Engineer
Matmuls get the attention, but every transformer layer also runs reductions: RMSNorm (sum of squares), softmax (max and sum), sampling (max, sum and sometimes a sort), and the softmax inside attention. They're all bandwidth-bound and all need threads to combine their results. GPUs have a fast path for exactly that.
#Warp shuffles
Threads in the same warp can read each other's registers directly, without touching memory, using shuffle instructions:
float v = ...; // each lane has a value
float w = __shfl_xor_sync(0xffffffff, v, 16); // read v from lane (my_lane XOR 16)The first argument is a mask of lanes taking part (all 32 here). __shfl_xor_sync(mask, v, k) gives each lane the value from the lane whose index differs in bit . There are other variants: __shfl_down_sync (read from lane + offset), __shfl_up_sync, and __shfl_sync (read from any lane).
A warp sum takes five steps. With offsets 16, 8, 4, 2 and 1, each lane adds the value from its partner, and after the fifth step every lane holds the total:
__device__ float warp_sum(float v) {
for (int o = 16; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffff, v, o);
return v;
}Step through it. Each colour is one partial sum; watch them merge.
Start: one value per lane
Each of the 32 lanes holds its own number. No shared memory, no barriers: the warp will combine them through registers.
Lanes with the same colour hold the same partial sum. The numbers are each lane's register value after the step.
Five shuffles for 32 values, no shared memory and no barriers. The same pattern works for max (fmaxf instead of +) or any associative operation.
#Block reductions
Shuffles stop at the warp boundary. To reduce across a whole block, each warp reduces its own values, lane 0 of each warp writes the warp's partial result to shared memory, the block synchronises, and the first warp reduces those partial results:
// Sum over all threads of a block. blockDim.x must be a multiple of 32 (up to 1,024).
__device__ float block_sum(float v) {
__shared__ float part[32];
int lane = threadIdx.x % 32, warp = threadIdx.x / 32;
v = warp_sum(v);
if (lane == 0) part[warp] = v;
__syncthreads();
v = (threadIdx.x < blockDim.x / 32) ? part[lane] : 0.f;
if (warp == 0) v = warp_sum(v);
if (threadIdx.x == 0) part[0] = v;
__syncthreads();
return part[0]; // every thread gets the total
}If you call block_sum twice in one kernel, add a __syncthreads() before the second call, so no thread overwrites part while another is still reading the first result.
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, including 1 animated diagram, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- RMSNorm
- Softmax, and the online trick
- Where else reductions appear
- What you can now do
- Common mistakes
- Exercise
- Going deeper
You will be able to answer
- How many shuffle steps does a warp need to sum 32 values with __shfl_xor_sync?
- Why is a block reduction done in two stages, warps first and then one warp over the partial sums?
- What problem does online softmax solve?
- An RMSNorm kernel reads FP16 inputs. Why does it add the squares up in FP32?
- An RMSNorm kernel on [8, 4096] FP16 inputs runs at 5% of peak bandwidth. What is the most likely cause?
Articles that build on this lesson