PyTorch's BatchNorm2d causes script conversion error
- Dominant language
- Python
- Stars
- 5.4k
- Forks
- 850
- Avg merge
- 4d 5h
- Merged PRs (30d)
- 10
Description
PyTorch's *BatchNorm2d* in
https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/batchnorm.py
causes issues on *PyTorch 1.9.1* - *1.11* with *Python 3.8* and *CoreMLTools 5.2.0*.
```
import coremltools as ct
import torch
from torch import nn, Tensor
import sys
class MyBatchNormModule1(nn.Module):
def __init__(self):
super().__init__()
self.bn = nn.BatchNorm2d(100, affine=False)
def forward(self, x: Tensor)->Tensor:
res: Tensor = self.bn(x)
return res
if __name__ == '__main__':
t1 = MyBatchNormModule1()
t1.eval()
x = torch.rand(20, 100, 35, 45)
t = torch.jit.script(t1)
result = t1(x)
model = ct.convert(t, inputs=[ct.TensorType(shape=x.shape)], convert_to='mlprogram', debug=True)
sys.exit(0)
```
The error is an `AssertionError: assert len(cond) == len(node.outputs)` in *converters/mil/frontend/torch/ops.py*
https://github.com/apple/coremltools/blob/main/coremltools/converters/mil/frontend/torch/ops.py
```
@register_torch_op(torch_alias=["if"])
def _if(context, node):
```
Contributor guide
Research direction
Run the supplied Python reproducer with the listed PyTorch and CoreMLTools versions, then inspect coremltools/converters/mil/frontend/torch/ops.py at _if and the len(cond) assertion. Done means the BatchNorm2d affine=False scripted model converts to an mlprogram without the reported AssertionError.
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