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.46k stars 648 forks source link

temporary: the only valid use of a module is looking up an attribute but found = prim::SetAttr[name="output"](%self, %x.1) #1242

Open MHGL opened 3 years ago

MHGL commented 3 years ago

🐞Describe the bug

torch_model -> torch.jit.script -> coreml
I got this error while try to setattr in forward.

Trace

Traceback (most recent call last):
  File "mini_code.py", line 21, in <module>
    model = ct.convert(
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/_converters_entry.py", line 175, in convert
    mlmodel = mil_convert(
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 128, in mil_convert
    proto = mil_convert_to_proto(model, convert_from, convert_to,
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 171, in mil_convert_to_proto
    prog = frontend_converter(model, **kwargs)
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 85, in __call__
    return load(*args, **kwargs)
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 70, in load
    converter = TorchConverter(torchscript, inputs, outputs, cut_at_symbols)
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 145, in __init__
    raw_graph, params_dict = self._expand_and_optimize_ir(self.torchscript)
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 262, in _expand_and_optimize_ir
    graph, params = _torch._C._jit_pass_lower_graph(
RuntimeError: 
temporary: the only valid use of a module is looking up an attribute but found  = prim::SetAttr[name="output"](%self, %x.1)
:

To Reproduce

import torch
import coremltools as ct

# init torch module
class MyModule(torch.nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()
        self.output: torch.Tensor = torch.empty(1)

    def forward(self, x):
        self.output = x
        return

torch_model = MyModule()

# script
script_model = torch.jit.script(torch_model)

# Convert to Core ML using the Unified Conversion API
model = ct.convert(
    script_model,
    inputs=[ct.ImageType(name="input", shape=(1, 3, 224, 224))],
)

System environment (please complete the following information):

TobyRoseman commented 2 years ago

With coremltools 6.0 and torch 1.21.1, this error is fixed.

However we get a new error due to the fact that the forward method is not returning any value. If we update forward to return x, we another error: RuntimeError: PyTorch convert function for op 'setattr' not implemented.

That is caused by this line in forward: self.output = x. Given that MLModel don't have state, I don't think this is something we can support.