Skip to main content

executorch_sys/
lib.rs

1// some new clippy::lint annotations are supported in latest Rust but not recognized by older versions
2#![allow(unknown_lints)]
3#![deny(missing_docs)]
4#![cfg_attr(docsrs, feature(doc_cfg))]
5
6//! Unsafe bindings for ExecuTorch - On-device AI across mobile, embedded and edge for PyTorch.
7//!
8//! Provides a low level Rust bindings for the ExecuTorch library.
9//! For the common use case, it is recommended to use the high-level API provided by the `executorch` crate, where
10//! a more detailed documentation can be found.
11//!
12//!
13//! To build the library, you need to build the C++ library yourself first.
14//! Currently the supported Cpp executorch version is `1.4.1`.
15//! The C++ library allow for great flexibility with many flags, customizing which modules, kernels, and extensions are
16//! built.
17//! Multiple static libraries are built, and the Rust library links to them.
18//! In the following example we build the C++ library with the necessary flags to run example `hello_world`:
19//! ```bash
20//! # Clone the C++ library
21//! cd ${EXECUTORCH_CPP_DIR}
22//! git clone --depth 1 --branch v1.4.1 https://github.com/pytorch/executorch.git .
23//! git submodule sync --recursive
24//! git submodule update --init --recursive
25//!
26//! # Install requirements
27//! ./install_requirements.sh
28//!
29//! # Build C++ library
30//! mkdir cmake-out && cd cmake-out
31//! cmake \
32//!     -DDEXECUTORCH_SELECT_OPS_LIST=aten::add.out \
33//!     -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \
34//!     -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=OFF \
35//!     -DEXECUTORCH_BUILD_PORTABLE_OPS=ON \
36//!     -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
37//!     -DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \
38//!     -DEXECUTORCH_BUILD_EXTENSION_NAMED_DATA_MAP=ON \
39//!     -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
40//!     -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
41//!     -DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=ON \
42//!     -DEXECUTORCH_ENABLE_LOGGING=ON \
43//!     ..
44//! make -j
45//!
46//! # Run example
47//! # We set EXECUTORCH_RS_EXECUTORCH_LIB_DIR to the path of the C++ build output
48//! cd ${EXECUTORCH_RS_DIR}/examples/hello_world
49//! python export_model.py
50//! EXECUTORCH_RS_EXECUTORCH_LIB_DIR=${EXECUTORCH_CPP_DIR}/cmake-out cargo run
51//! ```
52//!
53//! The `executorch` crate will always look for the following static libraries:
54//! - `libexecutorch.a`
55//! - `libexecutorch_core.a`
56//!
57//! Additional libs are required if feature flags are enabled.
58//! For example the `libextension_data_loader.a` is required if the `data-loader` feature is enabled,
59//! and `libextension_tensor.a` is required if the `tensor-ptr` feature is enabled.
60//! See the feature flags section for more info.
61//!
62//! The static libraries of the kernels implementations are required only if your model uses them, and they should be
63//! **linked manually** by the binary that uses the `executorch` crate.
64//! For example, the `hello_world` example uses a model with a single addition operation, so it compile the C++
65//! library with `DEXECUTORCH_SELECT_OPS_LIST=aten::add.out` and contain the following lines in its `build.rs`:
66//! ```rust
67//! println!("cargo::rustc-link-lib=static:+whole-archive=portable_kernels");
68//! println!("cargo::rustc-link-lib=static:+whole-archive=portable_ops_lib");
69//!
70//! let libs_dir = std::env::var("EXECUTORCH_RS_EXECUTORCH_LIB_DIR").unwrap();
71//! println!("cargo::rustc-link-search=native={libs_dir}/kernels/portable/");
72//! ```
73//! Note that the ops and kernels libs are linked with `+whole-archive` to ensure that all symbols are included in the
74//! binary.
75//!
76//! The `EXECUTORCH_RS_EXECUTORCH_LIB_DIR` environment variable should be set to the path of the C++ build output.
77//! If its not provided, its the responsibility of the binary to add the libs directories to the linker search path, and
78//! the crate will just link to the static libraries using `cargo::rustc-link-lib=...`.
79//!
80//! If you want to link to executorch libs yourself, set the environment variable `EXECUTORCH_RS_LINK` to `0`, and
81//! the crate will not link to any library and not modify the linker search path.
82//!
83//! The crate contains a small C/C++ bridge that uses the headers of the C++ library,
84//! and it is compiled using the `cc` crate (and the `cxx` crate, that uses `cc` under the hood).
85//! If custom compiler flags (for example `-DET_MIN_LOG_LEVEL=Debug`) are used when compiling the C++ library,
86//! you should set the matching environment variables that `cc` reads during `cargo build`
87//! (for example `CFLAGS=-DET_MIN_LOG_LEVEL=Debug CXXFLAGS=-DET_MIN_LOG_LEVEL=Debug`),
88//! see the [cc docs](https://docs.rs/cc/latest/cc/).
89//!
90//!
91//! ## Cargo Features
92//! By default the `std` feature is enabled.
93//! - `data-loader`:
94//!   Includes the [`ET_FileDataLoader`] and [`ET_MmapDataLoader`] structs. Without this feature the only available
95//!   data loader is [`ET_BufferDataLoader`]. The `libextension_data_loader.a` static library is required, compile C++
96//!   `executorch` with `EXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON`.
97//! - `module`:
98//!   Includes the `Module` struct, a high-level API for loading and executing PyTorch models. It is an alternative to
99//!   the lower-level `Program` API, which is more suitable for embedded systems.
100//!   The `libextension_module_static.a` static library is required, compile C++ `executorch` with
101//!   `EXECUTORCH_BUILD_EXTENSION_MODULE=ON`.
102//!   Also includes the `std`, `data-loader` and `flat-tensor` features.
103//! - `tensor-ptr`:
104//!   Includes a few functions creating `cxx::SharedPtr<Tensor>` pointers, that manage the lifetime of the tensor
105//!   object alongside the lifetimes of the data buffer and additional metadata. The `libextension_tensor.a`
106//!   static library is required, compile C++ `executorch` with `EXECUTORCH_BUILD_EXTENSION_TENSOR=ON`.
107//!   Also includes the `std` feature.
108//! - `flat-tensor`:
109//!   Includes the `FlatTensorDataMap` struct that can read `.ptd` files with external tensors for models.
110//!   The `libextension_flat_tensor.a` static library is required,
111//!   compile C++ `executorch` with `EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON`.
112//! - `etdump`:
113//!   Includes the `ETDumpGen` struct, an implementation of an `EventTracer`, used for debugging and profiling.
114//!   The `libetdump.a` static library is required, compile C++ `executorch` with `EXECUTORCH_BUILD_DEVTOOLS=ON` and
115//!   `EXECUTORCH_ENABLE_EVENT_TRACER=ON`.
116//!   In addition, the `flatcc` (or `flatcc_d`) library is required, available at `{CMAKE_DIR}/third-party/flatcc_ep/lib/`,
117//!   and should be linked by the user.
118//! - `std`:
119//!   Enable the standard library. This feature is enabled by default, but can be disabled to build `executorch` in a `no_std` environment.
120//!   NOTE: no_std is still WIP, see <https://github.com/pytorch/executorch/issues/4561>
121//!
122//! [`ET_FileDataLoader`]: crate::ET_FileDataLoader
123//! [`ET_MmapDataLoader`]: crate::ET_MmapDataLoader
124//! [`ET_BufferDataLoader`]: crate::ET_BufferDataLoader
125//! [`Module`]: crate::Module
126
127#![cfg_attr(not(feature = "std"), no_std)]
128
129#[cfg(not(feature = "std"))]
130extern crate core as std;
131
132#[cfg(all(feature = "std", link_cxx))]
133extern crate link_cplusplus;
134
135/// The version of the ExecuTorch C++ library that this crate is compatible and linked with.
136pub const EXECUTORCH_CPP_VERSION: &str = "1.4.1";
137
138mod c_bridge;
139pub use c_bridge::*;
140
141#[cfg(feature = "std")]
142mod cxx_bridge;
143#[cfg(feature = "std")]
144pub use cxx_bridge::*;
145
146/// Utility functions and structs.
147pub mod util {
148    #[cfg(feature = "tensor-ptr")]
149    pub use super::cxx_bridge::tensor_ptr::cxx_util::*;
150}
151
152// Re-export cxx
153#[cfg(feature = "std")]
154pub use cxx;