Repository navigation
[Bugfix] Declare gemm_a16w16 non-mutating so torch.compile stops reading past packed bias views - #6902
Conversation
…ing past packed bias views
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
Summary
aiter::gemm_a16w16(aiter/tuned_gemm.py) withmutates_args=[].torch_compile_guarddefaults tomutates_args="unknown", which marks every tensor argument as mutated, but the op only reads its inputs and returns a new tensor.torch.compilethe 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.op_tests/test_gemm_a16w16.py -t compile, which compilestgemm.mmover 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:
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 emitsas_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):Where it shows up
Any
torch.compilecaller oftgemm.mm/gemm_a16w16whose bias is a view into a shared storage. Seen in SGLang diffusion withSGLANG_USE_ROCM_AITER_TGEMM=1 --enable-torch-compile trueon Wan2.2-T2V-A14B (MI450, gfx1250), where the first denoising step dies withMemory access fault ... Page not presentorHW 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):torch.AcceleratorError: CUDA error: an illegal memory access was encountered(abort)err vs eager= 0 for all 3 packed layersblackandruff checkare clean on both files.-t outfails 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.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.