Skip to content

add kernel for rmsnorm and per-token quant - #3844

Merged
junhaha666 merged 9 commits into
ROCm:mainfrom
mqhc2020:marv/gated_rmsnorm_fp8_per_token_quant
Jul 17, 2026
Merged

junhaha666 merged 9 commits into
ROCm:mainfrom
mqhc2020:marv/gated_rmsnorm_fp8_per_token_quant

Conversation

@mqhc2020

@mqhc2020 mqhc2020 commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

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
a8w8 GEMM, with no change to the GEMM — only the activation production is fused.
This targets the Gated DeltaNet out_proj activation 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, gate z (both [num_tokens, num_heads, head_dim]) and RMSNorm
weight ([head_dim]):

  1. Per-head RMSNorm over head_dim: norm(x) = x * weight / sqrt(mean(x^2) + eps)
  2. SiLU gating: out = norm(x) * silu(z)
  3. Flatten to [num_tokens, num_heads * head_dim]
  4. FP8 per-token quantization: one scale per token over the full flattened row
    (scale = rowwise_amax / FP8_MAX), so it matches a per-token-act × per-channel-
    weight a8w8 scheme exactly.

Implementation notes

  • One block per token; the block is sized so all heads of a token are resident
    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.
  • Per-head RMSNorm reduction is warp-group-local (__shfl_xor, no shared memory);
    the per-token amax is reduced across warps via a small shared-memory array.
  • Vectorized 16-elem/thread loads; head_dim (and the inner stride) must be
    unit-stride, while token/head dims may be strided slices.
  • Shares the JIT module module_gated_rmsnorm_quant with the existing fused
    gated-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, contiguous
  • scale: [num_tokens], fp32, pre-allocated
  • x, z: [num_tokens, num_heads, head_dim], bf16/fp16
  • weight: [head_dim], bf16/fp16

Supported / constraints

  • Input: bf16 or fp16. Output: float8_e4m3fnuz or float8_e4m3fn.
  • head_dim == 128, num_heads <= 128 (block must cover all heads).
  • AMD warp size 64.

Robustness

  • Validates x.stride(2) == 1, z.stride(2) == 1, and out.is_contiguous()
    (the kernel addresses out as a flat row-major buffer; a strided out would be
    silently corrupted).
  • Flushes the e4m3fnuz 0x80 encoding (which that format reads as NaN, not −0)
    to +0, so a tiny-negative value rounding to zero magnitude can't become NaN
    downstream. Harmless for e4m3fn.

Testing

op_tests/test_gated_rmsnorm_fp8_per_token_quant.py validates the kernel against a
PyTorch reference (dequantized values + per-token scales) across:

  • TP/DP head counts and token counts from 1 → 8192,
  • edge cases: single token, tiny odd token counts, single head, partial-warp head
    counts, and the max 128-head case,
  • both FP8 output dtypes (e4m3fnuz, e4m3fn).
    It also reports kernel vs reference bandwidth/latency per shape.

Submission Checklist

@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 3844 --add-label <label>

@zufayu
zufayu requested review from junhaha666 and removed request for junhaha666 June 26, 2026 06:14
@mqhc2020
mqhc2020 marked this pull request as ready for review June 26, 2026 15:17
@mqhc2020
mqhc2020 requested review from a team and Copilot June 26, 2026 15:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Python compile_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.

@mqhc2020

Copy link
Copy Markdown
Contributor Author

Hi @junhaha666 Could you help to review this PR?

@junhaha666

Copy link
Copy Markdown
Contributor

Hello @mqhc2020, can you merge gated_rmsnorm_fp8_per_token_quant and gated_rmsnorm_fp8_per_group_quant into the one aiter/ops/gated_rmsnorm_fp8_quant.py file and one op_test?
Thanks.

@mqhc2020

Copy link
Copy Markdown
Contributor Author

Hello @mqhc2020, can you merge gated_rmsnorm_fp8_per_token_quant and gated_rmsnorm_fp8_per_group_quant into the one aiter/ops/gated_rmsnorm_fp8_quant.py file and one op_test? Thanks.

Merge the tests but keep the per-group interface intact.

@junhaha666
junhaha666 merged commit 2174467 into ROCm:main Jul 17, 2026
56 checks passed
@mqhc2020
mqhc2020 deleted the marv/gated_rmsnorm_fp8_per_token_quant branch July 20, 2026 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants