Skip to content

[FlyDSL] [AOT] Fix mm out_dtype replay on torch 2.9.1 - #7561

Closed
zhiding512 wants to merge 3 commits into
mainfrom
zhiding512/fix-gemm-aot-mm-dtype-torch291
Closed

zhiding512 wants to merge 3 commits into
mainfrom
zhiding512/fix-gemm-aot-mm-dtype-torch291

Conversation

@zhiding512

@zhiding512 zhiding512 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix FlyDSL GEMM AOT replay on the Nightly ROCm 7.2 / PyTorch 2.9.1 images. An untuned BF16 GEMM that falls back to torch.mm(..., out_dtype=torch.float32, out=out) now produces the correct FakeTensor output instead of failing with TypeError: meta_mm() takes 2 positional arguments but 3 were given.

Motivation

These torch wheels expose aten.mm.dtype and aten.mm.dtype_out, but their registered meta_mm(a, b) does not accept the output-dtype argument. The single tuned M=56 row below succeeds at its tuned size; probing the M=1 it covers finds no tuned config and reaches this broken torch meta implementation:

flydsl_hgemm_abf16_wbf16_fp32_t64x32x64x4_ks8_w4x1x1_bias0_ktail0_gm0_pft_gfx950
M=1, N=896, K=7168, input=bfloat16, output=float32

Before this fix the CLI reports one successful tuned replay, then [BISECT] 1 sizes inside rows' ranges, 1 failed, and exits 1. With this fix both sizes succeed and the CLI exits 0.

Changes

  • Detect the missing out_dtype parameter by inspecting the torch meta function's signature.
  • During dry_run, temporarily supply FakeTensor rules for the two dtype overloads, using PyTorch 2.10's shape and output-dtype validation and preserving out identity through out_wrapper(exact_dtype=True).
  • Restore the original fake rules and clear the FakeTensor cache on normal and exceptional exit. Torch versions that already support the meta argument use their original implementation.
  • Add regression coverage for BF16/FP16 with FP32 output, both forms of out, invalid shapes/dtypes, and restoration after replay.
  • Add a test that simulates torch 2.9.1's two-argument meta_mm and its failure, so the backport and its restoration are exercised on every torch, including CI's 2.10, where the guard otherwise skips the backport.

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

The helper's docstring explains the wheel incompatibility and the scope of the temporary rule. The public GEMM API and the documented AOT command remain the same.

Dependencies

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

Breaking Changes

None.

PR Self-Review

Reviewed revision

08f247123cb7e2d51f12834992580e1b657bfc2d

Behavior and intent

The M=1 probe continues through GEMM's original torch fallback. Only its FakeTensor metadata implementation is supplied during AOT replay; real CPU/CUDA GEMM dispatch and arithmetic keep their original implementation. Dimension and dtype errors still fail the replay.

Validation and unverified scope

Validated on a MI355X node, with GPUs hidden during replay:

  • rocm/pytorch:rocm7.2_ubuntu22.04_py3.10_pytorch_release_2.9.1
  • rocm/pytorch:rocm7.2_ubuntu24.04_py3.12_pytorch_release_2.9.1

Both contain torch 2.9.1+rocm7.2.0.git7e1940d4. On failure SHA 0be9de16 plus this fix, pytest tests/test_flydsl_aot_replay.py -q passes 14 tests in each image. The one-row original AITER replay also passes in both images, with a fresh cache:

awk 'NR==1 || /flydsl_hgemm_abf16_wbf16_fp32_t64x32x64x4_ks8_w4x1x1_bias0_ktail0_gm0_pft_gfx950/' \
  aiter/configs/model_configs/kimik3_bf16_tuned_gemm.csv \
  > /tmp/repro_bf16_tuned_gemm.csv

GPU_ARCHS=gfx950 FLYDSL_RUNTIME_CACHE_DIR=/tmp/flydsl-mm-fix-cache \
python -m aiter.aot.flydsl.gemm --csv /tmp/repro_bf16_tuned_gemm.csv

Result: tuned M=56 succeeds, the M=1 probe succeeds, [BISECT] ... 0 failed, exit 0. The single-row CLI was repeated at 0bd238e5 in the Py3.10 image, with the same results. On this PR head (df6de622, which only adds a test), pytest tests/test_flydsl_aot_replay.py -q passes 16 tests in each image.

On this PR head with torch 2.10 (CI's), pytest tests/test_flydsl_aot_replay.py tests/test_aot_drivers.py -q passes 20 tests and 2 subtests, with the two legacy-only restoration cases skipped. There, test_mm_dtype_backport_runs_against_a_simulated_legacy_meta runs the backport, and it fails when the backport registers no fake rule. Ruff, Black and git diff --check pass.

After merging main (1350defcf) into this branch (08f24712; the only conflict was both sides appending tests at the end of tests/test_flydsl_aot_replay.py, resolved by keeping both), pytest tests/test_flydsl_aot_replay.py -q passes 20 tests on torch 2.9.1+rocm7.2.0.git7e1940d4 with main's flydsl 0.5.0, and 18 with the 2 legacy-only cases skipped on torch 2.10, GPUs hidden. Replaying all seven gfx950 N=896 K=7168 FP32-output rows of kimik3_bf16_tuned_gemm.csv with the CLI above gives 7 ok and [BISECT] 7 sizes inside rows' ranges, 0 failed; on main without this PR the same replay fails the M=1 probe with TypeError: meta_mm. The earlier red Standard Tests shard (NameError: name 'DROPPED_ROUTE_ROW' is not defined in moe_contiguous_psum.py) came from the old base, which had #7494 but not its fix #7583; the merge picks that fix up.

Full AOT and wheel builds were not run. Performance benchmarks and other GPU models are outside this metadata compatibility change; the targeted replay executes no GPU kernels.

PR-specific question

How is behavior outside replay restored? The fake implementation dictionary is restored by the context manager, and cached fake outputs are cleared when entering and leaving it. The regression tests confirm that the original torch 2.9.1 TypeError returns outside dry_run, including after an exception in its body. A test that simulates torch 2.9.1's meta checks the same on every torch.

Changed op tests

None.

@zhiding512
zhiding512 requested review from a team and a balanced review from Copilot November 6, 2026 08:10
@github-actions

github-actions Bot commented Nov 6, 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 gfx1201 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 7561 --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.

@zufayu

zufayu commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

@aiter-bot review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the FlyDSL AOT replay compatibility change.

No open issues 🎉

What changed in this PR

Fixes FlyDSL AOT replay on affected PyTorch 2.9.1 ROCm wheels when untuned GEMMs fall back to torch.mm with out_dtype.

Changes:

  • Adds temporary FakeTensor rules for mm.dtype and mm.dtype_out when the installed meta implementation lacks out_dtype support.
  • Restores the original rules and clears cached fake outputs on normal and exceptional exit, leaving real GEMM execution unchanged.
  • Adds regression tests for FP32 outputs, output identity, validation errors, and restoration after replay.
File Description
tests/​test_flydsl_aot_replay.py Adds dtype fallback and restoration regression tests.
aiter/​aot/​flydsl/​dry_run.py Adds the compatibility context and enables it during dry-run replay.

Review effort: Balanced

@aiter-bot aiter-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ROCm/aiter PR #7561 — [FlyDSL] [AOT] Fix mm out_dtype replay on torch 2.9.1

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — the PR changes runtime code but ships no test target; the changed path is executed AOT-replay-time code, not a data-only diff, and its regression tests ship in tests/test_flydsl_aot_replay.py, which CI collects via pytest -q tests but the validator does not sample, so no target existed to run
Perf (advisory): NOT RUN — no perf target for the same reason there is no test target: the PR changes runtime code but ships no test target; the patch is scoped to dry_run's context and inactive in the serving path, so no serving-path latency exists to measure

1 finding(s) posted inline, on the lines they are about:

  • aiter/aot/flydsl/dry_run.py:145-150 — On the torch CI runs (2.10.0, where meta_mm already carries out_dtype) the guard at…

Comment on lines +145 to +150
if (
not hasattr(torch.ops.aten.mm, "dtype")
or "out_dtype" in inspect.signature(meta_mm).parameters
):
yield
return

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [verified] On the torch CI runs (2.10.0, where meta_mm already carries out_dtype) the guard at aiter/aot/flydsl/dry_run.py:145-150 makes _mm_dtype_meta a no-op, so the patch branch this PR adds is never entered by the new tests — test_mm_fallback_with_fp32_output_under_dry_run passes through torch's own meta and test_legacy_mm_meta_is_restored_after_dry_run skips — and a later regression inside the patch code would stay green in CI until a torch 2.9.1 AOT image run hits it. Reviewer should ask for one test that simulates the legacy 2-arg meta_mm (the same signature the guard probes) so the patch branch is exercised on every torch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test_mm_dtype_backport_runs_against_a_simulated_legacy_meta in df6de62. It replaces meta_mm with torch 2.9.1's two-argument signature and makes the mm.dtype / mm.dtype_out fake rules fail as they do there, so the backport runs, and its restoration is checked, on torch 2.10 as well. On torch 2.10 it fails when the backport registers no fake rule.

On a torch whose meta_mm takes out_dtype, as CI's 2.10 does, the backport
returns before patching anything, so no test entered it and a regression
inside it would stay green until a torch 2.9.1 AOT build hit it. The new
test replaces meta_mm with torch 2.9.1's two-argument signature and makes
the fake rules of mm.dtype and mm.dtype_out fail as they do there, then
checks that the dry run answers an FP32 output, with and without out=,
and that the failure is back once the dry run exits.
Copilot AI balanced review requested due to automatic review settings November 6, 2026 09:32
@aiter-bot

aiter-bot commented Nov 6, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 08f247123cb7 · 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

The M=1 probe continues through GEMM's original torch fallback. Only its FakeTensor metadata implementation is supplied during AOT replay; real CPU/CUDA GEMM dispatch and arithmetic keep their original implementation. Dimension and dtype errors still fail the replay.

Validation and unverified scope

Validated on a MI355X node, with GPUs hidden during replay:

- `rocm/pytorch:rocm7.2_ubuntu22.04_py3.10_pytorch_release_2.9.1`
- `rocm/pytorch:rocm7.2_ubuntu24.04_py3.12_pytorch_release_2.9.1`

Both contain `torch 2.9.1+rocm7.2.0.git7e1940d4`. On failure SHA `0be9de16` plus this fix, `pytest tests/test_flydsl_aot_replay.py -q` passes **14 tests** in each image. The one-row original AITER replay also passes in both images, with a fresh cache:

```bash
awk 'NR==1 || /flydsl_hgemm_abf16_wbf16_fp32_t64x32x64x4_ks8_w4x1x1_bias0_ktail0_gm0_pft_gfx950/' \
  aiter/configs/model_configs/kimik3_bf16_tuned_gemm.csv \
  > /tmp/repro_bf16_tuned_gemm.csv

GPU_ARCHS=gfx950 FLYDSL_RUNTIME_CACHE_DIR=/tmp/flydsl-mm-fix-cache \
python -m aiter.aot.flydsl.gemm --csv /tmp/repro_bf16_tuned_gemm.csv
```

Result: tuned M=56 succeeds, the M=1 probe succeeds, `[BISECT] ... 0 failed`, exit 0. The single-row CLI was repeated at `0bd238e5` in the Py3.10 image, with the same results. On this PR head (`df6de622`, which only adds a test), `pytest tests/test_flydsl_aot_replay.py -q` passes **16 tests** in each image.

On this PR head with torch 2.10 (CI's), `pytest tests/test_flydsl_aot_replay.py tests/test_aot_drivers.py -q` passes **20 tests and 2 subtests**, with the two legacy-only restoration cases skipped. There, `test_mm_dtype_backport_runs_against_a_simulated_legacy_meta` runs the backport, and it fails when the backport registers no fake rule. Ruff, Black and `git diff --check` pass.

After merging main (`1350defcf`) into this branch (`08f24712`; the only conflict was both sides appending tests at the end of `tests/test_flydsl_aot_replay.py`, resolved by keeping both), `pytest tests/test_flydsl_aot_replay.py -q` passes **20 tests** on torch `2.9.1+rocm7.2.0.git7e1940d4` with main's flydsl 0.5.0, and 18 with the 2 legacy-only cases skipped on torch 2.10, GPUs hidden. Replaying all seven gfx950 N=896 K=7168 FP32-output rows of `kimik3_bf16_tuned_gemm.csv` with the CLI above gives 7 ok and `[BISECT] 7 sizes inside rows' ranges, 0 failed`; on main without this PR the same replay fails the M=1 probe with `TypeError: meta_mm`. The earlier red Standard Tests shard (`NameError: name 'DROPPED_ROUTE_ROW' is not defined` in `moe_contiguous_psum.py`) came from the old base, which had #7494 but not its fix #7583; the merge picks that fix up.

Full AOT and wheel builds were not run. Performance benchmarks and other GPU models are outside this metadata compatibility change; the targeted replay executes no GPU kernels.

PR-specific question

How is behavior outside replay restored? The fake implementation dictionary is restored by the context manager, and cached fake outputs are cleared when entering and leaving it. The regression tests confirm that the original torch 2.9.1 TypeError returns outside `dry_run`, including after an exception in its body. A test that simulates torch 2.9.1's meta checks the same on every torch.

Changed op tests

None.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply

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
  • FAIL — Aiter Test Gate: failed
  • Required CI failed; fix it before requesting human review.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The shim modifies PyTorch’s private FakeTensor registry and cache, requiring runtime validation across affected 2.9.1 wheels and newer versions that this dependency-free review environment cannot independently perform.

No open issues 🎉

Review effort: Balanced

zufayu
zufayu previously approved these changes Nov 6, 2026
@zufayu

zufayu commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

ci failed

1 similar comment
@zufayu

zufayu commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

ci failed

Co-authored-by: Cursor <[email protected]>

# Conflicts:
#	tests/test_flydsl_aot_replay.py
Copilot AI balanced review requested due to automatic review settings November 11, 2026 06:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the scoped FakeTensor compatibility change supporting FlyDSL AOT replay.

No open issues 🎉

Review effort: Balanced

@zufayu
zufayu requested review from aoli26 and removed request for coderfeli November 11, 2026 06:54
@zufayu
zufayu requested a review from coderfeli November 11, 2026 06:54
zufayu pushed a commit that referenced this pull request Nov 13, 2026
 + #7561) (#7892)

* [FlyDSL] [AOT] Fix mm out_dtype replay on torch 2.9.1

* Test the mm out_dtype backport against a simulated torch 2.9.1 meta

On a torch whose meta_mm takes out_dtype, as CI's 2.10 does, the backport
returns before patching anything, so no test entered it and a regression
inside it would stay green until a torch 2.9.1 AOT build hit it. The new
test replaces meta_mm with torch 2.9.1's two-argument signature and makes
the fake rules of mm.dtype and mm.dtype_out fail as they do there, then
checks that the dry run answers an FP32 output, with and without out=,
and that the failure is back once the dry run exits.

* [FlyDSL] [AOT] Assign into FakeTensor slices without reaching HIP on torch 2.9.1

On torch 2.9.1, Tensor.__setitem__ (like __getitem__, contiguous and copy_)
takes a device guard in its own Python binding, before the call reaches the
dispatcher and FakeTensorMode. With the GPUs hidden, as replay_env hides them,
that guard initializes HIP and raises hipErrorNoDevice. The weights the MoE
replay prepares as callers do are assigned through an index: e8m0_shuffle pads
every MXFP4 scale with `padded[:m, :n] = src`, shuffle_scale_a16w4 pads a scale
whose K/32 is not a multiple of 8 with `padded[:, :k] = src`, and the int4
packing writes `out[:, 0] = ...`. MegaMoEV2 fills its peer pointer table with
`table[peer] = ptr`. So the Nightly wheel builds, whose images ship torch
2.9.1, failed 8.7k MoE and all 192 MegaMoE replay jobs. torch 2.10 takes no
guard there, which is why CI passes.

dry_run now stands in for __setitem__ on FakeTensors under basic indexing: a
copy (or fill) into the view getitem answers, through the aten op, since the
Tensor.copy_ binding takes the guard too.

The test that pads a scale in a process with the GPUs hidden fails only on
torch 2.9.1, so a second one simulates the guarded binding and runs the
stand-in on CI's torch as well.

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Cursor <[email protected]>
vpietila-amd pushed a commit that referenced this pull request Nov 13, 2026
 + #7561) (#7892)

* [FlyDSL] [AOT] Fix mm out_dtype replay on torch 2.9.1

* Test the mm out_dtype backport against a simulated torch 2.9.1 meta

On a torch whose meta_mm takes out_dtype, as CI's 2.10 does, the backport
returns before patching anything, so no test entered it and a regression
inside it would stay green until a torch 2.9.1 AOT build hit it. The new
test replaces meta_mm with torch 2.9.1's two-argument signature and makes
the fake rules of mm.dtype and mm.dtype_out fail as they do there, then
checks that the dry run answers an FP32 output, with and without out=,
and that the failure is back once the dry run exits.

* [FlyDSL] [AOT] Assign into FakeTensor slices without reaching HIP on torch 2.9.1

On torch 2.9.1, Tensor.__setitem__ (like __getitem__, contiguous and copy_)
takes a device guard in its own Python binding, before the call reaches the
dispatcher and FakeTensorMode. With the GPUs hidden, as replay_env hides them,
that guard initializes HIP and raises hipErrorNoDevice. The weights the MoE
replay prepares as callers do are assigned through an index: e8m0_shuffle pads
every MXFP4 scale with `padded[:m, :n] = src`, shuffle_scale_a16w4 pads a scale
whose K/32 is not a multiple of 8 with `padded[:, :k] = src`, and the int4
packing writes `out[:, 0] = ...`. MegaMoEV2 fills its peer pointer table with
`table[peer] = ptr`. So the Nightly wheel builds, whose images ship torch
2.9.1, failed 8.7k MoE and all 192 MegaMoE replay jobs. torch 2.10 takes no
guard there, which is why CI passes.

dry_run now stands in for __setitem__ on FakeTensors under basic indexing: a
copy (or fill) into the view getitem answers, through the aten op, since the
Tensor.copy_ binding takes the guard too.

The test that pads a scale in a process with the GPUs hidden fails only on
torch 2.9.1, so a second one simulates the guarded binding and runs the
stand-in on CI's torch as well.

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Cursor <[email protected]>
@zufayu

zufayu commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Superseded by #7892, which merged the mm out_dtype replay fix and regression tests from this PR into main. Closing this duplicate PR. The Nightly wheel packaging failure (Git dubious ownership at /workspace) is a separate CI issue and remains under follow-up; this closure does not mark Nightly healthy.

@zufayu zufayu closed this Nov 16, 2026
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.

6 participants