Skip to main content

NetBuilder

Struct NetBuilder 

Source
pub struct NetBuilder { /* private fields */ }
Expand description

Configures a network and loads its model in one expression.

Each method changes one field and returns the builder, so a rejected value surfaces before any model is read. load_files and load_bytes consume the builder and return a Net whose options were applied through Net::configure.

Net::builder starts from Options::new, which disables light mode. Use with_options with Options::native_defaults to keep the native preset instead.

Implementations§

Source§

impl NetBuilder

Source

pub fn vulkan(self, device: Device) -> Self

Selects Vulkan for the network this builder creates.

The device is retained for the network’s lifetime and has to be selected before parameters load, which is why the builder offers it here. ncnn keeps per-layer CPU fallback, so this selects a backend rather than guaranteeing GPU placement. FP32 output guards become unavailable on a device network; Extractor::extract with TensorWidth::Stored and Net::infer_stored still serve it.

Source

pub fn reuse_blob_memory(self) -> Result<Self>

Reuses the memory of intermediate tensors across requests.

The builder creates one native pool allocator for intermediate blobs and one for layer workspace, attaches both to the option block before parameters load, and hands them to the network, which outlives them. Every extractor of the network then takes its scratch memory from those pools, so a request stops allocating and freeing intermediate tensors through the process heap. The pools keep the largest blocks they served for as long as the network lives, which is the memory the retained set grows by.

This is the lever for hosts whose heap is slow at the allocate-and-free of an output-sized buffer; docs/performance.md records the measured effect. It is independent of heap::retain_pages, which is about the pages of a buffer the caller allocates.

Source

pub fn with_options(options: Options) -> Self

Starts from the supplied options; subsequent flags override only their field. Use Options::native_defaults() explicitly for the native preset.

Source

pub fn flag(self, flag: OptionFlag, enabled: bool) -> Result<Self>

Overrides one boolean option and keeps every other field.

Source

pub fn option(self, key: CpuOption, value: i32) -> Result<Self>

Overrides one integer option after validating its range.

CpuOption::Threads must be positive, CpuOption::FlushDenormals stays within 0..=3, CpuOption::OpenmpBlocktime and CpuOption::KvCacheSequenceHint accept 0, and other keys accept 0 or 1. Anything else returns Error::Invalid.

Source

pub fn threads(self, count: usize) -> Result<Self>

Sets CpuOption::Threads, the native worker count.

A count of zero returns Error::Invalid; one thread runs the graph without native workers, which is the setting the conformance suite uses.

Source

pub fn load_files( self, parameters: impl AsRef<Path>, weights: impl AsRef<Path>, ) -> Result<Net>

Loads text parameters and weights from files.

The parameter file is the text form that begins with the 7767517 magic line. ncnn reads each layer’s weights in declaration order, so the two files must come from the same model. A graph without weight layers accepts an empty weights file.

Source

pub fn load_bytes(self, parameters: &str, weights: &[u8]) -> Result<Net>

Loads text parameters and weights from memory.

parameters is the text form, not the binary form. Pass &[] for a graph whose layers carry no weights. On an existing network, Net::load_param followed by Net::load_model performs the same sequence.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.