Building and Running ExecuTorch with Core ML Backend¶
Core ML delegate uses Core ML APIs to enable running neural networks via Apple’s hardware acceleration. For more about Core ML you can read here. In this tutorial, we will walk through the steps of lowering a PyTorch model to Core ML delegate
In this tutorial you will learn how to export MobileNet V3 model so that it runs on Core ML backend.
You will also learn how to deploy and run the exported model on a supported Apple device.
Prerequisites (Hardware and Software)¶
In order to be able to successfully build and run the ExecuTorch’s Core ML backend you’ll need the following hardware and software components.
Hardware:¶
Setting up your developer environment¶
Make sure that you have completed the ExecuTorch setup tutorials linked to at the top of this page and setup the environment.
Run
install_requirements.sh
to install dependencies required by the Core ML backend.
cd executorch
./backends/apple/coreml/scripts/install_requirements.sh
Install Xcode.
Install Xcode Command Line Tools.
xcode-select --install
Build¶
AOT (Ahead-of-time) components:¶
Exporting a Core ML delegated Program:
In this step, you will lower the MobileNet V3 model to the Core ML backend and export the ExecuTorch program. You’ll then deploy and run the exported program on a supported Apple device using Core ML backend.
cd executorch
# Generates ./mv3_coreml_all.pte file.
python3 -m examples.apple.coreml.scripts.export --model_name mv3
Core ML backend uses coremltools to lower Edge dialect to Core ML format and then bundles it in the
.pte
file.
Runtime:¶
Running a Core ML delegated Program:
Build the runner.
cd executorch
# Builds `coreml_executor_runner`.
./examples/apple/coreml/scripts/build_executor_runner.sh
Run the CoreML delegated program.
cd executorch
# Runs the exported mv3 model using the Core ML backend.
./coreml_executor_runner --model_path mv3_coreml_all.pte
Profiling a Core ML delegated Program:
Note that profiling is supported on macOS >= 14.4.
[Optional] Generate an ETRecord when exporting your model.
cd executorch
# Generates `mv3_coreml_all.pte` and `mv3_coreml_etrecord.bin` files.
python3 -m examples.apple.coreml.scripts.export --model_name mv3 --generate_etrecord
Build the runner.
# Builds `coreml_executor_runner`.
./examples/apple/coreml/scripts/build_executor_runner.sh
Run and generate an ETDump.
cd executorch
# Generate the ETDump file.
./coreml_executor_runner --model_path mv3_coreml_all.pte --profile_model --etdump_path etdump.etdp
Create an instance of the Inspector API by passing in the ETDump you have sourced from the runtime along with the optionally generated ETRecord from step 1 or execute the following command in your terminal to display the profiling data table.
python examples/apple/coreml/scripts/inspector_cli.py --etdump_path etdump.etdp --etrecord_path mv3_coreml.bin
Deploying and running on a device¶
Running the Core ML delegated Program in the Demo iOS App:
Please follow the Export Model step of the tutorial to bundle the exported MobileNet V3 program. You only need to do the Core ML part.
Complete the Build Runtime and Backends section of the tutorial. When building the frameworks you only need the
coreml
option.Complete the Final Steps section of the tutorial to build and run the demo app.
Running the Core ML delegated Program in your App
Build frameworks, running the following will create a
executorch.xcframework
andcoreml_backend.xcframework
in thecmake-out
directory.
cd executorch
./build/build_apple_frameworks.sh --coreml
Create a new Xcode project or open an existing project.
Drag the
executorch.xcframework
andcoreml_backend.xcframework
generated from Step 2 to Frameworks.Go to the project’s Build Phases - Link Binaries With Libraries, click the + sign, and add the following frameworks:
executorch.xcframework
coreml_backend.xcframework
Accelerate.framework
CoreML.framework
libsqlite3.tbd
Add the exported program to the Copy Bundle Phase of your Xcode target.
Please follow the Runtime APIs Tutorial to integrate the code for loading an ExecuTorch program.
Update the code to load the program from the Application’s bundle.
NSURL *model_url = [NBundle.mainBundle URLForResource:@"mv3_coreml_all" extension:@"pte"];
Result<executorch::extension::FileDataLoader> loader =
executorch::extension::FileDataLoader::from(model_url.path.UTF8String);
Use Xcode to deploy the application on the device.
The application can now run the MobileNet V3 model on the Core ML backend.
In this tutorial, you have learned how to lower the MobileNet V3 model to the Core ML backend, deploy, and run it on an Apple device.