AttributeError: 'list' object has no attribute 'val'
🐞Describing the bug
- When I try to convert torchscript model to core ML model using coremltools. I get the following bug:
- 'AttributeError: 'list' object has no attribute 'val'
Stack Trace
File "/Users/deepak/startup/SnapTagger/ai/app.py", line 33, in <module>
vision_model = ct.convert(
^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/_converters_entry.py", line 574, in convert
mlmodel = mil_convert(
^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/mil/converter.py", line 188, in mil_convert
return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/mil/converter.py", line 212, in _mil_convert
proto, mil_program = mil_convert_to_proto(
^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/mil/converter.py", line 286, in mil_convert_to_proto
prog = frontend_converter(model, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/mil/converter.py", line 108, in __call__
return load(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 80, in load
return _perform_torch_convert(converter, debug)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 99, in _perform_torch_convert
prog = converter.convert()
^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 519, in convert
convert_nodes(self.context, self.graph)
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 88, in convert_nodes
add_op(context, node)
File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 1665, in pad
if pad.val is not None:
^^^^^^^
AttributeError: 'list' object has no attribute 'val'
To Reproduce
from transformers import ViltProcessor, ViltForQuestionAnswering
from PIL import Image
import sys
import torch, os, psutil
import coremltools as ct
processor = ViltProcessor.from_pretrained("MariaK/vilt_finetuned_200")
image = Image.open(sys.argv[1])
question = "Where is the man?"
inputs = processor(image, question, return_tensors="pt")
model = ViltForQuestionAnswering.from_pretrained("MariaK/vilt_finetuned_200")
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
idx = logits.argmax(-1).item()
print("Predicted answer:", model.config.id2label[idx])
traced_vision_model = torch.jit.trace(model, [inputs.input_ids, inputs.attention_mask, inputs.token_type_ids, inputs.pixel_values, inputs.pixel_mask], strict= False)
torch.jit.save(traced_vision_model, "traced_vision_model.pt")
loaded_model = torch.jit.load("traced_vision_model.pt")
vision_model = ct.convert(
loaded_model,
inputs=[
ct.TensorType(shape = inputs.input_ids.shape, name="input_ids"),
ct.TensorType(shape = inputs.attention_mask.shape, name="attention_mask"),
ct.TensorType(shape = inputs.token_type_ids.shape, name="token_type_ids"),
ct.TensorType(shape = inputs.pixel_values.shape, name="pixel_values"),
ct.TensorType(shape = inputs.pixel_mask.shape, name="pixel_mask"),
],
minimum_deployment_target=ct.target.iOS17,
)
vision_model.save("vilt.mlmodel")
System environment (please complete the following information):
- coremltools version:
- OS (e.g. MacOS version or Linux type): macOS Sonoma 14.3.1
- Any other relevant version information (e.g. PyTorch or TensorFlow version): PyTorch Version 2.1.0 and 2.2.1
Are you using the most recent version of coremltools?
Loading an untrusted PyTorch model is a security risk. So I'm not able to run your code.
If this is still an issue with the most recent version of coremltools, please give us self-contained code (i.e. doesn't load an external model) to reproduce the issue.
Yes I'm using coremltools v7.1
Ok I will try to provide a self-contained code
Hello, I am facing the same error. In my case, pad function called inside for loop causes the error. I am not sure this is the same cause with the @deepakHonakeri05's case, though. hope this is the only cause. Here is the self-contained code. Could you check this out?
I am using coremltools==7.2
import torch
import torch.nn as nn
import coremltools as ct
@torch.jit.script
def pad(x):
for i in range(3):
x = torch.nn.functional.pad(x, (0, x.size(-1)))
return x
# # It works without for loop
# @torch.jit.script
# def pad(x):
# x = torch.nn.functional.pad(x, (0, x.size(-1)))
# return x
class SimpleModule(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
padded_tensor = pad(x)
return padded_tensor
module = SimpleModule()
module.eval()
example_input = torch.rand(3, 4, 4)
traced_module = torch.jit.trace(module, example_input)
model = ct.convert(
traced_module,
convert_to="mlprogram",
inputs=[ct.TensorType(shape=example_input.shape)]
)
Here is the TorchScript graph.
print(pad.graph)
graph(%x.1 : Tensor):
%18 : Function = prim::Constant[name="pad"]()
%17 : NoneType = prim::Constant()
%16 : str = prim::Constant[value="constant"]()
%12 : int = prim::Constant[value=-1]() # /var/folders/c5/yknsh7rd1jz7schyq8qt4lp8000507/T/ipykernel_55333/923301787.py:9:50
%6 : bool = prim::Constant[value=1]() # /var/folders/c5/yknsh7rd1jz7schyq8qt4lp8000507/T/ipykernel_55333/923301787.py:8:4
%2 : int = prim::Constant[value=0]() # /var/folders/c5/yknsh7rd1jz7schyq8qt4lp8000507/T/ipykernel_55333/923301787.py:8:26
%3 : int = aten::size(%x.1, %2) # /var/folders/c5/yknsh7rd1jz7schyq8qt4lp8000507/T/ipykernel_55333/923301787.py:8:19
%x : Tensor = prim::Loop(%3, %6, %x.1) # /var/folders/c5/yknsh7rd1jz7schyq8qt4lp8000507/T/ipykernel_55333/923301787.py:8:4
block0(%i : int, %x.15 : Tensor):
%13 : int = aten::size(%x.15, %12) # /var/folders/c5/yknsh7rd1jz7schyq8qt4lp8000507/T/ipykernel_55333/923301787.py:9:43
%15 : int[] = prim::ListConstruct(%2, %13)
%x.9 : Tensor = prim::CallFunction(%18, %x.15, %15, %16, %17) # /var/folders/c5/yknsh7rd1jz7schyq8qt4lp8000507/T/ipykernel_55333/923301787.py:9:12
-> (%6, %x.9)
return (%x)
Thanks @w4-jihunlorenzopark - I can reproduce the bug using your code.