Repository navigation
[FlyDSL] [CI] [a16w4] Support MXFP4 weights on gfx942 (CDNA3) - #5583
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
7c50006 to
35c5120
Compare
`make_b_loader`'s `upconvert_b` feeds MXFP4 weights through
`v_cvt_scalef32_pk_bf16_fp4`, which is CDNA4-only, so on gfx942 the kernel does not
assemble at all -- 1792 occurrences of
error: instruction not supported on this GPU (gfx942): v_cvt_scalef32_pk_bf16_fp4
and it is the only unsupported instruction in the kernel. Everything else is already
arch-gated and fine on CDNA3: the K=16 MFMA path, the int4 dequant one line above the
fp4 branch, the `dma_via_vgpr` A-tile staging. CDNA3 has no fp4 MFMA either, so there
the weights genuinely have to be decoded to bf16 before the MFMA rather than fed in
directly as on CDNA4.
`_fp4_nibble_to_bf16x8` is the CDNA3 counterpart of the existing
`_int4_nibble_to_bf16x8` in the same file. It is a v_perm_b32 byte lookup rather than
per-element arithmetic, because for E2M1 it can be: every magnitude is exactly
representable in bf16, so the 16-entry table is a byte permute over two constant pools
selected by `nibble & 7`; the sign is the nibble's bit 3 shifted into bf16 bit 15; and
the E8M0 scale is a power of two, so applying it keeps the result exactly representable
and the f32 -> bf16 step is a truncation. E2M1 has no NaN encoding, so no NaN handling
is needed either.
`upconvert_b` selects it under an `is_gfx942` flag, following the
`is_gfx942 = str(gpu_arch).startswith("gfx942")` / `const_expr(is_gfx942)` idiom already
used in `preshuffle_gemm.py`. The flag is computed inside `make_b_loader` because, as
that factory's docstring notes, FlyDSL hashes its source for the cache key and not
nested helpers.
The arch comes in as a new `rocm_arch` parameter rather than being derived from
`use_k16`. `use_k16` means "K=16 MFMA", which is only correlated with CDNA3 -- forcing
the K=16 path on a gfx950 box to validate it must not also switch the weight decode.
`rocm_arch` is threaded explicitly alongside `use_k16` (compile_gemm{1,2}_a16w4_port ->
the local `_use_k16` alias -> _gemm{1,2}_body_a16w4 -> make_b_loader) rather than
resolved inside the builder, so `compile_gemm{1,2}_a16w4_port`'s functools.cache stays
keyed on it and two targets cannot share a cached kernel. It has no default at the
compile entry point on purpose: a silently-wrong arch produces a kernel that does not
assemble, so every caller is updated -- `moe_2stage_a16wmix/__init__.py` and
`ops/flydsl/moe_kernels.py`.
No change on gfx950: the new branch is `const_expr`, so there is no runtime check and
the emitted gfx950 ISA is unchanged.
Verified on gfx942 (MI308X, ROCm 7.14, flydsl 0.3.2):
- `op_tests/flydsl_tests/test_flydsl_moe_a16wfp4.py`: 12 failed before, 12 passed
after. That is every case of this repo's own a16w4 SiTUv2 e2e test -- model_dim x
inter_dim in {3584x512, 3584x384}, tokens in {1, 16, 128}, both the plain and the
Kimi-K3 production situ_beta/situ_linear_beta pairs -- against a bf16 SiTUv2 torch
reference with a strict cosine gate. No test changes: the file is already gated for
get_gfx() in ("gfx942", "gfx950").
- Assembly of compile_gemm1_a16w4_port for 3584x512 E896 topk16: 1792 unsupported
instructions before, 0 after. With ARCH=gfx950, 0 both before and after.
- Cross-checked through ROCm/FlyDSL's tests/kernels/test_moe_gemm.py, with the decode
block lifted verbatim from here (3919 bytes, sha256 prefix 8cda818e15177887):
5 passed, against 3 failed / 2 passed with only the kernel change reverted -- the
three failures being exactly the fp4 cases and the two passes the a16w16
bf16-weight variants, which never reach the decode.
35c5120 to
963b6d2
Compare
|
python3 -m pytest op_tests/flydsl_tests/test_flydsl_moe_a16wfp4.py -v Test result: w/o this patch w/ this patch |
|
Advisory review (static + hand-run; not a merge gate). Validation/Perf ran no GPU stage — reasons are on their lines below. Findings tagged Adds a gfx942 (CDNA3) weight-decode path to the FlyDSL a16wmix MoE kernels: MXFP4 (E2M1) weights are decoded to bf16 through v_perm_b32 byte-lookup constant pools with the E8M0 group scale applied in f32, replacing the CDNA4-only v_cvt_scalef32_pk_bf16_fp4 upconvert that made the kernel impossible to assemble on gfx942. Review (advisory):
|
Review follow-up on the gfx942 MXFP4 weight-decode path.
Arch gating: the a16wmix kernels took both use_k16 and rocm_arch, which encode
the same bit. They now take rocm_arch alone and derive what they need from it.
The derived checks are positive (startswith gfx942) rather than "not gfx950".
use_k16 was really four unrelated gfx942 workarounds sharing one name -- the
16x16x16 MFMA split, the lshr-16 bf16 pack in _int4_nibble_to_bf16x8 (no
v_cvt_pk_bf16_f32), the global-packed atomic epilogue (no
buffer_atomic_pk_add_bf16), and the dma_via_vgpr LDS staging -- and only the
first has anything to do with the MFMA K width. "gfx95" not in arch is true
for every arch that is not gfx950, so a future CDNA would silently inherit all
four; a positive test sends it down the gfx950 path, where the missing CDNA4 op
is a build error instead. FlyDSL has no gfx940/gfx941 support, so nothing that
can reach this kernel is excluded by the narrower test.
Verified by counting intrinsics in the emitted IR for the kimik3 gemm1: gfx942
emits mfma<16x16x16> x1793 and llvm.amdgcn.perm x5376 with zero
cvt_scalef32_pk_bf16_fp4; gfx950 emits mfma<16x16x32> x225 and
cvt_scalef32_pk_bf16_fp4 x448 with zero perm. All three w_dtype values
(fp4/int4/bf16) still build on both archs.
Test placement: op_tests/flydsl_tests/ is collected by no CI job
(.github/scripts/split_tests.sh takes op_tests at -maxdepth 1, and its
FILE_TIMES has no entry under that directory), so the a16w4 coverage never ran
in CI. Moved to the first level, which is where the other 17 flydsl tests live;
the move itself needed no edits (no conftest dependency, only absolute
imports). FILE_TIMES gains its measured time as well, so the shard splitter
weights it properly instead of falling back to its 15 s default.
Benchmark: this path had no benchmark entry point --
op_tests/op_benchmarks/triton/bench_moe_gemm_a16w4.py times the Triton
moe_gemm_a16w4, and the mxfp4 tuner in gemm_moe_tune.py drives the a4w4/a8w4
flydsl_mxmoe_g* families, so the cost of the path this enables could not be
measured with the repo's own tools. Adds
op_tests/op_benchmarks/flydsl/bench_moe_a16wmix_2stage.py, which drives the two
launchers production reaches after config resolution (_flydsl_stage1_wrapper /
_flydsl_stage2_wrapper), so the kernel name a row reports is the kernel that
was timed. Candidates come from the production registry rather than hand-built
strings, TFLOPS/BW reuse gemm_moe_tune.py's fused-MoE formulas so the numbers
are comparable with the existing rows, and every candidate is checked against a
torch reference before it is timed -- a config that drops tiles is fast and
wrong and would otherwise win a sweep. --sweep enumerates every registered
candidate and emits tuned-CSV rows; stage1 and stage2 are swept independently
because they are separate back-to-back launches with separate timings.
Tuned configs: kimik3_a16w4_tuned_fmoe.csv carried gfx950,256 only. Adds
gfx942,80 (MI308X) and gfx942,304 (MI300X), 16 tokens x inter_dim {384, 512}
each, keeping the file's one-row-per-(inter_dim, token) convention with the
winning block_m in the block_m column. Every candidate was checked against a
torch reference before being timed, and get_2stage_cfgs was then asked to
confirm each row is the config it actually dispatches: 30/30 reachable rows on
both boxes (the token=3 rows are unreachable because get_padded_M(3) == 4, as
they already are for gfx950).
Unlike gfx950, both gfx942 parts prefer block_m=16 up to token 512 at
inter_dim=512; at inter_dim=384 all three archs agree.
* [FlyDSL] [CI] [a16w4] Support MXFP4 weights on gfx942 (CDNA3) (ROCm#5583) * [a16w4] Support MXFP4 weights on gfx942 (CDNA3) `make_b_loader`'s `upconvert_b` feeds MXFP4 weights through `v_cvt_scalef32_pk_bf16_fp4`, which is CDNA4-only, so on gfx942 the kernel does not assemble at all * support a16w4 moe-silu(from Mimov2.6) on gfx942 * add a8w4(ptpc) support on gfx942 * improve a16w4 gemm2 perf by relaxing LDS requirement * mv per-channel-max into gemm2 for accurate tunning * tune mimo v2.6 flash for MI300X --------- Co-authored-by: MHYangAMD <[email protected]> Co-authored-by: Li,Tingqian <tingqli.amd.com>
Motivation
moe_2stage_a16wmixcannot run with MXFP4 weights on gfx942 (CDNA3) at all — it does noteven assemble.
make_b_loader'supconvert_bupconverts the weights withv_cvt_scalef32_pk_bf16_fp4, which is CDNA4-only:1792 occurrences, and it is the only unsupported instruction in the kernel. Everything
else in the pipeline is already arch-gated and fine on CDNA3 — the
use_k16K=16 MFMApath, the int4 dequant one line above the fp4 branch, the
dma_via_vgprA-tile staging.So the weight upconvert is the single thing standing between this kernel and CDNA3.
This is architectural rather than SKU-specific: the instruction is absent from all of
CDNA3, so every gfx942 part is affected (MI300X, MI300A, MI308X, MI325X). CDNA3 also has
no fp4 MFMA, so the weights genuinely have to be decoded to bf16 before the MFMA rather
than fed in directly as on CDNA4.
The goal is to make the existing a16w4 MXFP4 path work on gfx942, with the gfx950 path
bit-unchanged.
op_tests/flydsl_tests/test_flydsl_moe_a16wfp4.pyis already gated forget_gfx() in ("gfx942", "gfx950"), so no test gating changes here — that test issupposed to run on CDNA3 already and only fails because the kernel will not build.
Technical Details
_fp4_nibble_to_bf16x8inmoe_2stage_a16wmix/utils.pyis the CDNA3 counterpart of the_int4_nibble_to_bf16x8already in that file, andupconvert_bselects it the same wayit selects the int4 one. It is a
v_perm_b32byte lookup rather than per-elementarithmetic, because for E2M1 it can be:
a byte permute over two constant pools — one for the bf16 high bytes, one for the low
bytes — selected by
nibble & 7.representable and the f32 -> bf16 step is a truncation, not a round-to-nearest.
The result is no reconstruction through f32, no compares or selects for the subnormal
cases, and no full
cvt.Arch gating. The selection is an
is_gfx942flag, following theis_gfx942 = str(gpu_arch).startswith("gfx942")/const_expr(is_gfx942)idiom alreadyused in
preshuffle_gemm.py. It is computed insidemake_b_loaderbecause, as thatfactory's docstring notes, FlyDSL hashes its source for the cache key and not nested
helpers.
The arch arrives as a new
rocm_archparameter rather than being derived fromuse_k16.use_k16means "K=16 MFMA", which is only correlated with CDNA3 — forcing the K=16 pathon a gfx950 box to validate it must not also switch the weight decode.
rocm_archisthreaded explicitly alongside
use_k16(compile_gemm{1,2}_a16w4_port-> the local_use_k16alias ->_gemm{1,2}_body_a16w4->make_b_loader) rather than resolvedinside the builder, so
compile_gemm{1,2}_a16w4_port'sfunctools.cachestays keyed onit and two targets cannot share a cached kernel. It has no default at the compile entry
point on purpose — a silently-wrong arch produces a kernel that does not assemble — so
both callers are updated:
moe_2stage_a16wmix/__init__.pyandops/flydsl/moe_kernels.py.gfx950 is unaffected: the new branch is
const_expr, so there is no runtime check and theemitted gfx950 ISA is unchanged.
+100 / -0across 5 files, no deletions.Related: ROCm/FlyDSL#424 and
#431 introduced these kernels as gfx950 work;
vllm-project/vllm#50319 asked for
exactly this gfx942 MXFP4-bf16 MoE capability and had to route around its absence.
Test Plan
Environment: gfx942 (MI308X), ROCm 7.14, flydsl 0.3.2.
compile_gemm1_a16w4_portfor the Kimi-K33584x512 E896 topk16geometry andcount unsupported instructions, before and after, natively on gfx942 and with
ARCH=gfx950to confirm the CDNA4 path is untouched.module_aiter_core.sopredates aiter mainand raises
ActivationType has no attribute 'GeluTanh'inaiter/ops/quant.py,op_tests/flydsl_tests/test_flydsl_moe_a16wfp4.pycould not be run here. The decodewas instead validated through
ROCm/FlyDSL'stests/kernels/test_moe_gemm.py(
test_a16w4_moe_e2e, strict cosine gate against a torch reference), with the decodeblock lifted verbatim from this change — byte-identical, 3919 bytes, sha256 prefix
8cda818e15177887.are not passing vacuously.
Test Result
ARCH=gfx950assembly, unsupported instructionstest_a16w4_moe_e2e(a16w4 selection), gfx942The
beforecolumn of row 3 is the load-bearing one: the three failures are exactly thefp4 cases (
small-a16w4,kimi512-a16w4,native_layout[small]), while the two thatstill pass are the
a16w16bf16-weight variants, which never reach the decode. So thegate is neither vacuous nor over-broad.
Not claimed here: no gfx950 measurements or behavioural claims — this box is CDNA3 only.
Since the new path sits behind
const_expr(is_gfx942)and the gfx950 ISA is unchanged,gfx950 behaviour cannot move, but a gfx950 CI run to confirm that would be welcome. The
aiter-side e2e test is also still unrun for the environment reason above; it needs a
container whose
module_aiter_corematches aiter main.Out of scope, happy to file separately:
test_flydsl_moe_a16wfp4.pycoversinter_dim512 and 384, but FlyDSL'stest_moe_gemm.pyrequiresinter_dim % 256 == 0,so the two suites do not cover the same shapes for this kernel.
Submission Checklist