Skip to content

[Urgent] PyTorch GroupNorm and Meshgrid conversion fails for dynamic input #1303

Closed
@CanyonWind

Description

@CanyonWind

🐞Describe the bug

  • When converting a PyTorch model where GN is used and input is dynamic, the GN conversion fails.
  • The problem happens when converting PyTroch traced model -> CoreML
  • It seems like here h and w are specified for integers, but dynamic input model's h and w are placeholder.
  • Any advice/quick hack would be really appreciated.

Trace

Please run the code below to see the error.

To Reproduce

  • Here are the minimum code to reproduce the error
import torch
import torch.nn as nn
import coremltools as ct


class DynamicGN(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3,
                         stride=1, padding=1, bias=False)
        self.gn = nn.GroupNorm(num_groups=4, num_channels=16)
    def forward(self, x):
        y = self.gn(self.conv(x))
        return y

def main():
    model = DynamicGN()
    input = torch.ones((1,3,16,16))
    output = model(input)
    traced_model = torch.jit.trace(model, input, check_trace=True)
    img_shape = ct.Shape(shape=(1, 3, ct.RangeDim(8, 128), ct.RangeDim(8, 128)))
    img = ct.TensorType(name='image', shape=img_shape)
    mlmodel = ct.convert(model=traced_model, inputs=[img])

if __name__ == '__main__':
    main()

Error:

ValueError: Cannot add const [1, 4, 4, is2, is3]

System environment (please complete the following information):

  • coremltools version (e.g., 3.0b5): 5.0b2
  • OS (e.g., MacOS, Linux): MacOS 11.3.1
  • macOS version (if applicable):
  • XCode version (if applicable):
  • How you install python (anaconda, virtualenv, system):
  • python version (e.g. 3.7): 3.8.5
  • any other relevant information:
    • PyTorch 1.9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions