apple / apple/coremltools

PyTorch to CoreML model conversion via scripting (torch.jit.script) gives 3 different errors (missing op 'dim', IndexError: out of Range, and INTERNAL ERROR)

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

Description

Title:

PyTorch to CoreML model conversion via scripting (torch.jit.script) gives error messages.

Relevance:

For certain models, torch scripting is preferred over the JIT trace as explained here
https://coremltools.readme.io/docs/model-scripting

When we use the torch.jit.script in this test model, we see that we get error messages in to CoreML conversion which is probably wrong.

Reproducible:

Yes

Testcase:

Attached
[testScripting.txt](https://github.com/apple/coremltools/files/4891101/testScripting.txt)

We have 4 modes here.
1) With and without scripting.
2) And two branches in the forward.

The issues are that the
scripted_model = torch.jit.script(model, dummy_input)
operation gives several error messages including
a) RuntimeError: PyTorch convert function for op dim not implemented
Which op?
b) IndexError: list index out of range
(Makes no sense as we should not specify the outputs in ct.convert() via PyTorch if I understand correctly)
while the JIT tracing seems fine.

Setup:

Torch version : 1.5.0
CoreML tools version : 4.0b1

Logs (2):

useScriptingFlag = False AND "if 0" in def forward(self, x_):
All fine for the JIT tracing, no Log.

useScriptingFlag = False AND "if 1" in def forward(self, x_):
All fine for the JIT tracing, no Log.

==================================================================
useScriptingFlag = True AND "if 0" in def forward(self, x_):

Torch version : 1.5.0
CoreML tools version : 4.0b1
TestModel(
(fc1): Linear(in_features=28, out_features=10, bias=True)
)
~/Library/Python/3.7/lib/python/site-packages/torch/jit/__init__.py:1256: UserWarning: `optimize` is deprecated and has no effect. Use `with torch.jit.optimized_execution() instead
warnings.warn("`optimize` is deprecated and has no effect. Use `with torch.jit.optimized_execution() instead")
Converting Frontend ==> MIL Ops: 40%|███████████████████████████████████████████████████████████████████████████████████████████████████▏ | 2/5 [00:00<00:00, 4957.81 ops/s]
Traceback (most recent call last):
File "testScripting.py", line 44, in
inputs= [ ct.TensorType(name="input1", shape=dummy_input.shape) ]
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/_converters_entry.py", line 299, in convert
**kwargs
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/converter.py", line 120, in _convert
prog = frontend_converter(model, **kwargs)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/converter.py", line 62, in __call__
return load(*args, **kwargs)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 84, in load
raise e
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 76, in load
prog = converter.convert()
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 302, in convert
convert_nodes(self.context, self.graph)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 52, in convert_nodes
"PyTorch convert function for op {} not implemented".format(node.kind)
RuntimeError: PyTorch convert function for op dim not implemented

==================================================================
useScriptingFlag = True AND "if 1" in def forward(self, x_):

Torch version : 1.5.0
CoreML tools version : 4.0b1
TestModel(
(fc1): Linear(in_features=28, out_features=10, bias=True)
)
testScripting.py:16: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
x = x_[: mySeqLen, :]
~/Library/Python/3.7/lib/python/site-packages/torch/jit/__init__.py:1256: UserWarning: `optimize` is deprecated and has no effect. Use `with torch.jit.optimized_execution() instead
warnings.warn("`optimize` is deprecated and has no effect. Use `with torch.jit.optimized_execution() instead")
Traceback (most recent call last):
File "testScripting.py", line 44, in
inputs= [ ct.TensorType(name="input1", shape=dummy_input.shape) ]
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/_converters_entry.py", line 299, in convert
**kwargs
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/converter.py", line 120, in _convert
prog = frontend_converter(model, **kwargs)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/converter.py", line 62, in __call__
return load(*args, **kwargs)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 73, in load
converter = TorchConverter(torchscript, inputs, outputs, cut_at_symbols)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 142, in __init__
raw_graph, params_dict, self.inputs, cut_at_symbols
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/internal_graph.py", line 176, in __init__
self.nodes.append(InternalTorchIRNode(raw_node))
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/internal_graph.py", line 97, in __init__
self.name = self.outputs[0]
IndexError: list index out of range

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.