coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

`pad = None` in `ops.py` throws error for checks that require iterables

Open gudgud96 opened this issue 1 year ago • 1 comments

🐞Describing the bug

Currently in converters/mil/frontend/torch/ops.py, for convolution, pad is set to None even if all paddings in convolution/transpose convolution are zeros. See this line of code.

For several checks in the code that requires pad to be an iterable, having it set to None will throw errors. An example is the sum check and pad copy.

In this case setting pad to [0] would work but I am not sure the intention behind using None instead.

Stack Trace

File "/Users/haohao/miniconda3/envs/xxx/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 1226, in _convolution
    if sum(pad) == 0 and any(output_padding):
TypeError: 'NoneType' object is not iterable

To Reproduce

  • Please add a minimal code example that can reproduce the error when running it.
# Paste Python code snippet here, complete with any required import statements.
import torch
from torch import nn


class SimpleUNetLike(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv1d(1, 16, 3, padding=0, stride=2)
        self.convt1 = nn.ConvTranspose1d(16, 1, 3, padding=0, stride=2)
    
    def forward(self, x):
        x = self.conv1(x)
        print("conv1", x.shape)
        x = self.convt1(x, output_size=(1024,))     # (1023,) if without output_padding
        print("convt1", x.shape)
        return x


if __name__ == "__main__":
    model = SimpleUNetLike()
    model.eval()
    x = torch.randn(1, 1, 1024)
    
    traced_model = torch.jit.trace(model, x)
    import coremltools as ct
    ct_model = ct.convert(
        traced_model,
        convert_to="mlprogram",
        inputs=[
            ct.TensorType(name="x", shape=x.shape),
        ],
        outputs=[
            ct.TensorType(name="y"),
        ],
    )

System environment (please complete the following information):

  • coremltools version: 8.0 (code still is problematic on current main branch)
  • OS (e.g. MacOS version or Linux type): Sonoma
  • Any other relevant version information (e.g. PyTorch or TensorFlow version):

Additional context

  • Add anything else about the problem here that you want to share.

gudgud96 avatar Oct 29 '24 09:10 gudgud96

Nice catch 🚀 I missed it and our unit test missed these cases

YifanShenSZ avatar Oct 31 '24 06:10 YifanShenSZ