pytorch - ✅(Solved) Fix [inductor] torch.compile ignores a complex indexing assignment and produces incorrect result [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#177821Fetched 2026-04-08 01:01:41
View on GitHub
Comments
0
Participants
1
Timeline
52
Reactions
0
Author
Participants
Timeline (top)
mentioned ×21subscribed ×21labeled ×6assigned ×2

Error Message

out1=tensor([[ 2, 2], [10, 10]]) out2=tensor([[ 4, 0], [12, 8]]) AssertionError: Tensor-likes are not equal!

Mismatched elements: 4 / 4 (100.0%) Greatest absolute difference: 2 at index (0, 0) Greatest relative difference: inf at index (0, 1)

Fix Action

Fixed

PR fix notes

PR #178032: [inductor] Avoid aliasing mutated cat results

Description (problem / solution / changelog)

Fix #177821

Summary

  • Root cause: ConcatKernel rewires cat inputs into non-owning views of the concat output. That is only valid while the concat result stays immutable. When a later mutation like index_put_ writes into the concat, subsequent uses of an input can incorrectly observe the mutated concat slice.
  • Proposed fix: if an aten.cat result has downstream mutation, lower it through the normal aten.cat fallback instead of ConcatKernel, and add a regression test for the advanced-index assignment repro from the issue.
  • Why this is the right long term fix: it keeps the concat aliasing optimization where it is semantically valid, but disables it exactly where mutation would make that optimization incorrect.

Drafted via Codex, published after manual review by @bobrenjc93

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

Changed files

  • test/inductor/test_torchinductor.py (modified, +18/-0)
  • torch/_inductor/lowering.py (modified, +56/-0)

Code Example

import torch

def fn(x, y):
    x = 2 * x  # x = [[0,2],[4,6]]
    c = torch.cat([x, y], dim=1)  # c = [[0,2,0,1], [4,6,2,3]]
    c[:, [1, 0]] = c[:, [0, 1]]  # c should be [[2,0,0,1], [6,4,2,3]]
    return c[:, :2] + x  # expected result: `[[2,2],[10,10]]`
    
x1 = torch.arange(4).reshape(2, 2)
y1 = torch.arange(4).reshape(2, 2)
x2 = torch.arange(4).reshape(2, 2)
y2 = torch.arange(4).reshape(2, 2)

cfunc = torch.compile(fn, backend='inductor')
out1 = fn(x1, y1)
out2 = cfunc(x2, y2)

print(f'{out1=}')
print(f'{out2=}')

torch.testing.assert_close(out1, out2, equal_nan=True)

---

out1=tensor([[ 2,  2],
        [10, 10]])
out2=tensor([[ 4,  0],
        [12,  8]])
AssertionError: Tensor-likes are not equal!

Mismatched elements: 4 / 4 (100.0%)
Greatest absolute difference: 2 at index (0, 0)
Greatest relative difference: inf at index (0, 1)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

When applying torch.compile (with backend 'inductor') on a model including complex indexing assignment, the compiler incorrectly ignores this indexing assignment. To reproduce:

import torch

def fn(x, y):
    x = 2 * x  # x = [[0,2],[4,6]]
    c = torch.cat([x, y], dim=1)  # c = [[0,2,0,1], [4,6,2,3]]
    c[:, [1, 0]] = c[:, [0, 1]]  # c should be [[2,0,0,1], [6,4,2,3]]
    return c[:, :2] + x  # expected result: `[[2,2],[10,10]]`
    
x1 = torch.arange(4).reshape(2, 2)
y1 = torch.arange(4).reshape(2, 2)
x2 = torch.arange(4).reshape(2, 2)
y2 = torch.arange(4).reshape(2, 2)

cfunc = torch.compile(fn, backend='inductor')
out1 = fn(x1, y1)
out2 = cfunc(x2, y2)

print(f'{out1=}')
print(f'{out2=}')

torch.testing.assert_close(out1, out2, equal_nan=True)

Outputs:

out1=tensor([[ 2,  2],
        [10, 10]])
out2=tensor([[ 4,  0],
        [12,  8]])
AssertionError: Tensor-likes are not equal!

Mismatched elements: 4 / 4 (100.0%)
Greatest absolute difference: 2 at index (0, 0)
Greatest relative difference: inf at index (0, 1)

The indexing assignment c[:, [1,0]]=c[:, [0, 1]] is ignored by torch.compile, then the compiled model incorrectly outputs [[4,0],[12,8]] instead of [[2,2],[10,10]].

I notice that backend aot_eager and eager do not have this issue. Note that changing return c[:, :2] + x to return c[:, :2] will make the outputs consistent between torch.compile and eager.

Error logs

No response

Versions

Versions of relevant libraries: [pip3] numpy==2.4.2 [pip3] torch==2.12.0.dev20260316+cpu [pip3] torchvision==0.26.0.dev20260223+cpu [pip3] triton==3.6.0 [conda] numpy 2.4.2 pypi_0 pypi [conda] torch 2.12.0.dev20260316+cpu pypi_0 pypi [conda] torchvision 0.26.0.dev20260223+cpu pypi_0 pypi [conda] triton 3.6.0 pypi_0 pypi

cc @ezyang @gchanan @kadeng @msaroufim @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @muchulee8 @amjames @aakhundov @coconutruben @jataylo

extent analysis

Fix Plan

To fix the issue with torch.compile ignoring the indexing assignment, we can try the following steps:

  • Update PyTorch to the latest version, as the issue might be resolved in newer versions.
  • If updating PyTorch is not possible, we can use the aot_eager or eager backend instead of inductor, as they do not have this issue.
  • Alternatively, we can modify the code to avoid using indexing assignment, which might be causing the issue.

Here is an example of how to modify the code to avoid indexing assignment:

import torch

def fn(x, y):
    x = 2 * x  
    c = torch.cat([x, y], dim=1)  
    # Avoid indexing assignment
    c = torch.cat([c[:, 1:2], c[:, 0:1], c[:, 2:]], dim=1)
    return c[:, :2] + x  

x1 = torch.arange(4).reshape(2, 2)
y1 = torch.arange(4).reshape(2, 2)
x2 = torch.arange(4).reshape(2, 2)
y2 = torch.arange(4).reshape(2, 2)

cfunc = torch.compile(fn, backend='inductor')
out1 = fn(x1, y1)
out2 = cfunc(x2, y2)

print(f'{out1=}')
print(f'{out2=}')

torch.testing.assert_close(out1, out2, equal_nan=True)

Verification

To verify that the fix worked, we can compare the outputs of the compiled and non-compiled functions using torch.testing.assert_close.

Extra Tips

  • Make sure to update PyTorch and other dependencies to the latest versions to ensure that any known issues are resolved.
  • If the issue persists, try using different backends or modifying the code to avoid using indexing assignment.
  • Always verify the correctness of the compiled function by comparing its output with the non-compiled function.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix [inductor] torch.compile ignores a complex indexing assignment and produces incorrect result [1 pull requests, 1 participants]