Skip to content

[FlyDSL] [MoE] DeepSeek-V4.1 TP4 A8W4: stage 1 -> stage 2 in one launch up to 512 tokens, exact stage-1 FP8 quant rule (traceability) - #8

Draft
amd-pedghazi wants to merge 1 commit into
dsv41-build/moe-basefrom
w48/dvh-fused-s12
Draft

amd-pedghazi wants to merge 1 commit into
dsv41-build/moe-basefrom
w48/dvh-fused-s12

Conversation

@amd-pedghazi

@amd-pedghazi amd-pedghazi commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Upstream draft of this change, ported to ROCm/aiter main (FlyDSL 0.5.3, stacked on ROCm#7917 and ROCm#7918, not GPU-tested yet): ROCm#8658.

Draft inside the fork, on the AITER base of a tested DeepSeek-V4.1-Flash build (MI355X, ROCm 7.2.4, FlyDSL 0.4.0). Not opened against ROCm/aiter main yet, see "Why not main" below.

Summary

For DeepSeek-V4.1-Flash TP4 on gfx950 (A8W4: MXFP8 activations x MXFP4 weights; model_dim 5120, inter_dim 640, 385 experts, top-7), fused_moe runs MoE stage 1 and the v2-layout stage 2 as one kernel launch for calls of up to 512 tokens (DSpark verify steps), and the stage-1 FP8 epilogue can quantize with the exact rule of the two-kernel path. Both are opt-in through environment variables.

The only similar upstream PR, ROCm#8381 (closed, draft), is written for another shape (H5120 / I2304, top-6 / top-3, EP8) with its own kernels on FlyDSL 0.5.0. This PR instead traces AITER's own stage-1 body and AITER's v2-layout stage-2 tile (compile_gemm2_a4w4_port(_composition=...)) into one kernel, so it keeps the tuned tiles and the fused output can be compared bit for bit with the two-launch path.

Motivation

At 257-512 tokens, the stage-1 and stage-2 launches of a verify-step MoE call each leave the GPU partly idle at their start and end. One launch removes one launch gap and lets stage-2 tiles start while later stage-1 tiles still run.

Changes

  • New aiter/ops/flydsl/kernels/devb_fused_s12.py:
    • one 1-D grid: stage-1 tiles first, in AITER's order, then the stage-2 tiles in the stage-2 kernel's own order;
    • a stage-1 tile writes its FP8 rows and sorted e8m0 scales write-through (sc1), waits for its stores, and adds 1 to its sort block's ready counter;
    • a stage-2 tile waits until its block's counter reaches the number of stage-1 N tiles, and the last stage-2 tile of a block resets the counters for the next call;
    • no co-residency assumption, no grid barrier, no fences: a stage-2 tile waits only on lower workgroup ids;
    • AITER_DEVB_FUSED_S12=1 installs it for the process (it holds the stage-1 launch of a fused_moe call and launches it with the stage-2 launch of the same call); DEVB_FUSED_MAX_ROWS (512 in the tested build) caps the token count, DEVB_FUSED_OCC (3 in the tested build) sets the LDS size for 3 workgroups per CU. The reduction kernel after stage 2 is unchanged.
  • mixed_moe_gemm_2stage_common.py:
    • trace-time hooks that are empty unless the fused kernel traces a body (block ids, write-through stores, one SharedAllocator per kernel, a call after each stage-1 tile and around each stage-2 tile); the stage-1 grid moves into _grid1 so the fused kernel can reuse it;
    • fp8_exact for the stage-1 FP8 epilogue (AITER_DEVB_S1FP8_EXACT=1): values rounded to bf16, amax floored at 1e-10, e8m0 = ceil_pow2(amax / 448), as fused_dynamic_mxfp8_quant_moe_sort does. Without it the FP8 epilogue uses AITER's own rule (round to the nearest power of two, 8 bits of headroom), and the MoE output error goes from 0.0268 to 0.0305 (relative L2);
    • the exact rule applies to stage-1 builds with xcd_swizzle 0 and to the <tile_m>/<waves_per_eu>/<xcd_swizzle> keys listed in AITER_DEVB_S1FP8_EXACT_KEYS. The tested build sets 32/2/1,32/3/4,64/3/1: the stage 1 of its 512, 1024 and 2048 verify rows. The prefill rows (t64, w4, xcd1; 4096-32768 tokens) keep AITER's rule. Exact builds get _fp8qx in the kernel name instead of _fp8q.
  • With the switches off, an earlier revision of these hooks (the same hooks, before the AITER_DEVB_S1FP8_EXACT_KEYS list) built the same ISA as the unchanged builder for the six standalone kernels of these rows (compiled and compared on CPU). Not repeated for this revision.

Why not main

Performance (if applicable)

Hardware Workload/configuration Before After Change
MI355X (gfx950), ROCm 7.2.4, FlyDSL 0.4.0 fused_moe call, 512-token row (stage 1 t32x128x256_w2_gui_xcd1, v2-layout reduce stage 2), real verify routing, CUDA graph, median of 10 rounds, 288 / 320 / 384 / 480 tokens two launches: 136.4 / 172.2 / 164.4 / 261.1 us one launch: 128.5 / 165.7 / 157.9 / 252.8 us (repeat 129.6 / 166.7 / 157.3 / 252.4) -5.8 % / -3.8 % / -3.9 % / -3.2 %
4 x MI355X SGLang serving, DeepSeek-V4.1-Flash TP4, DSpark, random 8k/1k, c256, 4 order-balanced pairs the same build without this PR and with plain-stage-1 rows (FP8 mixed stage 2) at 512 / 1024 / 2048 tokens this PR + those rows throughput +1.30 % (se 0.13 %)

The serving rows measure this PR together with the XCD-remap verify rows of ROCm#8650; the fused launch alone was not measured in serving. An earlier arm with the fused launch and v2-layout rows but no XCD stage 1 gave +0.90 % (random 8k/1k c256, 2 pairs) against the same baseline.

Testing

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

gfx950 only (MI355X), so MI250X and MI300X do not apply. No op test is added to op_tests/ yet: the checks below ran with our own scripts (named in "Validation"). The performance box stays unticked until an 8-point serving A/B of this switch alone has run (pending, see below).

Documentation

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

N/A (small opt-in change): the switches are off by default and documented in the module docstring.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Base of this PR (0bd8f9f0e6): AITER 285579b6 with earlier ports of ROCm#6170 (stage-1 LDS-DMA wait, dma_wait_fix), ROCm#7917, ROCm#7918 and ROCm#7940 (#2) and of ROCm#7949. None of these is part of this PR. The tested build adds ROCm#8550's hunk on top of this PR's builder (#9); that is not ours either and is not in this PR.

Breaking Changes

None. With AITER_DEVB_FUSED_S12 and AITER_DEVB_S1FP8_EXACT unset, nothing changes.

PR Self-Review

Reviewed revision

23d4f8169bfe46949c5a8aeb188a23f7f592b395

Behavior and intent

With AITER_DEVB_FUSED_S12=1, a fused_moe call of up to DEVB_FUSED_MAX_ROWS tokens whose row names an FP8-epilogue stage 1 and a reduce v2-layout stage 2 (or an _fp8 stage 1 and a mixed stage 2) runs both stages in one launch. Other calls, and calls during a CUDA-graph capture before the workspace exists, run as before. With AITER_DEVB_S1FP8_EXACT=1, the selected stage-1 builds quantize exactly like the two-kernel path.

Validation and unverified scope

Tested on MI355X (gfx950), ROCm 7.2.4, FlyDSL 0.4.0 (this PR's files at 23d4f8169b plus ROCm#8550's hunk, #9 at f617635ce2):

  • Correctness: with the exact rule, the fused output equals the two-launch output bit for bit at 288-2048 tokens, eager and in CUDA-graph replays with 4-20 new routings per size; the counters are back to 0 after every call; 0 wait timeouts. The fused v2-layout output has the same error as today's path (0.0268 relative L2).
  • Determinism (with the tested build's LDS-DMA wait): 8 eager reruns and 8 graph replays of the same input: 0 differing calls, 0 NaN, at 64-2048 tokens.
  • Serving: GSM8K 0.9723 vs 0.9730 over 8 order-balanced pairs (paired difference -0.0007, within run-to-run noise). The server log shows [devb_fused_s12] installed: occ 3, max rows 512 on all 4 ranks.
  • Not verified: main / FlyDSL 0.5.3, other shapes, other GPUs, the mixed-stage-2 fused variant in serving (the tested build serves only the v2-layout one).
  • Style: ruff check (the repo's config at the base) reports two C408 findings (dict(...) calls) in the added lines; black --check would reformat both files (the base file also fails it). Kept as in the tested build.

PR-specific question

N/A (fork draft).

Changed op tests

None.

🤖 Generated with Claude Code

Note (2026-11-25): commit messages on this fork were reworded without file changes; the earlier commit ids stay reachable as tags keep/<12-char id> (for example keep/4234d6fc2ec7).

The module, env var and log tag names in this PR are the tested build's; ROCm#8658 renames them (moe_fused_s12.py, AITER_MOE_FUSED_S12, AITER_MOE_FUSED_S12_MAX_ROWS, AITER_MOE_FUSED_S12_OCC, AITER_MOE_S1_FP8_EXACT, AITER_MOE_S1_FP8_EXACT_KEYS).

… exact stage-1 FP8 quant rule

The tested build's code: a new FlyDSL module (md5 be081d13) traces AITER's stage-1 body and the v2-layout
stage-2 tile into one kernel with per-sort-block ready counters, behind an opt-in env switch with row-limit and
occupancy settings. The builder gets trace-time hooks and the exact stage-1 FP8 epilogue (opt-in switch and key
list), md5 7bdda7b7. File and env-var names are the build's; ROCm#8658 is the upstream form.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@amd-pedghazi amd-pedghazi changed the title [w48] [FlyDSL] [MoE] DSv4.1 TP4 A8W4: stage 1 -> stage 2 in one launch up to 512 tokens, exact stage-1 FP8 quant rule [FlyDSL] [MoE] DeepSeek-V4.1 TP4 A8W4: stage 1 -> stage 2 in one launch up to 512 tokens, exact stage-1 FP8 quant rule (traceability) Nov 25, 2026
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.

1 participant