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

Core ML Model performance far lower on iOS 17 vs iOS 16 (iOS 17 not using Neural Engine) #2004

Open mrdbourke opened 11 months ago

mrdbourke commented 11 months ago

🐞Describing the bug

Hello,

I'm not sure if this is 100% coremltools related or an iOS level issue.

But I've found since iOS 17 release that none of my Core ML models running on devices with iOS 17 use the neural engine, thus resulting in far slower performance.

The following screenshots show the performance of the same model (a PyTorch computer vision model) on an iPhone SE 3rd gen and iPhone 13 Pro (both use the A15 Bionic).

iOS 16 - iPhone SE 3rd Gen

iOS 16 uses the ANE and results in fast prediction, load and compilation times.

ios16-mlmodel-prediction-time

iOS 17 - iPhone 13 Pro

iOS 17 doesn't seem to use the ANE, thus the prediction, load and compilation times are all slower.

ios17-mlmodel-prediction-time

To Reproduce

The following is my code I'm using to export my PyTorch vision model.

I've used the same code for the past few months with sensational results on iOS 16.

# Convert to Core ML using the Unified Conversion API
coreml_model = ct.convert(
    model=traced_model,
    inputs=[image_input],
    outputs=[ct.TensorType(name="output")],
    classifier_config=ct.ClassifierConfig(class_names),
    convert_to="neuralnetwork",
    # compute_precision=ct.precision.FLOAT16,
    compute_units=ct.ComputeUnit.ALL
)

System environment:

Additional context

If anyone has a similar experience, I'd love to hear more.

Otherwise, if I'm doing something wrong for the exporting of models for iOS 17+, please let me know.

Thank you!

aseemw commented 11 months ago

Can you please share the CoreML model which runs on NE with iOS16 but not with iOS17 (on the same device). If everything else is same (model, device) and the only change is the iOS version , this would be a bug in the Core ML Framework (not coremltools since the model is same). You can either share the model here or file a bug report and attach it there.

mrdbourke commented 11 months ago

Hi @aseemw ,

Thank you for the response.

Please find the attached model:

2023-09-26_food_not_food_2_classes.mlmodel.zip

Yeah I think it may be a Core ML issue, see how you go with the model and if it doesn't work, I'll go ahead and try filing a bug report.

mrdbourke commented 11 months ago

Update: also posted this on the Apple Developer Forums, I'll report any further updates here too - https://developer.apple.com/forums/thread/739286

Moeflon commented 11 months ago

I faced a similar issue with iOS 17. Not only was my performance way worse, the output of the model was way off. In my case, the confidence column in a YOLOv8 model contained integer values orders of magnitude above 1.

I currently restored my model's performance (both in terms of speed and accuracy) on iOS 17 by downgrading coremltools to 6.2 and exporting to a .mlmodel instead of a .mlpackage. Perhaps this also solves your issues.

TobyRoseman commented 11 months ago

@Moeflon - Please create a new GitHub issue with steps to reproduce your problem that was solved by downgrading coremltools.

mrdbourke commented 11 months ago

I faced a similar issue with iOS 17. Not only was my performance way worse, the output of the model was way off. In my case, the confidence column in a YOLOv8 model contained integer values orders of magnitude above 1.

I currently restored my model's performance (both in terms of speed and accuracy) on iOS 17 by downgrading coremltools to 6.2 and exporting to a .mlmodel instead of a .mlpackage. Perhaps this also solves your issues.

@Moeflon glad to hear downgrading worked for you.

Is all that you changed the coremltools version?

Perhaps it may be coremltools package related then?

Moeflon commented 11 months ago

@Moeflon - Please create a new GitHub issue with steps to reproduce your problem that was solved by downgrading coremltools.

I will attempt to reproduce the issue with a non-confidential model tonight

SupenBook commented 1 day ago

same on ios 18