ExecuTorch-rs
executorch is a Rust library for executing PyTorch models in Rust.
It is a Rust wrapper around the ExecuTorch C++ API.
It depends on version 0.7.0 of the Cpp API, but will advance as the API does.
The underlying C++ library is still in development, and its API is subject to change together with the Rust API.
Usage
Create a model in Python and export it:
return +
=
=
=
Execute the model in Rust:
use ;
use Module;
use tensor_ptr;
use array;
let mut module = from_file_path;
let = ;
let inputs = ;
let outputs = module.forward.unwrap;
let : = outputs.try_into.expect;
let output = output.as_tensor.;
println!;
assert_eq!;
See example/hello_world for a complete example.
Build
To build the library, you need to build the C++ library first.
The C++ library allow for great flexibility with many flags, customizing which modules, kernels, and extensions are built.
Multiple static libraries are built, and the Rust library links to them.
In the following example we build the C++ library with the necessary flags to run example hello_world:
# Clone the C++ library
# Install requirements
# Build C++ library
&&
# Static libraries are in cmake-out/
# core:
# cmake-out/libexecutorch.a
# cmake-out/libexecutorch_core.a
# kernels implementations:
# cmake-out/kernels/portable/libportable_ops_lib.a
# cmake-out/kernels/portable/libportable_kernels.a
# extension data loader, enabled with EXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON:
# cmake-out/extension/data_loader/libextension_data_loader.a
# extension module, enabled with EXECUTORCH_BUILD_EXTENSION_MODULE=ON:
# cmake-out/extension/module/libextension_module_static.a
# extension tensor, enabled with EXECUTORCH_BUILD_EXTENSION_TENSOR=ON:
# cmake-out/extension/tensor/libextension_tensor.a
# extension tensor, enabled with EXECUTORCH_BUILD_DEVTOOLS=ON:
# cmake-out/devtools/libetdump.a
# Run example
# We set EXECUTORCH_RS_EXECUTORCH_LIB_DIR to the path of the C++ build output
EXECUTORCH_RS_EXECUTORCH_LIB_DIR=/cmake-out
The executorch crate will always look for the following static libraries:
libexecutorch.alibexecutorch_core.a
Additional libs are required if feature flags are enabled (see next section):
libextension_data_loader.alibextension_module_static.alibextension_tensor.alibetdump.a
The static libraries of the kernels implementations are required only if your model uses them, and they should be linked manually by the binary that uses the executorch crate.
For example, the hello_world example uses a model with a single addition operation, so it compile the C++ library with DEXECUTORCH_SELECT_OPS_LIST=aten::add.out and contain the following lines in its build.rs:
println!;
println!;
let libs_dir = var.unwrap;
println!;
Note that the ops and kernels libs are linked with +whole-archive to ensure that all symbols are included in the binary.
The build (and library) is tested on Ubuntu and MacOS, not on Windows.
Cargo Features
-
data-loaderIncludes the
FileDataLoaderandMmapDataLoaderstructs. Without this feature the only available data loader isBufferDataLoader. Thelibextension_data_loader.astatic library is required, compile C++executorchwithEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON. -
moduleIncludes the
Modulestruct. Thelibextension_module_static.astatic library is required, compile C++executorchwithEXECUTORCH_BUILD_EXTENSION_MODULE=ON. Also includes thestdfeature. -
tensor-ptrIncludes the
TensorPtrstruct, a smart pointer for tensors that manage the lifetime of the tensor object alongside the lifetimes of the data buffer and additional metadata. Thelibextension_tensor.astatic library is required, compile C++executorchwithEXECUTORCH_BUILD_EXTENSION_TENSOR=ON. Also includes thestdfeature. -
etdumpIncludes the
ETDumpGenstruct, an implementation of anEventTracer, used for debugging and profiling. Thelibetdump.astatic library is required, compile C++executorchwithEXECUTORCH_BUILD_DEVTOOLS=ONandEXECUTORCH_ENABLE_EVENT_TRACER=ON. In addition, theflatcc(orflatcc_d) library is required, available at{CMAKE_DIR}/third-party/flatcc_external_project/lib/, and should be linked by the user. -
ndarrayConversions between
executorchtensors andndarrayarrays. Adds a dependency to thendarraycrate. This feature is enabled by default. -
halfAdds a dependency to the
halfcrate, which provides a fully capablef16andbf16types. Without this feature enabled, both of these types are available with a simple conversions to/fromu16only. Note that this only affect input/output tensors, the internal computations always have the capability to operate on such scalars. -
num-complexAdds a dependency to the
num-complexcrate, 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. -
stdEnable the standard library. This feature is enabled by default, but can be disabled to build
executorchin ano_stdenvironment. See theexamples/no_stdexample. Also includes theallocfeature. NOTE: no_std is still WIP, see https://github.com/pytorch/executorch/issues/4561 -
allocEnable allocations. When this feature is disabled, all methods that require allocations will not be compiled. This feature is enabled by the
stdfeature, which is enabled by default. Its possible to enable this feature without thestdfeature, and the allocations will be done using thealloccrate, that requires a global allocator to be set.
By default the std and ndarray features are enabled.