Torch var not found in context - although variable is class attribute
- Dominant language
- Python
- Stars
- 5.4k
- Forks
- 850
- Avg merge
- 4d 5h
- Merged PRs (30d)
- 10
Description
## ❓Converting simple PyTorch model via TorchScript to CoreML fails, as a member variable cannot be found in forward() call
## System Information
- Mac Mini M1 (2020)
- MacOS 11.6
- Python 3.9
- PyTorch 1.9.1
- CoreMLTools 5.1.0
I am a newbie in converting models and maybe someone please can tell me, why the *self.attribute1* value can not be found in the *forward()* call? How can I work around that error?
```python
import torch
import torch.nn as nn
from torch import Tensor
from typing import Optional, Dict, List
import coremltools as ct
print(torch.__version__) # 1.9.1
print(ct.__version__) # 5.1.0
class SimpleTest(nn.Module):
def __init__(self) -> None:
super().__init__()
self.attribute1: float = 42.
def forward(self, x: Tensor) -> Tensor:
fillval: float = 42. # this alone works
fillval: float = self.attribute1 # ValueError: Torch var fillval.3 not found in context
some_result = torch.full((3, 550, 550), fillval)
return some_result
test_batch = torch.rand(1,3,550,550)
m = SimpleTest()
m.eval()
result = m(test_batch)
print('1 Before torch-script -------------------')
s = torch.jit.script(m)
print('2 Before coreml-convert -------------------')
c = ct.convert(s,
inputs=[ct.TensorType(name="test", shape=test_batch.shape)],
source='auto',
minimum_deployment_target=ct.target.iOS15,
compute_units=ct.ComputeUnit.CPU_ONLY,
compute_precision=ct.precision.FLOAT32,
convert_to='mlprogram',
debug=True
)
```
Contributor guide
Research direction
Run the provided Python example with the listed PyTorch 1.9.1 and CoreMLTools 5.1.0 versions, starting at SimpleTest.forward, torch.jit.script(m), and ct.convert(s). Compare the literal fillval case with the self.attribute1 case; done means the reported context error is explained and the conversion behavior or workaround is verified.
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
- Mostly clear
- Newbie friendliness
- 45/100