mnn/
lib.rs

1#![deny(missing_docs)]
2//!
3//! Ergonomic rust bindings for [MNN](https://github.com/alibaba/MNN)  
4//!
5//! The main data structures used are [`Tensor`] and [`Interpreter`].   
6//! [Interpreter] should be thread safe and can be used to run multiple sessions concurrently.  
7//! [Send] / [Sync] is not implemented for Interpreter yet since we don't know how it will be used.  
8//!
9//! ![Codecov](https://img.shields.io/codecov/c/github/aftershootco/mnn-rs?link=https%3A%2F%2Fapp.codecov.io%2Fgithub%2Faftershootco%2Fmnn-rs)
10//! ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/aftershootco/mnn-rs/build.yaml?link=https%3A%2F%2Fgithub.com%2Faftershootco%2Fmnn-rs%2Factions%2Fworkflows%2Fbuild.yaml)
11//! # Example  
12//! ```rust,no_run
13//! use mnn::*;
14//! let mut interpreter = Interpreter::from_bytes([0;100]).unwrap();
15//! let mut sc = ScheduleConfig::new();
16//! let session = interpreter.create_session(sc).unwrap();
17//! let mut input = interpreter.input::<f32>(&session, "input").unwrap();
18//! let mut tensor = input.create_host_tensor_from_device(false);
19//! tensor.host_mut().fill(1.0f32);
20//! input.copy_from_host_tensor(&tensor).unwrap();
21//! interpreter.run_session(&session).unwrap();
22//! let output = interpreter.output::<u8>(&session, "output").unwrap();
23//! let mut output_tensor = output.create_host_tensor_from_device(true);
24//! std::fs::write("output.bin", output_tensor.host().to_vec()).unwrap();
25//! ```
26//! **NOTE:**  The library is still in development and the API is subject to change.   
27//!
28//! ## Features
29//! - `metal`: Enable mnn Metal backend
30//! - `coreml`: Enable mnn CoreML backend
31//! - `vulkan`: Enable mnn Vulkan backend (unimplemented from rust wrapper)
32//! - `opencl`: Enable mnn OpenCL backend
33//! - `opengl`: Enable mnn OpenGL backend (unimplemented from rust wrapper)
34//! - `openmp`: Enable mnn Openmp ( disable the mnn-threadpool feature to enable this)
35//! - `mnn-threadpool`: Enable mnn threadpool ( enabled by default can't be used with openmp)
36//! - `sync`: Enable sync api
37//! - `profile`: Enable profiling ( emits some profiling tracing events )
38//! - `tracing`: Enable tracing ( emits some tracing events )
39//! - `crt_static`: Link statically to the C runtime on windows (noop on other platforms)
40//! ## License
41//! This links to the MNN library which is licensed under the Apache License 2.0.  
42//! The rust bindings are licensed under the same Apache License 2.0.  
43//!
44//! ## Building
45//! The flake.nix provides a nix-shell with all the dependencies required to build the library.  
46//! If not using nix you'll need to clone the git submodule to get the MNN source code in mnn-sys/vendor first  
47//! Or you can export the MNN_SRC environment variable to point to the MNN source code.  
48//!
49//! ## Compatibility Chart for current crate
50//! | MNN Backend | Compiles | Works |
51//! | ----------- | -------- | ----- |
52//! | CPU         | ✅       | ✅    |
53//! | OpenCL      | ✅       | ✅    |
54//! | Metal       | ✅       | ✅    |
55//! | CoreML      | ✅       | 🚸    |
56//! | OpenGL      | ❌       | ❌    |
57//! | Vulkan      | ❌       | ❌    |
58//!
59//! - ✅ - Works  
60//! - 🚸 - Some models work  
61//! - ❌ - Doesn't work
62
63/// Re-export of whole mnn-sys
64pub mod ffi {
65    pub use mnn_sys::*;
66}
67
68mod profile;
69
70pub mod backend;
71/// Error handling
72pub mod error;
73/// MNN::Interpreter related items
74pub mod interpreter;
75/// Schedule configuration
76pub mod schedule;
77/// MNN::Session related items
78pub mod session;
79/// MNN::Tensor related items
80pub mod tensor;
81
82pub use backend::*;
83pub use error::*;
84pub use interpreter::*;
85pub use schedule::*;
86pub use session::*;
87pub use tensor::*;
88
89pub use ffi::HalideType;
90pub use ffi::MapType;
91
92/// Re-export of commonly used items
93pub mod prelude {
94    pub use crate::error::*;
95    pub(crate) use crate::profile::profile;
96    pub use core::marker::PhantomData;
97    pub use error_stack::{Report, ResultExt};
98    pub use libc::*;
99    pub use mnn_sys::{HalideType, MapType};
100}