apple / apple/coremltools

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

Open
#1,243 1 comment 0 reactions 0 assignees View on GitHub
bug triaged
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## 🐞Describe the bug
I got this error while:
nn.BatchNorm2d -> torch.jit.script -> coreml

## Trace
```python3
Traceback (most recent call last):
File "mini_code.py", line 13, in
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):
- 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

Contributor guide

Open the contributing guide

Research direction

Reproduce the BatchNorm2d conversion from the provided script, then start in coremltools/converters/mil/frontend/torch/load.py and converter.py around _expand_and_optimize_ir and the torch lower-graph call. Compare the generated TorchScript graph with the reported prim::SetAttr error. Done means this repro converts successfully without the runtime error, with regression coverage for the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.