apple / apple/coremltools

[pytorch] cat&slice bug

Open
#1,182 2 comments 0 reactions 0 assignees View on GitHub
bug PyTorch (traced) triaged
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## 🐞Describe the bug
I got the following error when I tried to convert the coreml model from the pytorch model.
If you run `x[:, :] = 0` after `torch.cat`, the coreml conversion will fail.

## Trace

```
ValueError: Op "13" (op_type: reshape) Input shape="12" expects integer tensor but got tensor[0,fp32]
```

## To Reproduce

Here is a colab to reproduce.
https://colab.research.google.com/drive/1eR3HLQh5zdPQzg9rMFt641NH7h7T85bE?usp=sharing

```
import torch
import torch.nn as nn

class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()

def forward(self, x):
x[:, :] = 0 # Here
z = torch.cat((x, x), 0)
return z

net = Net()
input = torch.randn([256, 256])
trace_model = torch.jit.trace(net, input)

import coremltools as ct
from coremltools.converters.mil.mil import types
coreml_model = ct.convert(
trace_model,
inputs=[ct.TensorType(name="x", shape=input.shape, dtype=types.float)]
)
```

## System environment (please complete the following information):
- coremltools version: 4.1
- OS: Linux
- How you install python: system
- python version: Python 3.7.10
- any other relevant information:
- pytorch version: 1.8.1+cu101

## Additional context

I wrote the following, and it worked fine.

```
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()

def forward(self, x):
x[:, :] = torch.zeros(x.size()) # Here
z = torch.cat((x, x), 0)
return z
```

Contributor guide

Open the contributing guide

Research direction

Start with the linked Colab reproduction and inspect the `ct.convert` path for the traced PyTorch model containing `x[:, :] = 0` followed by `torch.cat`. Compare it with the working `torch.zeros(x.size())` variant and verify that conversion succeeds without the reshape integer-tensor error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.