1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Clippy doesnt detect the 'Safety' comments in the cxx bridge.
#![allow(clippy::missing_safety_doc)]
use cxx::{type_id, ExternType};
#[cxx::bridge]
pub(crate) mod ffi {
unsafe extern "C++" {
include!("executorch-sys/cpp/executorch_rs/cxx_bridge.hpp");
/// Cpp executorch error type.
type Error = crate::Error;
/// Types of validation that a `Program` can do before parsing the data.
type ProgramVerification = crate::ProgramVerification;
/// Describes a method in an ExecuTorch program.
///
/// The program used to create a MethodMeta object must outlive the MethodMeta.
/// It is separate from Method so that this information can be accessed without
/// paying the initialization cost of loading the full Method.
type MethodMeta = crate::MethodMeta;
/// A facade class for loading programs and executing methods within them.
#[namespace = "executorch::extension"]
type Module;
/// Enum to define loading behavior.
type ModuleLoadMode = crate::ModuleLoadMode;
/// A specification of `ArrayRef<EValue>`.
type ArrayRefEValue = crate::ArrayRefEValue;
/// A vector of `EValue`.
type VecEValue = crate::VecEValue;
/// EventTracer is a class that users can inherit and implement to log/serialize/stream etc.
#[namespace = "executorch::runtime"]
type EventTracer;
/// An allocator used to allocate objects for the runtime.
type MemoryAllocator = crate::MemoryAllocator;
/// Redefinition of the [`HierarchicalAllocator`](crate::HierarchicalAllocator).
type HierarchicalAllocator = crate::HierarchicalAllocator;
/// Constructs an instance by loading a program from a file with specified
/// memory locking behavior.
///
/// # Arguments
///
/// - `file_path`: The path to the ExecuTorch program file to load.
/// - `data_files`: The path to one or more .ptd file/s.
/// - `load_mode`: The loading mode to use.
/// - `event_tracer`: An EventTracer used for tracking and logging events, or null if not needed.
#[namespace = "executorch_rs"]
fn Module_new(
file_path: &CxxString,
data_files: &[&str],
load_mode: ModuleLoadMode,
event_tracer: UniquePtr<EventTracer>,
memory_allocator: UniquePtr<MemoryAllocator>,
temp_allocator: UniquePtr<MemoryAllocator>,
) -> UniquePtr<Module>;
/// Load the program if needed.
///
/// # Arguments
///
/// - `verification`: The type of verification to do before returning success.
///
/// # Returns
///
/// An Error to indicate success or failure of the loading process.
#[namespace = "executorch_rs"]
fn Module_load(self_: Pin<&mut Module>, verification: ProgramVerification) -> Error;
/// Checks if the program is loaded.
#[namespace = "executorch_rs"]
fn Module_is_loaded(self_: &Module) -> bool;
/// Get the number of methods available in the loaded program.
///
/// # Safety
///
/// The `method_num_out` is valid only if the function returns `Error::Ok`.
#[namespace = "executorch_rs"]
unsafe fn Module_num_methods(self_: Pin<&mut Module>, method_num_out: *mut usize) -> Error;
/// Get a list of method names available in the loaded program.
///
/// Loads the program and method if needed.
///
/// # Arguments
///
/// - `method_names_out`: A pointer to a (non initialized) vector that will be created and filled with
/// the method names.
///
/// # Returns
///
/// A error indicating whether the method names retrieval was successful or not.
///
/// # Safety
///
/// The `method_names_out` vector can be used only if the function returns `Error::Ok`.
#[namespace = "executorch_rs"]
unsafe fn Module_method_names(
self_: Pin<&mut Module>,
method_names_out: *mut Vec<String>,
) -> Error;
/// Load a specific method from the program and set up memory management if
/// needed.
///
/// The loaded method is cached to reuse the next time it's executed.
///
/// # Arguments
///
/// - `method_name`: The name of the method to load.
///
/// # Returns
///
/// An Error to indicate success or failure.
#[namespace = "executorch_rs"]
unsafe fn Module_load_method(
self_: Pin<&mut Module>,
method_name: &CxxString,
planned_memory: *mut HierarchicalAllocator,
event_tracer: *mut EventTracer,
) -> Error;
/// Unload a specific method from the program.
///
/// # Arguments
/// - `method_name`: The name of the method to unload.
///
/// # Returns
///
/// True if the method is unloaded, false if no-op.
#[namespace = "executorch_rs"]
unsafe fn Module_unload_method(self_: Pin<&mut Module>, method_name: &CxxString) -> bool;
/// Checks if a specific method is loaded.
///
/// # Arguments
///
/// - `method_name`: The name of the method to check.
///
/// # Returns
///
/// `true` if the method specified by `method_name` is loaded, `false` otherwise.
#[namespace = "executorch_rs"]
fn Module_is_method_loaded(self_: &Module, method_name: &CxxString) -> bool;
/// Get a method metadata struct by method name.
///
/// Loads the program if needed.
///
/// # Arguments
///
/// - `method_name`: The name of the method to get the metadata for.
/// - `method_meta_out`: A mutable reference to a `MethodMeta` struct that will be filled with the metadata.
///
/// # Returns
///
/// A error indicating whether the metadata retrieval was successful or not.
///
/// # Safety
///
/// The `method_meta_out` struct must be valid for the lifetime of the function.
/// The `method_meta_out` struct can be used only if the function returns `Error::Ok`.
#[namespace = "executorch_rs"]
unsafe fn Module_method_meta(
self_: Pin<&mut Module>,
method_name: &CxxString,
method_meta_out: *mut MethodMeta,
) -> Error;
/// Execute a specific method with the given input values and retrieve the
/// output values. Loads the program and method before executing if needed.
///
/// # Arguments
///
/// - `method_name`: The name of the method to execute.
/// - `inputs`: A vector of input values to be passed to the method.
/// - `outputs`: A mutable reference to a vector that will be filled with the output values from the method.
///
/// # Returns
///
/// A error indicating whether the execution was successful or not.
///
/// # Safety
///
/// The `outputs` vector must be valid for the lifetime of the function.
/// The `outputs` vector can be used only if the function returns `Error::Ok`.
#[namespace = "executorch_rs"]
unsafe fn Module_execute(
self_: Pin<&mut Module>,
method_name: &CxxString,
inputs: ArrayRefEValue,
outputs: *mut VecEValue,
) -> Error;
}
}
unsafe impl ExternType for crate::HierarchicalAllocator {
type Id = type_id!("HierarchicalAllocator");
type Kind = cxx::kind::Trivial;
}
unsafe impl ExternType for crate::ModuleLoadMode {
type Id = type_id!("ModuleLoadMode");
type Kind = cxx::kind::Trivial;
}