Skip to main content

On-Device Training

The ONNX Runtime Training API enables training directly on edge devices, mobile platforms, and embedded systems. It provides a lightweight, cross-platform solution for federated learning, model personalization, and privacy-preserving on-device learning.

Overview

Unlike ORTModule which wraps PyTorch models, the On-Device Training API works with pre-compiled ONNX models. This approach offers:
  • Minimal dependencies: No PyTorch or heavy ML framework required
  • Small binary size: Optimized for resource-constrained devices
  • Cross-platform: Works on iOS, Android, Linux, Windows, and embedded systems
  • Fast startup: Pre-compiled models eliminate export overhead
  • Privacy-preserving: Train models locally without sending data to the cloud

Key Concepts

Training Artifacts

The Training API requires four artifacts:
  1. Training Model (training_model.onnx): Base model + loss + gradient graph
  2. Evaluation Model (eval_model.onnx): Base model + loss (optional)
  3. Optimizer Model (optimizer.onnx): Optimizer update graph (optional)
  4. Checkpoint (checkpoint.ckpt): Model parameters and optimizer state
These artifacts are generated offline using the generate_artifacts utility.

Quick Start

Step 1: Generate Training Artifacts

First, export your PyTorch model to ONNX and generate training artifacts:

Step 2: Training Loop

Use the generated artifacts for training:

Complete Example

Here’s a complete example with a simple classifier:

Advanced Features

Custom Loss Functions

Define custom loss functions using ONNXBlock:

Nominal Checkpoints

For on-device applications, use nominal checkpoints to reduce package size:
Nominal checkpoints contain only parameter metadata, not actual values. They’re useful when:
  • Packaging models with mobile apps
  • Parameters will be loaded from a separate source
  • Reducing initial app download size

ORT Format

Convert models to ORT format for faster loading:

Working with OrtValues

For better performance, use OrtValues instead of numpy arrays:

Checkpoint Management

The CheckpointState provides parameter access and management:

Supported Loss Functions

  • LossType.MSELoss: Mean squared error loss
  • LossType.CrossEntropyLoss: Cross-entropy loss for classification
  • LossType.BCEWithLogitsLoss: Binary cross-entropy with logits
  • LossType.L1Loss: L1 (absolute error) loss

Supported Optimizers

  • OptimType.AdamW: Adam with weight decay
  • OptimType.SGD: Stochastic gradient descent

Mobile and Edge Deployment

iOS Example

Android Example

Use Cases

Federated Learning

Train models across multiple devices without centralizing data:
  1. Deploy initial model to all devices
  2. Each device trains locally
  3. Aggregate parameter updates on server
  4. Distribute updated model

Model Personalization

Adapt pre-trained models to individual users:
  1. Ship pre-trained model with app
  2. Fine-tune on user’s device with their data
  3. Keep personalized model local

Edge AI Applications

Continuous learning on edge devices:
  1. Deploy model to edge device (IoT, robotics)
  2. Collect local data
  3. Train incrementally
  4. Adapt to changing conditions

Performance Tips

  1. Use OrtValues: Avoid numpy conversion overhead
  2. Batch Processing: Process multiple samples together
  3. ORT Format: Use .ort format for faster loading
  4. Quantization: Consider quantized models for mobile
  5. Memory Management: Reuse buffers when possible

Next Steps

ORTModule

For cloud-based PyTorch training

Training Overview

Explore all training options