Skip to content

Add fused Allreduce + RMSNorm + MXFP4 quant - #3229

Merged
valarLip merged 5 commits into
ROCm:mainfrom
hubertlu-tw:fused_ar_rms_mxfp4_quant
Jun 3, 2026
Merged

valarLip merged 5 commits into
ROCm:mainfrom
hubertlu-tw:fused_ar_rms_mxfp4_quant

Conversation

@hubertlu-tw

@hubertlu-tw hubertlu-tw commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Quantized ROCm/AITER model paths often follow a layer-boundary pattern where the previous MLP / MoE output is tensor-parallel all-reduced and RMS-normalized before the next attention or MLP block consumes a quantized activation:

output_local --AR--> RMSNorm --quantize--> next-layer input

Today this runs as three sequential kernels with bf16 traffic between them. AITER already has a fused allreduce + RMSNorm + per-group FP8 quantization path from #2823, and this PR adds an MXFP4 quantization epilogue for DeepSeek-R1-MXFP4. This PR reworks the Python API so both quantization schemes use one generic entry point that can be extended to future quantization formats.

Technical Details

Public API

from aiter.dist.communication_op import (
    tensor_model_parallel_fused_allreduce_rmsnorm_quant,
)

# quant_type="per_token" keeps the existing FP8 per-token behavior.
# quant_type="per_group" / "per_1x128" selects FP8 per-group quantization.
# quant_type="mxfp4" / "per_1x32" selects MXFP4 quantization.
out = tensor_model_parallel_fused_allreduce_rmsnorm_quant(
    input_,         # (M, K) bf16/fp16, layer output to all-reduce
    residual_,      # (M, K), residual added before RMSNorm
    weight_,        # (K,), RMSNorm gain
    eps,            # RMSNorm epsilon
    quant_type="mxfp4",
    group_size=128, # used by per-group FP8
    emit_bf16=False,
)

Return values are (quantized_out, residual_out, scale). With emit_bf16=True, the API returns (quantized_out, residual_out, scale, bf16_normed), where bf16_normed is the bf16-rounded RMSNorm output for shadow consumers such as NSA / indexer paths.

The previous public functions remain as compatibility wrappers:

  • tensor_model_parallel_fused_allreduce_rmsnorm_quant_per_group(...) calls the generic API with quant_type="per_group".
  • tensor_model_parallel_fused_allreduce_rmsnorm_mxfp4_quant(...) calls the generic API with quant_type="mxfp4".

Behavior and dispatch

The generic API normalizes quantization aliases and dispatches to the matching implementation:

  • per_token: existing fused AR+RMSNorm + FP8 per-token quantization path.
  • per_group / per_1x128: fused AR+RMSNorm + FP8 per-group quantization path from Add fused AR + RMSNorm + per-group FP8 quant: optional bf16 side-output #2823, with composed fallback to fused_allreduce_rmsnorm + HIP per-group quantization.
  • mxfp4 / per_1x32: new fused AR+RMSNorm + MXFP4 quantization path, with composed fallback to fused_allreduce_rmsnorm + Triton dynamic_mxfp4_quant.

For MXFP4, the implementation auto-selects between specialized direct kernels and fallback based on shape, TP world size, and the kernel’s shared-memory budget. Callers do not need to know which path runs: semantics and the reference composed path match across direct and fallback routes.

AITER_AR_1STAGE is reserved as a test/benchmark override and should be left unset in production.

Code map (AITER)

  • Python / dispatch
    • aiter/dist/communication_op.py
    • aiter/dist/parallel_state.py
    • aiter/dist/device_communicators/communicator_cuda.py
    • aiter/dist/device_communicators/custom_all_reduce.py
    • aiter/ops/custom_all_reduce.py
  • HIP kernel family + dispatcher
    • csrc/include/custom_all_reduce.cuh
    • csrc/include/custom_all_reduce.h
    • csrc/include/rocm_ops.hpp
    • csrc/kernels/custom_all_reduce.cu
  • Tests and microbench (op_tests/multigpu_tests/)

SGLang integration (companion change)

A separate SGLang PR wires LayerCommunicator.prepare_attn to call the generic API with quant_type="mxfp4" on the DeepSeek-R1-MXFP4 path. It is activated with SGLANG_USE_AITER=1 and --enable-aiter-allreduce-fusion, and falls back to the existing fused AR+RMSNorm sequence when any gate (hardware, model, communicator support) fails.

Profiling

A torch profiler trace during decode shows allreduce_fusion_kernel_*_mxfp4 active and the layer-boundary _dynamic_mxfp4_quant_kernel cost folding into the fused kernel. The standalone _fused_rms_mxfp4_quant_kernel on the local DeepSeek MLA Q/K RMSNorm+MXFP4 path remains as expected (no all-reduce to fuse).

Risks and mitigations

  • API compatibility: The old per-group FP8 and MXFP4 helper names are preserved as wrappers, while new callers can select quantization behavior through quant_type.
  • Quantized numerics: Bitwise equality with a bf16 reference is not expected after quant/dequant. Unit tests compare FP8 per-group and MXFP4 dequantized outputs to reference composed paths, and compare optional bf16 side-output to a bf16 reference.
  • Cross-device shared-memory reduce-scatter stage: Same memory-ordering invariants as hardened in Fix 2-stage fused_allreduce_rmsnorm memory ordering #2890; covered by test_fused_ar_rms_memory_order.py.
  • Unsupported shapes: Fall through to existing AITER fused AR+RMSNorm + Triton MXFP4 quant; behavior is unchanged on that path.

Test Plan

AITER unit correctness — FP8 per-group (test_fused_ar_rms_per_group_quant.py)

Validates the previously added #2823 feature through the generic API:

tensor_model_parallel_fused_allreduce_rmsnorm_quant(
    input_, residual_, weight_, eps,
    quant_type="per_group",
    group_size=128,
    emit_bf16=...,
)

Focused smoke command used during this rework:

python3 op_tests/multigpu_tests/test_fused_ar_rms_per_group_quant.py \
  -t 2 -s 1,4096 32,4096 --group-size 128 --skip-python-check

AITER unit correctness — MXFP4 (test_fused_ar_rms_mxfp4_quant.py)

  • CI smoke (picked up by .github/scripts/aiter_test.sh, no flags): 3 shapes (one per dispatch path) × tp_size in {2, 4, 8} via the user-facing dispatcher.

    python3 op_tests/multigpu_tests/test_fused_ar_rms_mxfp4_quant.py
  • Local full matrix (developer opt-in, pre-merge / pre-release):

    python3 op_tests/multigpu_tests/test_fused_ar_rms_mxfp4_quant.py --full

Per row the test reports mxfp4_dequant_err, residual_err, and optional bf16_err against an fp32 reference of the composed path (tolerances as in

Technical Details).

Focused smoke command used during this rework:

python3 op_tests/multigpu_tests/test_fused_ar_rms_mxfp4_quant.py \
  -t 2 -s 1,4096 16,7168 128,7168 --skip-python-check

Memory-ordering regression (test_fused_ar_rms_memory_order.py)

python3 op_tests/multigpu_tests/test_fused_ar_rms_memory_order.py \
  --tp 8 --iters 2000

CUDA-graph microbench — MXFP4 (benchmark_fused_ar_rms_mxfp4_quant.py)

Dense sweep across tp_size in {2, 4, 8} and 32 shapes per TP (decode and
prefill-style regions for multiple K). Example driver:

SHAPES="1,2880;16,2880;32,2880;64,2880;80,2880;128,2880;\
1,4096;16,4096;32,4096;64,4096;80,4096;128,4096;\
1,5120;16,5120;32,5120;80,5120;\
1,6144;16,6144;32,6144;80,6144;\
1,7168;4,7168;16,7168;32,7168;64,7168;80,7168;128,7168;\
1,8192;16,8192;32,8192;64,8192;80,8192"
for tp in 2 4 8; do
  python3 op_tests/multigpu_tests/benchmark_fused_ar_rms_mxfp4_quant.py \
    -t $tp --cuda-graph --warmup 5 --iters 30 --shapes "$SHAPES"
done

Focused command used during this rework:

python3 op_tests/multigpu_tests/benchmark_fused_ar_rms_mxfp4_quant.py \
  -t 2 --cuda-graph --warmup 3 --iters 10 \
  --shapes "1,4096;16,7168;128,7168"

SGLang E2E (companion stack; validates wiring + accuracy)

DeepSeek-R1-MXFP4-Preview at TP=4 with EAGLE, fp8_e4m3 KV, 200k context. Baseline: same server and benchmark commands without --enable-aiter-allreduce-fusion. Fused: add only that flag.

Server launch (representative; fused adds --enable-aiter-allreduce-fusion):

SGLANG_ENABLE_OVERLAP_PLAN_STREAM=1 \
SGLANG_ENABLE_SPEC_V2=1 \
ROCM_QUICK_REDUCE_QUANTIZATION=NONE \
SGLANG_AITER_FP8_PREFILL_ATTN=1 \
SGLANG_AITER_MLA_PERSIST=1 \
AITER_MXFP4_MOE_SF=1 \
SGLANG_USE_AITER=1 \
SGLANG_INT4_WEIGHT=0 \
SGLANG_MOE_PADDING=1 \
SGLANG_SET_CPU_AFFINITY=1 \
SGLANG_ROCM_FUSED_DECODE_MLA=1 \
SGLANG_USE_ROCM700A=1 \
SGLANG_DISABLE_FUSED_AR_MXFP4_QUANT=false \
python3 -m sglang.launch_server \
  --model-path /data2/deepseek-ai/DeepSeek-R1-MXFP4-Preview/ \
  --tensor-parallel-size 4 --trust-remote-code \
  --mem-fraction-static 0.9 --chunked-prefill-size 131072 \
  --attention-backend aiter \
  --speculative-algorithm EAGLE --speculative-num-steps 3 \
  --speculative-eagle-topk 1 --speculative-num-draft-tokens 4 \
  --max-running-requests 32 --context-length 200000 \
  --kv-cache-dtype fp8_e4m3 \
  --model-loader-extra-config '{"enable_multithread_load": true, "num_threads": 8}'

GSM8K:

python3 benchmark/gsm8k/bench_sglang.py \
  --num-questions 1319 --parallel 1319 --num-shots 5 --port 8000

Test Result

Generic API rework validation

Focused local validation after the API rework:

  • Format/lint: py_compile, Black, Ruff, and IDE lints passed on the touched Python files.
  • FP8 per-group path from Add fused AR + RMSNorm + per-group FP8 quant: optional bf16 side-output #2823: test_fused_ar_rms_per_group_quant.py -t 2 -s 1,4096 32,4096 --group-size 128 --skip-python-check passed through tensor_model_parallel_fused_allreduce_rmsnorm_quant(..., quant_type="per_group") for emit_bf16=False and emit_bf16=True. Reported per_group_err = 0 for all rows.
  • MXFP4 path: test_fused_ar_rms_mxfp4_quant.py -t 2 -s 1,4096 16,7168 128,7168 --skip-python-check passed through tensor_model_parallel_fused_allreduce_rmsnorm_quant(..., quant_type="mxfp4") across direct_1stage, direct_2stage, and fallback routes. Reported mxfp4_dequant_err = 0 and residual_err = 0 for all rows.
  • MXFP4 CUDA-graph microbench smoke: TP=2, shapes 1,4096;16,7168;128,7168, --warmup 3 --iters 10; all correctness checks passed and the generic API timing tracked the selected route.

Microbench smoke rows:

Shape (M x K) Expected route Fallback us Direct 1-stage us Direct 2-stage us API us Faster path
1 x 4096 direct 1-stage 16.54 15.24 17.92 15.64 direct 1-stage
16 x 7168 direct 2-stage 23.84 inf 21.70 22.15 direct 2-stage
128 x 7168 fallback 56.60 inf inf 56.92 fallback

Post-run GPU/core check: no lingering benchmark/test processes and no gpucore.* dumps found.

Existing MXFP4 dense validation

test_fused_ar_rms_mxfp4_quant.py (CI smoke)

Every row reported mxfp4_dequant_err = residual_err = bf16_err = 0.0 against the fp32 reference. Use -t N to restrict to a single TP for debugging.

test_fused_ar_rms_mxfp4_quant.py --full (local)

Last full sweep on the author workstation: 0 hard failures and 0 non-zero err lines at TP=2 and TP=4 (82 rows each). TP=8 inherits the same kernels and is additionally covered by the microbench correctness matrix below.

Microbench (96 rows: 32 shapes × TP 2/4/8)

0 correctness failures. Aggregate fused vs composed fallback (>= 1.05x = win, 0.95–1.05x = tie, < 0.95x = loss):

TP shapes wins ties losses median speedup
2 32 12 20 0 1.034x
4 32 13 19 0 1.039x
8 32 8 24 0 1.013x

TP=8 gate-boundary behavior: e.g. 32 x 4096 routes to 2-stage; 1 x 8192 in tie range. The TP=8 table includes a small post-crossover rerun for rows whose route changed after the dense sweep.

Per-shape latency — TP = 2
Shape (M x K) bytes Fused API us Fallback us Speedup
1 x 2880 5 KiB 15.06 15.87 1.054x
16 x 2880 90 KiB 16.79 17.43 1.038x
32 x 2880 180 KiB 18.33 22.53 1.229x
64 x 2880 360 KiB 28.29 28.27 0.999x
80 x 2880 450 KiB 30.21 30.16 0.998x
128 x 2880 720 KiB 35.73 35.69 0.999x
1 x 4096 8 KiB 15.25 15.84 1.039x
16 x 4096 128 KiB 17.29 18.15 1.050x
32 x 4096 256 KiB 19.52 23.83 1.221x
64 x 4096 512 KiB 26.70 30.89 1.157x
80 x 4096 640 KiB 33.94 33.92 0.999x
128 x 4096 1024 KiB 40.89 40.98 1.002x
1 x 5120 10 KiB 15.68 16.23 1.035x
16 x 5120 160 KiB 18.27 22.37 1.224x
32 x 5120 320 KiB 22.78 25.62 1.125x
80 x 5120 800 KiB 37.61 37.74 1.003x
1 x 6144 12 KiB 15.84 16.37 1.033x
16 x 6144 192 KiB 18.67 23.17 1.241x
32 x 6144 384 KiB 23.94 26.97 1.127x
80 x 6144 960 KiB 40.68 40.67 1.000x
1 x 7168 14 KiB 16.29 16.52 1.014x
4 x 7168 56 KiB 16.77 16.88 1.007x
16 x 7168 224 KiB 21.58 23.57 1.092x
32 x 7168 448 KiB 25.88 28.34 1.095x
64 x 7168 896 KiB 39.40 39.31 0.998x
80 x 7168 1120 KiB 43.48 43.45 0.999x
128 x 7168 1792 KiB 56.40 56.45 1.001x
1 x 8192 16 KiB 16.46 16.64 1.011x
16 x 8192 256 KiB 22.17 24.07 1.086x
32 x 8192 512 KiB 26.76 29.20 1.091x
64 x 8192 1024 KiB 41.31 41.24 0.998x
80 x 8192 1280 KiB 46.67 46.73 1.001x
Per-shape latency — TP = 4
Shape (M x K) bytes Fused API us Fallback us Speedup
1 x 2880 5 KiB 17.32 18.11 1.046x
16 x 2880 90 KiB 17.53 18.37 1.048x
32 x 2880 180 KiB 18.86 21.62 1.146x
64 x 2880 360 KiB 26.14 26.08 0.998x
80 x 2880 450 KiB 27.22 27.61 1.014x
128 x 2880 720 KiB 30.41 30.08 0.989x
1 x 4096 8 KiB 16.19 17.43 1.077x
16 x 4096 128 KiB 18.64 19.33 1.037x
32 x 4096 256 KiB 20.29 22.51 1.109x
64 x 4096 512 KiB 22.19 27.08 1.220x
80 x 4096 640 KiB 29.23 29.41 1.006x
128 x 4096 1024 KiB 33.58 33.52 0.998x
1 x 5120 10 KiB 18.19 19.23 1.057x
16 x 5120 160 KiB 18.96 21.80 1.150x
32 x 5120 320 KiB 20.81 23.70 1.139x
80 x 5120 800 KiB 31.43 31.53 1.003x
1 x 6144 12 KiB 18.20 18.39 1.010x
16 x 6144 192 KiB 20.98 23.30 1.111x
32 x 6144 384 KiB 21.45 24.26 1.131x
80 x 6144 960 KiB 33.07 33.06 1.000x
1 x 7168 14 KiB 17.67 17.91 1.014x
4 x 7168 56 KiB 18.39 19.14 1.041x
16 x 7168 224 KiB 21.74 23.25 1.069x
32 x 7168 448 KiB 22.47 24.86 1.106x
64 x 7168 896 KiB 31.91 32.17 1.008x
80 x 7168 1120 KiB 34.97 34.99 1.001x
128 x 7168 1792 KiB 41.79 41.95 1.004x
1 x 8192 16 KiB 18.10 18.15 1.003x
16 x 8192 256 KiB 21.28 22.40 1.053x
32 x 8192 512 KiB 23.11 25.38 1.098x
64 x 8192 1024 KiB 33.20 33.39 1.006x
80 x 8192 1280 KiB 36.08 36.23 1.004x
Per-shape latency — TP = 8
Shape (M x K) bytes Fused API us Fallback us Speedup
1 x 2880 5 KiB 17.65 18.40 1.042x
16 x 2880 90 KiB 20.55 22.29 1.085x
32 x 2880 180 KiB 22.82 22.92 1.004x
64 x 2880 360 KiB 27.58 27.28 0.989x
80 x 2880 450 KiB 27.52 27.94 1.015x
128 x 2880 720 KiB 29.05 29.08 1.001x
1 x 4096 8 KiB 19.98 20.21 1.012x
16 x 4096 128 KiB 20.54 21.51 1.047x
32 x 4096 256 KiB 21.50 23.68 1.101x
64 x 4096 512 KiB 22.79 27.02 1.186x
80 x 4096 640 KiB 28.95 29.07 1.004x
128 x 4096 1024 KiB 31.00 30.96 0.999x
1 x 5120 10 KiB 19.31 20.09 1.040x
16 x 5120 160 KiB 20.71 22.85 1.103x
32 x 5120 320 KiB 22.72 25.28 1.113x
80 x 5120 800 KiB 30.68 30.69 1.000x
1 x 6144 12 KiB 19.33 20.20 1.045x
16 x 6144 192 KiB 22.12 24.16 1.092x
32 x 6144 384 KiB 22.13 24.57 1.110x
80 x 6144 960 KiB 31.34 31.57 1.007x
1 x 7168 14 KiB 19.30 21.48 1.113x
4 x 7168 56 KiB 21.44 21.12 0.985x
16 x 7168 224 KiB 22.60 23.63 1.046x
32 x 7168 448 KiB 24.49 24.66 1.007x
64 x 7168 896 KiB 29.73 29.70 0.999x
80 x 7168 1120 KiB 32.26 32.02 0.993x
128 x 7168 1792 KiB 34.55 34.45 0.997x
1 x 8192 16 KiB 20.63 20.51 0.994x
16 x 8192 256 KiB 23.37 24.42 1.045x
32 x 8192 512 KiB 24.26 25.09 1.034x
64 x 8192 1024 KiB 31.72 32.05 1.010x
80 x 8192 1280 KiB 32.44 32.24 0.994x

SGLang E2E (companion)

GSM8K (three runs each; both configs pass >= 0.94 gate):

Run Baseline Fused (--enable-aiter-allreduce-fusion)
Avg over three runs 0.941 0.94

**Trace view
Unfused:
image

Fused:
image

@hubertlu-tw
hubertlu-tw requested review from a team, HaiShaw, TennyWang1223 and valarLip May 15, 2026 18:24
@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 3229 --add-label <label>

@valarLip

Copy link
Copy Markdown
Collaborator

maybe we need a unified tensor_model_parallel_fused_allreduce_rmsnorm_quant API...

@hubertlu-tw

hubertlu-tw commented May 19, 2026 •

Copy link
Copy Markdown
Contributor Author

maybe we need a unified tensor_model_parallel_fused_allreduce_rmsnorm_quant API...

@valarLip That makes sense.
It's probably cleaner in that way. Let me rework this PR so that we can have a generic tensor_model_parallel_fused_allreduce_rmsnorm_quant API which covers the previously-added per-group quantization (FP8) from #2823 (tensor_model_parallel_fused_allreduce_rmsnorm_quant_per_group) and the new MXFP4 quantization from this PR. With that, we can also expand other quantization schemes in the future.

CC: @TennyWang1223

Route FP8 per-group and MXFP4 fused allreduce RMSNorm quantization through a generic quant_type selector while keeping compatibility wrappers intact.
Apply Black formatting after rebasing the fused quant API changes onto origin/main.
@hubertlu-tw
hubertlu-tw force-pushed the fused_ar_rms_mxfp4_quant branch from 7a249be to edff6b9 Compare May 20, 2026 21:49
@hubertlu-tw

Copy link
Copy Markdown
Contributor Author

Hi @valarLip and @TennyWang1223, I have refactored the PR so that we can have an unified tensor_model_parallel_fused_allreduce_rmsnorm_quant API which normalizes quantization aliases and dispatches to the matching implementation:

  • per_token: existing fused AR+RMSNorm + FP8 per-token quantization path.
  • per_group / per_1x128: fused AR+RMSNorm + FP8 per-group quantization path from Add fused AR + RMSNorm + per-group FP8 quant: optional bf16 side-output #2823, with composed fallback to fused_allreduce_rmsnorm + HIP per-group quantization.
  • mxfp4 / per_1x32: new fused AR+RMSNorm + MXFP4 quantization path, with composed fallback to fused_allreduce_rmsnorm + Triton dynamic_mxfp4_quant.

Please help review it again. Thanks!

@valarLip
valarLip merged commit de7c43c into ROCm:main Jun 3, 2026
41 checks passed
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.

2 participants