1use ndarray::{ArrayD, ArrayViewD};
6use std::path::Path;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum MnnError {
13 InvalidParameter(String),
15 OutOfMemory,
17 RuntimeError(String),
19 Unsupported,
21 ModelLoadFailed(String),
23 BackendUnavailable(String),
25 NullPointer,
27 ShapeMismatch {
29 expected: Vec<usize>,
30 got: Vec<usize>,
31 },
32}
33
34impl std::fmt::Display for MnnError {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 write!(f, "{:?}", self)
37 }
38}
39
40impl std::error::Error for MnnError {}
41
42pub type Result<T> = std::result::Result<T, MnnError>;
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
49pub enum Backend {
50 #[default]
52 CPU,
53 Metal,
55 OpenCL,
57 OpenGL,
59 Vulkan,
61 CUDA,
63 CoreML,
65}
66
67impl Backend {
68 pub const fn as_str(self) -> &'static str {
70 match self {
71 Backend::CPU => "cpu",
72 Backend::Metal => "metal",
73 Backend::OpenCL => "opencl",
74 Backend::OpenGL => "opengl",
75 Backend::Vulkan => "vulkan",
76 Backend::CUDA => "cuda",
77 Backend::CoreML => "coreml",
78 }
79 }
80
81 pub const fn is_available(self) -> bool {
83 matches!(self, Backend::CPU)
84 }
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
89pub enum PrecisionMode {
90 #[default]
92 Normal,
93 Low,
95 High,
97 LowMemory,
99}
100
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
103pub enum DataFormat {
104 #[default]
106 NCHW,
107 NHWC,
109}
110
111#[derive(Debug, Clone)]
115pub struct InferenceConfig {
116 pub thread_count: i32,
117 pub precision_mode: PrecisionMode,
118 pub backend: Backend,
119 pub use_cache: bool,
120 pub data_format: DataFormat,
121}
122
123impl Default for InferenceConfig {
124 fn default() -> Self {
125 Self {
126 thread_count: 4,
127 precision_mode: PrecisionMode::Normal,
128 backend: Backend::CPU,
129 use_cache: true,
130 data_format: DataFormat::NCHW,
131 }
132 }
133}
134
135impl InferenceConfig {
136 pub fn new() -> Self {
138 Self::default()
139 }
140
141 pub fn with_threads(mut self, threads: i32) -> Self {
143 self.thread_count = threads;
144 self
145 }
146
147 pub fn with_precision(mut self, precision: PrecisionMode) -> Self {
149 self.precision_mode = precision;
150 self
151 }
152
153 pub fn with_backend(mut self, backend: Backend) -> Self {
155 self.backend = backend;
156 self
157 }
158
159 pub fn with_data_format(mut self, format: DataFormat) -> Self {
161 self.data_format = format;
162 self
163 }
164}
165
166pub struct SharedRuntime {
170 _private: (),
171}
172
173impl SharedRuntime {
174 pub fn new(_config: &InferenceConfig) -> Result<Self> {
176 unimplemented!(
177 "This feature is only available at runtime, not available during documentation build"
178 )
179 }
180}
181
182pub struct InferenceEngine {
186 _input_shape: Vec<usize>,
187 _output_shape: Vec<usize>,
188}
189
190impl InferenceEngine {
191 pub fn from_file(
193 _model_path: impl AsRef<Path>,
194 _config: Option<InferenceConfig>,
195 ) -> Result<Self> {
196 unimplemented!(
197 "This feature is only available at runtime, not available during documentation build"
198 )
199 }
200
201 pub fn from_buffer(_data: &[u8], _config: Option<InferenceConfig>) -> Result<Self> {
203 unimplemented!(
204 "This feature is only available at runtime, not available during documentation build"
205 )
206 }
207
208 pub fn from_buffer_with_runtime(
210 _model_buffer: &[u8],
211 _runtime: &SharedRuntime,
212 ) -> Result<Self> {
213 unimplemented!(
214 "This feature is only available at runtime, not available during documentation build"
215 )
216 }
217
218 pub fn input_shape(&self) -> &[usize] {
220 &self._input_shape
221 }
222
223 pub fn output_shape(&self) -> &[usize] {
225 &self._output_shape
226 }
227
228 pub fn infer(&self, _input: ArrayViewD<f32>) -> Result<ArrayD<f32>> {
230 unimplemented!()
231 }
232
233 pub fn infer_dynamic(&self, _input: ArrayViewD<f32>) -> Result<ArrayD<f32>> {
235 unimplemented!()
236 }
237
238 pub fn run_dynamic(&self, _input: ArrayViewD<f32>) -> Result<ArrayD<f32>> {
240 unimplemented!()
241 }
242
243 pub fn run_dynamic_raw(
245 &self,
246 _input_data: &[f32],
247 _input_shape: &[usize],
248 _output_data: &mut [f32],
249 ) -> Result<Vec<usize>> {
250 unimplemented!()
251 }
252}
253
254pub fn get_version() -> String {
258 "unknown (docs.rs build)".to_string()
259}