apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.33k stars 627 forks source link

Cannot convert Sentence Transformer into coreml #2104

Open hassanzadeh opened 8 months ago

hassanzadeh commented 8 months ago

❓Question

I'm trying to convert this model: https://huggingface.co/hkunlp/instructor-large into the coreml format, however, I can't make it work, one reason is that the input and output are both dicts, can you please help me understand how that model can be stored in coreml format?

TobyRoseman commented 8 months ago

Yes, coremltools currently does not support PyTorch models with dictionary inputs or outputs. However it's fairly simple to workaround this limitation, just create a wrapper PyTorch model that converts to/from dictionaries.

Something like the following:

class MyWrapper(nn.Module):
    def __init__(self):
        super().__init__()
        self.baseModel = <. . . . . .>

    def forward(self, value1, value2, value3):
        baseModelInput = {'inputKey1': value1, 'inputKey2': value2, 'inputKey3': value3}
        result = self.baseModel(baseModelInput)
        return (result['outputKey1'], result['outputKey2'])

Then trace and convert an instance of your wrapper model.

hassanzadeh commented 8 months ago

Thanks, I still get an error, ValueError: _internal_op_tensor_inplace_fill does not support dynamic index And it does not tell me exactly which line that operation is being performed. Is there a solution to that (eg, conversion to onnx first)?

Thanks