Repository navigation
[decomp] Check self/source dtype in index_add/index_copy decompositions - #206704
gavinwang269 wants to merge 3 commits into
Conversation
The eager ATen kernels reject mixed self/source dtypes for index_add and index_copy, but the decompositions used by torch.compile validated device, rank, sizes and alpha while skipping the dtype comparison, so the compiled path silently cast the source and produced plausible wrong results (issue repro: index_add_ float32 dst + int64 src -> [2.0, 1.0] instead of an error). Mirror the eager messages (index_add_: 'must have the same scalar type', index_copy_: 'self and source expected to have the same dtype') so the check reads identically eager vs compiled. put_ has the same gap through the index_put lowering and is left as a follow-up to keep this diff minimal. Test Plan: - new test_index_add_copy_dtype_check in test/test_decomp.py covering the two rejections plus a same-dtype sanity path - overlay-verified on 2.16.0.dev20261029 wheel: compiled index_add raised after the patch and remained silently wrong before it; environment restored afterwards Authored with assistance from an AI assistant (Claude Code); the repro, the overlay verification and both messages against the eager TORCH_CHECKs were checked by me. Fixes pytorch#205291
PR Status: ready for maintainer reviewWaiting for a maintainer with merge rights for the changed paths to review this PR. Please address any feedback they leave. ✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:
Findings (3): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/206704
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit 50289c6 with merge base cf717e3 ( NEW FAILURES SUPPRESSED BY AI (non-blocking) - The following job failed, but the AI CI Advisor judged the failure not to be evidence against this change:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
The dtype check in _index_copy also fired for index_fill, whose decomposition expands the 0-d value into index_copy's source. Eager casts that value to the self dtype (source.to(self.scalar_type()).item()) instead of rejecting it, so cast it in _index_fill before the expand; index_fill keeps eager behavior and index_copy keeps its own check. Also check the dtype before the device in _index_add to match eager's meta order, and drive the regression test through torch.compile, which is the path the issue is about, including index_fill with a mismatched 0-d tensor value.
There was a problem hiding this comment.
Automated review of 6a9f218: nothing blocking; ready for human review.
The PR adds self/source dtype checks to the _index_add and _index_copy decompositions so compiled, FakeTensor and meta runs raise the same error eager does, and casts the 0-d value in _refs._index_fill to x.dtype so index_fill keeps working. Recommendation: Approve. The previous blocking index_fill regression is fixed, and only non-blocking points remain.
⚪ 3 non-blocking findings are attached to the code below, collapsed.
This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review
Withdrawn: the automated review of 6a9f218 found nothing blocking.
…version Eager routes the 0-d value through source.item() into the Scalar overload, which refuses a complex value for a non-complex self. The plain cast accepted it and dropped the imaginary part with a warning, so add the same rejection before the conversion and move the value onto the self device along with the dtype, which also lets a CPU 0-d value fill a tensor on another device like eager does. The test now compiles through aot_eager_decomp_partition to run the decomposition table without codegen, resets dynamo between compiles, pins the exact error wording per op, and covers the complex value.
|
Hi @albanD - this restores eager checked-conversion semantics in the decomposed path: index_add/index_copy lose the self/source dtype equality check and index_fill silently drops the imaginary part of a complex 0-dim fill value, while the eager kernels reject both today. Nothing new is invented here, each assertion mirrors what eager already enforces, and the regression tests route through aot_eager_decomp_partition. CI is green, the diff is small, one write-set approve would land it. Fixes #205291. |
|
Hi @anijain2305 - this is ready for review. The index_add/index_copy decompositions now mirror the eager TORCH_CHECKs on self/source dtype (and complex 0-dim index_fill), closing the fuzzer-found gap in #205291 where the compiled path silently rounded or dropped the imaginary part that eager rejects. Nothing new is invented: each assertion follows what eager already enforces. The diff is small (14 + 13 source lines, 51 test lines routed through aot_eager_decomp_partition). CI is otherwise green - the lone red, linux-jammy-rocm-py3.11 / build, is a Dr. CI-confirmed infra issue (HTTP 618 fetching the AOTriton tarball during build). One approve from a write-set member and merge should take it from there. Thanks! |
|
@pytorchbot label "topic: not user facing" |
|
Hi @nkaretnikov, this is the decomposition half of the fuzzer-found gap in #205291.
The only red on 50289c6 is a rocm build job that Dr.CI classifies as not evidence against the change, and the automated review recommends approve. One write-set approve lands it. |
|
Housekeeping: resolved the 7 automated-review threads from 11-26. They were written against 3ce96e1. |
|
Hi @anijain2305 @eqy, one more nudge and then I will leave this in the queue. The diff is two dtype checks that the eager index_add and index_copy kernels already enforce, plus the complex 0-dim index_fill case from the same #205291 fuzz report, so there is no new rule to review and the issue is already labeled actionable. @nkaretnikov owns the PrimTorch block and I have asked there as well. |
|
Hi @bdhirsh, these are your ops. The The only red on 50289c6 is a rocm build job Dr.CI marks as not evidence against the change. One write-set approve lands it. |
|
Hi @mruberry, this is an eager-versus-compiled error semantics gap on two of your ops, so flagging you directly. 78 lines including the regression test, and the only red on the head is a rocm build job Dr.CI marks as not evidence against the change. |
|
Hi @Skylion007, you signed #195373 and #193555 in torch/_decomp/decompositions.py, so this one is in your lane. eager raises for index_add and index_copy when the self and source dtypes differ. The decompositions did not check dtype, so torch.compile silently cast and returned plausible wrong numbers. The patch copies the two eager messages, keeps index_fill on the eager cast path, and rejects the complex 0-dim fill value that eager rejects. 27 lines of source plus tests through aot_eager_decomp_partition. Two notes that may save you time: the current head 50289c6 has all checks finished, with linux-jammy-rocm as the only red and Dr.CI marking it non-blocking, and the automated review that ran on that same head records Recommendation: Approve. If you would rather see the same dtype treatment applied to put_ here instead of in a follow up, say so and I will do it in this PR. |
|
Repro for reviewers, CPU only, on torch 2.16.0.dev20261102: Eager raises: If you want to see the fixed side without a GPU, test/test_decomp.py::test_index_add_copy_dtype_check goes through aot_eager_decomp_partition, so no CUDA and no triton is involved. |
|
Hi @slayton58, you signed #190245 and #186414 in torch/_decomp/decompositions.py, so this one is in your file. The gap: eager refuses mixed dtypes for index_add and index_copy, the decompositions did not check, so a compiled graph silently casts and returns plausible wrong numbers. On torch 2.16.0.dev20261102, CPU only, eager raises index_add_(): self (Float) and source (Double) must have the same scalar type, while torch.compile hands back tensor([-1.5521, -0.7337, -1.9588, -0.4731]). index_copy behaves the same way. I posted the six line repro in a comment above on this PR. The patch copies those two eager messages into the decompositions, keeps index_fill on the eager cast path, and rejects the complex 0-dim fill value that eager rejects. Checks are complete on head 50289c6 with linux-jammy-rocm as the only red and marked non-blocking, and the automated review that ran on that head records Recommendation: Approve. |
Fixes #205291
BC-breaking?
No for correct programs. Programs that currently get silently-wrong compiled results (mixed self/source dtype through index_add/index_copy under torch.compile) now get the same error eager already raises.
Summary
The eager kernels compare self/source dtypes; the decompositions used for lowering validated device/rank/sizes/alpha but not dtype, so the compiled path silently cast and produced plausible wrong numbers. This mirrors the two eager TORCH_CHECK messages in the decompositions (index_add: "must have the same scalar type", index_copy: "self and source expected to have the same dtype"), plus a regression test in test_decomp.py covering both rejections and the same-dtype happy path. put_ shares the gap through the index_put lowering; I left it out to keep this diff minimal and can follow up in a second PR if maintainers want it here instead.
Test plan
test_index_add_copy_dtype_check.