Skip to content

[Bugfix] Declare gemm_a16w16 non-mutating so torch.compile stops reading past packed bias views - #6902

Merged
yzhou103 merged 1 commit into
ROCm:mainfrom
yichiche:fix/gemm-a16w16-mutates-args
Oct 27, 2026
Merged

yzhou103 merged 1 commit into
ROCm:mainfrom
yichiche:fix/gemm-a16w16-mutates-args

Conversation

@yichiche

Copy link
Copy Markdown
Contributor

Summary

  • Register aiter::gemm_a16w16 (aiter/tuned_gemm.py) with mutates_args=[]. torch_compile_guard defaults to mutates_args="unknown", which marks every tensor argument as mutated, but the op only reads its inputs and returns a new tensor.
  • Under torch.compile the bogus mutation made Inductor clone the whole storage behind each bias. When weights and biases share one storage, that clone reads past the end of the bias allocation, causing an illegal memory access, a GPU memory fault, or a GPU hang.
  • Add op_tests/test_gemm_a16w16.py -t compile, which compiles tgemm.mm over weights and biases packed in one storage and compares against eager. It aborts with an illegal memory access before this fix and passes after it.

Details

What goes wrong
The op is registered with every tensor argument marked as mutated:

# before
aiter::gemm_a16w16(Tensor(a0!) A, Tensor(a1!) B, Tensor(a2!)? bias=None, ScalarType? otype=None, Tensor(a4!)? scale_a=None, Tensor(a5!)? scale_b=None, Tensor(a6!)? scale_c=None) -> Tensor
# after
aiter::gemm_a16w16(Tensor A, Tensor B, Tensor? bias=None, ScalarType? otype=None, Tensor? scale_a=None, Tensor? scale_b=None, Tensor? scale_c=None) -> Tensor

Functionalization therefore wraps every call in clone, op, and copy-back. When graph inputs alias one storage, as they do when a model packs each layer's [weight; bias] back to back, Inductor cannot pass the bias through. Instead it emits as_strided(bias, [base_numel], [1], 0) plus a clone of the entire base storage, and the generated Triton kernel indexes from the bias's own data pointer. From Wan2.2-T2V-A14B cross-attention (bias = last 5120 elements of a 183,577,600-element storage):

buf16 = empty_strided_cuda((183577600, ), (1, ), torch.bfloat16)
triton_poi_fused_as_strided_clone_3.run(arg8_1, buf16, 183577600, stream=stream0)   # reads ~367 MB past the bias
buf17 = torch.ops.aiter.gemm_a16w16.default(arg6_1, arg9_1, reinterpret_tensor(buf16, (5120, ), (1, ), 183572480), ...)
triton_poi_fused_as_strided_copy__4.run(buf16, arg8_1, 5120, stream=stream0)

Where it shows up
Any torch.compile caller of tgemm.mm / gemm_a16w16 whose bias is a view into a shared storage. Seen in SGLang diffusion with SGLANG_USE_ROCM_AITER_TGEMM=1 --enable-torch-compile true on Wan2.2-T2V-A14B (MI450, gfx1250), where the first denoising step dies with Memory access fault ... Page not present or HW Exception ... GPU Hang. Eager mode never takes the clone path, so only compiled runs fail. If the inputs are not aliased, Inductor passes them through and the bug stays latent, so a plain single-GEMM test does not catch it.

How to fix
Declare the op as non-mutating (mutates_args=[]), as other pure ops already do (e.g. aiter/ops/mhc.py), so functionalization emits a direct call with no clone. Removing the per-call clone and copy-back is also a small perf win in compiled graphs.

Tests

python op_tests/test_gemm_a16w16.py -t compile (MI450 / gfx1250):

Result
Before fix torch.AcceleratorError: CUDA error: an illegal memory access was encountered (abort)
After fix err vs eager = 0 for all 3 packed layers

black and ruff check are clean on both files. -t out fails the same way (torch bf16+bias: routed to triton) with and without this change on this machine, so that failure is not caused by this PR.

End-to-end: SGLang Wan2.2-T2V-A14B

720x1280, 193 frames, 8 steps, BF16, 1x MI450 (gfx1250), SGLANG_USE_ROCM_AITER_TGEMM=1, aiter attention backend. Steady-state times are the 2nd and 3rd requests in the same process, so compile time is excluded.

Stage compile off compile on (before fix) compile on (after fix)
Denoise (8 steps) 218.1 s crash (GPU memory fault / hang) 206.0 s (-5.6%)
VAE decode 22.6 s - 11.8 s (-48%)
End to end 241.1 s - 218.2 s (-9.5%, 1.10x)

Output sanity check: compile-on and compile-off videos show the same scene and composition on visual inspection of the first, middle, and last frames. PSNR between them is 16.5 dB, from frame-level drift between compiled and eager numerics over 8 steps. No formal accuracy metric was run.

@yichiche
yichiche requested a review from a team October 24, 2026 13:01
@github-actions

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 6902 --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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants