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
impl NetBuilder
Sourcepub fn vulkan(self, device: Device) -> Self
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.
Sourcepub fn reuse_blob_memory(self) -> Result<Self>
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.
Sourcepub fn with_options(options: Options) -> Self
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.
Sourcepub fn flag(self, flag: OptionFlag, enabled: bool) -> Result<Self>
pub fn flag(self, flag: OptionFlag, enabled: bool) -> Result<Self>
Overrides one boolean option and keeps every other field.
Sourcepub fn option(self, key: CpuOption, value: i32) -> Result<Self>
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.
Sourcepub fn threads(self, count: usize) -> Result<Self>
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.
Sourcepub fn load_files(
self,
parameters: impl AsRef<Path>,
weights: impl AsRef<Path>,
) -> Result<Net>
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.
Sourcepub fn load_bytes(self, parameters: &str, weights: &[u8]) -> Result<Net>
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.