FastAI Model to CoreML: RuntimeError: PyTorch convert function for op 'alias' not implemented

Hello - enjoying my time with FastAI, and working toward testing the FastAI image recognition model in iOS App. In order to efficiently run with ease in iOS, these models need to be converted to CoreML model. I have done this in the past with coremltools with Keras.

here is my code which produces the error

learn = torch.load("./models/")
traced_land_model = torch.jit.trace(learn, x)
# torch.jit._trace.TopLevelTracedModule

import coremltools as ct
# Using image_input in the inputs parameter:
# Convert to Core ML using the Unified Conversion API.
model = ct.convert(
# here is stack trace
Converting Frontend ==> MIL Ops:   6%|▌         | 14/246 [00:00<00:00, 2571.84 ops/s]
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_65689/ in <module>
      6     #inputs=[ct.TensorType(shape=example_input.shape)]
      7     traced_land_model,
----> 8     inputs=[ct.TensorType(shape=x.shape)]
      9  )

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/ in convert(model, source, inputs, outputs, classifier_config, minimum_deployment_target, convert_to, compute_precision, skip_model_load, compute_units, useCPUOnly, package_dir, debug)

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/mil/ in mil_convert(model, convert_from, convert_to, compute_units, **kwargs)

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/mil/ in _mil_convert(model, convert_from, convert_to, registry, modelClass, compute_units, **kwargs)

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/mil/ in mil_convert_to_proto(model, convert_from, convert_to, converter_registry, **kwargs)

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/mil/ in __call__(self, *args, **kwargs)

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ in load(model_spec, debug, **kwargs)

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ in _perform_torch_convert(converter, debug)

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ in _perform_torch_convert(converter, debug)

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ in convert(self)

/anaconda/envs/fastai2/lib/python3.7/site-packages/coremltools/converters/mil/frontend/torch/ in convert_nodes(context, graph)

RuntimeError: PyTorch convert function for op 'alias' not implemented.

In the coremltools docs, it should a workable way to convert, but doesn’t appear to be working with FastAI. PyTorch Conversion. Any insight appreciated.

Any chance you could show the full trace error it gives you?

@muellerzr - I just added the full trace to original post now. thank you

Doesn’t really help solve your problem, but my guess is the alias pytorch function needs to be lowered to torchscript for this to work. maybe open a pytorch bug. (I have the feeling apple would just punt it over to the pytorch guys)

I logged a bug like this a while back which feels pretty similar. when converting scripted model to coremltools, Unknown type bool encountered in graph lowering. This type is not supported in ONNX export. · Issue #1085 · apple/coremltools · GitHub

I encountered the same problem a few weeks ago. Turns out the problem was somehow triggered by the Flatten layer, which in my case was used after an AdaptiveConcatPool2d. Removing the TensorBase “cast” made it go away.

If this sounds familiar, read on for my solution.

Instead of modifying the fastai source, I patched my model replacing the layer with an alternative implementation.

First, I found where the Flatten layer was being used:

>>> model_to_export[0][-1]

  (0): AdaptiveConcatPool2d(
    (ap): AdaptiveAvgPool2d(output_size=1)
    (mp): AdaptiveMaxPool2d(output_size=1)
  (1): Flatten(full=False)
  (2): BatchNorm1d(4096, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
... other unrelated layers

>>> model_to_export[0][-1][1]


Then I created a replacement Flatten. (I called it CopyFlatten because my initial intention was to copy the data, but the name doesn’t matter).

# This is a delightful way to create a subclass of `torch.nn.Module`
from fastai.layers import module

def CopyFlatten(self, x):
    "Flatten `x` to a single dimension, e.g. at end of a model. `full` for rank-1 tensor"
    return x.view(-1) if self.full else x.view(x.size(0), -1)

Then I replaced the layer inside the model:

model_to_export[0][-1][1] = CopyFlatten()

Please, let me know if this helps, and good luck!

1 Like