Skip to content
Inference Academy

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

26 min · verified 2026-112 animated diagramsPro
Share
Open in ColabRuns on a free T4 GPU: pick Runtime → Change runtime type → T4 GPU.

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:

cpp
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 kk. 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:

cpp
__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.

Summing 32 values in 5 shuffles
Animated
10
81
62
43
24
95
76
57
38
19
810
611
412
213
914
715
516
317
118
819
620
421
222
923
724
525
326
127
828
629
430
231

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:

cpp
// 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

  1. RMSNorm
  2. Softmax, and the online trick
  3. Where else reductions appear
  4. What you can now do
  5. Common mistakes
  6. Exercise
  7. 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