Repository navigation
[HIP] [OPUS] feat(moe): fused route-sort + MXFP4 activation-quant kernel for… - #6165
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
… gfx950 decode
Add mxfp4_moe_sort_quant_fwd: a single-launch HIP kernel that fuses opus
route sorting, BF16->MXFP4 activation quantization (v_cvt_scalef32_pk_fp4_bf16)
and routed-output zeroing for the Qwen3.5 MXFP4 MoE decode path (model_dim=4096,
block_m=32, M in [1,128], routes (num_experts,topk) in {(512,10),(513,11)}).
CTA 0 builds the sorted metadata via an LDS expert histogram + DPP wave
prefix-scan; the remaining CTAs emit compact per-token E8M0 scales that the
flydsl GEMM1 consumes directly. Gated behind SGLANG_AITER_FUSED_DECODE_SORT_QUANT
and fed into fused_moe_2stages as prequantized_a1 on the expanded-scale path.
Adds an op_test (correctness vs opus + generic quant, decode-M gate, runtime-arch
gate, CUDA-graph replay) and a sort+quant latency benchmark.
Co-Authored-By: Claude <[email protected]>
Co-authored-by: yuychang <[email protected]>
The orphan compact-scale helper removal left four blank lines before the benchmark-hook comment; black requires two. Co-Authored-By: Claude <[email protected]> Co-authored-by: yuychang <[email protected]>
M=4/8/16 recover the 1-stage flat MXfp4 kernel (fmoe_bf16_pertokenMXfp4_g1u1_ flat_novs_silu_16x128E/16x256E) over the slower 2-stage path, and M=32 moves to the faster t32x64x256_w3 stage-1. All referenced kernels already ship in aiter. Co-Authored-By: Claude <[email protected]> Co-authored-by: yuychang <[email protected]>
… EXE001) The file carries a #!/usr/bin/env python3 shebang, so ruff requires the executable bit set. Co-Authored-By: Claude <[email protected]> Co-authored-by: yuychang <[email protected]>
f3f39db to
6cdc769
Compare
|
Current head |
|
Thanks for the rebase. I re-reviewed exact head
|
|
To reduce the scope here, I added For item 2, representative base-vs-head |
…H=4096, routes (512,10)/(513,11)) The gate docstring, Python wrapper docstring, C++ header, and thrown error still advertised the GPT-OSS contract (H=7168, routes (256,8)/(384,8)/(385,9)) while the kernel is instantiated for H=4096 and (512,10)/(513,11). Sync all four to the implemented shape set. Co-Authored-By: Claude <[email protected]>
Hi Zufa, Point 1 Point 2 Env: torch 2.9.1+rocm7.2.0 | hip 7.2.26015 | ROCm 7.2.0 | gfx950 Base = conventional two-launch (sort+quant). Head = fused+ss (sorted-row scale, matches compact_scale=False).
Point 3 |
|
Please avoid adding a new environment variable unless it is strictly necessary. The existing gfx950/dtype/shape/configuration gate already defines when this kernel is supported, so the optimized path should be selected automatically when all conditions match and fall back otherwise. Please remove Also, please integrate the new correctness and regression cases into the existing |
…nv var Address review feedback on PR ROCm#6165: - Remove the SGLANG_AITER_FUSED_DECODE_SORT_QUANT env gate (and the dead compact-scale env). The fused route-sort + MXFP4 quant kernel is now selected purely from the gfx950/shape/dtype/route gate and falls back to the generic sort+quant otherwise -- an AITER core optimization should not depend on a downstream-specific runtime switch. Add an internal _fused_decode_sort_quant_disabled hook for same-build base-vs-head A/B. - Fold the standalone op_tests/test_mxfp4_fused_sort_quant.py cases into the existing op_tests/test_moe_sorting_mxfp4.py operator-family harness (gate table, runtime-arch dispatch, disable toggle, wrapper, CUDA-graph replay, and per-route correctness+latency); remove the standalone file. The hip benchmark under op_benchmarks/hip/ stays separate. Co-Authored-By: Claude <[email protected]>
Sort imports, use `from aiter import fused_moe`, and make the shared gate kwargs a dict literal (ruff I001/PLR0402/C408). No behavior change. Co-Authored-By: Claude <[email protected]>
… 26.10.1) Co-Authored-By: Claude <[email protected]>
Thanks for the review! I've addressed both points.
Please take another look when you have time. Thanks again! |
…x tests - Remove x_layout="a8w4", x_scale="row" and the Triton MXFP8 quantiser from gemm1_a8w4_gfx942 (no production caller or test reached them). The gfx942 ISA is bitwise identical on 5 variants (moe_sort and _qi, k_wave 1/2/4). - _if_then: a local @flyc.jit dispatch instead of a raw scf.IfOp. - test_moe_2stage: always skip _g942 rows; the CSV sweep prepares neither the a8w4 relayout nor the separated gate/up layout they need, so they aborted the run on MI325X. test_flydsl_moe_a8w4_gfx942.py covers them. - test_flydsl_moe_a8w4_gfx942: set AITER_SITUV2_A8W4 to 1 or 0 explicitly and restore the caller's value; add M = 1024 and 4096. The merge with main (previous commit) resolves the fused_moe_2stages conflict with #6165 and rejects prequantized_a1 together with the gfx942 a8w4 stage 1. Co-Authored-By: Claude Opus 5.5 <[email protected]>
* [FlyDSL] gfx942 a8w4 MoE stage1 fp8 kernel * Raise on non-gfx942 instead of assert * Add Triton MXFP8 quant for a8w4 (FNUZ-correct, a8w4 byte order) * gfx942 a8w4: fused_moe wiring, tuned rows, op test - moe_kernels: register the _g942 stage-1 names (flydsl_moe1_afp8_wfp4_bf16_*_g942*) and route fp8 x fp4 stage 1 on gfx942 to gemm1_a8w4_gfx942 (bf16 sorted intermediate, consumed by the unchanged a16w4 stage 2). - fused_moe: no inter-stage requant for the _g942 stage 1 (a2/a2_scale = None). - gemm1_a8w4_gfx942: x_layout="moe_sort" reads x / scales as produced by fused_dynamic_mxfp8_quant_moe_sort; exponent rule bitwise equal to it. - kimik3_a8w4_tuned_fmoe.csv: 32 gfx942 (cu 304) rows, inter 384/512, M 1..32768 except M=3 (fused_moe pads 3 -> 4); input shapes in kimik3_a8w4_gfx942_untuned_fmoe.csv. - op_tests/test_flydsl_moe_a8w4_gfx942.py: a16w4 vs a8w4 through fused_moe. Co-Authored-By: Claude Opus 5.5 <[email protected]> * op test a8w4 gfx942: scale atol with the MoE output checkAllclose(rtol=atol=1e-2) reported err ~0.22 for both a16w4 and a8w4: the MoE output is O(1e3) and the paths and the torch reference round the bf16 intermediate differently (1-3 bf16 ulps, <= 1.2 % of max|ref| on all 8 shapes). atol = 2e-2 * max|ref| gives err 0 for both paths; cos checks unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * gfx942 a8w4: quantise x inside stage 1 for M=1,2 (_qi kernels) At M=1 the separate fused_dynamic_mxfp8_quant_moe_sort launch cost more than the fp8 stage 1 saved, so a8w4 lost to a16w4 (0.96x). The new x_layout="bf16" stage-1 variant reads bf16 x and quantises each K tile to MXFP8 straight into LDS in A8W4_K8_ORDER, bitwise equal to the separate quant: - x of tile kt+1 is loaded before compute_tile(kt), quantised after it; - block amax via maxnum + DPP quad swaps (no NaN-propagating max, no LDS swizzle with a full lgkmcnt wait); - padding rows of the M tile are not quantised (their LDS is zeroed once). Registry: every _g942 name gets a _qi variant (x_layout="bf16"); fused_moe skips the separate x quant for _qi rows and passes bf16 x. CSV: gfx942 M=1,2 rows use the _qi stage 1; inter 384 M=1 uses the a16w4 t16x256x128_atomic stage 2. MI300X, whole MoE vs a16w4 at M=1: 1.05x (inter 512), 1.11-1.16x (inter 384), was 0.96x / 1.00x. Non-_qi kernels compile to the same ISA as before. Co-Authored-By: Claude Opus 5.5 <[email protected]> * gfx942 a8w4: MI325X only; LDS swizzle in _qi kernels; _qi rows for M<=8 - Enable the gfx942 a8w4 stage 1 on MI325X only (PCI device id 0x74A5, read from sysfs); elsewhere raise NotImplementedError. On MI300X a8w4 is slower than a16w4 at large M and the tuned CSV rows are shared by both cards. The op test skips on other gfx942 parts. - XOR-swizzle the A tile in LDS (as gemm2) only in the _qi kernels, where the in-kernel quant hit LDS bank conflicts; other kernels unchanged. - With that, _qi also wins at M=4, 8: switch those CSV rows to _qi. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * gfx942 a8w4: allow MI325X VF; fix gate comment and error text - Add 0x74B9 (MI325X VF, SR-IOV guests) to the MI325X allow-list. - The PciChipId comment: chip_info.py's hardcoded 10019 is wrong on ROCm 7.2.4 / 7.13 / 7.14 (PciChipId is 10020), hence sysfs. - Error text: MI300X numbers of this PR (0.81-0.89x at M>=1024). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * gfx942 a8w4: skip _g942 stage 1 in FlyDSL MoE AOT; skip its rows off MI325X in test_moe_2stage - aot/flydsl/moe.py: the `_g942` stage-1 kernels are built by gemm1_a8w4_gfx942 (JIT on first use); the AOT stage-1 compile would build the gfx950 mixed-MoE kernel for them and fail (wheel build: 64 failed). - test_moe_2stage.py: tuned-CSV rows are selected by cu_num only, so on MI300X (cu 304) the gfx942 a8w4 rows were picked up and the MI325X-only stage 1 raises; skip them where a8w4_gfx942_supported() is False (test_flydsl_moe_a8w4_gfx942.py covers them on MI325X). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * [FlyDSL] a8w4 gfx942: drop unused x layouts, @flyc.jit runtime if, fix tests - Remove x_layout="a8w4", x_scale="row" and the Triton MXFP8 quantiser from gemm1_a8w4_gfx942 (no production caller or test reached them). The gfx942 ISA is bitwise identical on 5 variants (moe_sort and _qi, k_wave 1/2/4). - _if_then: a local @flyc.jit dispatch instead of a raw scf.IfOp. - test_moe_2stage: always skip _g942 rows; the CSV sweep prepares neither the a8w4 relayout nor the separated gate/up layout they need, so they aborted the run on MI325X. test_flydsl_moe_a8w4_gfx942.py covers them. - test_flydsl_moe_a8w4_gfx942: set AITER_SITUV2_A8W4 to 1 or 0 explicitly and restore the caller's value; add M = 1024 and 4096. The merge with main (previous commit) resolves the fused_moe_2stages conflict with #6165 and rejects prequantized_a1 together with the gfx942 a8w4 stage 1. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fixes for item1 and item 2 * item 3 * item 4 * [FlyDSL] a8w4 gfx942: prebuild the _g942 stage-1 kernels in the AOT replay main's AOT now replays fused_moe for every tuned row, with caller inputs built per quant family. For fp8 x fp4 it built the gfx950 gate/up-interleaved a16w4 weights, so all 360 replays of the gfx942 rows hit the W1-layout check and failed. A _g942 row now gets separated gate/up W1 relaid and tagged by shuffle_{weight,scale}_a8w4_gfx942, and a8w4_gfx942_supported() passes in an AOT replay, which has no device to check and compiles for the arch GPU_ARCHS names. The replay of kimik3_a8w4_tuned_fmoe.csv for gfx942 is now 360 ok / 0 failed, so these kernels are prebuilt instead of JIT-compiled on first use (review item 7). Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: name things in comments, not PR numbers Review item 11: comments referred to "the a16w4 port", "the a8w4 port" and "the #6144 table", which only make sense to someone who followed the development history. They now name the function or constant they mean (compile_gemm1_a16w4_port, compile_gemm1_a8w4_gfx942, _FP4_E2M1_FP8_LUT_LO / _HI). Comments only, no code change. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: tune the _g942 stage-1 kernels with the MoE tuner Review item 5: the tuner rejected per_1x32 on anything but gfx950, so the shipped gfx942 a8w4 rows could not be regenerated with it. On gfx942 it now tunes the _g942 stage-1 kernels together with the a16w4 stage 2. The _qi kernels get the bf16 x and the others the output of the HIP MXFP8 quant; the separate x-quant launch time is added to the moe_sort kernels and the _qi rows are marked xbf16, as is already done for per_1x128. The gfx950 inter-stage fp8 cast is not added, because the a16w4 stage 2 reads the bf16 intermediate. Timing and selection are unchanged. On MI325X it picks a _qi stage 1 at every M <= 8, for both inter dims. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: key the kernel cache on a source hash, not a hand-bumped rev The hand-bumped `rev` tag in the kernel name did not invalidate anything in FlyDSL 0.3.4.1: the kernel name is not part of FlyDSL's cache key, and the key follows the functions the kernel body calls but not one reached two calls deep. With the decoder in _fp4x8_to_fp8x8 (called from _gemm1_body_a8w4) changed to give wrong numbers, the cached binary was still served and the test passed; with an empty cache the same edit fails it (cos diff 0.024). As pa_decode does in implementation_cache_tag, the factory now hashes the sources the device code comes from once per process, and the kernel and launcher bodies read the hash, which puts it into the key. The same helper edit now rebuilds the kernel and fails the test, an unchanged file still hits the cache, and no tag has to be bumped by hand (review item 9). Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: cache the stage-1 compile factory again The source-hash change (63c794a) put _source_tag between the @functools.cache line and compile_gemm1_a8w4_gfx942, so the hash function got the decorator twice and the factory none. Every stage-1 call then built a new launcher, which FlyDSL loads from its disk cache and unloads again when the launcher is dropped. On MI325X this made an eager fused_moe call on the a8w4 path take 37.6 ms instead of 0.33 ms (M = 1, inter 512), and CUDA-graph capture of it failed with hipErrorStreamCaptureUnsupported on hipModuleUnload, so the path could not run under the graphs vLLM uses for decode. The a16w4 path was unaffected. The factory is cached again (one compiled launcher per config) and _source_tag keeps a single decorator. The stage-1 kernel test, which runs on every gfx942, now also captures the launch in a CUDA graph and checks that the replay gives the same output: it fails on 63c794a for both kernels and passes with this change. The full op test passes on MI325X (exit 0). This was not pushed when found, so CI has not run it. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] moe_2stage_a16wmix: share the stage-1 XCD tile map and grid size The a16w4 stage 1 (gemm1.py) and the gfx942 a8w4 stage 1 each carried their own copy of the bijective XCD round-robin + M-group swizzle and of the flattened grid size (review item 10). Both now come from utils.py (xcd_tile_map, gemm1_grid, NUM_XCD); gemm1_a16w4_grid stays as the public name the package exports. The divisions of the valid-tile bound still happen where the caller builds the map, as they did inline, so the generated code is unchanged. Checked by dumping the final ISA of every production config through the AOT replay of the tuned CSVs that use these kernels (kimik2_i4, kimik3_a16w4, kimik3_a8w4, kimik3_i4, minimax_m3_a16w4), before and after, with the source-hash suffix of the _g942 kernel names stripped and nothing else ignored: identical for all 46 a16w4 and 17 _g942 stage-1 kernels on gfx942 and all 82 a16w4 stage-1 kernels on gfx950 (21 and 31 of the a16w4 ones use the XCD swizzle); the stage-2 and reduction kernels compiled alongside are identical too. No registered _g942 name uses the XCD swizzle, so that branch of the a8w4 kernel is covered only through the shared helper. The a8w4 op test passes on MI325X and the AOT replay of kimik3_a8w4_tuned_fmoe.csv builds 360 gfx942 and 732 gfx950 jobs without failures. The k_wave reduction and the epilogue stay separate: gemm1.py keeps its accumulators in memrefs (fx.memref_load_vec / acc.store) while the a8w4 kernel keeps them as SSA values, so sharing them would mean rewriting one of the kernels rather than moving code. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: use the stable FlyDSL APIs where the ISA stays the same Review item 8 asked the gfx942 a8w4 stage 1 to move off FlyDSL calls outside the stable API. Each swap was applied on its own and the final ISA of the 17 _g942 stage-1 kernels of kimik3_a8w4_tuned_fmoe.csv's gfx942 rows (AOT replay, FLYDSL_DUMP_IR) compared with the previous commit, source-hash suffix stripped and nothing else ignored: - .shrui(n) -> >> n (both results are masked to bits the arithmetic shift leaves alone): 17/17 identical. - gpu.thread_id / gpu.block_id -> fx.thread_idx.x / fx.block_idx.x: 17/17 identical. - _raw(x) -> x.ir_value() at all 25 sites (the buffer resource is already an ir.Value, so its wrapper is dropped; tensor_shim._to_raw is no longer imported): 17/17 identical. - rocdl.mfma_f32_16x16x32_fp8_fp8 -> fx.make_mma_atom(fx.rocdl.MFMA(16, 16, 32, fx.Float8E4M3FNUZ)) + fx.gemm on register fragments: 17/17 identical. Not taken: the output store through fx.rocdl.make_buffer_tensor + BufferCopy16b changes one instruction in every kernel. BufferCopy takes a bf16 element index, so a masked row can no longer get buffer_store's byte offset 0x7FFFFFFF (v_bfrev_b32 -2); it gets 0x7FFFFFFE (v_mov_b32 with a literal). The store itself and its addressing are unchanged, but the ISA is not identical, so buffer_ops.create_buffer_resource_from_addr / buffer_store stay. perm_b32, cvt_pk_fp8_f32 and readfirstlane have no stable FlyDSL wrapper (they are not in rocdl.__all__ in FlyDSL 0.3.4.1); a one-line comment at their uses says so. With all of this the final ISA of the 17 kernels is identical to the previous commit's, the a8w4 op test passes on MI325X, and the AOT replay of kimik3_a8w4_tuned_fmoe.csv builds 360 gfx942 and 732 gfx950 jobs without failures. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Read the gfx942 a8w4 PCI chip id from sysfs via a shared chip_info helper The a8w4 gfx942 stage-1 dispatch is gated on the PCI device id so it only enables on MI325X, and reading that id through hipDeviceGetAttribute(PciChipId) is unreliable because the attribute ordinal is not stable across ROCm releases (10019 before HIP 7.1, 10020 after, and 10019 returns a bogus 0x200 on ROCm 7.2.4 / 7.13 / 7.14). The sysfs `device` file holds the id directly, so move that sysfs read into chip_info as a reusable get_pci_chip_id_sysfs() and have a8w4_gfx942_supported() use it instead of its own inlined copy. The existing hipDeviceGetAttribute path (_get_pci_chip_id, attribute 10019/10020) is left untouched so the MI308 device-name gate keeps its current behaviour. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * gfx942 a8w4: adopt tuner-picked rows where the whole-MoE A/B wins Update the tuned rows for the _g942 stage-1 / a16w4 stage-2 kernels to the in-PR tuner's picks for the 12 shapes where a whole-MoE CUDA-graph A/B confirms a speedup over the previously shipped rows: M=1,2,8 at inter 512 and 384, and inter-384 M=256,1024,2048,4096,8192,16384 (2.4-8.0% faster). M=4 and inter-384 M=512 are kept on the existing graph-measured rows, where the tuner regresses (M=512 +4.8%, M=4 +0.6-0.8%). inter-512 large-M, inter-384 M=16..128 and M=32768 were not re-tuned and keep their current rows. The swapped rows are numerically equivalent to the ones they replace (cos ~1e-6, no NaN); only the kernel/tile choice changes. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * [FlyDSL] a8w4 gfx942: decide the stage-1 arch at runtime with get_gfx_runtime The gfx942 a8w4 decision was made in four places from two helpers: the stage-1 launcher (twice) and the compile factory used FlyDSL's get_rocm_arch(), which follows FLYDSL_GPU_ARCH / ARCH, and the tuner used get_gfx(), which follows GPU_ARCHS. The launcher and the tuner now use chip_info.get_gfx_runtime(), which reads the live device and answers the single GPU_ARCHS target in an AOT replay. The launcher's second "gfx942 only" check is dropped; compile_gemm1_a8w4_gfx942 keeps the one hard check against its compile target (review item 15). The op test passes on MI325X (non-_g942 fp8 x fp4 name still refused, 24/24 fused_moe checks) and the AOT replay of the gfx942 kimik3_a8w4 rows builds 360/360 jobs. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: read the MI325X chip id through chip_info._get_pci_chip_id main (#5182) fixed _get_pci_chip_id to pick the PciChipId attribute from the HIP runtime version, which was the reason this PR read the PCI id from sysfs instead. That left chip_info with two chip-id readers that failed differently (the sysfs one raised OSError and the gate then guessed from the device name). The sysfs reader and the name fallback are removed; the MI325X ids sit next to MI308_CHIP_IDS, and the a8w4 gate reads the id through _get_pci_chip_id and fails closed if it cannot (review item 16). On MI325X with ROCm 7.2 _get_pci_chip_id returns 0x74a5, as the sysfs read did. With the reader stubbed, the gate is on for 0x74A5 / 0x74B9 and off for 0x74A1 and for a failed read. The op test passes (24/24 fused_moe checks) and the AOT replay of the gfx942 kimik3_a8w4 rows builds 360/360 jobs. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: one predicate for the _g942 stage-1 kernels Whether a stage-1 kernel is the gfx942 a8w4 one was written out in six places: the stage-1 dispatch, fused_moe, the AOT replay and the tuner compared in_dtype with the "a8w4_gfx942" string, the op test did too, and test_moe_2stage.py looked for "_g942" in the name (review item 17). moe_kernels.py now exports is_a8w4_gfx942_kernel(name or params) and A8W4_GFX942_IN_DTYPE, and all of them use it, so a rename or a second gfx942 a8w4 family changes one line. The tuner's _is_a8w4_g942 stays: it decides per shape, from arch and dtypes, which candidate family to sweep, before any kernel name exists. Checked: true for all 128 _g942 names (by name and by params), false for the other 5965 registered kernels and for None / {} / an unknown name; the op test passes and the AOT replay of the gfx942 kimik3_a8w4 rows builds 360/360 jobs. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: check the stage-1 inputs in one helper, not in fused_moe_2stages The gfx942 a8w4 input checks (W1 / W1-scale layout tag in both directions, no caller-prequantized A for a _g942 stage 1, bf16 / fp16 x only for the _qi kernels) were spread over about 40 lines of fused_moe_2stages, next to the shared a16w4 logic (review item 18). They are now check_a8w4_gfx942_inputs in moe_kernels.py, beside is_a8w4_gfx942_kernel, called once before the stage-1 input is prepared. The checks and their messages are unchanged. Checked: each of the seven input combinations (good _qi and moe_sort inputs, untagged W1, tagged W1 with an a16w4 stage 1, untagged a16w4, prequantized A, fp8 x on a _qi row) passes or raises as before; the op test passes (24/24 fused_moe checks, including the fp8-x and mixed-layout cases) and the AOT replay of the gfx942 kimik3_a8w4 rows builds 360/360 jobs. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: bound the x-scale buffer descriptor Every buffer descriptor in the gfx942 a8w4 stage 1 was bounded except the one for the MXFP8 x scales, which set num_records to 0xFFFFFFFF, so hardware bounds checks never applied to those reads (review item 20). The kernel reads scale rows below cumsum[0] only, and fused_dynamic_mxfp8_quant_moe_sort allocates pad32(sorted rows) x _ms_npad bytes, so the descriptor is now bounded at pad32(cumsum[0]) x _ms_npad, the same source the output descriptor uses. An indexing slip would now read zeros instead of neighbouring memory. Checked on MI325X: the bound never exceeds the quantiser's allocation (M 1 to 32768, BM 16 / 32); the 12 CSV _g942 kernels give bitwise-identical output (36/36); the `_qi` kernels compile to identical ISA (they do not read the scale buffer), the moe_sort ones gain 12 to 15 scalar setup instructions with the same VGPR count; whole fused_moe a8w4 time is unchanged (new/old 0.999 to 1.001, 6 interleaved runs, M 16 / 128 / 1024 / 2048, inter 384 / 512). The op test passes and the AOT replay builds 360/360 jobs. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: drop the source-hash cache tag, FlyDSL 0.5.0 tracks the helpers The source-hash tag (_source_tag, read as `_ = _a8w4_src_tag` in the kernel and launcher bodies and appended to the kernel name) worked around FlyDSL 0.3.4.1's cache key, which did not follow a helper reached two calls deep. main now pins FlyDSL 0.5.0 (#7600), which dropped the same pattern from pa_decode (review item 21). Re-ran the repro at 0.5.0 without the tag: with _fp4x8_to_fp8x8 (called from _gemm1_body_a8w4) broken, the same cache got a new entry and the kernel-only test failed as it should (cos diff 0.024, as with an empty cache), and an unchanged file still hit the cache. So the tag, its reads and its name suffix are removed. The factory keeps its functools.cache. Final ISA of all 17 _g942 kernels is identical to the previous commit apart from the name, outputs of the 12 CSV kernels are bitwise identical, the op test (including its CUDA-graph replay check) passes, and the AOT replay builds 360/360 jobs. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: keep the kernel module to the kernel, move the oracle and relayouts out The torch MXFP8 oracle, the W1 relayouts and their layout tag are host-side tensor helpers, so they now live next to the other shuffles in aiter/ops/shuffle.py and the shared exponent rule in fp4_utils, where the tuner imports it as well. The op test builds its own oracle from that rule, and the kernel entry point is exported from the moe_2stage_a16wmix package like the a16w4 ones. Outputs of the 12 tuned kernels are bitwise unchanged and AOT replay compiles all 360 rows. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: make the stage-1 op test fail on one wrong block scale A cos-diff bound of 1e-3 passed outputs with one zeroed column, so test_stage1_kernel now requires every element within checkAllclose's default tolerances and proves it on a W1 whose up scale is doubled for one 32-wide block of one column. W1 is scaled by 1/8, which keeps the FP4 codes and keeps every output below SiTUv2's saturation at 100, where about 1 % of the outputs sat before and hid such errors. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: say why the output descriptor and store stay on buffer_ops The reasons were only in the PR thread, so the next FlyDSL pin bump would have to rediscover why these two calls were not moved to make_buffer_tensor / BufferCopy16b. Co-Authored-By: Claude Opus 5.5 <[email protected]> * FlyDSL a8w4 gfx942: raise on GUGU input in shuffle_weight/scale_a8w4_gfx942 Both relayout functions require GGUU (separated gate/up) layout as input. A GUGU-preshuffled (gate_up_interleave=True) W1 passes the relayout silently and receives the a8w4_gfx942_layout tag, but half its bytes are misplaced relative to the GGUU order the stage-1 kernel reads, giving wrong output without an error. Add an explicit ValueError check on the gate_up_interleave attribute so the mislayout is caught at relayout time. * FlyDSL a8w4 gfx942: assert err == 0 in test_moe_a8w4_gfx942, add self-check The checkAllclose result was recorded but never asserted, so a localized output error (e.g. one wrong token) could pass silently. Add assert err == 0 after each checkAllclose call in the candidates loop. Add a self-check that zeroes one output row and confirms the tolerance catches it (err > 0), so the check cannot go silent if the tolerance is too loose. Remove the weaker assert on a8w4 cos < 1e-3 (superseded by err == 0). * FlyDSL a8w4 gfx942: extend test_stage1_kernel to cover all CSV tile families The M=1 kernel name in main() was stale since 5d7e4e1: the CSV M=1 rows now use t16x32x256_g942_kw2_qi, not t16x32x128_g942_kw4_qi. Fix it. test_stage1_kernel hardcoded tile_m=16 and asserted it, so BM=32 kernels (t32x*) could not be tested. Remove the tile_m==16 assertion; derive tile_m from the kernel params instead. Add five more calls in main() to cover each distinct tile family in kimik3_a8w4_tuned_fmoe.csv that was not reached on MI300X CI runners: TILE_K=256 (t16x128x256_g942), bnt0 (t16x128x128_g942_bnt0_kw2), BM=32 (t32x128x128_g942), and BM=32+TILE_K=256 (t32x128x256_g942_bnt0). * FlyDSL a8w4 gfx942: apply black formatting to test file * FlyDSL a8w4 gfx942: fix pre-existing ruff errors in gemm1.py and gemm.py gemm1.py imported gemm1_grid from .utils but never used it (F401); remove it. gemm1.py called _udiv and _umod from .utils but did not import them (F821); add them to the import list. aiter/aot/flydsl/gemm.py has a shebang but was not executable (EXE001); set the executable bit. * FlyDSL a8w4 gfx942: fix pre-existing ruff errors in gemm1.py gemm1.py imported gemm1_grid from .utils but never used it (F401); remove it. gemm1.py called _udiv and _umod from .utils but did not import them (F821); add them to the import list. * FlyDSL a8w4 gfx942: mark aiter/aot/flydsl/gemm.py executable (ruff EXE001) The file has a #!/usr/bin/env python3 shebang but was not marked executable (mode 100644). Set the executable bit (100755) to match. Pre-existing lint error caught by ruff 0.16.0. * Revert "FlyDSL a8w4 gfx942: mark aiter/aot/flydsl/gemm.py executable (ruff EXE001)" This reverts commit f9f5db0. * Revert "Revert "FlyDSL a8w4 gfx942: mark aiter/aot/flydsl/gemm.py executable (ruff EXE001)"" This reverts commit 2b777e7. * FlyDSL a8w4 gfx942: fix test_plain_fp8_swiglu_limit_dispatch for #8178 #8178 changed get_2stage_cfgs so that per-token FP8 + clamped SiLU on gfx942 uses the FlyDSL full_impl path (stage1=None) instead of CK stage1. The test asserted stage1.func is ck_moe_stage1 without guarding against the None case, crashing on MI300X CI. Accept both outcomes: if stage1 is None assert full_impl is set (FlyDSL took over), otherwise keep the existing CK assertions. * [FlyDSL] a8w4 gfx942: keep _qi stage 1 rows at M <= 8 so pre-quantised x is accepted above that The inter-384 M = 256 row used a _qi stage 1, which quantises x itself and refuses MXFP8 x + a1_scale, so a caller passing pre-quantised x at 129-256 tokens got a ValueError although the contract promises the passthrough from M = 16 on. The row goes back to the moe_sort kernel it had before the tuner swap. The op test now checks that the CSV uses _qi only up to M = 8, and accepts a refused pre-quantised x only there. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: check every _qi stage 1 of the CSV on full M tiles The only _qi case in CI was M = 1 with 8 experts, so each M tile held one real row, where the A swizzle and the per-row x-scale slots are the identity; a mismatch between the in-kernel quant's write and the read would have passed. test_stage1_kernel now also runs all five _qi kernels of the CSV with 2 experts / top 2 and 16 tokens, so every tile row is real. A read without the swizzle, or x-scales written to the neighbouring row, fails these cases and still passes the M = 1 one. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: tag the a16w4 scale preshuffle with its gate/up order shuffle_scale_a8w4_gfx942 refuses a gate/up-interleaved (GUGU) scale by its gate_up_interleave tag, but only shuffle_weight set that tag, so a GGUU W1 paired with a GUGU W1 scale passed both relayouts and gave wrong output without an error. shuffle_scale now tags its interleave-branch output the way shuffle_weight does, and the op test checks that both relayouts refuse a GUGU input. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: check all twelve CSV stage-1 kernels on every gfx942 Two kernels of the CSV, t16x128x128_g942_kw2 (inter-384 M = 256 and 512) and t32x128x128_g942_bnt0 (eight large-M rows), were checked only through fused_moe on MI325X, which CI does not have. test_stage1_kernel now runs them too, and main() asserts that every stage-1 kernel in the CSV was run, so a row with a new kernel cannot slip past the MI300X runners. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Revert "FlyDSL a8w4 gfx942: fix test_plain_fp8_swiglu_limit_dispatch for #8178" This reverts commit 7cb2fdc. test_plain_fp8_swiglu_limit_dispatch is not part of this PR; it fails on main since #8178 and #8350 fixes it there. Keeping a partial fix here made the PR fail at a different assertion than main, which hid that the failure is the same one. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Retrigger CI without the ci:mi300x label. MI300X shard 5 fails on main since #8178 (fixed by #8350); this PR's gfx942 test passed on MI300X in run 45177648946. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Retrigger CI without the ci:mi300x label. MI300X shard 5 fails on main since #8178 (fixed by #8350); this PR's gfx942 test passed on MI300X in run 45348931160, attempt 2. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: tune the _g942 stage 1 with TUNE_ONLY unset, and fail loudly when the selection leaves it out With TUNE_ONLY unset the tuner asked the flydslv2 generator first; it has no arch check, so it returned tasks for a gfx942 a8w4 row, TUNE_ONLY became "flydslv2", and the a8w4 branch, which only queues the FlyDSL `_g942` stage 1, then queued nothing and reported no error. The default now skips flydslv2 for these rows, and a TUNE_ONLY or OPUS_ONLY that excludes flydsl raises instead of tuning nothing. A CPU test drives tune() with the generators mocked: 0 queued stage-1 tasks before, all `_g942` ones after. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: offer the _qi stage 1 to the tuner only up to 8 tokens A `_qi` stage 1 quantises x itself and refuses pre-quantised x, so the CSV keeps it to M <= 8, but the tuner offered all 52 `_qi` names at every M and a re-tune could again pick one for M = 256, breaking test_csv_qi_rows and the pre-quantised x path there. The limit now lives in moe_kernels.A8W4_GFX942_QI_MAX_TOKEN, used by the tuner and the op test. A CPU test checks that the tuner offers `_qi` at 8 tokens and none at 9. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: refuse a stage-1 tile over the 64 KiB LDS at compile time and keep it out of the tuner At K = 7168 a BM 32, TILE_K 256, k_wave 4 `_qi` tile needs 73728 B of LDS (double-buffered A plus its f32 scales). Nothing checked it: the compiler only printed "local memory (73728) exceeds limit (65536)", FlyDSL went on, and the launch failed with hipErrorIllegalState, which says nothing about LDS and can leave the GPU context unusable. a8w4_gfx942_stage1_lds_bytes now sizes the tile, compile_gemm1_a8w4_gfx942 raises a ValueError over the limit, and the tuner skips those tiles. K = 3584 (Kimi-K3) has no such tile. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: refuse a moe_sort stage 1 with TILE_K other than 128 or 256 load_x_scale_moe_sort builds one E8M0 base from y0 // 8 for the whole tile, which holds only while every block of the tile is in the same 8-block group, i.e. TILE_K 128 or 256. The check only asked for a multiple of 128, so a direct call with 384 or 512 built a kernel that read the wrong scale for half the blocks, up to a 256x error, without any message. The registered names use 128 and 256 only; compile now raises for the others, and a CPU test covers it. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: stop a tuner run off MI325X with one up-front error The tuner recognised a gfx942 a8w4 row by the arch alone, while the runtime path also requires a8w4_gfx942_supported(), so on an MI300X every `_g942` candidate raised NotImplementedError on its own and nothing said the shape is MI325X-only. The a8w4 branch now checks the gate once and raises with that message before queuing anything. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: apply swiglu_limit to the silu stage 1, as the a16w4 one does With act="silu" the epilogue called gate_up_act with no SiTU parameters, so a finite swiglu_limit was ignored, while the a16w4 stage 1 clamps gate and up with clamp_gate_up before the unclamped SiLU and runtime_swiglu_limit documents that a positive SiLU limit is applied. The silu path now builds the parameters and clamps the same way (+inf without a limit). test_stage1_kernel takes act and limit and checks silu with limit 1.0 against torch: equal with the change, 84 % of the elements outside the tolerance without it. The shipped rows are SiTUv2 and unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: gate the op test on the running GPU, not on GPU_ARCHS get_gfx() follows GPU_ARCHS and takes its last entry, so with GPU_ARCHS="gfx942;gfx950" the test decided it was on gfx950 and skipped everything on a real MI325X, still exiting 0. It now uses get_gfx_runtime(), as the dispatcher does, for the gate and for the gfx column: with that GPU_ARCHS on MI325X the old test ran 0 stage-1 checks, the new one all 14. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] a8w4 gfx942: make the stage-1 shape checks raise, so python -O keeps them The eleven shape checks in compile_gemm1_a8w4_gfx942 were asserts, which python -O removes; with D_INTER not a multiple of TILE_N, for example, the kernel would then floor NUM_N_BLOCKS and leave the last columns of the intermediate unwritten, and the `_g942` branch returns before any upstream tile check. They now raise ValueError through one helper, with the same conditions, messages and order. A test compiles a bad shape in a python -O subprocess: it raises now and passed silently before. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Drop the gfx942 a8w4 FlyDSL rows for M >= 1024 so main's asm 1-stage rows (#8211) serve those keys again. The main merge kept both rows for the same 12 keys and the first one, ours, won, so test_moe_mxfp4fp8_gfx942_asm.py failed on MI300X. Measured on MI325X in one process, eager wall clock, A/B/B/A pairs, null control within 0.9%: asm is 20-33% faster on all 12 keys and won every pair. The FlyDSL rows for M = 1..512, where main has no gfx942 row, stay. The untuned CSV drops the same tokens so a re-tune reproduces the tuned CSV, and the op test runs fused_moe only at M served by FlyDSL rows (1..512); the stage-1 kernel checks are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Justify the MI325X-only a8w4 gate with the MI300X numbers at the M the rows still serve, now that #8211 serves M >= 1024. The gate docstring, the NotImplementedError and the op test's skip message cited the 0.81-0.89x slowdown at M >= 1024, where the _g942 stage 1 no longer runs. On MI300X a8w4 is also slower than a16w4 at M = 128-512 (0.92-0.99x at inter 384), which is what the texts now give. The "MI325X only" substring the tests match is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Drop test_a8w4_gfx942_tune_sources.py: no CI job runs it, and review suggested removing tests that only serve local validation. It held the unit tests of the review-round-3 tuner fixes; they passed at bd616e3 and d9ab86b (10 tests), and the result is posted in the review thread. The op test test_flydsl_moe_a8w4_gfx942.py stays, as the only numeric check of the _g942 stage-1 kernels on the MI300X CI runners. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Vedenev <[email protected]>


The PR is an extension of PR #4443 by @yuychang
We adapt the same way to have the same optimization on model Qwen3.5-397B-A17B-MXFP4-AttnFP8-V2.
… gfx950 decode
Add mxfp4_moe_sort_quant_fwd: a single-launch HIP kernel that fuses opus route sorting, BF16->MXFP4 activation quantization (v_cvt_scalef32_pk_fp4_bf16) and routed-output zeroing for the Qwen3.5 MXFP4 MoE decode path (model_dim=4096, block_m=32, M in [1,128], routes (num_experts,topk) in {(512,10),(513,11)}).
CTA 0 builds the sorted metadata via an LDS expert histogram + DPP wave prefix-scan; the remaining CTAs emit compact per-token E8M0 scales that the flydsl GEMM1 consumes directly. Gated behind SGLANG_AITER_FUSED_DECODE_SORT_QUANT and fed into fused_moe_2stages as prequantized_a1 on the expanded-scale path.
Adds an op_test (correctness vs opus + generic quant, decode-M gate, runtime-arch gate, CUDA-graph replay) and a sort+quant latency benchmark.
Motivation
Technical Details
Test Plan
Test Result
Submission Checklist