Skip to content

[Triton/Gluon] act_mul_and_mxfp4_quant: honour scaling_mode, add "roundup" and round_to_input_dtype; round to E2M1 with the gfx950 instruction - #7116

Merged
azaidy merged 4 commits into
ROCm:mainfrom
vorapolsiloai:fix/act-mul-mxfp4-quant-scaling-mode
Nov 24, 2026

Conversation

@vorapolsiloai

@vorapolsiloai vorapolsiloai commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

act_mul_and_mxfp4_quant: scaling_mode was ignored; it is now honoured, with a new "roundup" mode and round_to_input_dtype, so the fused op gives the same bytes as an unfused act-and-mul plus per_1x32_f4_quant_hip. On gfx950 the kernel also rounds to E2M1 with v_cvt_scalef32_pk_fp4_f32 (folded in from #7279), same output bytes.

MI355X, Qwen3.8-27B MXFP4 on SGLang: kernel -24% to -30% at M >= 4096; with SGLang's fused producer TTFT -1.7% to -1.9% (TP1), TP2 GSM8K 0.880-0.900 -> 0.935-0.950. Not claimed: fused_reduce_act_mul_and_mxfp4_quant is not changed (waits for #6024); MI250X/MI300X not run.

Full description, folded-in PRs, effect per commit, both parts

Two changes to act_mul_and_mxfp4_quant and its Triton kernel:

  1. scaling_mode is honoured (it was ignored), plus a new "roundup" mode and round_to_input_dtype, so the fused op
    can produce the same bytes as an unfused act-and-mul followed by per_1x32_f4_quant_hip. This was the original PR
    (first commit, plus the review commit).
  2. On gfx950 the kernel rounds to E2M1 with v_cvt_scalef32_pk_fp4_f32 (_mxfp4_quant_op(USE_ASM=True)), with the same
    output bytes; 24-30% faster at prefill sizes. Was [Perf] act_mul_and_mxfp4_quant: Round to E2M1 with the gfx950 instruction #7279.

Folded-in PRs

#7279 was stacked on this PR by the same author, on the same call site, and is closed in favour of it. Its commit was
cherry-picked unchanged (git cherry-pick -x); the picked commit keeps its message, plus the (cherry picked from commit ...) line and a Folded in from #7279 (<sha>) line. Before the rebase onto main the tree was byte-identical to #7279's head; the rebase did not touch that commit's diff.

source PR original commit commit here what it does measured effect
#7279 0c96ad36ef 88e048e2d9 _act_mul_and_dynamic_mxfp4_quant_kernel passes USE_ASM=True to _mxfp4_quant_op kernel -24% at M 16384 x N 17408 (325 -> 248 us); Qwen3.8-27B TP1 TTFT -0.8..-1.0% at 100k-200k tokens

Effect per commit

MI355X (gfx950), Qwen3.8-27B MXFP4 on SGLang, TP1; setups in the parts below.

commit change effect
a5c57bfbe8 scaling_mode honoured, "roundup", round_to_input_dtype enables SGLang's bit-exact fused SiLU-and-mul + MXFP4 producer (sgl-project/sglang#39912, Part 4): TTFT -1.7..-1.9%, TPOT -3.1% / -2.1% / -1.1% at 16 / 64 / 256 concurrent; fixes the TP2 GSM8K drop of the inexact producer (0.880-0.900 -> 0.935-0.950)
88e048e2d9 E2M1 rounding with the gfx950 instruction kernel -24..-30% at M >= 4096; TTFT -0.8..-1.0%

The defaults ("even", no rounding) keep the existing output, and other GPUs keep the bit-arithmetic rounding, so their
compiled code is unchanged.

Part 1 (original #7116): honour scaling_mode, add "roundup" and round_to_input_dtype

Motivation

act_mul_and_mxfp4_quant accepts scaling_mode but always launches the kernel with SCALING_MODE=0. The kernel also does not pass the mode on to _mxfp4_quant_op. As a result, "ceil" silently produces the "even" scale.

Frameworks that fuse SiLU-and-mul with the MXFP4 quant of the next GEMM's input need the fused op to produce the same bytes as the unfused chain, or the fusion changes accuracy. One example is the MXFP4 MLP of Qwen3.x in SGLang: silu_and_mul -> per_1x32_f4_quant_hip -> down_proj.

Modifications

  • scaling_mode now maps "even" -> 0 (default), "ceil" -> 1 and "roundup" -> 2, and is passed through to _mxfp4_quant_op. Any other value raises ValueError.

  • "roundup" is the scale per_1x32_f4_quant_hip computes: the block amax floored at 1e-10 (aiter::kAmaxFloor), then fp_f32_to_e8m0_scale<MxScaleRoundMode::RoundUp>, i.e. 2^ceil(log2(amax / 6)). Both ceil modes use _mxfp4_ceil_scale_from_amax, which now takes the floor as AMAX_FLOOR (after the MAX_SCALE_E8M0 cap from [Triton/Gluon] [QSA] FP8 KV and FP8 / FP4 index caches, stacked-head decode scorer #7008); "roundup" passes 1e-10. The "ceil" bytes do not change, with or without the cap.

    "roundup" equals "ceil" except for the floor: an all-zero (or sub-1e-10) block gets scale byte 92, as from the HIP quant, where "ceil" gives 1.

  • New round_to_input_dtype: bool = False. It rounds act(x1) to x.dtype, then rounds act(x1) * x2 to x.dtype again, before quantizing. That is what an unfused act-and-mul writing x.dtype produces. The name and default follow round_to_input_dtype of fused_reduce_act_mul_and_mxfp4_quant in [Triton/Gluon] [Perf] fused_reduce_act_mul_and_mxfp4_quant: wider launch tile, add round_to_input_dtype and transpose_scale #6024, which rounds only the product.

  • The defaults ("even", no rounding) leave the existing output unchanged.

The RoundUp E8M0 scale is based on an AITER patch by the AMD ArC team; thanks to them. This PR adds the 1e-10 floor so the result matches the HIP quant.

Not in this PR: fused_reduce_act_mul_and_mxfp4_quant has the same hard-coded mode. #6024 changes that function, so adding "roundup" there once #6024 lands is a one-line follow-up.

Accuracy

  • With scaling_mode="roundup" and round_to_input_dtype=True, the output is byte-identical to a bf16 SiLU-and-mul (exp2 SiLU form, as in the Triton kernels) followed by per_1x32_f4_quant_hip. This covers the fp4 values and the scale bytes of the valid rows, with plain and shuffled scales.
  • The fused kernel's FP4 bytes and E8M0 scales are byte-identical to SGLang's unfused silu_and_mul + per_1x32_f4_quant_hip in all 60 cases tested: M from 1 to 16,384 rows; N 8,704 and 17,408; unit, large, tiny and all-zero inputs.
  • The defaults ("even", no rounding) take the same path as before; the existing tests in test_activation.py pass (see Tests).
  • End to end: Qwen3.8-27B MXFP4 on MI355X, SGLang TP2, with the fused SiLU-and-mul + MXFP4 producer of [Perf] [AMD] qwen3_5: Fuse add+RMSNorm and the attention output gate with the MXFP4 quant of ASM FP4 GEMM inputs sgl-project/sglang#47154 using scaling_mode="roundup" and round_to_input_dtype=True: GSM8K (5-shot, 200 questions) 0.935 and 0.950, all 1,319 questions 0.923, with no empty answers; needle in a haystack 30/30 at 100k-200k tokens. With the earlier, inexact producer (fp32 SiLU product, default scales), GSM8K-200 was 0.880-0.900, with 7-12 empty answers per 200.

Performance

What the option enables, measured through sgl-project/sglang#47154 (with vs without that producer): TP1 time to first token at 100k / 131k / 200k tokens: -1.8% / -1.9% / -1.7%. Median time per output token: -3.1% / -2.1% / -1.1% at 16 / 64 / 256 concurrent requests.

Test configuration: MI355X (gfx950), Qwen3.8-27B MXFP4 (amd/Qwen3.8-27B-Quark-AWQ-MXFP4), SGLang TP1 with --attention-backend aiter, FP8 KV cache, --enable-amd-monokernel and chunked prefill 16384 (TP2 with INT4 quick reduce). "With vs without" = the full optimized stack vs the same build without that one PR, on the same GPU, averaged over 2 GPUs. Decode: bench_serving random, 8,192 input / 1,024 output tokens. Prefill: 1 request at a time, 1 output token. Repeat noise of the full stack on one GPU: TTFT 0.1-0.8%; TPOT 0.1-0.8%, except 2.4% at 16 concurrent requests on one GPU.

Tests

op_tests/triton_tests/fusions/test_activation.py:

  • Byte equality of fp4 values and valid-row scales against a bf16 SiLU-and-mul followed by per_1x32_f4_quant_hip:
    • M in {1, 2, 8, 9, 32, 33, 255, 256, 257, 2048} x N in {256, 8704, 17408};
    • two magnitudes, plain and shuffled scales;
    • an all-zero block and a tiny block.
  • The scale byte each mode gives on hand-picked blocks.
  • The effect of round_to_input_dtype on a bf16 rounding tie.
  • ValueError for an unknown scaling_mode.

op_tests/triton_tests/quant/test_quant_mxfp4.py:

  • "roundup" (mode 2) added to test_mxfp4_quant_op_asm_matches_bits, checked against 2^ceil(log2(max(amax, 1e-10) / 6)).

Results on MI355X (gfx950): test_activation.py 519 passed, 228 skipped; test_quant_mxfp4.py -k asm_matches_bits 3 passed.

Part 2 (was #7279): round to E2M1 with the gfx950 instruction

Summary

_act_mul_and_dynamic_mxfp4_quant_kernel now rounds to E2M1 with v_cvt_scalef32_pk_fp4_f32 on gfx950, as
_mxfp4_quant_op(USE_ASM=True) already does for the RoPE and KV-cache MXFP4 kernels. The output bytes are unchanged;
the kernel is 24-30% faster at prefill sizes on MI355X.

Motivation

The fused activation-and-mul + MXFP4 quantization of the down-projection input rounds to E2M1 with the integer bit
arithmetic of _mxfp4_pack_bits. On MI355X this keeps the kernel ALU-bound, well below HBM bandwidth: for the
Qwen3.8-27B dense MLP (gate_up [16384, 2 x 17408] bf16, 1.29 GB moved per call) it takes 325 us, 4.0 TB/s. In a
long-prompt prefill that is 20 ms of every 16384-token chunk (64 layers), about 5% of the GPU time of a 131k-token prompt.

_mxfp4_quant_op already has USE_ASM: on gfx950 it rounds with v_cvt_scalef32_pk_fp4_f32, one instruction per pair
of values, and gives the same byte as the bit arithmetic for every non-NaN input (test_mxfp4_quant_op_asm_matches_bits).

Changes

  • _act_mul_and_dynamic_mxfp4_quant_kernel passes USE_ASM=True to _mxfp4_quant_op.
  • Other GPUs keep the bit arithmetic: _mxfp4_pack_op only takes the instruction path when is_hip_cdna4().
  • No API change; FP4 bytes and E8M0 scales are identical on every GPU.

Performance

Kernel time from 10 back-to-back calls (so the Python wrapper's CPU time is hidden), median of 15; silu, "roundup"
scales, shuffled scales, bf16 input [M, 2 x N]:

Hardware Workload/configuration Before After Change
MI355X M 16384, N 17408 325 us (4.0 TB/s) 248 us (5.2 TB/s) -24%
MI355X M 8192, N 17408 173 us 127 us -27%
MI355X M 4096, N 17408 92 us 64 us -30%
MI355X M 16384, N 8704 175 us 125 us -29%
MI355X M 8192, N 8704 91 us 65 us -29%
MI355X M <= 1024 ~25 us ~25 us launch-bound

Single calls including the wrapper: 395 -> 272 us at M 16384 (the number in the commit message). Compiled kernel (M 16384
x N 17408 launch): 1228 -> 857 VALU instructions, 94 -> 76 VGPRs, 89 -> 47 SGPRs, no spills.

End to end: Qwen3.8-27B MXFP4 (amd/Qwen3.8-27B-Quark-AWQ-MXFP4) on one MI355X, TP1, SGLang (this kernel is its MLP
activation + down_proj input quant), chunked prefill 16384, FP8 KV cache; python3 -m sglang.bench_serving --backend sglang-oai-chat --dataset-name random --random-range-ratio 1.0 --random-output-len 1 --max-concurrency 1 --request-rate inf --num-prompts 32 --warmup-requests 1 --flush-cache; same GPU, before / after / after / before, mean of the two median
TTFTs per side:

input tokens median TTFT before (ms) after (ms) change input tok/s change
100000 2731 2703 -28 ms (-1.0%) +1.1%
120000 3428 3400 -27 ms (-0.8%) +1.0%
140000 4182 4140 -42 ms (-1.0%) +1.0%
160000 5023 4982 -40 ms (-0.8%) +0.8%
180000 5854 5803 -51 ms (-0.9%) +0.8%
200000 6827 6774 -52 ms (-0.8%) +0.8%

The kernel saving is ~4.9 ms per 16384-token chunk (64 layers), so -30 ms at 100k and -60 ms at 200k expected.

Validation

  • Bit-exact: FP4 bytes and E8M0 scales equal to the previous kernel's (torch.equal) for M = 1 ... 16384 rows, N 17408, on
    normal, wide-range (magnitudes 1e-30 ... 1e4: saturating and subnormal values) and all-zero inputs.
  • MI355X (gfx950), ROCm 7.2: op_tests/triton_tests/fusions/test_activation.py 519 passed, 228 skipped (shuffled scales
    with N not a multiple of 512, by design); test_fused_silu_mul.py 23 passed; op_tests/triton_tests/quant/test_quant_mxfp4.py
    77 passed, 48 skipped, including test_mxfp4_quant_op_asm_matches_bits. The three
    test_dynamic_mxfp4_quant_gluon_matches_triton cases fail the same way with and without this change: the test image's
    Triton has no gl.amd.cdna4.scaled_downcast.
  • End to end (Qwen3.8-27B MXFP4, TP1, SGLang): greedy output ids identical with and without the change on four reference
    prompts (100k-200k tokens and a short one); needle in a haystack 30/30 at 100k-200k tokens; GSM8K-200 0.950 batched and
    0.960 one question at a time (0.935 / 0.955 without the change; run-to-run spread about 2 points).
  • Not run: MI250X / MI300X. There the kernel keeps the bit-arithmetic path (is_hip_cdna4() is false), so the compiled
    code is unchanged.

Motivation

The existing kernel is ALU-bound (325 us, 4.0 TB/s at gate_up [16384, 2 x 17408]), and frameworks need bit-exact fusion with the unfused path.

Changes

scaling_mode honoured plus "roundup" and round_to_input_dtype; USE_ASM=True on gfx950 only (other GPUs keep the bit arithmetic). Defaults keep the old output.

Performance (if applicable)

Per-commit table and the setup of each part are in the details above.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

MI355X: test_activation.py 519 passed, test_fused_silu_mul.py 23 passed, test_quant_mxfp4.py 77 passed; byte equality against the unfused SGLang path in 60 cases.

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Kernel option and tests only.

Dependencies

  • No new third-party dependencies added

None.

Breaking Changes

None; defaults keep the existing output.

PR Self-Review

Self-review (revision, scope, validation, op tests)

Reviewed revision

88e048e

Behavior and intent

  • act_mul_and_mxfp4_quant accepted scaling_mode but always launched the kernel with SCALING_MODE=0, so "ceil" silently produced the "even" scale. It now honours "even" / "ceil" / "roundup" and raises ValueError for anything else.
  • New "roundup" mode and round_to_input_dtype let the fused op produce the same bytes as an unfused act-and-mul followed by per_1x32_f4_quant_hip. Frameworks that fuse SiLU-and-mul into the next GEMM's MXFP4 input quant (SGLang Qwen3.x, [Perf] [AMD] qwen3_5: Qwen3.8 GDN and MXFP4 fusions on gfx950 (GDN prefill prologue and output epilogue, packed in_proj, bit-exact norm / SiLU quant fusion) sgl-project/sglang#39912 Part 4) need that, or accuracy moves (GSM8K 0.880-0.900 with the inexact producer, 0.935-0.950 with this PR).
  • The gfx950 kernel now rounds to E2M1 with v_cvt_scalef32_pk_fp4_f32 (_mxfp4_quant_op(USE_ASM=True)), the path the RoPE and KV-cache MXFP4 kernels already use. Output bytes are unchanged; the kernel is 24-30% faster at prefill sizes.
  • Defaults ("even", no rounding) keep the existing output; other GPUs keep the bit-arithmetic rounding.

Validation and unverified scope

Validated on MI355X (gfx950), ROCm 7.2, at a93eb332 (before the rebase onto main). The rebase merged the scale helper with #7008's MAX_SCALE_E8M0 cap; a host-side check over 4M fp32 ratios gives the same E8M0 bytes as main's "ceil" with cap 254 and 240. The GPU tests at 88e048e2d9 run in CI.

  • pytest op_tests/triton_tests/fusions/test_activation.py: 519 passed, 228 skipped (shuffled scales with N not a multiple of 512, by design).
  • pytest op_tests/triton_tests/fusions/test_fused_silu_mul.py: 23 passed.
  • pytest op_tests/triton_tests/quant/test_quant_mxfp4.py: 77 passed, 48 skipped.
  • Byte equality of fp4 values and E8M0 scales against SGLang's unfused silu_and_mul + per_1x32_f4_quant_hip in 60 cases (M 1-16384, N 8704 and 17408, unit, large, tiny and all-zero inputs).
  • Before/after of the instruction path: bit-identical bytes for M 1-16384, N 17408, on normal, wide-range and all-zero inputs.
  • End to end, Qwen3.8-27B MXFP4 TP1/TP2 on MI355X: GSM8K, needle-in-a-haystack 30/30 at 100k-200k tokens, greedy output ids identical with and without the E2M1 change.

Unverified:

PR-specific question

Why fold the E2M1 rounding commit (formerly #7279) into this PR? Both change the same call site of _mxfp4_quant_op in _act_mul_and_dynamic_mxfp4_quant_kernel, and the instruction-path claim "same bytes" is only meaningful against the "roundup" scale this PR adds. The commit is cherry-picked unchanged (88e048e2d9 after the rebase), so the code is the same as reviewed there.

Changed op tests

  • op_tests/triton_tests/fusions/test_activation.py: new tests for what no existing test covers. test_act_mul_and_mxfp4_quant_roundup_matches_hip_quant asserts byte equality with a bf16 SiLU-and-mul + per_1x32_f4_quant_hip (fails before: "roundup" and round_to_input_dtype do not exist). test_act_mul_and_mxfp4_quant_scaling_modes pins the scale byte per mode on hand-picked blocks (fails before: "ceil" returned the "even" scale). test_act_mul_and_mxfp4_quant_round_to_input_dtype checks the bf16 rounding tie. test_act_mul_and_mxfp4_quant_rejects_unknown_scaling_mode checks the ValueError. Existing tests there only cover the default mode.
  • op_tests/triton_tests/quant/test_quant_mxfp4.py: test_mxfp4_quant_op_asm_matches_bits gains scaling_mode=2 with a reference using the 1e-10 floor. It is the canonical test that the instruction path equals the bit arithmetic; without the new case the "roundup" scale path would be unchecked there.
  • op_tests/triton_tests/fusions/test_fused_silu_mul.py: no behaviour change; silu_exp2_ref moved to the shared helper (below) instead of test_activation.py importing it from this test module (Copilot review).
  • op_tests/triton_tests/utils/activation_ref.py: new shared home of silu_exp2_ref, used by test_activation.py and test_fused_silu_mul.py. It is a helper, not a test.

@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:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 7116 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@aiter-bot

aiter-bot commented Oct 30, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision d3d2feff12e0 · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

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.

🟡 Changes recommended

Unresolved findings flag missing explicit dtype and CUDA device settings in tests, a shared helper housed in a test module, and duplicated scale-calculation logic.

4 open issues
What changed in this PR

This PR fixes scaling-mode handling in AITER’s fused activation-and-MXFP4 quantization path. It adds options to match the rounding and scales of an unfused activation-and-multiply followed by HIP quantization, while preserving defaults.

Changes:

  • Pass the selected scaling mode through to quantization and reject unknown modes.
  • Add "roundup" scaling with a 1e-10 floor and optional input-dtype rounding.
  • Add tests for HIP byte equality, scaling modes, rounding, and invalid input.
File Description
op_tests/​triton_tests/​quant/​test_quant_mxfp4.py Extends scale and packing checks to roundup mode.
op_tests/​triton_tests/​fusions/​test_activation.py Adds HIP parity, scaling-mode, rounding, and validation tests.
aiter/​ops/​triton/​fusions/​activation.py Exposes rounding and validates and forwards scaling modes.
aiter/​ops/​triton/​_triton_kernels/​quant/​quant.py Implements roundup scales and mode selection.
aiter/​ops/​triton/​_triton_kernels/​fusions/​activation.py Applies optional dtype rounding and forwards the quantization mode.

Review effort: Balanced

@zufayu
zufayu requested review from a team and azaidy November 2, 2026 01:24
@vorapolsiloai vorapolsiloai changed the title [Triton/Gluon] act_mul_and_mxfp4_quant: honour scaling_mode, add "roundup" and round_to_input_dtype [Triton/Gluon] act_mul_and_mxfp4_quant: honour scaling_mode, add "roundup" and round_to_input_dtype; round to E2M1 with the gfx950 instruction Nov 2, 2026
@azaidy
azaidy requested a review from ahmed-bsod November 4, 2026 09:10
@azaidy azaidy assigned ahmed-bsod and unassigned azaidy Nov 4, 2026

@ahmed-bsod ahmed-bsod 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.

@vorapolsiloai Thanks for the PR.

  1. Please rebase on main. #7008 added a MAX_SCALE_E8M0 cap to _mxfp4_ceil_scale_from_amax and _mxfp4_quant_op, so quant.py now conflicts with this PR. Main passes MAX_SCALE_E8M0 as the second positional argument (QSA uses 240), so please put AMAX_FLOOR after it in the signature, or change that call to keywords.
  2. When you resolve the conflict, please keep the docstring of _mxfp4_ceil_scale_from_amax to one or two sentences (rule 18). The HIP details can go in the PR description.

Rest LGTM.

…ndup" and round_to_input_dtype

act_mul_and_mxfp4_quant took scaling_mode but always launched the kernel
with SCALING_MODE=0, and the kernel did not pass SCALING_MODE on to
_mxfp4_quant_op, so every mode gave the "even" scale. Map "even" -> 0 (the
default), "ceil" -> 1 and the new "roundup" -> 2, and raise ValueError for
anything else.

"roundup" is the scale per_1x32_f4_quant_hip computes: the block amax
floored at 1e-10 (aiter::kAmaxFloor), then
fp_f32_to_e8m0_scale<MxScaleRoundMode::RoundUp>, the exponent of
amax * (1/6) plus one if its mantissa is nonzero. It is "ceil" with that
floor in place of 6 * 2^-126: every block whose amax is below 1e-10, an
all-zero block included, gets scale byte 92 as from the HIP quant, where
"ceil" gives an all-zero block 1.

round_to_input_dtype=True rounds act(x1) to x.dtype, and act(x1) * x2 to
x.dtype again, before quantizing: the values an unfused act-and-mul in
x.dtype writes (F.silu(a) * b on bf16 tensors). With "roundup" the output
is then that act-and-mul followed by per_1x32_f4_quant_hip, byte for byte.
The name and default follow round_to_input_dtype of
fused_reduce_act_mul_and_mxfp4_quant in ROCm#6024, which rounds only the
product.

Both ceil modes share _mxfp4_ceil_scale_from_amax, which takes the floor
as AMAX_FLOOR after the MAX_SCALE_E8M0 cap; "roundup" passes 1e-10. The
"ceil" bytes do not change. silu_exp2_ref moves to
op_tests/triton_tests/utils/activation_ref.py, shared by both tests.

The defaults ("even", no rounding) keep the existing output.

Tests: fp4 bytes and the scale bytes the valid rows own against a bf16
silu-and-mul followed by per_1x32_f4_quant_hip, for M in {1, 2, 8, 9, 32,
33, 255, 256, 257, 2048}, N in {256, 8704, 17408}, two magnitudes, plain
and shuffled scales, with an all-zero and a tiny block; the scale byte of
each mode; the rounding; the ValueError; and "roundup" in the
_mxfp4_quant_op ASM-vs-bits test.

Signed-off-by: Vorapol Assavasangthong <[email protected]>
… instruction

The fused activation-and-mul MXFP4 kernel rounds to E2M1 with the bit
arithmetic of _mxfp4_pack_bits. Pass USE_ASM=True so that on gfx950 it uses
v_cvt_scalef32_pk_fp4_f32 (one instruction per pair of values), which gives
the same byte for every non-NaN input (test_mxfp4_quant_op_asm_matches_bits);
other GPUs keep the bit arithmetic.

MI355X, silu, "roundup" scales, shuffled scales, gate_up [16384, 2 x 17408]
bf16: 395 -> 272 us per call, fp4 bytes and scales identical.

(cherry picked from commit 0c96ad3)
Folded in from ROCm#7279 (0c96ad3)
@vorapolsiloai
vorapolsiloai force-pushed the fix/act-mul-mxfp4-quant-scaling-mode branch from a93eb33 to 88e048e Compare November 17, 2026 07:23
@vorapolsiloai

Copy link
Copy Markdown
Contributor Author

Thanks @ahmed-bsod. Both done in 88e048e:

  1. Rebased on main. _mxfp4_ceil_scale_from_amax(amax, MAX_SCALE_E8M0=254, AMAX_FLOOR=6 * 2**-126): AMAX_FLOOR comes after the cap, so the QSA call is unchanged. "roundup" passes AMAX_FLOOR=1e-10 and also honours the cap. The "ceil" bytes are the same as on main with cap 254 and 240.
  2. The docstring is now two lines. The HIP details are in the PR description.

@ahmed-bsod ahmed-bsod 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.

@vorapolsiloai ty LGTM

@azaidy azaidy 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.

LGTM!

@azaidy
azaidy merged commit f3da4e7 into ROCm:main Nov 24, 2026
72 checks passed
xiaohong42 added a commit to xiaohong42/aiter that referenced this pull request Dec 1, 2026
…nd "roundup"

dynamic_mxfp4_quant took scaling_mode but launched the Triton kernel with
SCALING_MODE=0, and the kernel did not pass SCALING_MODE on to
_mxfp4_quant_op, so every mode gave the "even" scale and any string was
accepted. Map "even" -> 0 (the default), "ceil" -> 1 and "roundup" -> 2, the
modes act_mul_and_mxfp4_quant takes since ROCm#7116, and raise ValueError for
anything else. The mapping is now defined once, in quant.py, and
activation.py imports it.

The Gluon kernels (gfx950 with bf16 input, gfx1250) compute the "even" scale
only. With backend=None, "ceil" and "roundup" run the Triton kernel on every
arch; backend="gluon" raises RuntimeError for them. use_sr=True still needs
"even", and the kernel now asserts it.

The defaults ("even", backend=None) launch the same kernels as before.

Tests: test_dynamic_mxfp4_quant runs each of its shapes in all three modes
against torch_dynamic_mxfp4_quant, which now computes the "ceil" and
"roundup" scales; a one-row test checks the scale and fp4 bytes of an
all-zero block, a block below the 1e-10 floor, the E2M1 ties and a scale that
rounds up; and the ValueError and the Gluon RuntimeError.

Signed-off-by: xiaohong42 <[email protected]>
xiaohong42 added a commit to xiaohong42/aiter that referenced this pull request Dec 2, 2026
…nd "roundup"

dynamic_mxfp4_quant took scaling_mode but launched the Triton kernel with
SCALING_MODE=0, and the kernel did not pass SCALING_MODE on to
_mxfp4_quant_op, so every mode gave the "even" scale and any string was
accepted. Map "even" -> 0 (the default), "ceil" -> 1 and "roundup" -> 2, the
modes act_mul_and_mxfp4_quant takes since ROCm#7116, and raise ValueError for
anything else. The mapping is now defined once, in quant.py, and
activation.py imports it.

The Gluon kernels (gfx950 with bf16 input, gfx1250) compute the "even" scale
only. With backend=None, "ceil" and "roundup" run the Triton kernel on every
arch; backend="gluon" raises RuntimeError for them. use_sr=True still needs
"even", and the kernel now asserts it.

The defaults ("even", backend=None) launch the same kernels as before.

Tests: test_dynamic_mxfp4_quant runs each of its shapes in all three modes
against torch_dynamic_mxfp4_quant, which now computes the "ceil" and
"roundup" scales; a one-row test checks the scale and fp4 bytes of an
all-zero block, a block below the 1e-10 floor, the E2M1 ties and a scale that
rounds up; and the ValueError and the Gluon RuntimeError.

Signed-off-by: xiaohong42 <[email protected]>
xiaohong42 added a commit to xiaohong42/aiter that referenced this pull request Dec 3, 2026
…nd "roundup"

dynamic_mxfp4_quant took scaling_mode but launched the Triton kernel with
SCALING_MODE=0, and the kernel did not pass SCALING_MODE on to
_mxfp4_quant_op, so every mode gave the "even" scale and any string was
accepted. Map "even" -> 0 (the default), "ceil" -> 1 and "roundup" -> 2, the
modes act_mul_and_mxfp4_quant takes since ROCm#7116, and raise ValueError for
anything else. The mapping is now defined once, in quant.py, and
activation.py imports it.

The Gluon kernels (gfx950 with bf16 input, gfx1250) compute the "even" scale
only. With backend=None, "ceil" and "roundup" run the Triton kernel on every
arch; backend="gluon" raises RuntimeError for them. use_sr=True still needs
"even", and the kernel now asserts it.

The defaults ("even", backend=None) launch the same kernels as before.

Tests: test_dynamic_mxfp4_quant runs each of its shapes in all three modes
against torch_dynamic_mxfp4_quant, which now computes the "ceil" and
"roundup" scales; a one-row test checks the scale and fp4 bytes of an
all-zero block, a block below the 1e-10 floor, the E2M1 ties and a scale that
rounds up; and the ValueError and the Gluon RuntimeError.

Signed-off-by: xiaohong42 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants