Repository navigation
add kernel for rmsnorm and per-token quant - #3844
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
There was a problem hiding this comment.
Pull request overview
This PR adds a new fused activation kernel path for FP8 workflows: per-head gated RMSNorm followed by FP8 per-token quantization (one scale per token across all heads), exposed via a Python JIT binding and validated with a new op_test.
Changes:
- Add HIP kernel + C++ entrypoint for
gated_rmsnorm_fp8_per_token_quant(per-token FP8 scale over flattened[num_heads*head_dim]). - Expose the new op via pybind (
GATED_RMSNORM_QUANT_PYBIND) and a Pythoncompile_ops("module_gated_rmsnorm_quant")wrapper. - Add a new test/benchmark script validating correctness vs a PyTorch reference across shapes and FP8 dtypes.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| op_tests/test_gated_rmsnorm_fp8_per_token_quant.py | New correctness + perf test for the per-token FP8 quant fused kernel. |
| csrc/kernels/gated_rmsnorm_quant_kernels.cu | Implements the per-token quant fused kernel + launcher + Torch entrypoint. |
| csrc/include/rocm_ops.hpp | Adds pybind registration for gated_rmsnorm_fp8_per_token_quant. |
| csrc/include/gated_rmsnorm_quant.h | Declares the new C++ API in the public header. |
| aiter/ops/gated_rmsnorm_fp8_per_token_quant.py | Adds the Python JIT stub binding to the new kernel in the shared module. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hi @junhaha666 Could you help to review this PR? |
|
Hello @mqhc2020, can you merge |
Merge the tests but keep the per-group interface intact. |
Summary
Adds a HIP kernel (and its Python binding) that fuses per-head gated RMSNorm
with dynamic FP8 per-token quantization. It emits the FP8 activation and the
per-token scale that feed directly into a per-token-activation × per-channel-weight
a8w8GEMM, with no change to the GEMM — only the activation production is fused.This targets the Gated DeltaNet
out_projactivation path in Qwen3.5 / Qwen3-Next(AttnFP8 checkpoints), where today the gated RMSNorm and the per-token activation
quant run as two separate kernels before the FP8 GEMM. Fusing them removes a full
read/write round-trip of the activation through HBM.
What the kernel computes
For input
x, gatez(both[num_tokens, num_heads, head_dim]) and RMSNormweight([head_dim]):head_dim:norm(x) = x * weight / sqrt(mean(x^2) + eps)out = norm(x) * silu(z)[num_tokens, num_heads * head_dim](
scale = rowwise_amax / FP8_MAX), so it matches a per-token-act × per-channel-weight
a8w8scheme exactly.Implementation notes
in a single pass (gated values kept in registers, then quantized) — this lets the
per-token amax be reduced across all heads within the block.
__shfl_xor, no shared memory);the per-token amax is reduced across warps via a small shared-memory array.
head_dim(and the inner stride) must beunit-stride, while token/head dims may be strided slices.
module_gated_rmsnorm_quantwith the existing fusedgated-RMSNorm + group-quant kernel (Fuse gated rmsnorm + group_quant for qwen3next and qwen3.5 #2499).
API
gated_rmsnorm_fp8_per_token_quant(out, scale, x, z, weight, epsilon)out:[num_tokens, num_heads*head_dim], FP8, pre-allocated, contiguousscale:[num_tokens], fp32, pre-allocatedx,z:[num_tokens, num_heads, head_dim], bf16/fp16weight:[head_dim], bf16/fp16Supported / constraints
float8_e4m3fnuzorfloat8_e4m3fn.head_dim == 128,num_heads <= 128(block must cover all heads).Robustness
x.stride(2) == 1,z.stride(2) == 1, andout.is_contiguous()(the kernel addresses
outas a flat row-major buffer; a stridedoutwould besilently corrupted).
e4m3fnuz0x80encoding (which that format reads as NaN, not −0)to
+0, so a tiny-negative value rounding to zero magnitude can't become NaNdownstream. Harmless for
e4m3fn.Testing
op_tests/test_gated_rmsnorm_fp8_per_token_quant.pyvalidates the kernel against aPyTorch reference (dequantized values + per-token scales) across:
counts, and the max 128-head case,
e4m3fnuz,e4m3fn).It also reports kernel vs reference bandwidth/latency per shape.
Submission Checklist