ExecuTorch

1.2.0 · active · verified Fri Apr 17

ExecuTorch is an on-device AI framework that extends PyTorch to mobile, embedded, and edge devices. It enables efficient inference by compiling PyTorch models into a compact, optimized format (.pte file) suitable for resource-constrained environments. The current version, 1.2.0, expands model and hardware support, including real-time speech and improved embedded targets. Releases are frequent, typically every 1-2 months, aligning with PyTorch releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple PyTorch model, export it using `torch.export`, and then convert it into an ExecuTorch program (.pte file) using the `to_executorch` function with an XNNPACK delegate. The resulting .pte file can then be deployed to target devices.

import torch
import torch.nn as nn
from torch._export.executorch import to_executorch
from torch.export import export
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner

# 1. Define a simple PyTorch model
class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 2)

    def forward(self, x):
        return self.linear(x)

model = SimpleModel()

# 2. Define example inputs (crucial for tracing and export)
example_inputs = (torch.randn(1, 10),)

# 3. Export the model to an ExportedProgram using PyTorch's export API
exported_program = export(model, example_inputs)

# 4. Convert the ExportedProgram to an ExecuTorch program (.pte file)
# This requires specifying a backend delegate. XNNPACK is a common choice.
# Ensure `executorch[xnnpack]` or equivalent is installed.
executorch_program = to_executorch(exported_program, [XnnpackPartitioner()])

# 5. Save the ExecuTorch program to a file
with open("model.pte", "wb") as f:
    f.write(executorch_program.buffer)

print("Model successfully exported to model.pte")

view raw JSON →