apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.42k stars 640 forks source link

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

Open hacx opened 1 year ago

hacx commented 1 year ago

❓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 "<class 'torch.Tensor'>"). 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 <module>
    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
TobyRoseman commented 1 year ago

@hacx - I've never seen this error before. In order to help, we need to be able to reproduce the problem. Cat you give us a minimal example to reproduce the problem? Also let us know which version of PyTorch you are using.