Skip to content

[FIX][S-TIR] Do not reverse-inline a computing consumer into a block with init - #20954

Open
JonSnow1807 wants to merge 1 commit into
apache:mainfrom
JonSnow1807:issue-20825
Open

JonSnow1807 wants to merge 1 commit into
apache:mainfrom
JonSnow1807:issue-20825

Conversation

@JonSnow1807

Copy link
Copy Markdown

A K = 1 matmul followed by an elementwise op gives wrong values on CUDA. DLight’s NormalizeFunction removes the unit reduction var but the block keeps its init, so it still passes the complete-block check. DLight Fallback then calls reverse_compute_inline on the elementwise consumer, which writes it into both the init and the update: an epilogue f becomes f(f(0) + a * b).

#17838 allowed reverse inline into a block with init for a transpose, which is correct because a copy only moves the output. This keeps that case: when the producer has init, the consumer must read the inlined tensor once, as the whole stored value. The same rule rejects a consumer like D[C[i]] = C[i], which before recursed until the process crashed.

The issue’s repro uses AdjustMatmulOrder, removed in #20712. The same wrong result happens on main with a(10x1)^T @ (b^T @ c)(1x3) and the same exp chain: max abs error 0.1929 before, 0.0 after.

Tests: three new tests (two in test_tir_schedule_compute_inline.py, one in test_gpu_fallback.py). Without the change two fail and the scatter test crashes. s_tir and relax suites show the same failures as main. Run on an RTX PRO 4000 (sm_120), CUDA 12.8.

Fixes #20825

Code drafted by both human and AI, and reviewed by human.

…with init

DLight's NormalizeFunction removes a unit-extent reduction block var but
keeps the init statement. For a matmul with K = 1, the block then has
only data-parallel block vars and passes CheckCompleteBlock.
reverse_compute_inline then puts the consumer into both the init and the
update, so an epilogue f is computed as f(f(0) + a * b) instead of
f(a * b).

When the producer has an init statement, only allow reverse inline if
the consumer copies the value of the inlined tensor to a new location
(for example a transpose). This keeps the case that apache#17838 added.

Fixes apache#20825

This branch has not been deployed

No deployments
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.

[Bug] [Relax][CUDA] Wrong result for a K=1 matmul chain after AdjustMatmulOrder

1 participant