apple / apple/coremltools

[torch.jit.trace] Inplace Index Put Silent Error

Open
#2,188 2 comments 0 reactions 1 assignee Claimed by @YifanShenSZ View on GitHub
bug PyTorch (traced)
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## 🐞Describing the bug
This simple model can be converted but gives incorrect result
```
class IndexPutModel(torch.nn.Module):
def forward(self, x, position, val):
y = x.clone()
y[:, position] = val
return y
```
The issue is in how we parse the TorchScript
```
graph(
%x : Tensor(2, 2, ".int'>"),
%position : Tensor(1, ".int'>"),
%val : Tensor(1, ".int'>"),
):
%4 = constant[]()
%y = clone[](%x, %4)
%6 = constant[value=0]()
%7 = constant[value=0]()
%8 = constant[value=9223372036854775807]()
%9 = constant[value=1]()
%10 = slice[](%y, %6, %7, %8, %9)
%11 = listconstruct[]()
%12 = view[](%val, %11)
%13 = constant[]()
%14 = listconstruct[](%13, %position)
%15 = constant[value=False]()
%16 = index_put_[](%10, %14, %12, %15)
return (%y)
```
That we did not realize `%10` is a view of `%y` and `%16` is a reference of `%10`, so we early terminate translation when we see the output `%y` has been created
```
main[CoreML5](%x: (2, 2, int32)(Tensor),
%position: (1,int32)(Tensor),
%val: (1,int32)(Tensor)) {
block0() {
%x_tmp: (2, 2, int32)(Tensor) = identity(x=%x, name="x_tmp")
%position_tmp: (1,int32)(Tensor) = identity(x=%position, name="position_tmp")
%val_tmp: (1,int32)(Tensor) = identity(x=%val, name="val_tmp")
} -> (%x)
}
```

## Potential Solution
We probably won't be able to fully fix this, since the solution is equivalent to torch's own functionalization 😂 But we do should find some way to error out, rather than silently produces a wrong Core ML model

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.