Fused RMSNorm + residual add
Every decoder block adds its output to the residual stream and then normalizes. Unfused, that is an add kernel, a norm kernel, and two extra trips through memory. The fused kernel reads x and the residual once, writes the new residual and the normalized output, and does the reduction in fp32. Numerics match Hugging Face's Qwen2RMSNorm step for step.
Kernel
kernels/rmsnorm.py: _rmsnorm_kernel (23 lines, click to show)
@triton.jit
def _rmsnorm_kernel(
x_ptr, res_ptr, w_ptr, out_ptr, res_out_ptr,
stride_x, stride_res, stride_out, stride_res_out,
N, eps,
HAS_RESIDUAL: tl.constexpr,
BLOCK: tl.constexpr,
):
row = tl.program_id(0)
cols = tl.arange(0, BLOCK)
mask = cols < N
x = tl.load(x_ptr + row * stride_x + cols, mask=mask, other=0.0)
if HAS_RESIDUAL:
r = tl.load(res_ptr + row * stride_res + cols, mask=mask, other=0.0)
x = x + r # fp16 add, same rounding as the unfused model
tl.store(res_out_ptr + row * stride_res_out + cols, x, mask=mask)
h = x.to(tl.float32)
var = tl.sum(h * h, axis=0) / N
normed = (h * tl.rsqrt(var + eps)).to(x.dtype)
w = tl.load(w_ptr + cols, mask=mask, other=0.0)
tl.store(out_ptr + row * stride_out + cols, normed * w, mask=mask)L4
| shape | triton (us) | eager (us) | compile (us) | speedup vs eager | triton % peak bw |
|---|---|---|---|---|---|
| M=1, N=1536, residual=True | 1.3 | 10.2 | 1.3 | 7.80x | 4% |
| M=16, N=1536, residual=True | 2.1 | 14.3 | 2.3 | 6.68x | 31% |
| M=4096, N=1536, residual=True | 214.8 | 476.1 | 213.1 | 2.22x | 78% |
| M=1, N=1536, residual=False | 1.2 | 9.1 | 1.2 | 7.35x | 2% |
| M=16, N=1536, residual=False | 1.7 | 13.0 | 1.8 | 7.73x | 20% |
| M=4096, N=1536, residual=False | 104.7 | 398.7 | 107.0 | 3.81x | 80% |
| M=128, N=1536, residual=True | 7.2 | 19.8 | 7.3 | 2.76x | 73% |
| M=1024, N=1536, residual=True | 57.1 | 82.3 | 54.4 | 1.44x | 73% |
| M=16384, N=1536, residual=True | 846.7 | 4505.3 | 846.9 | 5.32x | 79% |
| M=1, N=4096, residual=True | 1.6 | 10.7 | 1.6 | 6.63x | 8% |
| M=16, N=4096, residual=True | 3.5 | 18.8 | 3.5 | 5.38x | 51% |
| M=128, N=4096, residual=True | 16.6 | 39.1 | 16.5 | 2.35x | 84% |
| M=1024, N=4096, residual=True | 148.5 | 271.0 | 147.0 | 1.83x | 75% |
| M=4096, N=4096, residual=True | 574.5 | 2893.8 | 560.0 | 5.04x | 78% |
| M=16384, N=4096, residual=True | 2277.1 | 12021.2 | 2212.1 | 5.28x | 79% |
| M=128, N=1536, residual=False | 4.0 | 16.5 | 4.3 | 4.10x | 65% |
| M=1024, N=1536, residual=False | 25.6 | 59.0 | 26.9 | 2.30x | 82% |
| M=16384, N=1536, residual=False | 415.8 | 3860.2 | 427.5 | 9.28x | 81% |
| M=1, N=4096, residual=False | 1.4 | 9.4 | 1.4 | 6.53x | 6% |
| M=16, N=4096, residual=False | 2.3 | 17.1 | 2.4 | 7.34x | 39% |
| M=128, N=4096, residual=False | 8.6 | 29.2 | 8.9 | 3.41x | 82% |
| M=1024, N=4096, residual=False | 70.6 | 230.6 | 71.4 | 3.27x | 79% |
| M=4096, N=4096, residual=False | 277.5 | 2467.4 | 283.7 | 8.89x | 81% |
| M=16384, N=4096, residual=False | 1127.2 | 10290.9 | 1125.2 | 9.13x | 79% |
Profile on L4
| launch | time (ns) | DRAM % peak | SM % peak | occupancy % | regs/thread |
|---|---|---|---|---|---|
| rmsnorm_res_M4096 | 109888 | 94.59 | 8.04 | 91.84 | 30 |
H100
| shape | triton (us) | eager (us) | compile (us) | speedup vs eager | triton % peak bw |
|---|---|---|---|---|---|
| M=1, N=1536, residual=True | 1.5 | 12.7 | 1.4 | 8.69x | 0% * |
| M=16, N=1536, residual=True | 2.2 | 18.8 | 2.3 | 8.45x | 3% |
| M=128, N=1536, residual=True | 2.6 | 20.7 | 2.7 | 7.93x | 18% |
| M=1024, N=1536, residual=True | 6.1 | 33.3 | 6.0 | 5.49x | 62% |
| M=4096, N=1536, residual=True | 19.2 | 97.8 | 19.5 | 5.09x | 78% |
| M=16384, N=1536, residual=True | 69.8 | 419.8 | 70.9 | 6.02x | 86% |
| M=1, N=4096, residual=True | 1.7 | 13.3 | 1.8 | 8.01x | 1% |
| M=16, N=4096, residual=True | 2.5 | 23.5 | 2.4 | 9.36x | 6% |
| M=128, N=4096, residual=True | 3.5 | 27.0 | 3.5 | 7.69x | 36% |
| M=1024, N=4096, residual=True | 13.6 | 65.5 | 13.6 | 4.82x | 74% |
| M=4096, N=4096, residual=True | 47.2 | 285.1 | 47.7 | 6.03x | 85% |
| M=16384, N=4096, residual=True | 180.5 | 1075.6 | 183.2 | 5.96x | 89% |
| M=1, N=1536, residual=False | 1.4 | 11.3 | 1.4 | 8.00x | 0% |
| M=16, N=1536, residual=False | 2.1 | 17.5 | 2.2 | 8.40x | 1% |
| M=128, N=1536, residual=False | 2.4 | 18.8 | 2.5 | 7.88x | 10% |
| M=1024, N=1536, residual=False | 4.0 | 29.2 | 4.4 | 7.30x | 47% |
| M=4096, N=1536, residual=False | 11.0 | 87.6 | 12.2 | 8.00x | 69% |
| M=16384, N=1536, residual=False | 36.8 | 368.8 | 39.8 | 10.01x | 82% |
| M=1, N=4096, residual=False | 1.5 | 11.5 | 1.6 | 7.54x | 0% |
| M=16, N=4096, residual=False | 2.3 | 21.8 | 2.4 | 9.39x | 3% |
| M=128, N=4096, residual=False | 2.9 | 24.4 | 3.2 | 8.55x | 22% |
| M=1024, N=4096, residual=False | 8.0 | 58.5 | 8.1 | 7.33x | 63% |
| M=4096, N=4096, residual=False | 24.9 | 250.2 | 25.0 | 10.04x | 80% |
| M=16384, N=4096, residual=False | 91.9 | 943.4 | 91.6 | 10.26x | 87% |
Profile on H100
| launch | time (ns) | DRAM % peak | SM % peak | occupancy % | regs/thread |
|---|---|---|---|---|---|
| rmsnorm_res_M4096 | 15328 | 62.61 | 26.99 | 85.13 | 30 |
Times are CUDA-graph medians over 3 sweeps with rotating buffers (working set at least 4x L2). * = more than 10% spread between sweeps.