Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor<S, M, A = <S as TensorType>::H>
where A: HalideType, M: TensorMachine, S: TensorType<H = A>,
{ /* private fields */ }
Expand description

A generic tensor that can of host / device / owned / borrowed

Every Tensor owns the C++ MNN::Tensor object it wraps and destroys it on drop. The S parameter only describes who owns the data: Owned tensors allocate it, the View tensors created by Tensor::borrowed / Tensor::borrowed_mut borrow a user slice (which MNN never frees). To borrow a tensor itself use &TensorRef / &mut TensorRef, the Deref target of Tensor.

Implementations§

Source§

impl<H, T, M> Tensor<T, M>
where T: TensorType<H = H>, H: HalideType, M: TensorMachine,

Source

pub unsafe fn from_ptr(tensor: *mut Tensor) -> Self

This function constructs a Tensor type from a raw pointer

The returned tensor takes ownership of the MNN::Tensor object and destroys it on drop. Use from_raw_parts / from_raw_parts_mut to borrow a tensor instead.

§Safety

Since this constructs a Tensor from a raw pointer we have no way to guarantee that it’s a valid tensor, that nothing else owns it, or its lifetime

Source

pub fn print(&self)

Print the tensor

Source

pub unsafe fn halide_buffer(&self) -> *const halide_buffer_t

DO not use this function directly

§Safety

This is just provided as a 1:1 compat mostly for possible later use

Source

pub unsafe fn halide_buffer_mut(&self) -> *mut halide_buffer_t

Do not use this function directly

§Safety

This is just provided as a 1:1 compat mostly for possible later use

Source

pub fn get_type(&self) -> halide_type_t

Get the data type of the tensor

Source

pub unsafe fn as_any_tensor(&self) -> &AnyTensorRef

§Safety

Type erase the tensor

Source

pub unsafe fn as_any_tensor_mut(&mut self) -> &mut AnyTensorRef

§Safety

Type erase the tensor mutably

Source§

impl<H> Tensor<Owned<H>, Host>
where H: HalideType + Copy,

Source

pub fn from_shape_data( shape: impl AsTensorShape, dm_type: DimensionType, data: &[H], ) -> Self

Create a new tensor with the specified shape and dimension type and fill it with the data from the provided vector

§Panics

Panics if the shape is dynamic or the number of elements MNN allocates for the shape (which includes channel padding for DimensionType::CaffeC4) differs from data.len().

Source§

impl<H, M> Tensor<Owned<H>, M>

Source

pub fn new(shape: impl AsTensorShape, dm_type: DimensionType) -> Self

Create a new tensor with the specified shape and dimension type this allocates the tensor internally and the data is “owned” by the tensor itself.

§Panics

Host tensors are allocated immediately, so their shape must not be dynamic (contain -1). Device tensors are only described, not allocated, so any shape is accepted.

Source§

impl<'a, A> Tensor<View<&'a A>, Host, A>
where A: HalideType,

Source

pub fn borrowed( shape: impl AsTensorShape, dm_type: DimensionType, input: &'a [A], ) -> Self

Create a tensor that borrows input as its storage.

§Panics

Panics if the shape is dynamic or if the number of elements MNN needs for shape in the dm_type layout differs from input.len(). Note that DimensionType::CaffeC4 pads the channel dimension to a multiple of 4, so the slice must already be laid out (and sized) accordingly.

Source§

impl<'a, A> Tensor<View<&'a mut A>, Host, A>
where A: HalideType,

Source

pub fn borrowed_mut( shape: impl AsTensorShape, dm_type: DimensionType, input: &'a mut [A], ) -> Self

Create a tensor that mutably borrows input as its storage.

§Panics

Panics if the shape is dynamic or if the number of elements MNN needs for shape in the dm_type layout differs from input.len(). Note that DimensionType::CaffeC4 pads the channel dimension to a multiple of 4, so the slice must already be laid out (and sized) accordingly.

Methods from Deref<Target = TensorRef<H, M>>§

Source

pub fn copy_from_host_tensor( &mut self, tensor: &TensorRef<H, Host>, ) -> Result<()>

Copies the data from a host tensor to the self.as_ptr()

The tensors must have the same logical shape; MNN converts between layouts (e.g. NCHW <-> NC4HW4) as part of the copy.

Source

pub fn copy_to_host_tensor(&self, tensor: &mut TensorRef<H, Host>) -> Result<()>

Copies the data from the self.as_ptr() to a host tensor

The tensors must have the same logical shape; MNN converts between layouts (e.g. NCHW <-> NC4HW4) as part of the copy.

Source

pub fn fill(&mut self, value: T) -> Result<()>
where T: Copy,

Fill the tensor with the specified value

Fails if the tensor has a different element type, has not been resized yet (dynamic shape) or, for device tensors, if MNN refuses the copy (e.g. no backend is attached).

Methods from Deref<Target = AnyTensorRef>§

Source

pub fn device_id(&self) -> u64

Get the device id of the tensor

Source

pub fn shape(&self) -> TensorShape

Get the shape of the tensor

Source

pub fn dimensions(&self) -> usize

Get the dimensions of the tensor

Source

pub fn width(&self) -> u32

Get the width of the tensor

Source

pub fn height(&self) -> u32

Get the height of the tensor

Source

pub fn channel(&self) -> u32

Get the channel size of the tensor

Source

pub fn batch(&self) -> u32

Get the batch size of the tensor

Source

pub fn size(&self) -> usize

Get the size of the tensor when counted by bytes

Note that for packed layouts (NC4HW4) this includes the padding MNN adds to the channel dimension, so it can be larger than the product of the shape.

Source

pub fn element_size(&self) -> usize

Get the size of the tensor when counted by elements

Note that for packed layouts (NC4HW4) this includes the padding MNN adds to the channel dimension, so it can be larger than the product of the shape.

Source

pub fn get_type(&self) -> halide_type_t

Get the halide data type of the tensor

Source

pub fn is_type_of<Ha: HalideType>(&self) -> bool

Check if the tensor is of the specified data type

Source

pub fn is_same_type(&self, other: &AnyTensorRef) -> bool

Check if both tensors have the same halide data type

Source

pub fn get_dimension_type(&self) -> DimensionType

Get the dimension type of the tensor

Source

pub fn is_dynamic_unsized(&self) -> bool

Check if the tensor is dynamic and needs resizing

Source

pub fn copy_from_host_tensor(&mut self, tensor: &AnyTensorRef) -> Result<()>

Copies the data from a host tensor to the self.as_ptr()

Source

pub fn copy_to_host_tensor(&self, tensor: &mut AnyTensorRef) -> Result<()>

Copies the data from the self.as_ptr() to a host tensor

Source

pub fn create_host_tensor_from_device<T: HalideType + Copy>( &self, copy_data: bool, ) -> Tensor<Owned<T>, Host>

Create a host tensor from the device tensor with same dimensions and data type and optionally copy the data from the device tensor

§Panics

Panics if the tensor’s element type is not T or the copy fails.

Trait Implementations§

Source§

impl<H> Clone for Tensor<Owned<H>, Host>
where H: HalideType + Copy,

Source§

fn clone(&self) -> Tensor<Owned<H>, Host>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S, M, A> Debug for Tensor<S, M, A>
where A: HalideType + Debug, M: TensorMachine + Debug, S: TensorType<H = A> + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S, M, H> Deref for Tensor<S, M, H>
where S: TensorType<H = H>, H: HalideType, M: TensorMachine,

Source§

type Target = TensorRef<H, M>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<S, M, H> DerefMut for Tensor<S, M, H>
where S: MutableTensorType<H = H>, H: HalideType, M: TensorMachine,

Only tensors that own their data or hold a mutable borrow of it can be mutated through the TensorRef; a Tensor<View<&H>, _> built over a shared slice must not be able to write through that slice.

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<S, M, A> Drop for Tensor<S, M, A>
where A: HalideType, S: TensorType<H = A>, M: TensorMachine,

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
Source§

impl<T, H> PartialEq for Tensor<T, Host>
where T: TensorType<H = H>, H: HalideType + PartialEq,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, H> Eq for Tensor<T, Host>
where T: TensorType<H = H>, H: HalideType + Eq,

Auto Trait Implementations§

§

impl<S, M, A> Freeze for Tensor<S, M, A>

§

impl<S, M, A> RefUnwindSafe for Tensor<S, M, A>

§

impl<S, M, A = <S as TensorType>::H> !Send for Tensor<S, M, A>

§

impl<S, M, A = <S as TensorType>::H> !Sync for Tensor<S, M, A>

§

impl<S, M, A> Unpin for Tensor<S, M, A>
where S: Unpin, M: Unpin, A: Unpin,

§

impl<S, M, A> UnsafeUnpin for Tensor<S, M, A>

§

impl<S, M, A> UnwindSafe for Tensor<S, M, A>
where S: UnwindSafe, M: UnwindSafe, A: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> MaybeDebug for T
where T: Debug,