coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

Cannot convert Sentence Transformer into coreml

Open hassanzadeh opened this issue 1 year ago • 2 comments

❓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?

hassanzadeh avatar Jan 05 '24 03:01 hassanzadeh

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.

TobyRoseman avatar Jan 05 '24 18:01 TobyRoseman

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

hassanzadeh avatar Jan 06 '24 01:01 hassanzadeh