Expand description
Unsafe bindings for ExecuTorch - On-device AI across mobile, embedded and edge for PyTorch.
Provides a low level Rust bindings for the ExecuTorch library.
For the common use case, it is recommended to use the high-level API provided by the executorch
crate, where
a more detailed documentation can be found.
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
cd ${EXECUTORCH_CPP_DIR}
git clone --depth 1 --branch v0.6.0 https://github.com/pytorch/executorch.git .
git submodule sync --recursive
git submodule update --init --recursive
# Install requirements
./install_requirements.sh
# Build C++ library
mkdir cmake-out && cd cmake-out
cmake \
-DDEXECUTORCH_SELECT_OPS_LIST=aten::add.out \
-DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=OFF \
-DBUILD_EXECUTORCH_PORTABLE_OPS=ON \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
-DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=ON \
-DEXECUTORCH_ENABLE_LOGGING=ON \
..
make -j
# 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
cd ${EXECUTORCH_RS_DIR}/examples/hello_world
python export_model.py
EXECUTORCH_RS_EXECUTORCH_LIB_DIR=${EXECUTORCH_CPP_DIR}/cmake-out cargo run
The executorch
crate will always look for the following static libraries:
libexecutorch.a
libexecutorch_core.a
Additional libs are required if feature flags are enabled (see next section):
libextension_data_loader.a
libextension_module_static.a
libextension_tensor.a
libetdump.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!("cargo::rustc-link-lib=static:+whole-archive=portable_kernels");
println!("cargo::rustc-link-lib=static:+whole-archive=portable_ops_lib");
let libs_dir = std::env::var("EXECUTORCH_RS_EXECUTORCH_LIB_DIR").unwrap();
println!("cargo::rustc-link-search=native={libs_dir}/kernels/portable/");
Note that the ops and kernels libs are linked with +whole-archive
to ensure that all symbols are included in the
binary.
The EXECUTORCH_RS_EXECUTORCH_LIB_DIR
environment variable should be set to the path of the C++ build output.
If its not provided, its the resposibility of the binary to add the libs directories to the linker search path, and
the crate will just link to the static libraries using cargo::rustc-link-lib=...
.
If you want to link to executorch libs yourself, set the environment variable EXECUTORCH_RS_LINK
to 0
, and
the crate will not link to any library and not modify the linker search path.
§Cargo Features
By default the std
feature is enabled.
data-loader
: Includes theFileDataLoader
andMmapDataLoader
structs. 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
struct. Thelibextension_module_static.a
static library is required, compile C++executorch
withEXECUTORCH_BUILD_EXTENSION_MODULE=ON
.tensor-ptr
: Includes a few functions creatingcxx::SharedPtr<Tensor>
pointers, that manage the lifetime of the tensor object alongside the lifetimes of the data buffer and additional metadata. Thelibextension_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.std
: Enable the standard library. This feature is enabled by default, but can be disabled to buildexecutorch
in ano_std
environment. NOTE: no_std is still WIP, see https://github.com/pytorch/executorch/issues/4561
Re-exports§
pub use cxx;
std
Modules§
- cpp
std
- Bindings generated by the
cxx
crate.
Structs§
- Array
RefBool - Array
RefChar - Array
RefDim Order Type - Array
RefE Value - Array
RefE Value Ptr - Array
RefF64 - Array
RefI32 - Array
RefI64 - Array
RefOptional Tensor - Array
RefSizes Type - Array
RefStrides Type - Array
RefTensor - Array
RefU8 - Array
RefUsize Type - Boxed
Evalue List I64 - Boxed
Evalue List Optional Tensor - Boxed
Evalue List Tensor - Buffer
Data Loader - Data
Loader RefMut - ETDump
Gen - EValue
Ref - EValue
RefMut - EValue
Storage - Event
Tracer RefMut - File
Data Loader - Hierarchical
Allocator - Memory
Allocator - Memory
Manager - Method
- Method
Meta - Mmap
Data Loader - Optional
Tensor Ref - Optional
Tensor RefMut - Optional
Tensor Storage - Program
- Program__
bindgen_ ty_ 1 - SpanI64
- Span
Optional Tensor - Span
Span U8 - Span
Tensor - SpanU8
- Tensor
Impl - Tensor
Info - Tensor
Ref - Tensor
RefMut - Tensor
Storage - VecChar
- VecE
Value - VecVec
Char
Enums§
- Error
- ExecuTorch Error type.
- Mmap
Data Loader Mlock Config - Describes how and whether to lock loaded pages with
mlock()
. - Module
Load Mode - Enum to define loading behavior.
- Program
Header Status - Describes the presence of an ExecuTorch program header.
- Program
Verification - Types of validation that the Program can do before parsing the data.
- Scalar
Type - Tag
- Tensor
Shape Dynamism - The resizing capabilities of a Tensor.
Constants§
- EXECUTORCH_
CPP_ VERSION - The version of the ExecuTorch C++ library that this crate is compatible and linked with.
Functions§
- executorch_
Buffer ⚠Data Loader_ as_ data_ loader_ mut - executorch_
Buffer ⚠Data Loader_ new - executorch_
ETDump ⚠Gen_ as_ event_ tracer_ mut - executorch_
ETDump ⚠Gen_ get_ etdump_ data - executorch_
ETDump ⚠Gen_ new - executorch_
EValue_ ⚠as_ bool - executorch_
EValue_ ⚠as_ bool_ list - executorch_
EValue_ ⚠as_ f64 - executorch_
EValue_ ⚠as_ f64_ list - executorch_
EValue_ ⚠as_ i64 - executorch_
EValue_ ⚠as_ i64_ list - executorch_
EValue_ ⚠as_ optional_ tensor_ list - executorch_
EValue_ ⚠as_ string - executorch_
EValue_ ⚠as_ tensor - executorch_
EValue_ ⚠as_ tensor_ list - executorch_
EValue_ ⚠copy - executorch_
EValue_ ⚠destructor - executorch_
EValue_ ⚠move - executorch_
EValue_ ⚠new_ from_ bool - executorch_
EValue_ ⚠new_ from_ bool_ list - executorch_
EValue_ ⚠new_ from_ f64 - executorch_
EValue_ ⚠new_ from_ f64_ list - executorch_
EValue_ ⚠new_ from_ i64 - executorch_
EValue_ ⚠new_ from_ i64_ list - executorch_
EValue_ ⚠new_ from_ optional_ tensor_ list - executorch_
EValue_ ⚠new_ from_ string - executorch_
EValue_ ⚠new_ from_ tensor - executorch_
EValue_ ⚠new_ from_ tensor_ list - executorch_
EValue_ ⚠new_ none - executorch_
EValue_ ⚠tag - executorch_
File ⚠Data Loader_ as_ data_ loader_ mut - executorch_
File ⚠Data Loader_ destructor - executorch_
File ⚠Data Loader_ new - executorch_
Hierarchical ⚠Allocator_ destructor - executorch_
Hierarchical ⚠Allocator_ new - executorch_
Memory ⚠Allocator_ allocate - executorch_
Memory ⚠Allocator_ new - executorch_
Memory ⚠Manager_ new - executorch_
Method ⚠Meta_ get_ backend_ name - executorch_
Method ⚠Meta_ input_ tag - executorch_
Method ⚠Meta_ input_ tensor_ meta - executorch_
Method ⚠Meta_ memory_ planned_ buffer_ size - executorch_
Method ⚠Meta_ name - executorch_
Method ⚠Meta_ num_ backends - executorch_
Method ⚠Meta_ num_ inputs - executorch_
Method ⚠Meta_ num_ memory_ planned_ buffers - executorch_
Method ⚠Meta_ num_ outputs - executorch_
Method ⚠Meta_ output_ tag - executorch_
Method ⚠Meta_ output_ tensor_ meta - executorch_
Method ⚠Meta_ uses_ backend - executorch_
Method_ ⚠destructor - executorch_
Method_ ⚠execute - executorch_
Method_ ⚠get_ output - executorch_
Method_ ⚠inputs_ size - executorch_
Method_ ⚠outputs_ size - executorch_
Method_ ⚠set_ input - executorch_
Mmap ⚠Data Loader_ as_ data_ loader_ mut - executorch_
Mmap ⚠Data Loader_ destructor - executorch_
Mmap ⚠Data Loader_ new - executorch_
Optional ⚠Tensor_ get - executorch_
Program_ ⚠check_ header - executorch_
Program_ ⚠destructor - executorch_
Program_ ⚠get_ method_ name - executorch_
Program_ ⚠load - executorch_
Program_ ⚠load_ method - executorch_
Program_ ⚠method_ meta - executorch_
Program_ ⚠num_ methods - executorch_
Tensor ⚠Impl_ new - executorch_
Tensor ⚠Info_ dim_ order - executorch_
Tensor ⚠Info_ nbytes - executorch_
Tensor ⚠Info_ scalar_ type - executorch_
Tensor ⚠Info_ sizes - executorch_
Tensor_ ⚠const_ data_ ptr - executorch_
Tensor_ ⚠coordinate_ to_ index - executorch_
Tensor_ ⚠coordinate_ to_ index_ unchecked - executorch_
Tensor_ ⚠destructor - executorch_
Tensor_ ⚠dim - executorch_
Tensor_ ⚠dim_ order - executorch_
Tensor_ ⚠element_ size - executorch_
Tensor_ ⚠mutable_ data_ ptr - executorch_
Tensor_ ⚠nbytes - executorch_
Tensor_ ⚠new - executorch_
Tensor_ ⚠numel - executorch_
Tensor_ ⚠scalar_ type - executorch_
Tensor_ ⚠size - executorch_
Tensor_ ⚠sizes - executorch_
Tensor_ ⚠strides - executorch_
VecChar_ ⚠destructor - executorch_
VecE ⚠Value_ destructor - executorch_
VecVec ⚠Char_ destructor - executorch_
is_ ⚠valid_ dim_ order_ and_ strides - executorch_
pal_ ⚠init - executorch_
stride_ ⚠to_ dim_ order
Type Aliases§
- DimOrder
Type - The type used for elements of
Tensor.dim_order()
. - Sizes
Type - The type used for elements of
Tensor.sizes()
. - Strides
Type - The type used for elements of
Tensor.strides()
.