ValueError: Torch var training.2 not found in context | LSTM model
- Dominant language
- Python
- Stars
- 5.4k
- Forks
- 850
- Avg merge
- 4d 5h
- Merged PRs (30d)
- 10
Description
## 🐞Describing the bug
I have a relatively simple LSTM model that I need to `jit.script()` because I will add if conditional on the input soon, right now I don't have any data-dependent conditionals. Tracing works for now, but I need scripting to work. Scripting itself succeeded but conversion didn't. My model has 2 inputs and 3 outputs (but I don't think that's the problem).
I do NOT have any `training` variables in my project.
## Stack Trace
```
Pytorch scripting...
Support for converting Torch Script Models is experimental. If possible you should use a traced model for conversion.
Tuple detected at graph output. This will be flattened in the converted model.
Converting PyTorch Frontend ==> MIL Ops: 10%|██████▉ | 30/315 [00:00<00:00, 1876.65 ops/s]
Traceback (most recent call last):
File "convert_to_coreml.py", line 43, in
debug=True
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/_converters_entry.py", line 456, in convert
specification_version=specification_version,
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/converter.py", line 190, in mil_convert
return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/converter.py", line 222, in _mil_convert
**kwargs
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/converter.py", line 282, in mil_convert_to_proto
prog = frontend_converter(model, **kwargs)
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/converter.py", line 112, in __call__
return load(*args, **kwargs)
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 57, in load
return _perform_torch_convert(converter, debug)
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 96, in _perform_torch_convert
prog = converter.convert()
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 270, in convert
convert_nodes(self.context, self.graph)
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 103, in convert_nodes
add_op(context, node)
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 4136, in noop
inputs = _get_inputs(context, node)
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 200, in _get_inputs
inputs = [context[name] for name in node.inputs]
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 200, in
inputs = [context[name] for name in node.inputs]
File "/opt/anaconda3/envs/PIP/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 79, in __getitem__
"Torch var {} not found in context {}".format(torch_name, self.name)
ValueError: Torch var training.2 not found in context
```
## To Reproduce
```
torch_model.eval()
example_input = (torch.rand(10, 72), torch.rand(15), torch.rand(72))
scripted_model = torch.jit.script(torch_model, example_inputs=[example_input])
model = ct.convert(
scripted_model,
source='pytorch',
inputs=[
ct.TensorType(shape=example_input[0].shape),
ct.TensorType(shape=example_input[1].shape),
ct.TensorType(shape=example_input[2].shape),
],
debug=True
)
```
## System environment (please complete the following information):
- coremltools version: 6.1
- OS (e.g. MacOS version or Linux type): 10.15.7 (MacOS Catalina)
- Any other relevant version information (e.g. PyTorch or TensorFlow version): Pytorch 1.12.1
## Additional context
- I can share reproducible example upon request I just need to refactor it...
Contributor guide
Research direction
Start with the PyTorch conversion path in coremltools/converters/mil/frontend/torch/load.py, converter.py, and ops.py, especially _get_inputs and noop where the stack trace fails. Obtain the promised reproducible LSTM example, reproduce the missing training.2 context variable, and verify that converting the scripted model completes without this ValueError.
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
- Needs clarification
- Newbie friendliness
- 30/100