Overview
Model loading is the process of downloading resources, preparing native modules, and loading ExecuTorch.pte model files into memory. This guide covers the complete loading lifecycle from resource fetching to ready-for-inference state.
Loading Lifecycle
Basic Loading Pattern
All modules follow this pattern:Module-Specific Loading
Different modules require different resources:ExecutorchModule (Generic)
Simplest case - just a model binary:~/workspace/source/packages/react-native-executorch/src/modules/general/ExecutorchModule.ts:22-42
Implementation:
VisionModule (Image Models)
Vision models need only the model file:LLMModule (Language Models)
LLMs require multiple resources:~/workspace/source/packages/react-native-executorch/src/modules/natural_language_processing/LLMModule.ts:49-66
OCRModule (Text Recognition)
OCR needs detector, recognizer, and symbols:TextToImageModule (Stable Diffusion)
Most complex - multiple models:Download Progress Tracking
Single Resource
Progress from 0 to 1:Multiple Resources
Progress is weighted by file size:~/workspace/source/packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts:155-181
Native Module Initialization
After resources are downloaded, native modules are created via JSI:Global JSI Functions
These functions are installed at app startup:~/workspace/source/packages/react-native-executorch/src/index.ts:36-92
Installation
JSI functions are installed by the native module:~/workspace/source/packages/react-native-executorch/src/index.ts:94-117
Caching Behavior
Automatic Caching
Downloaded files are cached in the app’s document directory:Cache Check
Before downloading, the fetcher checks if the file exists:Subsequent Loads
After first load, subsequent loads are instant:Error Handling
Common Loading Errors
Download Interruption
If download is cancelled or paused,load() throws:
~/workspace/source/packages/react-native-executorch/src/modules/general/ExecutorchModule.ts:31-35
Memory Management
Model in Memory
Loaded models reside in native heap:Cleanup
Always calldelete() when done:
Multiple Models
You can load multiple models simultaneously:Preloading Strategies
App Startup
Load critical models at startup:Background Download
Download large models in background:Lazy Loading
Load models on-demand:Best Practices
- Initialize early: Call
initExecutorch()before any model loading - Show progress: Always provide progress callbacks for better UX
- Handle errors: Catch and display loading errors appropriately
- Clean up: Call
delete()in cleanup functions (componentWillUnmount, useEffect return) - Cache awareness: Subsequent loads are fast - don’t block UI unnecessarily
- Memory constraints: Monitor memory usage on low-end devices
- Preload critical models: Load frequently-used models at startup
- Lazy load optional models: Load feature-specific models on-demand
Next Steps
- Error Handling - Learn about all error types and how to handle them
- Resource Fetching - Deep dive into resource management