Skip to content

perf(dsv41): shared expert MXFP8 input from the FFN seam + silu_mul_mxfp8 on the dense path (fewer launches, bit-identical) - #3940

Draft
yhl-amd wants to merge 3 commits into
ROCm:mainfrom
yhl-amd:hyi/v41-ffn-seam-x-mxfp8
Draft

yhl-amd wants to merge 3 commits into
ROCm:mainfrom
yhl-amd:hyi/v41-ffn-seam-x-mxfp8

Conversation

@yhl-amd

@yhl-amd yhl-amd commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #3894 (MegaMoE shared_in caller, needs ROCm/aiter#8491). This branch carries #3894's commit rebased on current main; please review only the top two commits (perf(dsv41): the FFN seam writes the shared expert's MXFP8 input ... and perf(dsv41): the dense shared expert's SiLU-mul and w2 input quant in one launch (silu_mul_mxfp8)). I will rebase onto main once #3894 lands.

Depends on ROCm/aiter#8871 for the second commit (new op aiter.ops.dsv41_seg_fuse.silu_mul_mxfp8). Without it (or off gfx950) ATOM detects the missing op and keeps the unfused launches, so this PR is safe to merge before the AITER PR reaches the CI image.

Update (11-28): added the second commit (S2, silu_mul_mxfp8) and the full end-to-end A/B of S1 + S2 (see Performance).

Update (11-27): the implementation is replaced. The first version only covered the fused shared_in path and handed the pair over through a one-entry slot beside moe_forward. This version passes the pair as arguments through the MoE (a moe_forward variant custom op) and covers every place the shared expert quantizes its input: the target layers' fused shared_in path, its dense fallback (rows the small-input kernel does not take, prefill), the target layers without shared_in, and the three DSpark draft layers.

Root cause

DeepSeek-V4.1's shared expert is a native MXFP8 module (E4M3, one E8M0 scale per 32 columns), computed on its own next to the routed experts. Its gate_up GEMM quantized its BF16 input itself (quantize_fp8(x)) on every layer of every step:

x is the FFN seam's normed output, and the seam's HIP Mega-MHC launch can write the same MXFP8 pair next to the BF16 rows (mega_mhc(out_dtype="mxfp8"), which the attention seam already uses for wqkv_a). The pair is byte for byte quantize_fp8 of those BF16 rows, so the shared expert can read it instead of launching its own quantize.

Change

  • deepseek_v41/model.py (Block): the FFN seam asks for the MXFP8 output (hc_ffn_fuse_quant) where the HIP seam writes row-major scales (fuse_quant_supported) and the MoE takes the pair (MoE.takes_mxfp8_input); ffn_forward hands (bf16, fp8, e8m0) to the MoE. Decided once after loading, like the attention seam's hc_fuse_quant.
  • deepseek_v4/__init__.py:
    • Expert.reads_mxfp8_input(): a native group32 FP8 gate_up (not the BF16 fallback); Expert.forward(..., x_fp8=, x_scale=) feeds the pair to gate_up instead of quantizing x. Nothing else in the chain changes.
    • MoE.takes_mxfp8_input(): such a shared expert on the layer's own stream (the dual-stream and comm-fused ops carry the BF16 rows only, so they keep today's path).
    • MoE.single_stream_moe_forward(x, x_fp8=None, x_scale=None): the dense shared expert gets the pair; on a shared_in layer the routed call gets it (routed_with_shared_mxfp8). router_logits(x) is factored out of routed_expert_forward (V4.1 already overrides it).
  • flydsl_mega_experts.py: moe_forward_shared_mxfp8 is moe_forward with the pair as two more arguments of a custom op (aiter::moe_forward_shared_mxfp8; the pair crosses the op boundary as uint8 views because Inductor cannot functionalize a mutating op carrying an E8M0 tensor). The layer holds the pair while the call runs; shared_in's fused kernel (SharedExpertInput.x_fp8 / x_scale) and its dense fallback both read it. A pair whose row count does not match is ignored and x is quantized as before.
  • deepseek_v41/moe.py: forward(hidden, hidden_fp8=None, hidden_scale=None); the shared_in call records its TBO stream like routed_expert_forward.

No new switch or environment variable: the pair is exactly what the GEMM would compute, so it is on wherever the conditions above hold (native MXFP8 shared expert, single-stream MoE, HIP Mega-MHC seam with row-major scales: gfx950, and gfx1250 through scale_row_major). Precision and format are unchanged; the shared expert is still computed in FP8 on its own (SA rules: 48 local experts / top-6, shared expert not fused into the routed experts, draft not merged).

Second commit: SiLU-mul + w2's input quant in one launch (silu_mul_mxfp8)

On the dense shared-expert path (the three draft layers every step, target-layer rows shared_in does not fuse, prefill) the chain still ran silu_and_mul (BF16 product) and then w2's own quantize_fp8 of it. ROCm/aiter#8871 adds silu_mul_mxfp8: silu_and_mul's kernel body copied statement for statement with the Mega-MHC group-32 MXFP8 epilogue, so the product is rounded to BF16 and quantized with the same math; w2 gets the same codes and E8M0 scales.

  • model_ops/activation.py: silu_mul_mxfp8_available() (gfx950 and an AITER that has the op) and the silu_mul_mxfp8_fuse custom op.
  • Expert.w2_takes_silu_mul_mxfp8(): a native group32 FP8 w2 (not the BF16 fallback) whose (padded) input width is a multiple of 32 and half the gate_up output. Expert.forward uses it for an ungated call; the Triton fused_clamp_act_mul path (ATOM_V4_USE_TRITON_FUSION) keeps precedence, and a gated call (weights) keeps silu_and_mul.
  • Together with the first commit, the dense shared expert is gate_up(seam pair) -> silu_mul_mxfp8 -> w2(pair): 3 launches instead of 5. The fused shared_in path already does act + quant inside the MegaMoE kernel, so S2 only touches the dense path.

The kernel comes from the DeepSeek-V4.1 DEP8 AgentX PoC (coderfeli/plugin_poc PR #129, commit 158b4b5, author Bin Ding); the commit carries Co-authored-by: Bin Ding.

Correctness

  • GPU, MI355X (gfx950), this branch on current main with AITER main (tests/models/deepseek_v41/test_ffn_seam_mxfp8.py):
    • the FFN seam's pair vs quantize_fp8 of the BF16-only seam's norm, with and without the owed post, rows 1, 5, 6, 48, 128, 186, 192, 512, 513, 2048, 4096 x 2 inputs (with large per-row dynamic range): FP8 codes and E8M0 scales identical byte for byte, and every BF16 output (residual / normed / pre / post / comb) identical to the BF16-only seam. An extended sweep over 33 row counts from 1 to 16,384: 132 cases, 0 mismatches.
    • the dense shared expert on real checkpoint weights (target layer 10, draft layer mtp.0), from the seam's pair vs from the BF16 rows (weights preshuffled as loaded, 32x32 scales, swiglu limit 10): output identical byte for byte. Extended sweep: layers 10 and 39, draft mtp.0 and mtp.2, 33 row counts from 1 to 16,384 each, 0 mismatches; flipping one FP8 code of the pair does change the output, so the comparison is sensitive.
  • S2, GPU, MI355X (gfx950), this branch with the AITER op: (tests/models/deepseek_v41/test_ffn_seam_mxfp8.py, 39 passed): silu_mul_mxfp8 vs silu_and_mul + quantize_fp8, widths 2304 / 320 / 576 / 32 (and 4096 in an extended sweep), clamp 10 and off, 1-16,384 rows, 3 seeds, with all-zero, clamped, 1e-30-scale and silu ~ 0 rows: 894 cases, 0 mismatches in codes or scales. Real checkpoint shared experts (layers 10 / 39, draft mtp.0 / mtp.2), 33 row counts each, swiglu limit 10 and 0: S2 alone from BF16 rows and S1 + S2 from the seam's pair both give the fully unfused chain's bytes (264 cases, 0 mismatches); flipping one FP8 code of the op's output changes w2's output. Also under torch.compile(fullgraph=True, dynamic=True) and in a captured HIP graph replayed on new inputs: identical bytes. CPU tests: the S1 list above 269 passed / 81 skipped with the same 7 pre-existing test_moe_streams.py failures; the failure set over tests/models/deepseek_v41 is identical with and without this commit.
  • Same change on our experimental stack (DSV4.1-Flash, MI355X, DEP8, AgentX c512, SA-compliant): an online check compared every fused output with the unfused launches (torch.equal, first calls per row count and site, eager only): 49,941 checks over 2,293 row counts (1-16,384, decode and prefill), 0 mismatches (seam pairs vs quantize_fp8 of the seam's norm 25,294, shared_in input 12,233, the follow-up's act pair 12,414). Server logs: every fused shared_in call received the seam's pair (0 fell back to a separate quantize); draft layers never took shared_in.
  • GSM8K (same experimental stack, DEP8): 0.9318 for the shared_in-only first version; 0.9265 for stack v3 below (threshold 0.9157).
  • CPU unit tests (GPU hidden from torch): the new file plus tests/test_mega_shared_in.py, tests/test_mega_decode_capacity.py, tests/model_ops/test_comm_fused_moe.py, tests/model_ops/test_mhc_pre_delayed_backend.py, tests/models/deepseek_v41/{test_compilation,test_dspark,test_math}.py, tests/attentions/deepseek_v41/test_tbo.py: 261 passed, 71 skipped. (tests/models/deepseek_v41/test_moe_streams.py fails 7 tests on main as well: its V4 MoE stand-in does not take enable_eplb; unrelated to this change.)

Performance

All end-to-end numbers: DeepSeek-V4.1-Flash, MI355X, DEP8, AgentX, SA-compliant configuration (48 experts per rank / top-6, shared expert in FP8 on its own, draft not merged, draft lm_head BF16), dynamic EPLB, golden AL 3.51 for perf runs, server restarted every run. They come from our experimental stack (an earlier base with #3894 and aiter#8491 ported), not from this exact branch.

  • Full A/B of S1 + S2 (11-28), our experimental stack, same node, 900 s per run, ordered A,B,B,A (A = stack without, B = with S1 + S2; same AITER build in both arms):

    conc total tok/s (A -> B) interactivity p90 TPOT p90
    512 300,952 -> 301,223 (+0.09%) 140.05 -> 140.5 (+0.32%) 7.14 -> 7.115 ms
    128 83,393 -> 85,402 (+2.41%; one B run had ~260 more requests, the other B run is +0.12%: noise) 222.1 -> 223.15 (+0.47%) 4.505 -> 4.48 ms

    Both B runs' p90 are above both A runs' at both concurrencies. GSM8K (no fixed acceptance length): B 0.9325 +/- 0.0069, A 0.9303 +/- 0.0070 (threshold 0.9157); DSpark acceptance 0.4859 vs 0.4869. Smoke 16/16. An online torch.equal check run with S1 + S2 (c512, 300 s): 48,085 checks over 2,191 row counts, 0 mismatches. Every run's server log checked for the SA configuration (48 local experts / top-6, draft top-3 / 16 not merged, shared expert never inside the draft MegaMoE, no FP8 draft lm_head). Verdict: no regression, slightly better p90 / TPOT within noise; kept as fewer launches with identical numerics.

  • S2 kernel (one MI355X, CUDA graph, back to back): silu_and_mul + quantize_fp8(mid) 4.8-6.0 us vs silu_mul_mxfp8 2.6-3.6 us at 1-512 rows (2.1-2.4 us saved per dense shared expert call), 23 us saved at 16,384 rows.

  • Kernel level (one MI355X, inside a CUDA graph, per layer): quantize_fp8(x) costs 2.2-2.9 us at 1-512 rows; writing the pair from Mega-MHC adds 0.1-0.6 us, so 1.7-2.4 us saved per layer at decode widths (24 us at 16,384 prefill rows). About 0.08-0.10 ms per decode step over 40 target + 3 draft layers in isolation; under serving load quantize_fp8 measured 4.4 us per call in a trace.

  • Prototype of this version (shared_in + dense + draft coverage), c512, 900 s per run: total throughput -0.12%, interactivity p90 +0.21%, both inside run-to-run noise (+/-0.5%); one run per arm (the node went to maintenance before the second pair).

  • First version (shared_in only), c512, A,B,B,A x 900 s: mean decode step 16.29 -> 16.17 ms (-0.11 ms); throughput -0.11% and p90 -0.21% (noise).

  • Stack v3 (this item's first version together with two other small items, perf(dsv41): route V4.1 decode steps (<=128 rows) in one launch after the gate GEMM, 48-expert layout included #3938 and [DSV4.1] DSpark: DP-shard the draft head under ATOM_DP_DRAFT_ARGMAX (bit-identical, measured GEMM pad) #3939), A,B,B,A x 900 s: c512 total throughput +0.59%, p90 +1.12%; c128 +0.86%, p90 +3.46%, mean decode step 8.81 -> 8.51 ms.

So on its own this is below what an end-to-end A/B resolves at c512; it is kept as a strictly-fewer-launches, bit-identical change, measured together with the other small items.

Test plan

  • black / ruff on changed files
  • CPU unit tests
  • GPU bit-exactness: seam pair vs quantize_fp8, dense shared expert on real weights (target + draft layer)
  • GPU bit-exactness (S2): silu_mul_mxfp8 vs silu_and_mul + quantize_fp8; real-weight dense chain S1 + S2 vs fully unfused
  • End-to-end A/B c512 / c128 (A,B,B,A x 900 s) + GSM8K
  • gfx1250 (scale_row_major pair): pending; S2 is gfx950-only (the op is not used elsewhere)
  • CI (ci:atom)

🤖 Generated with Claude Code

…outed call (aiter shared_in)

DeepSeek-V4.1's shared expert (MXFP8 weights with 32x32-block scales) is not
fused into the MXFP4 routed experts by default, so a Mega layer ran it as its
own dense launches (quantize, gate_up GEMM, act, down GEMM) next to the
MegaMoE call. With ROCm/aiter#8491, MegaMoEV2.forward(shared_in=...)
computes it inside its small-input kernel where that kernel can, with the
same MXFP8 activations and the checkpoint's FP8 weights, and otherwise runs
it dense on the caller's stream; either way it returns routed + shared.

- attach_mega_shared_expert (flydsl_mega_experts): on a layer every routed
  call of which reaches run_mega_moe on the rank's own rows with nothing
  reduced after it (the Mega method; no comm-fused or SP MegaMoE, PCP merge,
  DP-logical split or experts reduction), run_mega_moe passes the shared
  expert as shared_in. DeepSeek-V4 MoE attaches it on its target layers
  (TP 1, single stream) and then adds no shared output of its own; the
  DSpark / MTP draft layers keep their dense shared expert.
- The MXFP8 copy of x is made only when AITER reads it, i.e. for a call it
  fuses; a call it cannot fuse runs the dense shared expert, which quantizes
  x itself, so x is quantized once a call either way.
- An AITER without shared_in (no weights.SharedExpertInput, which arrives
  with it) keeps today's path: the shared expert is computed apart.

No new switch or environment variable.

Tests: tests/test_mega_shared_in.py (lazy MXFP8 copy, where the shared
expert is attached, an AITER without shared_in); test_mega_decode_capacity
checks run_mega_moe passes shared_in exactly for an attached layer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every eligible PR before approval:

  • ✅ Pre Checkin: Black, Ruff, catalog schema validation, non-GPU unit tests

Heavy model tests:

  • ✅ Gate checks are published on every PR so required checks do not stay missing
  • ✅ Expensive jobs run only after the PR is approved, or when a matching ci:* label is present
  • ✅ Pre Checkin must pass before approved heavy jobs continue
  • ✅ Without a matching label, expensive jobs also require a relevant path
  • ✅ A matching ci:* label is an explicit request and can open the gate even when paths would otherwise skip it

Path scope:

  • Native ATOM uses an ignore list: docs/markdown and plugin or Mesh paths are skipped
  • vLLM and SGLang use include lists: their own plugin code under atom/plugin/, plugin CI scripts, model list and release images. A core-only change does not run them; add ci:vllm / ci:sglang to ask for them
  • ATOMesh uses an include list: Mesh code, the ATOMesh accuracy workflow and its 1P1D launcher, plus the shared models_accuracy.json and accuracy_to_dashboard.py. ATOMesh benchmarks run after merge or on dispatch, not on PRs
  • Relevant path alone does not start expensive jobs; approval still opens the automatic path-scoped gate
Label Tests
ci:full Run all heavy PR model tests: native ATOM, vLLM, SGLang, and ATOMesh
ci:atom Run native ATOM model accuracy tests
ci:vllm Run ATOM vLLM OOT model accuracy tests
ci:sglang Run ATOM SGLang model accuracy tests
ci:mesh Run the ATOMesh accuracy tests, standalone and 1P1D, for Mesh-related changes

Without a matching ci:* label, heavy jobs are skipped when the path is not relevant or the PR is not approved.
Add labels via the sidebar or gh pr edit 3940 --add-label <label>

…quantize launch before it

DeepSeek-V4.1's shared expert is a native MXFP8 module (group32 E8M0 scales)
computed apart from the routed experts. Its gate_up GEMM quantized its BF16
input itself (quantize_fp8(x)) on every layer and every step: on the target
layers' dense path, inside the routed MegaMoE call that computes it
(shared_in, ROCm#3894), and on the three DSpark draft layers. x is the FFN seam's
normed output, and the seam's HIP Mega-MHC launch can write the same MXFP8
pair next to the BF16 rows (out_dtype="mxfp8", what the attention seam
already does for wqkv_a), bit for bit quantize_fp8 of those rows.

- Block (deepseek_v41/model.py): the FFN seam asks for the pair
  (hc_ffn_fuse_quant) where the HIP seam writes row-major scales and the MoE
  takes it (MoE.takes_mxfp8_input); ffn_forward hands (bf16, fp8, e8m0) to
  the MoE.
- V4 MoE: takes_mxfp8_input holds for a shared expert that reads the pair
  (Expert.reads_mxfp8_input: native group32 FP8 gate_up, not the BF16
  fallback) on the layer's own stream (no dual-stream / comm-fused op, which
  carry the BF16 rows alone). single_stream_moe_forward passes the pair to the
  dense shared expert, whose gate_up reads it instead of quantizing x, or, on
  a shared_in layer, to the routed call (routed_with_shared_mxfp8).
- flydsl_mega_experts: moe_forward_shared_mxfp8 is moe_forward with the pair
  as two more (uint8) arguments of a custom op; the layer holds it while the
  call runs, and shared_in's fused kernel and its dense fallback both read it.
  A pair of other rows is ignored (x is quantized as before).

Precision and formats are unchanged (E4M3, one E8M0 ceil scale per 32
columns); the shared expert is still computed in FP8 on its own. No new switch
or environment variable: the pair is byte for byte what the GEMM would make,
so it is on wherever the conditions hold.

Tests: tests/models/deepseek_v41/test_ffn_seam_mxfp8.py (where the pair is
asked for and where it goes; GPU: the seam's pair against quantize_fp8 over
1-4096 rows, and the dense shared expert on real checkpoint weights of a
target and a draft layer, pair vs BF16 rows, byte for byte),
tests/test_mega_shared_in.py, tests/test_mega_decode_capacity.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yhl-amd
yhl-amd force-pushed the hyi/v41-ffn-seam-x-mxfp8 branch from 8a4ad5f to 8aeae71 Compare November 27, 2026 10:21
@yhl-amd yhl-amd changed the title [DeepSeek-V4.1] FFN seam writes the shared expert's MXFP8 input for MegaMoE shared_in (no quantize launch; on #3894) perf(dsv41): the FFN seam writes the shared expert's MXFP8 input (target, shared_in and draft layers), no quantize launch before it Nov 27, 2026
@yhl-amd

yhl-amd commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

Updated (force-pushed, new head 8aeae71): the implementation is replaced with a wider one.

  • Before: only the fused shared_in path, and the FFN seam's MXFP8 pair was handed to the MoE through a one-entry slot next to moe_forward (checked by storage and row count).
  • Now: the FFN seam returns (bf16, fp8, e8m0) like the attention seam does, and the pair is passed as arguments: MoE.single_stream_moe_forward(x, x_fp8, x_scale) hands it to the dense shared expert (Expert.forward(x_fp8=, x_scale=), gate_up reads it instead of quantizing x), or, on a shared_in layer, to the routed call through a moe_forward variant custom op (moe_forward_shared_mxfp8), where both the fused kernel and its dense fallback read it. So it now also covers the DSpark draft layers, the rows shared_in does not fuse, prefill, and target layers without shared_in. The slot code in mhc_pre_delayed.py is gone.
  • Still no new switch or environment variable; it applies where the shared expert is native MXFP8 on the layer's own stream and the HIP seam writes row-major scales.
  • Verified on MI355X: the seam's pair equals quantize_fp8 of its norm byte for byte (132 cases, 1-16,384 rows), and the dense shared expert on real checkpoint weights (target layers 10/39, draft mtp.0/mtp.2) gives identical bytes from the pair and from the BF16 rows (0 mismatches over 33 row counts each). Details and the CPU tests are in the description.
  • The silu_and_mul + quantize_fp8(mid) fusion (silu_mul_mxfp8, needs a new AITER op) stays a separate follow-up, posted after its end-to-end A/B.

Still draft: depends on #3894 and ROCm/aiter#8491.

… one launch (silu_mul_mxfp8)

On the dense shared-expert path (the three DSpark draft layers every step,
and target-layer row counts the routed MegaMoE call does not fuse,
prefill included) silu_and_mul wrote the BF16 product and w2's native
group32 FP8 GEMM then quantized it (quantize_fp8). AITER's silu_mul_mxfp8
(module_dsv41_seg_fuse, ROCm/aiter#8871) does both in one launch: the
product is rounded to BF16 and quantized with the same group-32 MXFP8
math, so w2 gets the same codes and E8M0 scales.

- model_ops/activation.py: silu_mul_mxfp8_available() (gfx950 and an
  AITER with the op; otherwise the unfused path is kept) and the
  silu_mul_mxfp8_fuse custom op.
- Expert.w2_takes_silu_mul_mxfp8(): a native group32 FP8 w2 (not the BF16
  fallback) whose padded input width is a multiple of 32 and half the
  gate_up output; Expert.forward uses it for an ungated call. The Triton
  fused_clamp_act_mul path (ATOM_V4_USE_TRITON_FUSION) keeps precedence.
- tests: which experts take it, the call wiring, the op against
  silu_and_mul + quantize_fp8 byte for byte, and real-checkpoint shared
  experts (layers.10/39, mtp.0/2) against the fully unfused chain.

Kernel from the DeepSeek-V4.1 DEP8 AgentX PoC (plugin_poc PR ROCm#129, Bin Ding).

Co-authored-by: Bin Ding <Bin.Ding@amd.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yhl-amd yhl-amd changed the title perf(dsv41): the FFN seam writes the shared expert's MXFP8 input (target, shared_in and draft layers), no quantize launch before it perf(dsv41): shared expert MXFP8 input from the FFN seam + silu_mul_mxfp8 on the dense path (fewer launches, bit-identical) Nov 27, 2026
@yhl-amd

yhl-amd commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

Update (11-28): added the second commit 6b86c6522 (S2): on the dense shared-expert path, SiLU-mul and w2's input quant now run as one silu_mul_mxfp8 launch (new AITER op, ROCm/aiter#8871; kernel from plugin_poc PR #129 by Bin Ding). Without that op or off gfx950, ATOM keeps the unfused launches.

  • Bit-exactness (MI355X): op vs silu_and_mul + quantize_fp8 894 cases, 0 mismatches; real-checkpoint shared experts (layers 10/39, mtp.0/2) x 33 row counts x limit 10/0, S1 + S2 vs fully unfused chain, 0 mismatches; also identical under torch.compile and in a replayed HIP graph.
  • End-to-end A/B of S1 + S2 (DSV4.1-Flash, MI355X DEP8, AgentX, SA-compliant, 900 s, A,B,B,A): c512 total throughput +0.09%, p90 interactivity +0.32%; c128 p90 +0.47% (throughput within noise); GSM8K 0.9325 vs 0.9303. No regression; details in the description.

Still stacked on #3894; will rebase and mark ready once it lands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant