apple / apple/coremltools

Error "argument 'input' (position 1) must be Tensor, not torch._C.ScriptObject" when converting Language Model in Pytorch

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

Description

## ❓Question
Using: Coremltools 6.2

I'm getting this error although the input i use for the model is torch.Tensor.
(When i print the type of the input i forward to the model i get "").
And yes, i did eval and trace.

When i trace the model i get this warning in console:
`/miniconda3/lib/python3.9/site-packages/torch/jit/_trace.py:753: UserWarning: The input to trace is already a ScriptModule, tracing it is a no-op. Returning the object as is.
`

This is the error when i run convert:

```
ml_model = ct.convert(
traced_model,
inputs=[ct.TensorType(name="input", shape=tokens_tensor.shape, dtype=np.int32)],
)

```
Error:
```
Support for converting Torch Script Models is experimental. If possible you should use a traced model for conversion.
Traceback (most recent call last):
File "/Documents/Projects/d.py", line 114, in
cml_model = ct.convert(
File "/miniconda3/lib/python3.9/site-packages/coremltools/converters/_converters_entry.py", line 444, in convert
mlmodel = mil_convert(
File "/miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 187, in mil_convert
return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
File "/miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 211, in _mil_convert
proto, mil_program = mil_convert_to_proto(
File "//miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 281, in mil_convert_to_proto
prog = frontend_converter(model, **kwargs)
File "/miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 109, in __call__
return load(*args, **kwargs)
File "//miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 56, in load
converter = TorchConverter(torchscript, inputs, outputs, cut_at_symbols, specification_version)
File "/miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 169, in __init__
raw_graph, params_dict = self._expand_and_optimize_ir(self.torchscript)
File "/miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 489, in _expand_and_optimize_ir
graph, params_dict = TorchConverter._jit_pass_lower_graph(graph, torchscript)
File "//miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 434, in _jit_pass_lower_graph
_lower_graph_block(graph)
File "//miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 399, in _lower_graph_block
_lower_graph_block(block)
File "/miniconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 427, in _lower_graph_block
assert _torch.equal(module, params_dict[prefix])
TypeError: equal(): argument 'input' (position 1) must be Tensor, not torch._C.ScriptObject
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the reported ct.convert call with the traced model and inspect the TorchScript frontend entry points named in the traceback: converters/mil/frontend/torch/load.py and converter.py, especially _jit_pass_lower_graph. Determine why a ScriptObject reaches torch.equal instead of a Tensor; done means the conversion either succeeds for this model or the unsupported case is clearly identified with a focused regression test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.