Row-parallel RMS normalization.
(
X_ptr, W_ptr, OUT_ptr,
M, N,
stride_xm, stride_xn,
stride_om, stride_on,
eps,
BLOCK_SIZE: tl.constexpr,
)
| 12 | |
| 13 | @triton.jit |
| 14 | def rmsnorm_kernel( |
| 15 | X_ptr, W_ptr, OUT_ptr, |
| 16 | M, N, |
| 17 | stride_xm, stride_xn, |
| 18 | stride_om, stride_on, |
| 19 | eps, |
| 20 | BLOCK_SIZE: tl.constexpr, |
| 21 | ): |
| 22 | """Row-parallel RMS normalization.""" |
| 23 | row = tl.program_id(0) |
| 24 | offs = tl.arange(0, BLOCK_SIZE) |
| 25 | mask = offs < N |
| 26 | |
| 27 | # Load row into float32 for numerical stability |
| 28 | x = tl.load(X_ptr + row * stride_xm + offs * stride_xn, mask=mask, other=0.0).to(tl.float32) |
| 29 | |
| 30 | # Compute RMS |
| 31 | sq_mean = tl.sum(x * x, axis=0) / N |
| 32 | rms = tl.sqrt(sq_mean + eps) |
| 33 | |
| 34 | # Normalize |
| 35 | x_norm = x / rms |
| 36 | |
| 37 | # Scale by weight |
| 38 | w = tl.load(W_ptr + offs, mask=mask, other=0.0).to(tl.float32) |
| 39 | out = x_norm * w |
| 40 | |
| 41 | # Store (cast back to input dtype via the output tensor's dtype) |
| 42 | tl.store(OUT_ptr + row * stride_om + offs * stride_on, out, mask=mask) |
| 43 | |
| 44 | |
| 45 | def kernel_fn(x: torch.Tensor, weight: torch.Tensor, eps: float = 1e-6) -> torch.Tensor: |
nothing calls this directly
no outgoing calls
no test coverage detected