apple / apple/coremltools

PyTorch to CoreML BatchNorm model SCRIPT conversion v4 fails with "temporary: the only valid use of a module is looking up an attribute but found = prim::SetAttr[name="num_batches_tracked"](%13, %80)"

Open
#817 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

Relevance:-----------------------------------------------------------

Firstly, while the JIT conversion of the attached test modeland code works fine, the torch.jit.script() fails on the BatchNorm conversion.
The conversion of both BatchNorm1D and BatchNorm2D fails which is a blocker for using the script mode and converting dynamic models.

Secondly, I have seen another error on prim::SetAttr mentioned in Issue #802 on FasterRCNN conversion which may or may not be related.

Reproducible:-----------------------------------------------------------

Yes

Testcase:-----------------------------------------------------------

Attached.
[testScripting.bn.txt](https://github.com/apple/coremltools/files/4976267/testScripting.bn.txt)

Run e.g. as

python3 -O testScripting.bn.py

We see the error with
useScriptingFlag = True
but you could compare to the JIT models and behavior when used with
useScriptingFlag = False

PS: Use can change
self.use1DBatchNorm = True
to check whether there is any difference in behavior on 1D or 2D BatchNorm.
Both give the same error.

Error message/ Log:-----------------------------------------------------------

Traceback (most recent call last):
File "testScripting.bn.py", line 64, in
inputs= [ ct.TensorType(name="input1", shape=dummy_input.shape) ]
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/_converters_entry.py", line 299, in convert
**kwargs
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/converter.py", line 120, in _convert
prog = frontend_converter(model, **kwargs)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/converter.py", line 62, in __call__
return load(*args, **kwargs)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 73, in load
converter = TorchConverter(torchscript, inputs, outputs, cut_at_symbols)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 140, in __init__
raw_graph, params_dict = self._expand_and_optimize_ir(self.torchscript)
File "~/Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 329, in _expand_and_optimize_ir
torchscript.forward.graph, torchscript._c
RuntimeError:
temporary: the only valid use of a module is looking up an attribute but found = prim::SetAttr[name="num_batches_tracked"](%13, %80)
:

Contributor guide

Open the contributing guide

Research direction

Start with the attached testScripting.bn.py testcase and run it with python3 -O, comparing useScriptingFlag=True and False for BatchNorm1D and BatchNorm2D. Trace the failure through coremltools/converters/mil/frontend/torch/converter.py at _expand_and_optimize_ir and confirm that scripted BatchNorm models convert without the prim::SetAttr error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.