Skip to content
EdwardssssPublic

About

Rust bindings for ncnn inference with resource-safe ownership, reusable output buffers and an optional Vulkan backend.

Topics

Resources

Contributing

Security policy

Stars

44 stars

Watchers

2 watching

Forks

Repository files navigation

English | 简体中文

ncnn.rust

Rust bindings for ncnn with resource-safe ownership.

The repository contains two packages:

Package Role
ncnn-rust Safe API: networks, tensors, images, options, GPU resources
ncnn-rust-sys Generated C bindings and the project C++ adapters

Features

  • Owned-input inference with final-vector or reusable-buffer output.
  • Typed tensor access: ElementKind readers and writers cover FP16, BF16 and the integer widths, and infer_stored keeps the element width a layer wrote.
  • Batched tensors, layout rewriting with flatten and with_packing, and consuming helpers (into_normalized/into_reshape) that skip the copy when the caller owns the storage.
  • Zero-copy requests: Mat::from_external, Extractor::input_borrowed and Input::borrowed submit caller memory without a copy.
  • Graph introspection: Net::layers and Net::blobs report the wiring, the shapes and the support flags ncnn declares for each layer type, and layer_type_index resolves a type name to its index.
  • Scoped read-only CPU output guards that reserve the network without copying.
  • Graph-bound endpoint tokens that reject foreign or reloaded graphs.
  • Tensors, image preprocessing, custom layers, readers and allocators, including in-place normalization, pixel output, letterbox and depth borders, and affine warping.
  • Effective option readback on a loaded network, and caller-owned pools for blobs, workspace and the attention KV cache.
  • Optional Vulkan backend: device selection, resident tensors, allocator, recording commands, completed guards, buffer import and persistent pipeline caches.
  • Explicit native configuration mapping without Rust-side hardware gating.

Install

cargo add ncnn-rust

The crates ship Rust code only. Building them compiles the C++ adapters against an ncnn SDK that the build environment supplies, so the next section sets that SDK up.

Build

Cargo consumes an externally installed ncnn SDK. NCNN_INSTALL_DIR is required and must contain include/ncnn plus lib. Rust 1.85.1 is the tested MSRV.

export NCNN_INSTALL_DIR=/absolute/path/to/ncnn-install
export NCNN_LINK_KIND=dylib
cargo test --workspace
cargo run -p ncnn-rust --example add

Required native features: NCNN_C_API, NCNN_STDIO, NCNN_STRING, NCNN_PIXEL, NCNN_PIXEL_ROTATE, NCNN_PIXEL_AFFINE, NCNN_PIXEL_DRAWING, NCNN_BATCH, NCNN_INT8, NCNN_BF16, NCNN_VALIDATION. The default build requires NCNN_VULKAN=OFF; the vulkan Cargo feature requires NCNN_VULKAN=ON and a shared SDK with its dependency closure.

NCNN_LINK_KIND selects static (default) or dylib. A shared SDK also needs the directory that holds the shared library on the runtime loader path: lib through LD_LIBRARY_PATH or DYLD_LIBRARY_PATH, and the bin directory that CMake installs ncnn.dll into through PATH on Windows, where a static and an import library share the ncnn.lib name. NCNN_EXTRA_LIBS lists additional libraries in link order, optionally prefixed with static= or dylib=. NCNN_EXTRA_LIB_DIRS adds search directories. The SDK version is pinned to 20261020; NCNN_ALLOW_UNTESTED_VERSION=1 builds against another release for compatibility checks.

The raw feature exposes the generated bindings as ncnn_rust::sys. They carry ncnn's C ABI and no independent version policy, so the default build publishes none; the safe API is the surface that promises stability.

tools/fetch-ncnn.py verifies the pinned source archive, and tools/build-ncnn-sdk.py applies the patches in tools/patches/ and builds an SDK with the required feature set. --link-kind static builds the static library that NCNN_LINK_KIND=static links instead of the default shared one.

tools/install-sdk.py skips that local build by installing a published archive for the pinned release. The archives ship the shared library, so no linker choice is baked into them, and the installer checks the download against the release's SHA256SUMS and the release stamped in platform.h:

python3 tools/install-sdk.py --platform linux-x64-cpu --dest /opt/ncnn-20261020
export NCNN_INSTALL_DIR=/opt/ncnn-20261020

--list prints the published archives: linux-x64-cpu, linux-x64-vulkan, windows-x64-cpu and macos-arm64-cpu. The checksum detects a corrupted transfer rather than a substituted release, and no build script downloads anything on its own.

Usage

use ncnn_rust::{Input, Mat, Net};

let net = Net::builder()?.threads(2)?.load_files("model.param", "model.bin")?;
let input = Mat::from_f32(&values, &[224, 224, 3])?;
let outputs = net.infer([("input", input)], &["output"])?;
for output in outputs {
    println!("{:?} {}", output.info, output.values.len());
}

A request has one storage decision per side. On an extractor:

Form Storage
extract(endpoint, width) borrowed read-only guard, which reserves the network until it drops
extract_mat(endpoint, width) owned native tensor, without a second copy on the default pool
extract_into(endpoint, buffer) appended to a caller vector that keeps its capacity
extract_owned(endpoint) independent FP32 values

TensorWidth selects FP32 conversion or the width the layer wrote. On a network, Input carries that same decision, so one method covers owned storage, borrowed FP32 values and tensor sub views:

let outputs = net.infer([("input", Mat::from_f32(&values, &[224, 224, 3])?)], &["output"])?;
let borrowed = net.infer([("input", Input::borrowed(&values, &[224, 224, 3]))], &["output"])?;
let mut buffer = Vec::new();
net.infer_into([("input", Input::view(&image.channel(0)?))], &["output"], &mut buffer)?;

For repeated inference resolve endpoints once and fill one reusable vector:

let input_id = net.resolve_blob("input")?;
let output_id = net.resolve_blob("output")?;
let mut ex = net.extractor()?;
ex.input(&input_id, Mat::from_f32(&values, &[224, 224, 3])?)?;
let mut buffer = Vec::new();
ex.extract_into(&output_id, &mut buffer)?;

Run on a Vulkan device by attaching it before the model is read. Native per-layer CPU fallback stays available, so selection is not a guarantee that every layer runs on the GPU:

let device = ncnn_rust::gpu::Device::default_device()?;
let net = Net::builder()?.vulkan(device).load_files("model.param", "model.bin")?;

Quantized and half-precision outputs keep their stored width. infer converts to FP32, so read them through the stored forms and name the format:

let outputs = net.infer_stored([("input", input)], &["output"])?;
let (info, stored) = &outputs[0];
println!("{} bytes per element", info.elemsize);
let bytes: Vec<i8> = stored.to_i8()?;

Mat::from_typed and Mat::from_i8 build the same storage for an input side, and Extractor::extract with TensorWidth::Stored borrows it while one extractor runs. The typed tensor example and the resident Vulkan example run end to end.

An extractor is safe to reuse: submitting an input restarts the request, so a second inference recomputes instead of returning the output the first one cached. Per-extractor settings cover what the network options do not, and they survive that restart:

let mut ex = net.extractor()?;
if net.shared_blobs()?.is_empty() {
    ex.set_light_mode(true)?;
}
ex.set_kvcache_max_seqlen_hint(2048)?;

A graph that keeps state across inputs, such as an attention cache for token-by-token decoding, calls ex.continue_session() before the next input so that input extends the cached state instead of restarting the request. Light mode releases each intermediate after its first consumer, so set_light_mode(true) is refused while net.shared_blobs() lists a blob that feeds two layers; insert an explicit Split layer or keep light mode off.

Part of a tensor is readable without copying it. A view aliases the source storage: the slice readers borrow it, to_f32 copies out of it, and Extractor::input copies it because an alias keeps no storage of its own. Input::view reads the region where it lives, which is the zero-copy form for a model that takes a crop, a channel or one batch item:

let mut buffer = Vec::new();
let ranges = net.infer_into(
    [("input", Input::view(&image.channel(0)?))],
    &["output"],
    &mut buffer,
)?;
let range = &ranges[0].values;
let values = &buffer[range.start..range.end];

A network and its extractor stay on the thread that created them, so a service gives each worker its own pair; the thread example measures that against one network that uses every core.

Performance

Wrapper cost is measured against the native C API from the same SDK in one process, so the tables isolate the binding rather than the engine. Every number below comes from one machine and one campaign; the medians move with host load, so compare rows rather than absolute values.

Hardware: AMD Ryzen 7 5800H (8 cores, 16 threads), 16 GB DDR4-3200, NVIDIA GeForce RTX 3060 Laptop GPU (6 GB, driver 596.49), Windows 10 22H2 build 19045, MSVC 19.50, Rust 1.96.1, ncnn 20261020 built from source with the repository's build script. Vulkan rows run on the same GPU; the Linux figures quoted in Performance come from a Debian WSL2 guest on this machine with 8 logical cores.

Trained models, median milliseconds per request, one thread, release build:

Model Device native ncnn-rust Delta
NanoDet-m, 6 outputs CPU 23.011 23.379 +1.6%
PP-OCRv5 recognition CPU 57.566 58.842 +2.2%
NanoDet-m, 6 outputs Vulkan 4.489 4.702 +4.7%
PP-OCRv5 recognition Vulkan 10.863 10.210 within spread

The ncnn-rust column resolves endpoints once and fills a caller vector, which allocates nothing after warm-up. A vector owned per request instead adds one output copy, which is why the reuse form is the one quoted; the CPU deltas sit inside the campaign's 10% run-to-run spread.

A single-layer graph shows the same picture at microsecond scale, where the request path rather than the arithmetic dominates:

Case Device native ncnn-rust
ReLU CPU 0.002 ms 0.002 ms
AbsVal CPU 0.002 ms 0.002 ms
ReLU Vulkan 0.193 ms 0.197 ms
AbsVal Vulkan 0.183 ms 0.221 ms

The request path over one 65536-element FP32 tensor, median microseconds per request, one thread, pinned to one core, nine runs on the Debian WSL2 guest. The native column runs the three steps of a request through the C API and hands the tensor back where the engine left it:

Form native ncnn-rust Delta
owned input, borrowed output view 10.335 10.614 +3%
owned input, zero-copy owned output 10.335 10.356 within spread

Both rows keep the result in the buffer the extractor already detached, so the wrapper adds the call and the endpoint resolution and nothing else. Extractor::input consumes the tensor, which releases the caller's reference before the request runs: a native request that keeps its input handle alive blocks the light-mode reuse of that buffer and costs twenty times as much on the guest, so the wrapper's fast shape is also its default shape.

The forms that ask for a copy pay for one. A value vector per request allocates 256 KB on top of the native clone that Extractor::extract already performs, which costs 218 us on the guest against the 10.6 us above, and a cloned input and output allocate two such buffers. Performance records both with their allocation counts, the native C references they compare against, and the glibc and NT heap behaviour that decides what a fresh 256 KB buffer costs. That is why the reuse forms exist: NetBuilder::reuse_blob_memory() moves the engine's intermediate tensors into pools it owns, infer_into removes the caller vector for owned and borrowed inputs, and ncnn_rust::heap::retain_pages() keeps a caller-allocated buffer's pages mapped on glibc. The per-helper preprocessing medians, the pooling comparison, the Linux page-fault campaign, the worker scaling and the reproduction commands are in Performance.

Documentation

API documentation is generated by .github/workflows/docs.yml on every push to the default branch and published at https://edwardssss.github.io/ncnn.rust/ncnn_rust/. docs.rs cannot build this workspace: its sandbox has no ncnn SDK, and the build script compiles C++ adapters against the installed headers, so a missing NCNN_INSTALL_DIR fails before rustdoc starts.

License

BSD-3-Clause.

About

Rust bindings for ncnn inference with resource-safe ownership, reusable output buffers and an optional Vulkan backend.

Topics

Resources

Contributing

Security policy

Stars

44 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages