Skip to main content

mnn/
lib.rs

1//!
2//! Ergonomic rust bindings for [MNN](https://github.com/alibaba/MNN)
3//!
4//! The main data structures used are [`Tensor`] and [`Interpreter`].
5//! [Interpreter] should be thread safe and can be used to run multiple sessions concurrently.
6//! [Send] / [Sync] is not implemented for Interpreter yet since we don't know how it will be used.
7//!
8//! ![Codecov](https://img.shields.io/codecov/c/github/aftershootco/mnn-rs?link=https%3A%2F%2Fapp.codecov.io%2Fgithub%2Faftershootco%2Fmnn-rs)
9//! ![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)
10//! # Example
11//! ```rust,no_run
12//! use mnn::*;
13//! let mut interpreter = Interpreter::from_bytes([0;100]).unwrap();
14//! let mut sc = ScheduleConfig::new();
15//! let mut session = interpreter.create_session(sc).unwrap();
16//! let mut input = interpreter.input::<f32>(&mut session, "input").unwrap();
17//! let mut tensor = input.create_host_tensor_from_device(false);
18//! tensor.host_mut().fill(1.0f32);
19//! input.copy_from_host_tensor(&tensor).unwrap();
20//! interpreter.run_session(&session).unwrap();
21//! let output = interpreter.output::<u8>(&session, "output").unwrap();
22//! let mut output_tensor = output.create_host_tensor_from_device(true);
23//! std::fs::write("output.bin", output_tensor.host().to_vec()).unwrap();
24//! ```
25//! **NOTE:**  The library is still in development and the API is subject to change.
26//!
27//! ## Features
28//! - `metal`: Enable mnn Metal backend
29//! - `coreml`: Enable mnn CoreML backend
30//! - `vulkan`: Enable mnn Vulkan backend (unimplemented from rust wrapper)
31//! - `opencl`: Enable mnn OpenCL backend
32//! - `opengl`: Enable mnn OpenGL backend (unimplemented from rust wrapper)
33//! - `cuda`: Enable mnn CUDA backend (Linux only, requires CUDA toolkit)
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//! | CUDA        | ✅       | 🚸    |
57//! | OpenGL      | ❌       | ❌    |
58//! | Vulkan      | ❌       | ❌    |
59//!
60//! - ✅ - Works
61//! - 🚸 - Some models work
62//! - ❌ - Doesn't work
63
64/// Re-export of whole mnn-sys
65pub mod ffi {
66    pub use mnn_sys::*;
67}
68
69mod profile;
70
71pub mod backend;
72/// Error handling
73pub mod error;
74/// MNN::Interpreter related items
75pub mod interpreter;
76/// Schedule configuration
77pub mod schedule;
78/// MNN::Session related items
79pub mod session;
80/// MNN::Tensor related items
81pub mod tensor;
82
83pub use crate::tensor::list::{TensorList, TensorListMut};
84pub use backend::*;
85pub use error::*;
86pub use interpreter::*;
87pub use schedule::*;
88pub use session::*;
89pub use tensor::*;
90
91pub use ffi::HalideType;
92pub use ffi::MapType;
93
94/// Re-export of commonly used items
95pub mod prelude {
96    pub use crate::error::*;
97    pub(crate) use crate::profile::profile;
98    pub use crate::tensor::{
99        Device, Host, Owned, TensorMachine, TensorType, TensorView, TensorViewMut, View,
100    };
101    pub use core::marker::PhantomData;
102    pub use error_stack::{Report, ResultExt};
103    pub use libc::*;
104    pub use mnn_sys::{HalideType, MapType};
105}