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.44k stars 643 forks source link

temporary: the only valid use of a module is looking up an attribute but found = prim::SetAttr[name="num_batches_tracked"](%self, %11) #1243

Open MHGL opened 3 years ago

MHGL commented 3 years ago

🐞Describe the bug

I got this error while: nn.BatchNorm2d -> torch.jit.script -> coreml

Trace

Traceback (most recent call last):
  File "mini_code.py", line 13, in <module>
    model = ct.convert(
  File "/opt/conda/lib/python3.8/site-packages/coremltools/converters/_converters_entry.py", line 175, in convert
    mlmodel = mil_convert(
  File "/opt/conda/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 "/opt/conda/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 171, in mil_convert_to_proto
    prog = frontend_converter(model, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 85, in __call__
    return load(*args, **kwargs)
  File "/opt/conda/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 "/opt/conda/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 "/opt/conda/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="num_batches_tracked"](%self, %11)
:

To Reproduce

import torch
import coremltools as ct

# init torch module
torch_model = torch.nn.BatchNorm2d(3)

# 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):

bensyverson commented 3 years ago

Also running into this with TorchScript models from BackgroundMattingV2...

The error:

Traceback (most recent call last):
  File "/Users/ben/git/BackgroundMattingV2/coreml-torchscript-simple.py", line 10, in <module>
    ct_model = ct.convert(
  File "/usr/local/Caskroom/miniconda/base/envs/coreml/lib/python3.9/site-packages/coremltools/converters/_converters_entry.py", line 176, in convert
    mlmodel = mil_convert(
  File "/usr/local/Caskroom/miniconda/base/envs/coreml/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 128, in mil_convert
    proto = mil_convert_to_proto(model, convert_from, convert_to,
  File "/usr/local/Caskroom/miniconda/base/envs/coreml/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 171, in mil_convert_to_proto
    prog = frontend_converter(model, **kwargs)
  File "/usr/local/Caskroom/miniconda/base/envs/coreml/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 85, in __call__
    return load(*args, **kwargs)
  File "/usr/local/Caskroom/miniconda/base/envs/coreml/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 72, in load
    converter = TorchConverter(torchscript, inputs, outputs, cut_at_symbols)
  File "/usr/local/Caskroom/miniconda/base/envs/coreml/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 142, in __init__
    raw_graph, params_dict = self._expand_and_optimize_ir(self.torchscript)
  File "/usr/local/Caskroom/miniconda/base/envs/coreml/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 250, 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="num_batches_tracked"](%5180, %5183)

Relevant code:

import torch
import coremltools as ct

precision = torch.float16

src = torch.rand(1, 3, 2160, 3840).to(precision)
bgr = torch.rand(1, 3, 2160, 3840).to(precision)

ct_model = ct.convert(
    "models/torchscript_resnet101_fp16.pth",
    inputs=[ct.ImageType(name="input_source", shape=src.shape), ct.ImageType(name="input_bg", shape=bgr.shape)]
)

System environment (please complete the following information):