Skip to main content

ocr_rs/mnn/
docsrs_stub.rs

1//! docsrs stub module - for documentation generation
2//!
3//! This module is used during docs.rs build, providing type definitions without actual implementations
4
5use ndarray::{ArrayD, ArrayViewD};
6use std::path::Path;
7
8// ============== Error Types ==============
9
10/// MNN-related errors
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum MnnError {
13    /// Invalid parameter
14    InvalidParameter(String),
15    /// Out of memory
16    OutOfMemory,
17    /// Runtime error
18    RuntimeError(String),
19    /// Unsupported operation
20    Unsupported,
21    /// Model loading failed
22    ModelLoadFailed(String),
23    /// Requested inference backend is not available in the linked MNN build
24    BackendUnavailable(String),
25    /// Null pointer error
26    NullPointer,
27    /// Shape mismatch
28    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
42/// MNN Result type
43pub type Result<T> = std::result::Result<T, MnnError>;
44
45// ============== Backend Types ==============
46
47/// Computation backend
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
49pub enum Backend {
50    /// CPU backend (default)
51    #[default]
52    CPU,
53    /// Metal GPU (macOS/iOS)
54    Metal,
55    /// OpenCL GPU
56    OpenCL,
57    /// OpenGL GPU
58    OpenGL,
59    /// Vulkan GPU
60    Vulkan,
61    /// CUDA GPU
62    CUDA,
63    /// CoreML (macOS/iOS)
64    CoreML,
65}
66
67impl Backend {
68    /// Stable backend name used in errors, logs, and command-line options.
69    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    /// Runtime availability cannot be inspected while generating docs.
82    pub const fn is_available(self) -> bool {
83        matches!(self, Backend::CPU)
84    }
85}
86
87/// Precision mode
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
89pub enum PrecisionMode {
90    /// Normal precision
91    #[default]
92    Normal,
93    /// Low precision
94    Low,
95    /// High precision
96    High,
97    /// Low memory usage
98    LowMemory,
99}
100
101/// Data format
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
103pub enum DataFormat {
104    /// NCHW format
105    #[default]
106    NCHW,
107    /// NHWC format
108    NHWC,
109}
110
111// ============== Configuration Types ==============
112
113/// Inference configuration
114#[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    /// Create a new inference configuration
137    pub fn new() -> Self {
138        Self::default()
139    }
140
141    /// Set the number of threads
142    pub fn with_threads(mut self, threads: i32) -> Self {
143        self.thread_count = threads;
144        self
145    }
146
147    /// Set the precision mode
148    pub fn with_precision(mut self, precision: PrecisionMode) -> Self {
149        self.precision_mode = precision;
150        self
151    }
152
153    /// Set the backend
154    pub fn with_backend(mut self, backend: Backend) -> Self {
155        self.backend = backend;
156        self
157    }
158
159    /// Set the data format
160    pub fn with_data_format(mut self, format: DataFormat) -> Self {
161        self.data_format = format;
162        self
163    }
164}
165
166// ============== Shared Runtime ==============
167
168/// Shared runtime for sharing resources between multiple engines
169pub struct SharedRuntime {
170    _private: (),
171}
172
173impl SharedRuntime {
174    /// Create a new shared runtime
175    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
182// ============== Inference Engine ==============
183
184/// MNN inference engine
185pub struct InferenceEngine {
186    _input_shape: Vec<usize>,
187    _output_shape: Vec<usize>,
188}
189
190impl InferenceEngine {
191    /// Create inference engine from file
192    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    /// Create inference engine from memory
202    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    /// Create inference engine from model bytes using shared runtime
209    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    /// Get input shape
219    pub fn input_shape(&self) -> &[usize] {
220        &self._input_shape
221    }
222
223    /// Get output shape
224    pub fn output_shape(&self) -> &[usize] {
225        &self._output_shape
226    }
227
228    /// Perform inference
229    pub fn infer(&self, _input: ArrayViewD<f32>) -> Result<ArrayD<f32>> {
230        unimplemented!()
231    }
232
233    /// Perform inference (variable input shape)
234    pub fn infer_dynamic(&self, _input: ArrayViewD<f32>) -> Result<ArrayD<f32>> {
235        unimplemented!()
236    }
237
238    /// Perform inference (variable input shape) - alias
239    pub fn run_dynamic(&self, _input: ArrayViewD<f32>) -> Result<ArrayD<f32>> {
240        unimplemented!()
241    }
242
243    /// Perform inference (raw interface)
244    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
254// ============== Helper Functions ==============
255
256/// Get MNN version
257pub fn get_version() -> String {
258    "unknown (docs.rs build)".to_string()
259}