Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = { workspace = true }

[workspace.dependencies]
mnn = { version = "0.2.0", path = "." }
mnn-sync = { version = "0.1.0", path = "mnn-sync" }
error-stack = { version = "0.5" }

[dependencies]
Expand Down Expand Up @@ -47,6 +48,12 @@ clap = { version = "4.5", features = ["derive"] }
divan = "0.1.14"
tracing = "0.1.40"
tracing-subscriber = "0.3.19"
tokio = { version = "1.0", features = [
"rt",
"rt-multi-thread",
"macros",
"test-util",
] }
tracing-test = { version = "0.2.5", features = ["no-env-filter"] }

[[bench]]
Expand Down
65 changes: 56 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,65 @@
# mnn-rs
![Codecov](https://img.shields.io/codecov/c/github/aftershootco/mnn-rs?link=https%3A%2F%2Fapp.codecov.io%2Fgithub%2Faftershootco%2Fmnn-rs)
![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)
# MNN Rust Bindings

Rust wrapper over [alibaba/MNN](https://github.com/alibaba/MNN) c++ library with handwritten C wrapper over mnn
Rust bindings for the [MNN](https://github.com/alibaba/MNN) neural network inference engine.

If you have nix you can just build the inspect binary with
## Installation

### Option 1: Automatic Download (Default)

This library will attempt to automatically download MNN source code when needed, but due to potential repository structure changes, it may not always work with the latest versions.

### Option 2: Git Submodule (Recommended)

The most reliable method is to use git submodules:

```bash
git submodule update --init --recursive
```
nix build .#inspect

### Option 3: Manual Download

You can also manually download MNN and specify its location:

1. Clone MNN repository: `git clone https://github.com/alibaba/MNN.git /path/to/mnn`
2. Set environment variable: `export MNN_SRC=/path/to/mnn`
3. Build your project: `cargo build`


## Environment Variables

| Variable | Description |
|----------|-------------|
| `MNN_SRC` | Path to MNN source code |
| `MNN_VERSION` | When downloading, specifies version tag (default: "3.0.5") |
| `MNN_COMPILE` | Set to "0" to skip compilation (requires `MNN_LIB_DIR`) |
| `MNN_LIB_DIR` | Path to pre-built MNN libraries |
| `MNN_SYSTEM` | Set to "1" to use system-installed MNN libraries |
| `MNN_FORCE_DOWNLOAD` | Set to "1" to force re-download of MNN source |

## Using System Libraries

To use system-installed MNN libraries:
```bash
MNN_SYSTEM=1 cargo build
```

NOTES:
On windows it will only compile with --release mode
There's a few issues with rustc linking to msvcrt by default and anything compiled with /MTd will not link properly
## Features

- `metal` - Enable Metal backend (Apple platforms)
- `coreml` - Enable CoreML backend (Apple platforms)
- `vulkan` - Enable Vulkan backend
- `opencl` - Enable OpenCL backend
- `opengl` - Enable OpenGL backend
- `crt_static` - Use static CRT on Windows
- `openmp` - Enable OpenMP (disables MNN threadpool)
- `mnn-threadpool` - Use MNN's threadpool (default)
- `tracing` - Enable tracing support
- `profile` - Enable profiling support
- `serde` - Enable serialization/deserialization support

## Examples

Check the `examples` directory for usage examples.



20 changes: 18 additions & 2 deletions mnn-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,15 @@ impl SessionRunner {
}

pub fn unload(self) -> Result<mnn::Interpreter> {
let session = self.session;
let net = self.interpreter;
// 使用ManuallyDrop来避免Drop trait的干扰
use std::mem::ManuallyDrop;
let this = ManuallyDrop::new(self);

// 安全地取出字段
let session = unsafe { std::ptr::read(&this.session) };
let net = unsafe { std::ptr::read(&this.interpreter) };

// 手动释放session
drop(session);
Ok(net)
}
Expand Down Expand Up @@ -288,6 +295,15 @@ impl SessionRunner {
}
}

impl Drop for SessionRunner {
fn drop(&mut self) {
// 根据用户建议,明确按顺序释放MNN资源
// 先正确释放session,再让interpreter自动drop
self.session.destroy();
// interpreter会在结构体drop时自动释放
}
}

impl SessionHandle {
pub fn new(interpreter: Interpreter, config: ScheduleConfig) -> Result<Self> {
let (sender, receiver) = flume::unbounded::<CallbackSender>();
Expand Down
Loading