executorch_sys/
lib.rs

1#![cfg_attr(deny_warnings, deny(warnings))]
2#![cfg_attr(deny_warnings, deny(missing_docs))]
3#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
4
5//! Unsafe bindings for ExecuTorch - On-device AI across mobile, embedded and edge for PyTorch.
6//!
7//! Provides a low level Rust bindings for the ExecuTorch library.
8//! For the common use case, it is recommended to use the high-level API provided by the `executorch` crate, where
9//! a more detailed documentation can be found.
10//!
11//!
12//! To build the library, you need to build the C++ library first.
13//! The C++ library allow for great flexibility with many flags, customizing which modules, kernels, and extensions are
14//! built.
15//! Multiple static libraries are built, and the Rust library links to them.
16//! In the following example we build the C++ library with the necessary flags to run example `hello_world`:
17//! ```bash
18//! # Clone the C++ library
19//! cd ${EXECUTORCH_CPP_DIR}
20//! git clone --depth 1 --branch v0.5.0 https://github.com/pytorch/executorch.git .
21//! git submodule sync --recursive
22//! git submodule update --init --recursive
23//!
24//! # Install requirements
25//! ./install_requirements.sh
26//!
27//! # Build C++ library
28//! mkdir cmake-out && cd cmake-out
29//! cmake \
30//!     -DDEXECUTORCH_SELECT_OPS_LIST=aten::add.out \
31//!     -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \
32//!     -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=OFF \
33//!     -DBUILD_EXECUTORCH_PORTABLE_OPS=ON \
34//!     -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
35//!     -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
36//!     -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
37//!     -DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=ON \
38//!     -DEXECUTORCH_ENABLE_LOGGING=ON \
39//!     ..
40//! make -j
41//!
42//! # Static libraries are in cmake-out/
43//! # core:
44//! #   cmake-out/libexecutorch.a
45//! #   cmake-out/libexecutorch_core.a
46//! # kernels implementations:
47//! #   cmake-out/kernels/portable/libportable_ops_lib.a
48//! #   cmake-out/kernels/portable/libportable_kernels.a
49//! # extension data loader, enabled with EXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON:
50//! #   cmake-out/extension/data_loader/libextension_data_loader.a
51//! # extension module, enabled with EXECUTORCH_BUILD_EXTENSION_MODULE=ON:
52//! #   cmake-out/extension/module/libextension_module_static.a
53//! # extension tensor, enabled with EXECUTORCH_BUILD_EXTENSION_TENSOR=ON:
54//! #   cmake-out/extension/tensor/libextension_tensor.a
55//! # extension tensor, enabled with EXECUTORCH_BUILD_DEVTOOLS=ON:
56//! #   cmake-out/devtools/libetdump.a
57//!
58//! # Run example
59//! # We set EXECUTORCH_RS_EXECUTORCH_LIB_DIR to the path of the C++ build output
60//! cd ${EXECUTORCH_RS_DIR}/examples/hello_world
61//! python export_model.py
62//! EXECUTORCH_RS_EXECUTORCH_LIB_DIR=${EXECUTORCH_CPP_DIR}/cmake-out cargo run
63//! ```
64//!
65//! The `executorch` crate will always look for the following static libraries:
66//! - `libexecutorch.a`
67//! - `libexecutorch_core.a`
68//!
69//! Additional libs are required if feature flags are enabled (see next section):
70//! - `libextension_data_loader.a`
71//! - `libextension_module_static.a`
72//! - `libextension_tensor.a`
73//! - `libetdump.a`
74//!
75//! The static libraries of the kernels implementations are required only if your model uses them, and they should be
76//! **linked manually** by the binary that uses the `executorch` crate.
77//! For example, the `hello_world` example uses a model with a single addition operation, so it compile the C++
78//! library with `DEXECUTORCH_SELECT_OPS_LIST=aten::add.out` and contain the following lines in its `build.rs`:
79//! ```rust
80//! println!("cargo::rustc-link-lib=static:+whole-archive=portable_kernels");
81//! println!("cargo::rustc-link-lib=static:+whole-archive=portable_ops_lib");
82//!
83//! let libs_dir = std::env::var("EXECUTORCH_RS_EXECUTORCH_LIB_DIR").unwrap();
84//! println!("cargo::rustc-link-search=native={libs_dir}/kernels/portable/");
85//! ```
86//! Note that the ops and kernels libs are linked with `+whole-archive` to ensure that all symbols are included in the
87//! binary.
88//!
89//! The `EXECUTORCH_RS_EXECUTORCH_LIB_DIR` environment variable should be set to the path of the C++ build output.
90//! If its not provided, its the resposibility of the binary to add the libs directories to the linker search path, and
91//! the crate will just link to the static libraries using `cargo::rustc-link-lib=...`.
92//!
93//! If you want to link to executorch libs yourself, set the environment variable `EXECUTORCH_RS_LINK` to `0`, and
94//! the crate will not link to any library and not modify the linker search path.
95//!
96//! ## Cargo Features
97//! By default the `std` feature is enabled.
98//! - `data-loader`: Includes the [`FileDataLoader`] and [`MmapDataLoader`] structs. Without this feature the only available
99//!     data loader is [`BufferDataLoader`]. The `libextension_data_loader.a` static library is required, compile C++
100//!     `executorch` with `EXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON`.
101//! - `module`: Includes the [`Module`] struct. The `libextension_module_static.a` static library is required, compile C++
102//!     `executorch` with `EXECUTORCH_BUILD_EXTENSION_MODULE=ON`.
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//! - `etdump`
109//!     Includes the `ETDumpGen` struct, an implementation of an `EventTracer`, used for debugging and profiling.
110//!     The `libetdump.a` static library is required, compile C++ `executorch` with `EXECUTORCH_BUILD_DEVTOOLS=ON` and
111//!     `EXECUTORCH_ENABLE_EVENT_TRACER=ON`.
112//!     In addition, the `flatcc` (or `flatcc_d`) library is required, available at `{CPP_EXECUTORCH_DIR}/third-party/flatcc/lib/`,
113//!     and should be linked by the user.
114//! - `std`:
115//!     Enable the standard library. This feature is enabled by default, but can be disabled to build `executorch` in a `no_std` environment.
116//!     NOTE: no_std is still WIP, see <https://github.com/pytorch/executorch/issues/4561>
117//!
118//! [`FileDataLoader`]: crate::FileDataLoader
119//! [`MmapDataLoader`]: crate::MmapDataLoader
120//! [`BufferDataLoader`]: crate::BufferDataLoader
121//! [`Module`]: crate::cpp::Module
122
123#![cfg_attr(not(feature = "std"), no_std)]
124
125#[cfg(not(feature = "std"))]
126extern crate core as std;
127
128#[cfg(all(feature = "std", link_cxx))]
129extern crate link_cplusplus;
130
131mod c_bridge;
132pub use c_bridge::*;
133
134#[cfg(feature = "std")]
135mod cxx_bridge;
136
137/// Bindings generated by the `cxx` crate.
138#[cfg(feature = "std")]
139pub mod cpp {
140    pub use crate::cxx_bridge::core::ffi::*;
141
142    #[cfg(feature = "module")]
143    pub use crate::cxx_bridge::module::ffi::*;
144
145    #[cfg(feature = "tensor-ptr")]
146    pub use crate::cxx_bridge::tensor_ptr::ffi::*;
147
148    #[cfg(feature = "tensor-ptr")]
149    pub use super::cxx_bridge::tensor_ptr::cxx_util as util;
150}
151
152// Re-export cxx
153#[cfg(feature = "std")]
154pub use cxx;