Skip to content

[HIP] [Bugfix] mxfp4_moe_sort: keep the last row's tail scales when model_dim / 32 is not a multiple of 4 - #7653

Merged
zufayu merged 2 commits into
ROCm:mainfrom
jiangyon-amd:fix-mxfp4-moe-sort-tail
Nov 12, 2026
Merged

zufayu merged 2 commits into
ROCm:mainfrom
jiangyon-amd:fix-mxfp4-moe-sort-tail

Conversation

@jiangyon-amd

@jiangyon-amd jiangyon-amd commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #7646. When model_dim / 32 is not a multiple of 4, mxfp4_moe_sort_hip / mxfp4_moe_sort_fwd return 0 for the final scale bytes of the last scale row. K=2880 (gpt-oss hidden size) is one such case. fused_dynamic_mxfp4_quant_moe_sort is affected through its split path. This change reads that one load byte by byte. Every other shape compiles to the same code as before.

Motivation

mxfp4_moe_sort_kernel reads each scale row in 4- to 32-byte loads from a buffer that ends exactly at the end of the scales. The last row's final load runs past that end, and the buffer range check is per dword. When the row is not a whole number of dwords, the dword holding the row's last in-range bytes therefore comes back as 0. Earlier rows read into the next row instead, which is in range, and the extra bytes are masked by col < scaleN_valid.

The visible result: the last token gets e8m0 0 (2^-127) for its last 64 input channels in every route. With the repro in #7646 at K=2880, 16 bytes are wrong for any M (2 columns × 8 routes), and all of them are 0. In a prefill batch that token is the prompt's last one, which produces the first output token.

Changes

  • csrc/kernels/quant_kernels.cu:
    • mxfp4_moe_sort_kernel gets a byte_tail template parameter. With it set, a load that would cross the end of the scales is issued as byte loads.
    • The launcher sets byte_tail only when a row ends inside one of the kernel's loads: ceil(cols / 32) % load_bytes != 0, where load_bytes is the 2 to 16 bytes per load of that dispatch bucket. On gfx950 only rows that are not a whole number of dwords lose bytes. A whole-dword row that ends inside an 8- or 16-byte load (e.g. model_dim 1152) would also lose bytes if the range check were per access, so it takes the byte tail too. Every other shape keeps the old code unchanged.
    • Both store paths (the LDS-staged coalesced one and the scatter one) go through the same load.
  • op_tests/test_moe_sorting_mxfp4.py: a new asserting section, "Row tails of the scale sort". See Changed op tests.

Performance (if applicable)

mxfp4_moe_sort_hip alone on MI350X (gfx950), CUDA graph (50 launches per replay, median of 20), E=128, topk=8, block_m=32. Before/after runs were alternated twice on one GPU, and each time is the mean of the two runs.

Hardware Workload/configuration Before After Change
MI350X model_dim 4096, M=4096, stage 1 4.35 µs 4.36 µs 1.00× (unchanged code)
MI350X model_dim 7168, M=16384, stage 1 34.30 µs 34.32 µs 1.00× (unchanged code)
MI350X model_dim 2048, M=16384, stage 1 6.68 µs 6.66 µs 1.00× (unchanged code)
MI350X model_dim 2880, M=1, stage 1 2.62 µs (wrong) 2.82 µs +7.8%
MI350X model_dim 2880, M=16384, stage 1 8.87 µs (wrong) 9.43 µs +6.3%
MI350X model_dim 2880, M=4096, stage 2 4.12 µs (wrong) 4.43 µs +7.5%
MI350X model_dim 1152, M=16384, stage 1 5.88 µs 6.16 µs +4.8% (whole-dword row, ends inside an 8-byte load)

Across model_dim ∈ {256, 2048, 4096, 7168} and M ∈ {1, 64, 4096, 16384} at both stages, every ratio is within 0.97–1.02×. Dims that take the byte tail cost 4–9% where they were wrong (2880), and 2–5% for whole-dword rows that take it only as a guard against a per-access range check (1152). A variant that always issues the wide load and then re-reads the tail cost the same, so the simpler form is kept.

Testing

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

Documentation

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

This is a bug fix inside a kernel with no API change.

Dependencies

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

Breaking Changes

None.

PR Self-Review

Reviewed revision

c478dfb

Behavior and intent

When model_dim / 32 is not a multiple of 4, the last route's final scale bytes are now the input's bytes instead of 0. Every other shape runs the same code as before. The existing behavior is wrong because the per-dword buffer range check zeroes a dword that straddles the end of the scales, taking its in-range bytes with it.

Validation and unverified scope

On MI350X (gfx950), ROCm 7.2.4, this branch:

  • python3 op_tests/test_moe_sorting_mxfp4.py with default arguments exits 0. All 61 new row-tail checks pass, and no existing check or comparison reports a mismatch. On main the new section fails on its first case: model_dim=320 tokens=1 stage1: 16 scale bytes differ.
  • The repro from mxfp4_moe_sort_fwd zeroes the last token's final scales when K // 32 is not a multiple of 4 (e.g. K=2880) #7646 reports 0 wrong bytes for mxfp4_moe_sort_fwd and fused_dynamic_mxfp4_quant_moe_sort at M = 5, 300 and 4096. Before this change both were 16 wrong bytes at M = 300 and 4096, and mxfp4_moe_sort_fwd also at M = 5.
  • The timings above, with before and after runs alternated on one GPU.
  • black and ruff==0.16.0 are clean.

Unverified:

  • Not run on gfx942 or gfx1250. The kernel is arch-generic HIP and byte loads exist on all of them, but I have only run it on gfx950.
  • MXFP8 scales go through the same byte sort. They are covered by the dtype-agnostic sort test but not by a separate fp8 quant-sort run at these dims.

PR-specific question

The changed load is in mxfp4_moe_sort_kernel, which sorts e8m0 scale bytes for both MXFP4 and MXFP8. It is reached through mxfp4_moe_sort_hip, mxfp4_moe_sort_fwd and the split path of fused_dynamic_mx{fp4,fp8}_quant_moe_sort, on any arch that builds module_quant.

The byte-tail variant is selected on the host from the shape alone: ceil(cols / 32) % mxfp4_moe_sort_load_bytes(THREAD_DATA) != 0, i.e. a row ends inside a load. Within that variant, a load takes the byte path only when offset + vec_size > scale_per_row * num_tokens * topk, i.e. the final load of the last row. Rows that end on a load boundary cannot have a load crossing the end of the buffer, so they are launched as the unchanged template instance (byte_tail = false). Unsupported widths (cols > 16384) are still rejected by the existing dispatch check before any launch.

Changed op tests

op_tests/test_moe_sorting_mxfp4.py: the new section "Row tails of the scale sort" calls mxfp4_moe_sort_hip into a zeroed buffer with nonzero input scale bytes and asserts the whole output equals run_torch.

  • It covers model_dim ∈ {96, 224, 320, 800, 1152, 1888, 2880, 5824, 7232, 12352}. These are rows 1–3 bytes past a dword in every dispatch bucket and load width, a row shorter than one load (96), and a whole-dword row that ends inside an 8-byte load (1152). The 1152 case passes on main on gfx950, since its range check is per dword; it guards the byte-tail selection rather than reproducing mxfp4_moe_sort_fwd zeroes the last token's final scales when K // 32 is not a multiple of 4 (e.g. K=2880) #7646.
  • It covers stages 1 and 2 and tokens ∈ {1, 5, 300}.
  • One 30000-token case reaches the scatter store of the 32-row buckets.

On main it fails with the last row's tail bytes read as 0. The existing sections do not catch this for two reasons: their default dims (4096, 7168, 256, 2048) all have model_dim / 32 % 4 == 0, and test_moe_mxfp4_sort / test_moe_mx_quant_sort compare with checkAllclose, which only logs.

🤖 Generated with Claude Code

mxfp4_moe_sort_kernel reads each scale row in 4- to 32-byte loads from a
buffer bounded at the end of the scales. The buffer range check is per
dword, so when a row is not a whole number of dwords (model_dim / 32 not a
multiple of 4, e.g. 2880) the last row's final load read the dword holding
its last in-range bytes as 0, and the last route lost its final scales.
fused_dynamic_mxfp4_quant_moe_sort hits it through its split path.

Such rows now read that one load byte by byte. The choice is a template
parameter, so the other dims compile to the same code as before.

Fixes ROCm#7646.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7653 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@github-actions github-actions Bot added the HIP label Nov 9, 2026
jiangyon-amd added a commit to jiangyon-amd/aiter that referenced this pull request Nov 9, 2026
- MoE: stop writing the scale padding columns. As for
  fused_dynamic_mxfp4_quant_moe_sort, only the first K // 32 columns of
  valid-route rows are defined; mxfp4_moe_sort_fwd's scatter store leaves the
  padding columns unwritten, so the two-pass path never matched a "0" claim.
- Merge the no-SLP hint into existing llvm_options instead of replacing them.
- Module docstring shows the keyword API; note that the ROCm#7646 padding goes
  away with ROCm#7653.
- Test: extra block sizes and counted sorts only for M <= 1024 (48 -> 34 s).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jiangyon-amd added a commit to jiangyon-amd/aiter that referenced this pull request Nov 9, 2026
- MoE: stop writing the scale padding columns. As for
  fused_dynamic_mxfp4_quant_moe_sort, only the first K // 32 columns of
  valid-route rows are defined; mxfp4_moe_sort_fwd's scatter store leaves the
  padding columns unwritten, so the two-pass path never matched a "0" claim.
- Merge the no-SLP hint into existing llvm_options instead of replacing them.
- Module docstring shows the keyword API; note that the ROCm#7646 padding goes
  away with ROCm#7653.
- Test: extra block sizes and counted sorts only for M <= 1024 (48 -> 34 s).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aiter-bot

aiter-bot commented Nov 9, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision c478dfbfafb5 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

When `model_dim / 32` is not a multiple of 4, the last route's final scale bytes are now the input's bytes instead of 0. Every other shape runs the same code as before. The existing behavior is wrong because the per-dword buffer range check zeroes a dword that straddles the end of the scales, taking its in-range bytes with it.

Validation and unverified scope

On MI350X (gfx950), ROCm 7.2.4, this branch:
- `python3 op_tests/test_moe_sorting_mxfp4.py` with default arguments exits 0. All 61 new row-tail checks pass, and no existing check or comparison reports a mismatch. On `main` the new section fails on its first case: `model_dim=320 tokens=1 stage1: 16 scale bytes differ`.
- The repro from #7646 reports 0 wrong bytes for `mxfp4_moe_sort_fwd` and `fused_dynamic_mxfp4_quant_moe_sort` at M = 5, 300 and 4096. Before this change both were 16 wrong bytes at M = 300 and 4096, and `mxfp4_moe_sort_fwd` also at M = 5.
- The timings above, with before and after runs alternated on one GPU.
- `black` and `ruff==0.16.0` are clean.

Unverified:
- Not run on gfx942 or gfx1250. The kernel is arch-generic HIP and byte loads exist on all of them, but I have only run it on gfx950.
- MXFP8 scales go through the same byte sort. They are covered by the dtype-agnostic sort test but not by a separate fp8 quant-sort run at these dims.

PR-specific question

The changed load is in `mxfp4_moe_sort_kernel`, which sorts e8m0 scale bytes for both MXFP4 and MXFP8. It is reached through `mxfp4_moe_sort_hip`, `mxfp4_moe_sort_fwd` and the split path of `fused_dynamic_mx{fp4,fp8}_quant_moe_sort`, on any arch that builds `module_quant`.

The byte-tail variant is selected on the host from the shape alone: `ceil(cols / 32) % mxfp4_moe_sort_load_bytes(THREAD_DATA) != 0`, i.e. a row ends inside a load. Within that variant, a load takes the byte path only when `offset + vec_size > scale_per_row * num_tokens * topk`, i.e. the final load of the last row. Rows that end on a load boundary cannot have a load crossing the end of the buffer, so they are launched as the unchanged template instance (`byte_tail = false`). Unsupported widths (`cols > 16384`) are still rejected by the existing dispatch check before any launch.

Changed op tests

`op_tests/test_moe_sorting_mxfp4.py`: the new section "Row tails of the scale sort" calls `mxfp4_moe_sort_hip` into a zeroed buffer with nonzero input scale bytes and asserts the whole output equals `run_torch`.
- It covers model_dim ∈ {96, 224, 320, 800, 1152, 1888, 2880, 5824, 7232, 12352}. These are rows 1–3 bytes past a dword in every dispatch bucket and load width, a row shorter than one load (96), and a whole-dword row that ends inside an 8-byte load (1152). The 1152 case passes on `main` on gfx950, since its range check is per dword; it guards the byte-tail selection rather than reproducing #7646.
- It covers stages 1 and 2 and tokens ∈ {1, 5, 300}.
- One 30000-token case reaches the scatter store of the 32-row buckets.

On `main` it fails with the last row's tail bytes read as 0. The existing sections do not catch this for two reasons: their default dims (4096, 7168, 256, 2048) all have `model_dim / 32 % 4 == 0`, and `test_moe_mxfp4_sort` / `test_moe_mx_quant_sort` compare with `checkAllclose`, which only logs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

… load

Review follow-up. Select byte_tail when the last load of a row crosses the
row end (ceil(cols / 32) % load bytes), not only when the row is not whole
dwords: on gfx950 the range check is per dword, but a per-access check would
also drop the in-range part of an 8- or 16-byte load (e.g. model_dim 1152).
The load width now comes from one helper shared by the kernel and the
launcher. The test adds model_dim 96 and 224 (the <= 256 bucket) and 1152.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jiangyon-amd
jiangyon-amd force-pushed the fix-mxfp4-moe-sort-tail branch from 2219f5b to c478dfb Compare November 9, 2026 13:57
@jiangyon-amd

Copy link
Copy Markdown
Contributor Author

@zufayu @junhaha666 @rbrugaro-amd CI is green on c478dfb, ready for review. This fixes #7646: mxfp4_moe_sort dropped the last token's tail scales when model_dim / 32 is not a multiple of 4 (e.g. gpt-oss, K=2880). Shapes that are not affected compile to the same code as before; timings are in the description.

@zufayu
zufayu merged commit 1dfc749 into ROCm:main Nov 12, 2026
vpietila-amd pushed a commit that referenced this pull request Nov 12, 2026
…odel_dim / 32 is not a multiple of 4 (#7653)

* [HIP] [Bugfix] mxfp4_moe_sort: keep the last row's tail scales

mxfp4_moe_sort_kernel reads each scale row in 4- to 32-byte loads from a
buffer bounded at the end of the scales. The buffer range check is per
dword, so when a row is not a whole number of dwords (model_dim / 32 not a
multiple of 4, e.g. 2880) the last row's final load read the dword holding
its last in-range bytes as 0, and the last route lost its final scales.
fused_dynamic_mxfp4_quant_moe_sort hits it through its split path.

Such rows now read that one load byte by byte. The choice is a template
parameter, so the other dims compile to the same code as before.

Fixes #7646.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [HIP] mxfp4_moe_sort: take the byte tail whenever a row ends inside a load

Review follow-up. Select byte_tail when the last load of a row crosses the
row end (ceil(cols / 32) % load bytes), not only when the row is not whole
dwords: on gfx950 the range check is per dword, but a per-access check would
also drop the in-range part of an 8- or 16-byte load (e.g. model_dim 1152).
The load width now comes from one helper shared by the kernel and the
launcher. The test adds model_dim 96 and 224 (the <= 256 bucket) and 1152.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
jiangyon-amd added a commit to jiangyon-amd/aiter that referenced this pull request Nov 14, 2026
- MoE: stop writing the scale padding columns. As for
  fused_dynamic_mxfp4_quant_moe_sort, only the first K // 32 columns of
  valid-route rows are defined; mxfp4_moe_sort_fwd's scatter store leaves the
  padding columns unwritten, so the two-pass path never matched a "0" claim.
- Merge the no-SLP hint into existing llvm_options instead of replacing them.
- Module docstring shows the keyword API; note that the ROCm#7646 padding goes
  away with ROCm#7653.
- Test: extra block sizes and counted sorts only for M <= 1024 (48 -> 34 s).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jiangyon-amd added a commit to jiangyon-amd/aiter that referenced this pull request Nov 14, 2026
…in main

mxfp4_moe_sort_fwd keeps the last row's tail scales since ROCm#7653, so the
MoE two-pass path no longer pads the scale rows, and the 2^24 threshold
that compensated for the padding goes too. With the fixed sort the
padded-K dims cross over where the others do: one 2^21 switch for every K
is within 5% of the faster path on 567 of 600 measured points (MI350X,
K 2048-7168, every rotation size, topk 4 and 8), 0.9% off on average.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
yadaish pushed a commit that referenced this pull request Nov 17, 2026
…e and MoE stage-1 inputs (flydsl_rot_quant) (#4549)

* [FlyDSL] Fused online Hadamard rotation + MXFP4 quantization

Add aiter.ops.flydsl.flydsl_rot_quant(x, RS, shuffle_scales=): one gfx950
kernel that rotates each RS-wide block of a bf16 activation by a normalized
Sylvester Hadamard matrix (RS in {32, 64, 128}), quantizes the result to
MXFP4 with the default RoundUp e8m0 scale, and optionally writes the scales
in the CK layout that gemm_a4w4 consumes. The output equals
per_1x32_f4_quant applied to the bf16-rounded rotated tensor.

Rotated MXFP4 checkpoints fold the rotation into the weights, so the
activation of every rotated projection needs it online. Unfused, that is a
Hadamard matmul followed by a quant pass. The HIP per-1x32 quant fuses a
32-point rotation since #6734; this op adds 64 and 128 in the same single
pass and agrees with the HIP path byte for byte at 32, apart from groups
under its amax floor.

One thread owns one RS-wide block, so the FWHT runs in registers without
cross-lane traffic. At RS=64 the 1/8 normalization is applied to each
group's amax and to the conversion exponent instead of to every element.

MI350X, K=4096, RS=64, CK-layout scales, CUDA graph: 1.8x (M=32) to 2.9x
(M>=1024) over a bf16 Hadamard matmul plus per_1x32_f4_quant_hip.

op_tests/test_flydsl_rot_quant.py checks the fp4 bytes and e8m0 scales
exactly against per_1x32_f4_quant (RS 32/64/128, both scale layouts, M
1..16384, K 2880/4096/7168, zero, tiny and NaN rows), parity with the HIP
path at RS=32, and the CK scale layout end to end through gemm_a4w4.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [FlyDSL] rot_quant: one MXFP4 group per lane

Split each RS-wide block across RS/32 adjacent lanes. Every lane runs the
first five FWHT stages on its 32 values in registers, and the lanes finish
the remaining log2(RS/32) stages with xor shuffles in the same stage order,
so the output is unchanged byte for byte. A lane now owns exactly one MXFP4
group: its scale stays thread-local, VGPRs drop from 101/228 to 60 at
RS=64/128, and small M spreads over 2-4x more lanes.

MI350X, K=4096, CK-layout scales, CUDA graph: RS=64 is 1.13-1.26x and
RS=128 1.15-1.77x faster than the previous revision; RS=32 is unchanged.
CTA size and the SLP setting were re-swept for the new mapping and kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [FlyDSL] rot_quant: MoE stage-1 input with sorted scales

Add flydsl_rot_quant_moe_sort(x, sorted_ids, num_valid_ids, topk,
block_size, RS): the rotated MXFP4 quantization of the MoE stage-1 input
with its scales already sorted for the MoE GEMMs, i.e.
fused_dynamic_mxfp4_quant_moe_sort applied to the rotated tensor (without
that op's amax floor). Without it a rotated MoE layer runs a rotation pass
before the quant-and-sort kernel.

For small batches one kernel takes a thread per (sorted row, group):
valid routes rotate and quantize their token's group and scatter the scale
to the sorted row in the mxfp4_moe_sort_fwd layout, padding rows exit.
1024-thread CTAs keep the cost of the exiting threads down. Because a
token is quantized once per route, past M * topk * K = 2^21 the op instead
runs flydsl_rot_quant once and mxfp4_moe_sort_fwd. mxfp4_moe_sort_fwd loses
the last token's final scales when K // 32 is not a multiple of 4, so
those K always take the fused kernel.

MI350X, E=128, topk=8, CUDA graph: 1.3-2.8x faster than a Hadamard matmul
followed by fused_dynamic_mxfp4_quant_moe_sort; at K=2048 within ~0.5 us
of the unrotated quant-and-sort.

The op test gains test_rot_quant_moe_sort: fp4 bytes and sorted scales
compared exactly with the torch reference for K 2880/4096/7168, RS
32/64/128 and M 1..16384.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [FlyDSL] rot_quant: pre-review fixes

- flydsl_rot_quant_moe_sort takes token_num in the position
  fused_dynamic_mxfp4_quant_moe_sort has it and rejects a stage-2 input;
  rotation_size and shuffle (were RS and shuffle_scales) are keyword-only.
- Skip the padding slots of a num_local_tokens sort (topk << 24 | count,
  a real token): M=64 with 40 local tokens 8.6 -> 5.8 us.
- Write 0 to the MoE scale padding columns, as mxfp4_moe_sort_fwd does.
- K // 32 not a multiple of 4 now takes the two-pass path as well, with
  zero-padded scale columns to avoid #7646: K=2880 M=16384 141.6 -> 42.4 us.
- Launch under the input's device.
- Set the no-SLP compile hint directly; one helper for the CK padded
  scale columns.
- Test: block_m 16/32/64 and counted sorts in the MoE check, padding
  columns compared, per-candidate column names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [FlyDSL] rot_quant: review follow-ups

- MoE: stop writing the scale padding columns. As for
  fused_dynamic_mxfp4_quant_moe_sort, only the first K // 32 columns of
  valid-route rows are defined; mxfp4_moe_sort_fwd's scatter store leaves the
  padding columns unwritten, so the two-pass path never matched a "0" claim.
- Merge the no-SLP hint into existing llvm_options instead of replacing them.
- Module docstring shows the keyword API; note that the #7646 padding goes
  away with #7653.
- Test: extra block sizes and counted sorts only for M <= 1024 (48 -> 34 s).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [FlyDSL] rot_quant: keep padded K on the fused MoE kernel longer

When K // 32 is not a multiple of 4 the two-pass path also pads the scales
for #7646, which moves its crossover with the fused kernel about 8x higher.
Switch at M * topk * K > 2^24 for those K and keep 2^21 otherwise.
K=2880, rotation size 64, MI350X: 1.27-1.68x faster for M * topk from 768 to
4096; other K are unchanged. Over 544 measured points (K 2048-7168, every
rotation size, topk 4 and 8) the dispatch is within 0.7% of the faster path
on average, against 2.4% before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [FlyDSL] rot_quant: drop the #7646 padding now that #7653 is in main

mxfp4_moe_sort_fwd keeps the last row's tail scales since #7653, so the
MoE two-pass path no longer pads the scale rows, and the 2^24 threshold
that compensated for the padding goes too. With the fixed sort the
padded-K dims cross over where the others do: one 2^21 switch for every K
is within 5% of the faster path on 567 of 600 measured points (MI350X,
K 2048-7168, every rotation size, topk 4 and 8), 0.9% off on average.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

mxfp4_moe_sort_fwd zeroes the last token's final scales when K // 32 is not a multiple of 4 (e.g. K=2880)

3 participants