apple / apple/coremltools

AttributeError: 'list' object has no attribute 'val'

Open
#2,160 4 comments 0 reactions 0 assignees View on GitHub
bug PyTorch (traced) triaged
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## 🐞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
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
```python
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

Contributor guide

Open the contributing guide

Research direction

Start with coremltools/converters/mil/frontend/torch/ops.py at the pad conversion path around line 1665, then reproduce the failure using the Python script and listed PyTorch versions. Trace the value passed as pad and verify that converting the provided TorchScript model completes without the AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.