Skip to main content

OutputGuard

Struct OutputGuard 

Source
pub struct OutputGuard<'extractor> { /* private fields */ }
Expand description

A read-only output that borrows its extractor and reserves the network.

ⓘ
fn reuse(ex: &mut ncnn_rust::Extractor<'_>) {
    let output = ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap();
    ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap();
    println!("{:?}", output.as_f32_slice());
}
ⓘ
fn escape(net: &ncnn_rust::Net) -> ncnn_rust::OutputGuard<'_> {
    let mut ex = net.extractor().unwrap();
    ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap()
}
ⓘ
fn escape_slice(ex: &mut ncnn_rust::Extractor<'_>) {
    let output = ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap();
    let values = output.as_f32_slice().unwrap();
    drop(output);
    println!("{}", values[0]);
}
ⓘ
fn escape_handle(output: ncnn_rust::OutputGuard<'_>) -> ncnn_rust::Mat {
    output.into_raw()
}
ⓘ
fn destroy_extractor(net: &ncnn_rust::Net) {
    let mut ex = net.extractor().unwrap();
    let _output = ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap();
    drop(ex); // The unused guard must still release its handle first.
}

Implementations§

Source§

impl OutputGuard<'_>

Source

pub fn info(&self) -> TensorInfo

Returns the borrowed tensor metadata.

The metadata describes the layout the native extractor produced: extents, elemsize, elempack and the channel stride. elemsize decides which reader applies.

Source

pub fn as_f32_slice(&self) -> Result<&[f32]>

Borrows contiguous unpacked storage with an FP32 interpretation.

The borrow needs aligned, unpacked, single-batch storage whose channels are adjacent, so a 16-bit or packed output returns Error::Invalid with the stored width in the message. A multi-channel output with padded planes needs channel_f32 or to_f32.

Source

pub fn channel_f32(&self, channel: usize) -> Result<&[f32]>

Borrows one channel, skipping inter-channel padding.

ncnn pads each channel plane, so this returns the logical elements of channel without the padding a whole-tensor slice would include. An index outside the channel count returns Error::Invalid.

Source

pub fn into_mat(self) -> Result<Mat>

Consumes the guard and keeps the extracted tensor without copying it.

The guard hands out the buffer the native extractor detached when this extractor can keep it, and copies in the two cases where it cannot: a network built with NetBuilder::reuse_blob_memory returns storage inside its pools, and an extractor that submitted a borrowed input returns caller storage. Either way the tensor is independent of the guard, extractor and network once this returns, and the network stops being reserved.

Source

pub fn to_f32(&self) -> Result<Vec<f32>>

Copies logical FP32 elements into a new vector.

Native padding is excluded, so the length is the product of the reported extents. Quantized and half-precision outputs convert here; use to_f32_typed to name a 16-bit format.

Source

pub fn to_f32_typed(&self, kind: ElementKind) -> Result<Vec<f32>>

Copies any scalar format into FP32 values, including quantized outputs.

The tensor records an element width without a format tag, so a 16-bit output needs the caller to say whether it holds IEEE half or bfloat16. A width that disagrees with kind returns Error::Invalid.

Source

pub fn to_i8(&self) -> Result<Vec<i8>>

Copies signed 8-bit quantized values.

Trait Implementations§

Source§

impl Drop for OutputGuard<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'extractor> !RefUnwindSafe for OutputGuard<'extractor>

§

impl<'extractor> !Send for OutputGuard<'extractor>

§

impl<'extractor> !Sync for OutputGuard<'extractor>

§

impl<'extractor> !UnwindSafe for OutputGuard<'extractor>

§

impl<'extractor> Freeze for OutputGuard<'extractor>

§

impl<'extractor> Unpin for OutputGuard<'extractor>

§

impl<'extractor> UnsafeUnpin for OutputGuard<'extractor>

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.