PyTorch convert function for op 'dictconstruct' not implemented
- Dominant language
- Python
- Stars
- 5.4k
- Forks
- 850
- Avg merge
- 4d 5h
- Merged PRs (30d)
- 10
Description
Hi
I'm trying to convert RobertaForMaskedLM into coreml model, but I encountered a runtime error : PyTorch convert function for op 'dictconstruct' not implemented. My code shows as below:
```
from transformers import RobertaTokenizer, RobertaForMaskedLM
import torch
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
model = RobertaForMaskedLM.from_pretrained('roberta-base')
example_input = torch.randint(1,100,(1,512))
model.cpu()
model.eval()
traced_model = torch.jit.trace(model, example_input ,strict=False)
traced_model.eval()
traced_model.save('./model_RoBERTa.pt')
# coreml conversion
from coremltools.converters.mil import register_torch_op
from coremltools.converters.mil.frontend.torch.ops import _get_inputs
from coremltools.converters.mil.mil import Builder as mb
@register_torch_op
def type_as(context, node):
inputs = _get_inputs(context, node)
context.add(mb.cast(x=inputs[0], dtype='int32'), node.name)
import numpy as np
mlmodel = ct.convert(traced_model, inputs=[ct.TensorType(name="input", shape=example_input.shape, dtype=np.int32)])
```
Add @register_torch_op because this issue: https://github.com/apple/coremltools/issues/960, if not added, "'type_as' not implemented" error will be showed.
environment:
coremltools 4.1
pytorch 1.8.1
Thanks for your help in advance.
Contributor guide
Research direction
Reproduce the report with the provided RobertaForMaskedLM tracing and ct.convert example, starting from coremltools.converters.mil.frontend.torch.ops and the register_torch_op entry point. Done means this example no longer raises “dictconstruct” not implemented during conversion; keep the reported coremltools 4.1 and PyTorch 1.8.1 versions in the reproduction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- devtools, machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100