-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
distilbert-onnx-coreml.py "works" for BERT, but I get "Error computing NN outputs." when predicting #16
Comments
Actually, I get the same error just trying to run distilbert-onnx-coreml.py unchanged. I had to update the "target_ios" param to "minimum_ios_deployment_target" to get the script to run, so perhaps the script isn't compatible with the current version of coremltools?
|
That’s very possible. What versions of XCode and Mac OS are you running? Do you get the same error on an iOS device? |
MacOS 10.15.2 and Xcode Version 11.3 (11C29). Running on an Xcode simulator I go the same error on my custom model with a bit more detail: "Cannot squeeze a dimension whose value is not 1", with the relevant dimension being 64. So somehow the shape was wrong when it got to that operation. In the interim I've moved on to translating the model from a TF version directly to coreml using tfcoreml. |
@aarmstrong78 Did you ever solve this problem? I have the same error message ( |
Hi @jbmaxwell , no I didn't. After a lot of effort managed to get the TensorFlow version of Bert to convert into CoreML instead. |
Just as an update, the model I was using above was based on the Pytorch-pretrianed-BERT repo, so I tried the huggingface distilBert model, but I get exactly the same error. (It's seeming like the problem is coming from onnx.) |
Another update on my |
If you can share the mlmodel file with the error, I can take a look. |
@hollance That would be extremely helpful, thanks! The error for from CoreML (in Xcode) is:
Just to note, I just ran that through my training script for a couple of epochs, as a test. |
Run this script to fix the issue:
The issue was the squeeze and whereNonZero layers at the beginning of the model. This script replaces them with harmless linear activation layers. |
Thanks so much, I'll give this a try! Do you understand why the squeeze was there in the first place? Searching ps - I naively tried removing the |
It probably got inserted in the ONNX conversion step. There are most likely a whole bunch of other layers that don't really need to be in there. :-D |
Wow, okay. It really is the Wild West! It would be great if Apple would throw a little more of its multi-billion dollar steam behind this process. It's generally pretty painful getting from PyTorch to CoreML, in my experience. Thanks again. |
To be fair @bhushan23 from onnx-coreml helped us a lot so far :) |
Absolutely! ONNX is invaluable, since presumably this wouldn't be possible at all without it. But by taking a proprietary approach, Apple must surely have realized there would be a significant demand on resources/human-hours trying to stay current in such a rapidly evolving field. Anyway, I don't mean to complain, it's just that it can be tricky enough getting a model working, only to face another significant (and sometimes insurmountable) hurtle in trying to get it converted to an mlmodel. |
So after testing the model on iOS I noticed that the outputs weren't the same. Trying to debug the problem led me to try |
ONNX and Core ML do not have 100% the same features, so it's possible your ONNX model contains an operator that is not supported by Core ML. Sometimes you can work around this by removing the offending operator from the ONNX model by hand. |
Okay, understood. Just out of curiosity, I decided to try modifying |
It seems clear that I'm blocked for now on DistilBert since a working ("working" as in, giving accurate results in So I'm trying to get regular Bert running, for now. I trained the model and I can start a new issue, if that's better. |
I'm not sure if this repo is still being maintained or not since the last commit seems to be several months ago but I did manage to get the distilbert-onnx-coreml.py to convert a finetuned distilbert model using opset10 that seems to give decent results. It basically involves iterating through the model and manually modifying the squeeze layers to use the axis with dimension 1. I can open a pull request to modify the script if that's something people would want. Just figured i'd throw this up here in case anyone has had this same issue because I didn't find any answer to this when googling around. |
Yeah, I've been noticing the silence of the onnx/conversion-related repos recently, which is pretty easily explained by the new PyTorch support in CoreML 4. That's also kind of incomplete, in my (limited) experience—e.g., no leaky_relu conversion—but very promising. (And obviously it's very new!) I'd be interested in distilbert, for sure, but I think waiting a little longer on CoreML 4 is worthwhile. |
@calderma We are not proactive in maintaining this repo, but if you have a fix for current version of CoreML or related tools, by all means, please submit it. |
Hello I decided i'm just going to post an example code snippet and people can modify it to their needs because i think it's not a fix that would be uniform for every use case but people should be able to modify it to their needs. Using this after converting the model via pytorch->onnx->coreml in the distilbert-onnx-coreml.py script i was able to get it to run on device and get similar results to the pytorch model. If it doesn't work for you post in this issue and i can try to help.
|
I am also facing the same issue with MobileDet Model
Was anyone able to find the solution for this. |
Hi,
I used distilbert-onnx-coreml.py to convert a custom PyTorch BertForSequenceClassification model to CoreML. The conversion finishes without error.
However I can't use the resulting CoreML model for prediction. The following code fails:
Note, my input dim is 64:
When I try to substitute my model into the DistilBERT demo app, I get the following error in Xcode when predicting:
The only hint that something might have gone wrong in the onnx->coreml conversion is a note about a deleted node, however I'm struggling to find out whether this is just a red herring:
Are there any particular layers that need custom conversion in BERT into coreml? Any suggestions on further debugging?
Thanks.
The text was updated successfully, but these errors were encountered: