[Bug] gfx1250 WMMAScale FP4: wrong A/B register layout breaks MXFP4 GEMM with non-uniform scales
Environment details:
Docker image: rocm/pytorch:rocm10.0_ubuntu26.04_py3.14_pytorch_release_2.13.0 flydsl: 0.4.0 (also tested on 0.3.4.1, 0.3.4, 0.3.2, 0.3.1, 0.3.0) OS: Ubuntu 26.04 LTS (Resolute Raccoon) amdgpu driver : 6.19.0.31300009 GPU : AMD Radeon Graphics, gfx1250 (DEVICE_ID 0x75c1, REV 0x00) ROCM_PATH=/opt/venv/lib/python3.14/site-packages/_rocm_sdk_core/lib
Summary:
On gfx1250, fx.rocdl.WMMAScale(16, 16, 128, fx.Float4E2M1FN) reports an A/B register layout that doesn't match the CDNA5 FP4 layout, in which lanes 0–15 hold K 0–31 and 64–95 and lanes 16–31 hold K 32–63 and 96–127. A and B are permuted identically, so unscaled products are unaffected. But V_WMMA_SCALE applies E8M0 scale byte j to the registers the ISA assigns to K block j, so half of the FP4 values are multiplied by another 32-K block's scale. As a result, an MXFP4 GEMM written with make_tiled_mma / make_fragment_A/B / make_tiled_copy_A/B / fx.gemm(..., [frag, scale], ...) is wrong whenever the block scales vary within a 128-K step, and correct with uniform scales. Loading A/B through a view that reorders K into the ISA order fixes it. The cause appears to be gfx1250::getThrValLayoutAB (lib/Dialect/FlyROCDL/GFX1250/MmaAtom.cpp), which has no 4-bit case, so FP4 takes the sub-32-bit path documented for f16/bf16/fp8/bf8/i8.
Reproducer:
FlyDSL layout, uniform scales: max relative error = 0.00e+00 OK FlyDSL layout, varying scales: max relative error = 9.85e-01 WRONG ISA-order view, varying scales: max relative error = 2.05e-07 OK
Reproducer script: wmmascale_fp4_repro.py
Hello, I would like to work on this.
From my understanding of the issue, FlyDSL puts FP4 values in different register slots than where the hardware expects them. The hardware picks each value's scale based on which slot it's in, so the misplaced values end up with their neighbor block's scale. Without scales (or with the same scale everywhere) you would never notice, because A and B get misplaced the same way and the addition does not care about the order.
This matters because different scales per block is the normal case, so any MXFP4 matmul built through FlyDSL's standard API gives wrong answers, and nothing crashes or warns about the error.
My suggested plan is to correct the FP4 layout in getThrValLayoutAB so it matches the hardware. I checked the CDNA5 ISA (section 7.12.6.1, block-scale matrix data layout), and FP6 uses the same 32-wide K layout as FP4, so I will check FP6 as well.
For testing: I do not have gfx1250 hardware (my AMD card is RDNA 4, gfx1201, which does not have this instruction), so my plan is:
- Unit test the layout itself, no GPU needed: check every lane/slot against the ISA layout, and make sure int4 and FP8 didn't change.
- Compile-only build for gfx1250 and run the repro through the full pipeline.
- A small Python model of the instruction on CPU to show the old layout breaks and the new one doesn't.
- Hardware check on real gfx1250.
@aris134 would you be willing to run your repro on my branch once it's up?
Hello, I would like to work on this.
From my understanding of the issue, FlyDSL puts FP4 values in different register slots than where the hardware expects them. The hardware picks each value's scale based on which slot it's in, so the misplaced values end up with their neighbor block's scale. Without scales (or with the same scale everywhere) you would never notice, because A and B get misplaced the same way and the addition does not care about the order.
This matters because different scales per block is the normal case, so any MXFP4 matmul built through FlyDSL's standard API gives wrong answers, and nothing crashes or warns about the error.
My suggested plan is to correct the FP4 layout in
getThrValLayoutABso it matches the hardware. I checked the CDNA5 ISA (section 7.12.6.1, block-scale matrix data layout), and FP6 uses the same 32-wide K layout as FP4, so I will check FP6 as well.For testing: I do not have gfx1250 hardware (my AMD card is RDNA 4, gfx1201, which does not have this instruction), so my plan is:
- Unit test the layout itself, no GPU needed: check every lane/slot against the ISA layout, and make sure int4 and FP8 didn't change.
- Compile-only build for gfx1250 and run the repro through the full pipeline.
- A small Python model of the instruction on CPU to show the old layout breaks and the new one doesn't.
- Hardware check on real gfx1250.
@aris134 would you be willing to run your repro on my branch once it's up?
Hi @edjson , sure would be happy to rerun it when it's ready.