deepvac icon indicating copy to clipboard operation
deepvac copied to clipboard

CoreML -- AttributeError: 'torch._C.Node' object has no attribute 'ival'

Open MHGL opened this issue 4 years ago • 1 comments

🐞Describe the bug

I got this error when convert coremlmodel after torch.jit.freeze

Trace

Traceback (most recent call last):
  File "mini_code.py", line 15, 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 146, in __init__
    self.graph = InternalTorchIRGraph(
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/internal_graph.py", line 241, in __init__
    new_node = InternalTorchIRNode(raw_node, parent=self)
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/internal_graph.py", line 140, in __init__
    self.attr = {
  File "/home/liyang/.local/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/internal_graph.py", line 141, in <dictcomp>
    name: getattr(node, node.kindOf(name))(name)
AttributeError: 'torch._C.Node' object has no attribute 'ival'

To Reproduce

  • If a python script can reproduce the error, please paste the code snippet
import torch
import coremltools as ct

# init maxpool module
torch_model = torch.nn.Conv2d(3, 3, 1, 1)

# Trace with random data
example_input = torch.rand(1, 3, 224, 224) 
trace_model = torch.jit.trace(torch_model, example_input).eval()
freeze_model = torch.jit.freeze(trace_model)

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

System environment (please complete the following information):

  • coremltools version (e.g., 3.0b5): 4.1
  • OS (e.g., MacOS, Linux): Ubuntu20.04 LTS
  • How you install python (anaconda, virtualenv, system): miniconda
  • python version (e.g. 3.7): 3.8.5
  • any other relevant information:
    • pytorch version: 1.9.0
    • gpu: GeForce GTX 1650
    • driver: Driver Version: 460.80
    • CUDA: CUDA Version: 11.2

MHGL avatar Jul 02 '21 11:07 MHGL

I get an exciting idea, i try to save freeze_model to disk, then use torch.jit.load get freeze_model before convert coreml, this problem not happened! like this:

import torch
import coremltools as ct

# init maxpool module
torch_model = torch.nn.Conv2d(3, 3, 1, 1)

# Trace with random data
example_input = torch.rand(1, 3, 224, 224) 
trace_model = torch.jit.trace(torch_model, example_input).eval()
freeze_model = torch.jit.freeze(trace_model)

# save freeze model to disk
freeze_model.save("tmp.trace.pt")
# get freeze_model from disk
freeze_model = torch.jit.load("tmp.trace.pt")

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

MHGL avatar Jul 07 '21 04:07 MHGL