Expand description
Bindings for ExecuTorch - On-device AI across mobile, embedded and edge for PyTorch.
Provides a high-level Rust API for executing PyTorch models on mobile, embedded and edge devices using the ExecuTorch library, specifically the C++ API. PyTorch models are created and exported in Python, and then loaded and executed on-device using the ExecuTorch library.
The following example create a simple model in Python, exports it, and then executes it in Rust:
Create a model in Python and export it:
import torch
from executorch.exir import to_edge
from torch.export import export
class Add(torch.nn.Module):
def __init__(self):
super(Add, self).__init__()
def forward(self, x: torch.Tensor, y: torch.Tensor):
return x + y
aten_dialect = export(Add(), (torch.ones(1), torch.ones(1)))
edge_program = to_edge(aten_dialect)
executorch_program = edge_program.to_executorch()
with open("model.pte", "wb") as file:
file.write(executorch_program.buffer)
Execute the model in Rust:
use executorch::evalue::IntoEValue;
use executorch::module::Module;
use executorch::tensor_ptr;
use ndarray::array;
let mut module = Module::new("model.pte", None);
let (tensor1, tensor2) = (tensor_ptr![1.0_f32], tensor_ptr![1.0_f32]);
let inputs = [tensor1.into_evalue(), tensor2.into_evalue()];
let outputs = module.forward(&[inputs]).unwrap();
assert_eq!(outputs.len(), 1);
let output = outputs.into_iter().next().unwrap();
let output = output.as_tensor().into_typed::<f32>();
println!("Output tensor computed: {:?}", output);
assert_eq!(array![2.0], output.as_array());
§Cargo Features
data-loader
: Includes additional structs in thedata_loader
module for loading data. Without this feature the only available data loader isBufferDataLoader.
Thelibextension_data_loader.a
static library is required, compile C++executorch
withEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON
.module
: Includes themodule
API, a high-level API for loading and executing PyTorch models. It is an alternative to the lower-levelProgram
API, which is more suitable for embedded systems. Thelibextension_module_static.a
static library is required, compile C++executorch
withEXECUTORCH_BUILD_EXTENSION_MODULE=ON
. Also includes thestd
feature.tensor-ptr
: Includes thetensor::TensorPtr
struct, a smart pointer for tensors that manage the lifetime of the tensor object alongside the lifetimes of the data buffer and additional metadata. Theextension_tensor.a
static library is required, compile C++executorch
withEXECUTORCH_BUILD_EXTENSION_TENSOR=ON
. Also includes thestd
feature.etdump
Includes theETDumpGen
struct, an implementation of anEventTracer
, used for debugging and profiling. Thelibetdump.a
static library is required, compile C++executorch
withEXECUTORCH_BUILD_DEVTOOLS=ON
andEXECUTORCH_ENABLE_EVENT_TRACER=ON
. In addition, theflatcc
(orflatcc_d
) library is required, available at{CPP_EXECUTORCH_DIR}/third-party/flatcc/lib/
, and should be linked by the user.ndarray
: Conversions betweenexecutorch
tensors andndarray
arrays. Adds a dependency to thendarray
crate. This feature is enabled by default.f16
: Adds a dependency to thehalf
crate, which provides a fully capablef16
andbf16
types. Without this feature enabled, both of these types are available with a simple conversions to/fromu16
only. Note that this only affect input/output tensors, the internal computations always have the capability to operate on such scalars.num-complex
: Adds a dependency to thenum-complex
crate, which provides a fully capable complex number type. Without this feature enabled, complex numbers are available as a simple struct with two public fields without any operations. Note that this only affect input/output tensors, the internal computations always have the capability to operate on such scalars.std
: Enable the standard library. This feature is enabled by default, but can be disabled to buildexecutorch
in ano_std
environment. See theexamples/no_std
example. Also includes thealloc
feature. NOTE: no_std is still WIP, see https://github.com/pytorch/executorch/issues/4561alloc
: Enable allocations. When this feature is disabled, all methods that require allocations will not be compiled. This feature is enabled by thestd
feature, which is enabled by default. Its possible to enable this feature without thestd
feature, and the allocations will be done using thealloc
crate, that requires a global allocator to be set.
By default the std
and ndarray
features are enabled.
§Build
To use the library you must compile the C++ executorch library yourself, as there are many configurations that
determines which modules, backends, and operations are supported. See the executorch-sys
crate for more info.
§Embedded Systems
The library is designed to be used both in std
and no_std
environments. The no_std
environment is useful for
embedded systems, where the standard library is not available. The alloc
feature can be used to provide an
alternative to the standard library’s allocator, but it is possible to use the library without allocations at all.
Due to some difference between Cpp and Rust, it is not trivial to provide such API, and the interface may feel
more verbose. See the memory::Storage
struct for stack allocations of Cpp objects, and the examples/no_std
example.
§API Stability
The C++ API is still in Beta, and this Rust lib will continue to change with it. Currently the supported
executorch version is 0.5.0
.
Re-exports§
pub use ndarray;
ndarray
pub use half;
half
pub use num_complex;
num-complex
Modules§
- data_
loader - Data loaders for loading execution plans (models) from a data source.
- evalue
- Module for
EValue
and related types. - event_
tracer - Event tracers for debugging and profiling.
- memory
- Memory management classes.
- module
module
- A higher-level for simple execution of programs.
- platform
- Platform abstraction layer to allow individual platform libraries to override symbols in ExecuTorch.
- program
- Lower-level API for loading and executing ExecuTorch programs.
- scalar
- Custom scalar types that can be used in tensors.
- tensor
- Tensor struct is an input or output tensor to an executorch program.
- util
- Utility functions and types.
Macros§
- storage
- A macro to create a pinned
Storage
object(s). - tensor_
ptr tensor-ptr
andndarray
- A short syntax for creating a
TensorPtr
.