Skip to content

[Perf] [AMD] quantization: Fuse SiLU-and-mul with the dynamic per-tensor fp8 quant of dense MLPs on gfx95 - #48536

Merged
BBuf merged 5 commits into
sgl-project:mainfrom
siliangchen-amd:rocm-silu-mul-fp8-quant
Nov 9, 2026
Merged

BBuf merged 5 commits into
sgl-project:mainfrom
siliangchen-amd:rocm-silu-mul-fp8-quant

Conversation

@siliangchen-amd

@siliangchen-amd siliangchen-amd commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

On ROCm, a dense MLP whose down projection has per-tensor FP8 weights and dynamic activation scales, such as an online --quantization fp8 Qwen3-8B or Llama-3.1-8B, runs SiluAndMul and then aiter's dynamic_per_tensor_quant on every decode step, as separate launches around a small decode GEMM. DenseSiluAndMulQuant already fuses the activation and its quant for the block-FP8 DeepGEMM path on SM100; this adds the per-tensor case on gfx95.

Modifications

  • python/sglang/kernels/ops/quantization/aiter_fp8_quant_triton.py: silu_and_mul_fp8_per_tensor, a two-launch Triton kernel (each tile's absmax, then the reduction and the quant) that writes the bytes and the scale SiluAndMul followed by dynamic_per_tensor_quant writes: the SiLU and the product rounded to bf16, amax * (1 / max) as the scale, and the activation times the scale's approximate v_rcp_f32. For an all-zero tensor it writes 0 where aiter writes -448 at scale 0; both dequantize to 0.
  • python/sglang/srt/layers/quantization/fp8_activation.py: on gfx95 with aiter and TP 1, for a RowParallelLinear down projection under Fp8LinearMethod with dynamic activations, per-tensor weights and no static input scale, DenseSiluAndMulQuant.forward_decode returns (q, scale, dtype) for decode batches of up to 128 tokens, which Fp8LinearMethod.apply already takes. Everything else keeps SiluAndMul.
  • Tests, both in the MI35x suite:
    • test/registered/kernels/ops/quantization/test_aiter_fp8_quant_triton.py: the kernel's bytes and scale against silu_and_mul followed by dynamic_per_tensor_quant, and an all-zero input.
    • test/registered/unit/layers/quantization/test_dense_silu_fp8_per_tensor_gfx95.py (new): real Llama and Qwen2 MLPs, fused against unfused bit for bit, under CUDA graph replay too; batches past 128 tokens and non-decode modes keep SiluAndMul.

Accuracy Tests

GSM8K, all 1319 questions (python -m sglang.test.few_shot_gsm8k --num-questions 1319 --parallel 128), one MI355X (gfx950), TP 1, SGLANG_USE_AITER=1, --quantization fp8:

Model main this PR
Qwen/Qwen3-8B 0.893 0.898

Invalid answers are 0.000 on both.

Speed Tests and Profiling

python -m sglang.benchmark.one_batch --quantization fp8 --batch-size 1 16 64 --input-len 1024 --output-len 64, same setup, median decode step:

Model Batch main this PR
Qwen/Qwen3-8B 1 4.08 ms 3.84 ms (-5.9%)
Qwen/Qwen3-8B 16 4.46 ms 4.25 ms (-4.7%)
Qwen/Qwen3-8B 64 6.44 ms 6.23 ms (-3.3%)
meta-llama/Llama-3.1-8B-Instruct 1 3.85 ms 3.63 ms (-5.7%)
meta-llama/Llama-3.1-8B-Instruct 16 4.44 ms 4.15 ms (-6.5%)
meta-llama/Llama-3.1-8B-Instruct 64 6.03 ms 5.81 ms (-3.6%)

Setup: the lmsysorg/sglang-rocm:v0.5.23-rocm720-mi35x-20261103 image, with sglang from source at 1d062d2 and blake3 installed. main is the same tree with fp8_activation.py at 1d062d2, which never calls the new kernel.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ✅ Run #42438315678
Latest PR Test (Extra): ❌ Run #42438315661
Latest PR Test (AMD ROCm 10): ✅ Run #42438315623

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

…e MLPs on gfx95

A dense MLP whose down projection has per-tensor fp8 weights and dynamic activation scales, such as an
online --quantization fp8 model, runs SiluAndMul and then aiter's dynamic per-tensor quant on every decode
step. On gfx95, with TP 1 and up to 128 decode tokens, DenseSiluAndMulQuant now hands that down projection
its quantized activation from a two-launch Triton kernel instead. The kernel writes the bytes and the scale
aiter writes: the SiLU and the product rounded to bf16 as SiluAndMul rounds them, amax * (1 / max) as the
scale, and the activation times the scale's approximate v_rcp_f32.

test_aiter_fp8_quant_triton checks the kernel byte for byte against silu_and_mul and
dynamic_per_tensor_quant; test_dense_silu_fp8_per_tensor_gfx95 checks real Llama and Qwen2 MLPs against their
unfused output, under CUDA graph replay too.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added quant LLM Quantization jit-kernel labels Nov 5, 2026
@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Nov 5, 2026
@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

@BBuf could you take a look? On gfx95 this fuses SiluAndMul with the dynamic per-tensor fp8 quant of dense MLP down projections in decode, writing the same bytes and scale as aiter's dynamic_per_tensor_quant: decode steps get 3-6% faster on Qwen3-8B and Llama-3.1-8B with --quantization fp8, and GSM8K holds. Thanks!

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[BBuf Review Agent] LGTM. Please wait for the related AMD CI.

@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

@BBuf the AMD MI35x stage-b 1-GPU small stage is green on this head (634abeed3). Both of this PR's tests ran there on gfx950 and passed: test/registered/kernels/ops/quantization/test_aiter_fp8_quant_triton.py (14 passed) and test/registered/unit/layers/quantization/test_dense_silu_fp8_per_tensor_gfx95.py (3 passed). Ready whenever you are.

@BBuf

BBuf commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@BBuf

BBuf commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@BBuf

BBuf commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] @siliangchen-amd these still fail after a rerun, and I can't tell from the logs whether it's this PR. Could you take a look?

  • base-b-test-1-gpu-small (5): Artifact cuda-coredumps-run-5 has been successfully uploaded! Final size is 312608 bytes. Artifact ID is 13459018510" The tail contains artifact upload and cleanup, but no failing test, exception, or

@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

@BBuf none of these come from this PR. The NVIDIA jobs never reached a test that touches it, and the Arm64 one comes from this branch's base predating #37597:

  • base-b-test-1-gpu-small (5) in attempt 3: no test failed. The 16 files it finished all passed, then the Run test step hit its 30-minute limit (The action 'Run test' has timed out after 30 minutes). test_marlin_moe.py alone took 723 s against an estimate of 242 s, and none of the partition's 24 files use this change.
  • base-b-test-1-gpu-small (6) in that attempt started at 22:27Z, after MIN_BASE_SHA was set, and stopped at Rebase Required Before CI Can Run, so wait-for-base-b failed. In the 01:57Z rerun, wait-for-base-b stopped at the same gate (MIN_BASE_SHA=50322ac7ba85) after 13 s, and partitions 5 and 6 fail-fast on it without running a test.
  • build-test (PR Test (Arm64)) fails the same 57 cases of registered/cpu/test_norm.py::TestLayerNorm in all three attempts, with RuntimeError: Input tensor dtype mismatch from layernorm_cpu. The job builds sglang-kernel-cpu from the PR branch but runs the tests from the merge ref. [Feat] [CPU] glm_ocr: Add model support #37597 changed both kernels/aot/csrc/cpu/norm.cpp and test_norm.py (mixed parameter and input dtypes), and this branch's base predates it, so the new tests meet the old kernel. This PR touches neither file.
  • The three call-gate / pr-gate failures are Require opt-in tier label (optional), and the two *-extra-finish jobs fail on them. [Fix] [AMD] aiter: Quantize q with its own scale in the fp8 prefills over a gathered cache #48434 shows the same after its rerun.

The change only runs under _is_hip and is_gfx95_supported(), and its tests are registered for the AMD MI35x suite only. stage-b-test-1-gpu-small-amd-mi35x, which runs them, passed on this head. A rebase onto current main would clear both the gate and the Arm64 mismatch; I can do it if you'd like.

@BBuf

BBuf commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] @siliangchen-amd Agreed, the HIP/gfx95 guards and AMD-only test registration support treating this failure as unrelated. The local history also confirms the branch predates #37597; please go ahead with the rebase.

@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

@BBuf done: current main is merged into the branch (head 955a7686), so it now contains 50322ac7ba85 and #37597, and the rebase gate's compare reads ahead. The PR's own diff is unchanged: the same 4 files, +319/−4.

@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

@BBuf for context on the red MI35x small stage here: it fails only on test/registered/amd/test_minimax_hd128_asm_prefill.py::test_first_chunk_reads_fp8_pool, with AttributeError: 'types.SimpleNamespace' object has no attribute 'layer_router' (job). That test is main's, and this PR does not touch it: it fakes token_to_kv_pool with a SimpleNamespace that has only get_kv_buffer, while aiter_backend.py on main now reads token_to_kv_pool.layer_router (line 4897), and #48366 made the test run on that stage today. #48438 replaces the fake with a pool that has layer_router, so the stage goes green again once it lands.

@BBuf

BBuf commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] @siliangchen-amd Agreed, the MiniMax test and attention backend are unchanged from the merged main, and the fake pool lacks the required layer_router. Treating this MI35x stage failure as unrelated to this PR.

@BBuf

BBuf commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@peymanr

peymanr commented Nov 7, 2026

Copy link
Copy Markdown

PR Pundit review

(Testing PR Pundit Code Review agent. Not all comments may be relevant. This is a one-time automated pass: please address what is useful, no need to request a re-review. Please reply to each point, whether it applies or not: we harvest these replies, and both kinds are enormously helpful for tuning the agent. Reach out to @peymanr for suggestions.)

# Finding Where
1 🟠 ROCm decode fp8 activations drift by bf16 rounding under aiter SiLU: this PR's gate omits forward-method check python/sglang/srt/layers/quantization/fp8_activation.py:120
2 🟠 Second quant kernel does redundant O(n_tiles^2) tile-max loads and extra warmup compiles python/sglang/kernels/ops/quantization/aiter_fp8_quant_triton.py:240
3 🟠 Parity asserts may fail spuriously outside CI: test sets SGLANG_USE_AITER only during MLP construction test/registered/unit/layers/quantization/test_dense_silu_fp8_per_tensor_gfx95.py:34

1. 🟠 Should fix: ROCm decode fp8 activations drift by bf16 rounding under aiter SiLU: this PR's gate omits forward-method check

python/sglang/srt/layers/quantization/fp8_activation.py:120

_forward_decode_rocm gates only on type(act_fn) is SiluAndMul; unlike the CUDA branch, it does not check that the resolved forward method is forward_cuda. With the opt-in SGLANG_OPT_USE_AITER_SILU_MUL=1, SiluAndMul runs forward_aiter, which keeps the SiLU-times-up product in fp32 and rounds once to bf16. The fused Triton kernel instead reproduces sgl_kernel's double bf16 rounding, so decode activations can differ from the unfused/prefill path by bf16 rounding before the fp8 quant. Gating on the resolved forward method, as the CUDA branch does, would keep the fusion bit-exact with whatever activation the user selected.

Who is affected: gfx95 users of dense per-tensor FP8 models at tp=1 with SGLANG_USE_AITER=1 who also set SGLANG_OPT_USE_AITER_SILU_MUL=1.

Checked against the code by an independent pass.

Related past reviews: #37376 on python/sglang/srt/layers/moe/moe_runner/triton_utils/fused_moe.py

2. 🟠 Should fix: Second quant kernel does redundant O(n_tiles^2) tile-max loads and extra warmup compiles

python/sglang/kernels/ops/quantization/aiter_fp8_quant_triton.py:240

In _silu_and_mul_fp8_per_tensor_kernel every one of the n_tiles programs reloads all TILES_POW2 tile maxima, so the redundant loads grow as n_tiles^2. The PR does this deliberately and bounds it with SILU_AND_MUL_FP8_PER_TENSOR_MAX_ROWS = 128, so the worst case is about 1792 programs each re-reading the same ≤8 KB buffer (n=28672). That buffer should stay cache-resident, so DRAM traffic should not scale quadratically; the cost was not measured here. TILES_POW2 as a constexpr gives about 8 JIT variants per MLP width over rows 1..128, compiled once during warmup or capture and then cached. A two-stage reduction, or a single pid writing the scale, would remove the redundancy if profiling shows it matters.

Who is affected: gfx95 ROCm users with SGLANG_USE_AITER, tp=1, online/dynamic per-tensor FP8 dense MLPs, at decode batch sizes ≤128.

Checked against the code by an independent pass.

Related past reviews: #6369 on python/sglang/srt/layers/moe/fused_moe_triton/fused_moe.py

3. 🟠 Should fix: Parity asserts may fail spuriously outside CI: test sets SGLANG_USE_AITER only during MLP construction

test/registered/unit/layers/quantization/test_dense_silu_fp8_per_tensor_gfx95.py:34

The test enables SGLANG_USE_AITER only around MLP construction via envs.SGLANG_USE_AITER.override(True), so the fusion is turned on there. The unfused reference quantizer is chosen from _use_aiter flags read once at import (fp8_kernel.py:40, fp8.py:153). If the process lacks SGLANG_USE_AITER=1, the reference mlp(x) would not use aiter's dynamic_per_tensor_quant. It could fall back to _native_dynamic_per_tensor_quant_fp8, and the rtol=0, atol=0 parity asserts could then fail. AMD CI always exports the variable, so this only affects manual runs; setting the env before import, or skipping when it is unset, would make the test self-contained.

Who is affected: Developers running test_dense_silu_fp8_per_tensor_gfx95.py by hand on gfx95 without SGLANG_USE_AITER=1 in the environment; AMD CI is not affected.

Checked against the code by an independent pass.

Shared from a pilot of PR Pundit, an agentic code reviewer that builds and tests the change and reads the project's past reviews. Reviewed at 955a7686; findings checked and posted by @peymanr; points already raised on this PR are left out.

@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

Replies to the PR Pundit review above, checked against 955a7686a:

  1. Applies, under SGLANG_OPT_USE_AITER_SILU_MUL=1 only. On ROCm, _forward_decode_rocm checks type(act_fn) is SiluAndMul but not the resolved forward method, as the CUDA branch does (fp8_activation.py:160-162). With that flag, SiluAndMul.__init__ sets _forward_method = self.forward_aiter (activation.py:138-139). The unfused path then runs aiter's silu_and_mul, while the fused decode path reproduces forward_cuda's rounding: the SiLU and the product are each rounded to bf16 (aiter_fp8_quant_triton.py:211-212). Wherever the two kernels round differently, the activations before the FP8 quant differ. The default path is unaffected, and the parity test pins it bit for bit. The fix is one more fallback condition: use act_fn(x) unless the activation resolves to the sgl_kernel path.

  2. No change. The repeated reads are deliberate. Each program of the second launch reduces all the tile maxima itself (aiter_fp8_quant_triton.py:240-241). Reducing them once would take a third launch, or an atomic max into a zero-filled scalar, which is one more fill on every call. SILU_AND_MUL_FP8_PER_TENSOR_MAX_ROWS = 128 (:200) keeps the fusion to decode, as the comment at :199 says, so the buffer holds at most 128 x ceil(n / 2048) fp32 values. The cost is in the measured decode step in the description: -3.3% to -6.5% on Qwen3-8B and Llama-3.1-8B at batch 1 to 64. The TILES_POW2 variants compile during warmup and graph capture.

  3. Applies to runs outside the ROCm image. The image sets ENV SGLANG_USE_AITER=1 (docker/rocm.Dockerfile:1340), so in CI the reference quantizer, chosen at import, is aiter's. Run by hand without it, the reference can fall back and the atol=0 parity fails. test/registered/amd/test_smallm_fp8_proj_gfx950.py:34 shows the fix: os.environ.setdefault("SGLANG_USE_AITER", "1") before the sglang imports.

I'll send 1 and 3 as a small follow-up after this PR lands, so this approved head and its CI stay as they are. Say if you would rather have them in this PR.

@BBuf

BBuf commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] @siliangchen-amd The bounded reduction is fine. Could you include the forward-method guard and set AITER before the test's sglang imports in this PR, since both issues come from the new code here?

…re the test's imports

The ROCm gate now falls back unless SiluAndMul resolves to forward_cuda, as the
CUDA gate does: with SGLANG_OPT_USE_AITER_SILU_MUL=1 the activation is aiter's,
whose rounding the fused kernel does not reproduce. The test sets
SGLANG_USE_AITER before importing SGLang, since the unfused reference quantizer
is chosen at import, and covers the aiter activation.
@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

Both are in 2a468ba4e:

  • fp8_activation.py: the ROCm gate now has the CUDA gate's check, (act_fn._forward_method or act_fn._resolve_forward_method()) != act_fn.forward_cuda. SiluAndMul declares no backends, so on ROCm it resolves to forward_cuda through the HIP fallback and the fusion stays on. With SGLANG_OPT_USE_AITER_SILU_MUL=1 it is forward_aiter, and decode keeps it.
  • The test sets SGLANG_USE_AITER before the sglang imports, as test_compressed_tensors_fp8_moe_layout.py does. The new test_aiter_silu_and_mul_keeps_the_separate_activation checks that, with that flag, decode returns aiter's activation unchanged.

On MI355X (gfx950), image lmsysorg/sglang-rocm:v0.5.23-rocm720-mi35x-20261103:

  • With SGLANG_USE_AITER=1, this file and test_aiter_fp8_quant_triton.py: 18 passed, 15 subtests passed.
  • With SGLANG_USE_AITER unset, this file: 4 passed.
  • The new test with fp8_activation.py from 955a7686a: fails, since the fused (fp8, scale) tuple comes back instead of the activation.

@BBuf

BBuf commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] @siliangchen-amd Both requests are addressed in 2a468ba4e: the ROCm gate now requires forward_cuda, and the test sets AITER before the sglang imports. The added regression test checks that decode preserves the separate AITER activation.

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[BBuf Review Agent] LGTM.

@BBuf

BBuf commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] Could you rebase this onto the latest main? It conflicts with main now.

@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

Merged the latest main in e5ba60c5f. The only conflict was in fp8_activation.py, with #48043, which adds the gfx95 aiter block-FP8 path to DenseSiluAndMulQuant. Both paths are kept, and they apply to different down projections: this PR's to per-tensor FP8 (weight_block_size is None), #48043's to bpreshuffled block-FP8.

One change beyond the textual merge: git put #48043's if self.aiter: return self._forward_decode_aiter(...) first in forward_decode, which would skip the per-tensor path whenever AITER is on. That branch now runs only when the per-tensor path is off (if self.aiter and not self.enabled). test_mlp_parity_and_graph_replay asserts the fused tuple, so it covers this. The PR's diff against main is otherwise the same four files as before.

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[BBuf Review Agent] LGTM.

@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

@BBuf the merge of the latest main is pushed as e5ba60c5f (the conflict and how it was resolved are in my comment above), and CI is running.

@siliangchen-amd

Copy link
Copy Markdown
Contributor Author

@BBuf the required checks (lint and pr-test-finish) are green on e5ba60c5f. The only job still pending, stage-b-test-large-8-gpu-mi35x-disaggregation-amd, is not a required check and has been waiting for a runner since 02:38 UTC. Could you merge this when you have a moment? Thanks!

@BBuf
BBuf merged commit af72557 into sgl-project:main Nov 9, 2026
167 of 172 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel quant LLM Quantization run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants