ExecuTorch for Mobile & Edge

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

ExecuTorch is the PyTorch runtime for inference on edge devices: phones and tablets, wearables, desktops and microcontrollers. A model is exported with torch.export, optionally lowered to a hardware backend (a delegate), and serialised into a compact .pte file that a small C++ runtime executes. The runtime has no Python dependency. The PyTorch blog post "ExecuTorch becomes part of PyTorch Core" is listed in the references. The TorchScript-based mobile paths of older tutorials are not the way forward; see Legacy APIs & Migration. The cross-framework picture, including LiteRT for TensorFlow and Keras models, is in ONNX & Model Interoperability, and the decision of where to run a model is in Deploying & Serving ML Models.

The Python examples were run on Python 3.13 with PyTorch 2.15 and ExecuTorch 1.5.1 (CPU, XNNPACK backend, with the .pte executed by the Python runtime) on 2026-10-31. The documentation lists Python 3.10 to 3.14 and PyTorch 2.9 or later as prerequisites. The Android (Kotlin), iOS and C++ app examples need their own toolchains (Gradle, Xcode, CMake) and devices and were not executed.

Workflow

flowchart LR A["nn.Module + example inputs"] --> B["torch.export.export"] B --> C["to_edge_transform_and_lower(partitioner = backend)"] C --> D["to_executorch()"] D --> E["model.pte"] E --> F["ExecuTorch runtimeC++, Java/Kotlin, Objective-C, Python"]
  1. Select a backend. The backend decides what hardware accelerates the model and which operators are supported.

  2. Prepare the model: put it in eval() mode, choose example inputs, decide which dimensions are dynamic.

  3. Export with torch.export.export (see torch.export).

  4. Lower with to_edge_transform_and_lower, passing the backend’s partitioner. Parts of the graph the backend supports are delegated to it; the rest runs on the portable CPU kernels.

  5. Serialise with to_executorch() and write the .pte buffer to a file.

Installing

pip install executorch
# nightly builds
pip install executorch --pre --extra-index-url https://download.pytorch.org/whl/nightly/cpu

The documentation supports Linux (x86_64, ARM64), macOS (ARM64) and Windows (x86_64) as export hosts; on Intel macOS PyTorch must be built from source. A C++17 compiler is required for building the runtime, and Visual Studio 2022 or later on Windows (run from a Developer PowerShell).

Exporting a model to .pte

import torch
import torchvision.models as models
from torchvision.models.mobilenetv2 import MobileNet_V2_Weights

from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
from executorch.exir import to_edge_transform_and_lower

model = models.mobilenetv2.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).eval()
sample_inputs = (torch.randn(1, 3, 224, 224),)

et_program = to_edge_transform_and_lower(
    torch.export.export(model, sample_inputs),
    partitioner=[XnnpackPartitioner()],
).to_executorch()

with open("model.pte", "wb") as f:
    f.write(et_program.buffer)

The backend is chosen only by the partitioner argument. Several partitioners can be given in a list and are applied in priority order; operators that no backend takes fall back to the portable CPU kernels, which can be slow. For mobile the documentation recommends XNNPACK for Android and XNNPACK or Core ML for iOS.

Dynamic shapes

from torch.export import Dim, export

# MobileNetV2 downsamples 32x, so very small images are rejected: keep min >= 64.
dynamic_shapes = {"x": {2: Dim("h", min=64, max=1024), 3: Dim("w", min=64, max=1024)}}
exported_program = export(model, sample_inputs, dynamic_shapes=dynamic_shapes)
executorch_program = to_edge_transform_and_lower(
    exported_program, partitioner=[XnnpackPartitioner()]
).to_executorch()

Dimensions without a declaration stay at the example size. Keep upper bounds no larger than necessary, since the runtime plans memory from the bound. The same page documents that each method of a multi-method .pte is memory-planned separately, so activation memory is not shared between methods.

Pitfalls collected from the export guide

  • Mutable buffers: use in-place operations (names ending in an underscore) or copy_, do not reassign the buffer variable, call detach() on tensors before storing them in a buffer, and expect index_put_ to be a bottleneck. Buffers are not shared between methods, and not every backend supports buffer mutation; the page calls mutation support experimental.

  • Dynamic control flow: use torch.cond for data-dependent branches. Dynamic loops are not supported; export the loop body as a method and drive the loop from application code.

  • Only forward can be exported directly. Wrap other methods in their own nn.Module to export them as extra methods.

  • Large weights can be kept out of the .pte in a separate .ptd data file (tagged with a pass from executorch.exir.passes.external_constants_pass) so that they can be shared or updated independently.

Backends

The backend list in the documentation (all selected through a partitioner) includes:

Backend Platforms Hardware

XNNPACK

All

CPU

Core ML

iOS, macOS

Neural Engine, GPU, CPU

Vulkan

Android (described as mature), Linux, Windows (experimental on desktops)

GPU

Qualcomm (QNN)

Android

NPU

MediaTek

Android

NPU

Samsung Exynos

Android

CPU, GPU, NPU

CUDA

Linux, Windows

GPU

Arm Ethos-U, Arm Cortex-M

Embedded

NPU, CPU

OpenVINO, NXP, Cadence

Embedded

CPU, GPU, NPU, DSP (varies)

MLX, WebGPU

iOS and macOS (MLX, experimental on iOS), browser and desktop (WebGPU, experimental)

GPU

Each backend has its own page with installation steps, supported operators and options, for example XNNPACK, Core ML and Vulkan. NPU backends from chip vendors usually require the vendor SDK and a model quantised in a specific way. The status labels above are those of the documentation index in October 2026; recheck them before committing to a backend.

Quantization

Quantization is normally applied after export and before lowering, because backends execute integer kernels. ExecuTorch uses the PT2E flow from torchao, with a quantizer provided by the target backend:

import torch
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import (
    XNNPACKQuantizer,
    get_symmetric_quantization_config,
)
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e

quantizer = XNNPACKQuantizer().set_global(get_symmetric_quantization_config())
calibration_data = [torch.randn(1, 3, 224, 224) for _ in range(8)]   # use real samples

training_gm = torch.export.export(model, sample_inputs).module()

prepared = prepare_pt2e(training_gm, quantizer)   # backend-specific quantizer instance
for batch in calibration_data:                    # representative inputs
    prepared(batch)
quantized = convert_pt2e(prepared)

# Lower the quantized model as an unquantized one
et_program = to_edge_transform_and_lower(
    torch.export.export(quantized, sample_inputs),
    partitioner=[XnnpackPartitioner()],
).to_executorch()

The example uses XNNPACKQuantizer; other backends provide their own (for example CoreMLQuantizer), with import paths on their backend pages. The overview shows prepare, calibrate and convert, and says lowering follows the backend-specific page; the final lowering call above is the standard lowering step from the export guide applied to the quantized module. The accuracy check with compute_error (SQNR) from torchao.quantization.utils is also shown there. Background on what quantization does and its accuracy costs is in Model Compression; the eager-mode torchao APIs are in Quantization, Sparsity & Pruning with torchao.

Running a .pte

Python (for testing on the host)

import torch
from executorch.runtime import Runtime

runtime = Runtime.get()
program = runtime.load_program("model.pte")
method = program.load_method("forward")
output = method.execute([torch.randn(1, 3, 224, 224)])
print(output[0].shape)

Compare the output with the eager model (torch.allclose with a tolerance such as rtol=1e-3, atol=1e-5) before shipping, particularly for quantized models.

C++

#include <executorch/extension/module/module.h>
#include <executorch/extension/tensor/tensor.h>

using namespace ::executorch::extension;

Module module("/path/to/model.pte");

float input[1 * 3 * 224 * 224];
auto tensor = from_blob(input, {1, 3, 224, 224});

const auto result = module.forward(tensor);
if (result.ok()) {
  const auto output = result->at(0).toTensor().const_data_ptr<float>();
}
set(CMAKE_CXX_STANDARD 17)
find_package(executorch REQUIRED COMPONENTS kernels_optimized)
target_link_libraries(my_target PRIVATE executorch::runtime executorch::kernels_optimized)

The prebuilt C++ libraries come with the pip package (Linux and macOS); building from source with CMake uses a preset file (for example llm.cmake) and links executorch, executorch::backends, executorch::extensions and executorch::kernels.

Android

// app/build.gradle.kts
dependencies {
    implementation("org.pytorch:executorch-android:${executorch_version}")
}
import org.pytorch.executorch.EValue;
import org.pytorch.executorch.Module;
import org.pytorch.executorch.Tensor;

Module model = Module.load("/path/to/model.pte");
Tensor input = Tensor.fromBlob(floatData, new long[] {1, 3, height, width});
EValue[] output = model.forward(EValue.from(input));
float[] scores = output[0].toTensor().getDataAsFloatArray();

iOS

ExecuTorch is distributed as a Swift Package Manager package with xcframework targets: in Xcode choose File, Add Package Dependencies, paste the repository URL and set the branch to swiftpm-<version> (for example swiftpm-1.5.1). The getting-started page provides no Swift sample; models are loaded and run through the C++ APIs from Objective-C. Prefer checking the current iOS guide on the edge-platforms page before relying on this.

Large language models

The documentation describes a two-step LLM workflow: export the model to a .pte with export_llm (ExecuTorch own export API with optimisations) or, for Hugging Face models with broader architecture coverage, the Optimum integration, then run the file with the C++ runner or the Swift and Java bindings. Exact commands, flags and the list of supported models change quickly and were not verified for this page; follow the LLM guides at https://docs.pytorch.org/executorch/stable/llm/working-with-llms.html. Related background on running language models locally is in Quantization for Inference and Model Formats.

ExecuTorch or something else

Situation Prefer

Android or iOS app with a PyTorch-trained model, vendor NPU acceleration

ExecuTorch

Server GPU or CPU, no Python

AOTInductor

Many runtimes, languages and vendors from one file

ONNX

Model trained in Keras or TensorFlow for devices

LiteRT, see ONNX & Model Interoperability