pytorch / pytorch/pytorch

[Inductor] torch.index_add accepts negative index while eager raises out-of-bounds error

Open
#185,885 2 comments 0 reactions 0 assignees View on GitHub
bot-triaged module: inductor oncall: pt2 triaged
Dominant language
Python
Stars
103k
Forks
29.5k
PR merge metrics
PR metrics pending

Description

### 🐛 Describe the bug

`torch.index_add` rejects a negative index in eager mode with an out-of-bounds error. However, the same function compiled with `torch.compile(..., backend="inductor", fullgraph=True, dynamic=True)` silently accepts the invalid negative index and returns a tensor.

This is an eager-parity / legality mismatch. The eager error is not a missing kernel implementation; it is an input validation error:

```text
RuntimeError: index -1 is out of bounds for dimension 0 with size 4
```

In compiled mode, Inductor appears to treat `-1` as a valid negative index and updates the last row. Since eager rejects this input, Inductor should also raise an out-of-bounds error instead of producing output.

Expected behavior: compiled execution should raise the same out-of-bounds error as eager.

Actual behavior: Inductor accepts the invalid index and returns a tensor.

```python
import traceback
import torch

def fn():
x = torch.zeros(4, 3, dtype=torch.float32)
index = torch.tensor([0, -1], dtype=torch.int64)
src = torch.ones(2, 3, dtype=torch.float32)

result = torch.index_add(x, 0, index, src)
observer = result.clamp_max(1.0)
return result, observer

print("torch", torch.__version__)

try:
eager = fn()
print("eager ok")
for t in eager:
print(t)
except Exception as e:
print("eager error:", type(e).__name__, str(e).splitlines()[0])

try:
compiled = torch.compile(fn, backend="inductor", fullgraph=True, dynamic=True)
out = compiled()
print("compiled ok")
for t in out:
print(t)
except Exception as e:
print("compiled error:", type(e).__name__, str(e).splitlines()[0])
traceback.print_exc(limit=20)
```

Observed output:

```text
eager error: RuntimeError index -1 is out of bounds for dimension 0 with size 4
compiled ok
tensor([[1., 1., 1.],
[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.]])
tensor([[1., 1., 1.],
[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.]])
```

### Versions

`PyTorch version: 2.10.0+cpu`

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.