.pte format and integrate them into your React Native application.
Overview
To run custom models with ExecuTorch, you need to:- Export your PyTorch model to the
.pteformat - Optionally optimize the model for mobile devices
- Load the model using
useExecutorchModuleorExecutorchModule
Exporting Models to .pte Format
Using the Python API
The ExecuTorch Python API provides tools to export PyTorch models. Here’s a basic example:Key Export Steps
Step 1: Prepare Your Model Ensure your model is in evaluation mode and all operations are supported by ExecuTorch:Using optimum-executorch
optimum-executorch is a library by Hugging Face that simplifies exporting models:Installation
Export Hugging Face Models
Export Custom Models
Advanced Export Options
Quantization During Export
Reduce model size with quantization:Core ML Backend (iOS)
For iOS devices, you can use Core ML for hardware acceleration:Loading Custom Models in React Native
Using useExecutorchModule Hook
Once you have your.pte file, load it in React Native:
Using TypeScript API
For more control, use the TypeScript API directly:Input/Output Handling
Preparing Inputs
Inputs must match the model’s expected format:Processing Outputs
Debugging Export Issues
Common Problems
Unsupported Operations If your model uses operations not supported by ExecuTorch:Testing Your Exported Model
Validate with Python
Before using in React Native, test with Python:Best Practices
- Start Simple: Begin with basic models before attempting complex architectures
- Test Incrementally: Validate each export step before proceeding
- Check Operator Support: Verify all operations are supported by ExecuTorch
- Optimize for Mobile: Use quantization and backend delegation for better performance
- Version Control: Keep track of export scripts and model versions
Resources
- ExecuTorch Export Tutorial
- ExecuTorch Python API Documentation
- optimum-executorch GitHub
- Supported Operators
Next Steps
- Learn about Performance Optimization techniques
- Understand Memory Management for large models
- Explore Debugging strategies for custom models