apple / apple/coremltools

PyTorch convert function for op 'len' not implemented.

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

Description

## 🐞Describing the bug

I just used torch.jit.script to script model and convert it. It had a 'not implemented' error for 'len' function.

## Stack Trace

File "/Users/xxx/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 99, in _perform_torch_convert
prog = converter.convert()
File "/Users/xxx/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 519, in convert
convert_nodes(self.context, self.graph)
File "/Users/xxx/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 80, in convert_nodes
f"PyTorch convert function for op '{op_lookup}' not implemented."
RuntimeError: PyTorch convert function for op 'len' not implemented.

## To Reproduce

```
import torch
import torch.nn as nn
import numpy as np
import coremltools as ct

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

def forward(self, x):

b = len (x)

r = x *b
return r

def main():

input_tensor = torch.randn(2,4,2)

model = Net().to(torch.device("cpu"))
model.eval()

output = model(input_tensor)
print (input_tensor)
print (output)

print ("========script model=======")
trace = torch.jit.script(model, input_tensor)

input_image = ct.TensorType(
name='input_image',
shape=input_tensor.shape
)

print ("========convert to coreml model=======")
mlmodel = ct.convert(
trace,
inputs=[input_image],
)

mlmodel.save("test.mlpackage")


if __name__ == '__main__':
main()

```

## System environment (please complete the following information):
- coremltools version: 7.1
- OS: Ubuntu 20.04 (Both Linux and MacOS have the same issue. )
- Pytorch 2.1.0

Contributor guide

Open the contributing guide

Research direction

Start in coremltools/converters/mil/frontend/torch/ops.py at convert_nodes, then trace how conversion is invoked through converter.py and load.py. Reproduce the issue with the provided torch.jit.script example and verify that converting a model using len no longer raises the not-implemented error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.