Repository: apple/coremltools

Issue #1769: RuntimeError: PyTorch convert function for op 'repeat_interleave' not implemented.
Created: 2023-02-11T06:02:53Z
🐞Describing the bug
Another stumbling block as part of my journey to bring the detectron2 models to CoreML.
RuntimeError: PyTorch convert function for op 'repeat_interleave' not implemented.

Stack Trace
Converting PyTorch Frontend ==> MIL Ops:  75%|▊| 3/4 [00:00<00:00, 5647.63 ops/s

RuntimeError                              Traceback (most recent call last)
/var/folders/ss/ztzzqkcj7t163mv8b3y42m580000gn/T/ipykernel_2103/1247879774.py in 
14 example_input = torch.rand(1, 3, 640, 640)
15 image_input = ct.ImageType(name="input_1", shape=example_input.shape)
---> 16 coreml_model = ct.convert(
17     scripted_m,
18     source='pytorch',
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/_converters_entry.py in convert(model, source, inputs, outputs, classifier_config, minimum_deployment_target, convert_to, compute_precision, skip_model_load, compute_units, package_dir, debug)
442         specification_version = _set_default_specification_version(exact_target)
443
--> 444     mlmodel = mil_convert(
445         model,
446         convert_from=exact_source,
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/converter.py in mil_convert(model, convert_from, convert_to, compute_units, **kwargs)
185         See coremltools.converters.convert
186     """
--> 187     return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
188
189
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/converter.py in _mil_convert(model, convert_from, convert_to, registry, modelClass, compute_units, **kwargs)
209         kwargs["weights_dir"] = weights_dir.name
210
--> 211     proto, mil_program = mil_convert_to_proto(
212                             model,
213                             convert_from,
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/converter.py in mil_convert_to_proto(model, convert_from, convert_to, converter_registry, **kwargs)
279     frontend_converter = frontend_converter_type()
280
--> 281     prog = frontend_converter(model, **kwargs)
282
283     if convert_to.lower() != "neuralnetwork":
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/converter.py in call(self, *args, **kwargs)
107         from .frontend.torch import load
108
--> 109         return load(*args, **kwargs)
110
111
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/load.py in load(model_spec, inputs, specification_version, debug, outputs, cut_at_symbols, **kwargs)
55     inputs = _convert_to_torch_inputtype(inputs)
56     converter = TorchConverter(torchscript, inputs, outputs, cut_at_symbols, specification_version)
---> 57     return _perform_torch_convert(converter, debug)
58
59
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/load.py in _perform_torch_convert(converter, debug)
102             print("the following model ops are MISSING:")
103             print("\n".join(["  " + str(x) for x in sorted(missing)]))
--> 104         raise e
105
106     return prog
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/load.py in _perform_torch_convert(converter, debug)
94 def _perform_torch_convert(converter, debug):
95     try:
---> 96         prog = converter.convert()
97     except RuntimeError as e:
98         if debug and "convert function" in str(e):
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py in convert(self)
279
280             # Add the rest of the operations
--> 281             convert_nodes(self.context, self.graph)
282
283             graph_outputs = [self.context[name] for name in self.graph.outputs]
~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/ops.py in convert_nodes(context, graph)
82         logger.info("Converting op {} : {}".format(node.name, node.kind))
83         if add_op is None:
---> 84             raise RuntimeError(
85                 "PyTorch convert function for op '{}' not implemented.".format(node.kind)
86             )
RuntimeError: PyTorch convert function for op 'repeat_interleave' not implemented.
To Reproduce
import torch
import torch.nn as nn
import coremltools as ct

class M(nn.Module):
    def __init__(self):
        super(M, self).__init__()

    def forward(self, x):
        return x.repeat_interleave(2)

m = M().eval()
example_input = torch.rand(1, 3, 640, 640)
scripted_m = torch.jit.trace(m, example_input)
scripted_m(example_input) # this is works
image_input = ct.ImageType(name="input_1", shape=example_input.shape)
coreml_model = ct.convert(
    scripted_m,
    source='pytorch',
    inputs=[image_input]
)
System environment (please complete the following information):

coremltools version: 6.2 (also fails in 6.1)
OS (e.g. MacOS version or Linux type): MacOS

Duplicate issue #1895: PyTorch convert function for op 'repeat_interleave' not implemented.
Created: 2023-06-28T09:53:52Z
Name of layer type: repeat_interleave
Is this a PyTorch or a TensorFlow layer type: Pytorch
Your version of coremltools: 6.3
Your version of PyTorch/TensorFlow: 2.0.1
Impact of supporting this layer type. Why is adding support for this layer type important? Is it necessary to support a popular model or use case? Would allow conversion of the MobileSAM. The model they made is gaining popularity fast

Minimal Code:
import torch
import coremltools as ct

class MyModel(torch.nn.Module):

    def __init__(self):
        super().__init__()

    def forward(self, x):
        return torch.repeat_interleave(x, 3, dim=0)

model = MyModel()

x = torch.rand(2,2)

with torch.no_grad():
    traced_model = torch.jit.trace(model, example_inputs=x)

ct.convert(traced_model, inputs=[ct.TensorType(name="x", shape=(2,2))],source="pytorch")
Error:
PyTorch convert function for op 'repeat_interleave' not implemented.

Closing comment (2023-06-28T19:13:40Z): This is a duplicate of #1769.