Skip to main content

Tensor

Struct Tensor 

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

A multi-dimensional array for neural network operations.

Tensors are the primary data structure for MNN inference, holding input and output data for models.

Implementations§

Source§

impl Tensor

Source

pub unsafe fn from_ptr(ptr: *mut MNNTensor, name: Option<String>) -> Self

Create a tensor wrapper around an existing MNN tensor pointer (public version).

§Safety

The pointer must be valid and remain valid for the lifetime of this tensor.

Source

pub fn inner_mut(&mut self) -> *mut MNNTensor

Get the mutable raw pointer to the underlying MNN tensor.

Source

pub fn as_ptr(&self) -> *const MNNTensor

Get the raw pointer to the underlying MNN tensor.

Source

pub fn shape(&self) -> Vec<i32>

Get the shape of the tensor.

Source

pub fn ndim(&self) -> usize

Get the number of dimensions.

Source

pub fn dim(&self, axis: usize) -> MnnResult<i32>

Get the size of a specific dimension.

§Arguments
  • axis - The dimension index (0-based)
§Returns

The size of the dimension, or an error if the axis is out of bounds.

Source

pub fn dtype(&self) -> DataType

Get the data type of the tensor.

Source

pub fn format(&self) -> DataFormat

Get the data format of the tensor.

Source

pub fn element_count(&self) -> i32

Get the total number of elements in the tensor.

Source

pub fn byte_size(&self) -> usize

Get the size of the tensor data in bytes.

Source

pub fn name(&self) -> Option<&str>

Get the name of the tensor.

Source

pub fn write<T: TensorData>(&self, data: &[T]) -> MnnResult<()>

Write data to the tensor.

§Arguments
  • data - The data to write
§Errors

Returns an error if the data size doesn’t match.

Source

pub fn read<T: TensorData>(&self) -> MnnResult<Vec<T>>

Read data from the tensor.

§Returns

A vector containing the tensor data.

Source

pub unsafe fn as_slice_mut<T: TensorData>(&mut self) -> MnnResult<&mut [T]>

Get a mutable reference to the tensor’s host data.

§Safety

The returned slice is valid only as long as no other operations are performed on the tensor.

Source

pub unsafe fn as_slice<T: TensorData>(&self) -> MnnResult<&[T]>

Get a reference to the tensor’s host data.

§Safety

The returned slice is valid only as long as no other operations are performed on the tensor.

Source

pub fn info(&self) -> TensorInfo

Get the tensor info.

Source

pub fn copy_from_host(&mut self, host_tensor: &Tensor) -> MnnResult<()>

Copy data from a host tensor to this tensor (potentially on device).

§Arguments
  • host_tensor - The host tensor to copy from
§Returns

Ok(()) on success, or an error on failure.

Source

pub fn copy_to_host(&self, host_tensor: &mut Tensor) -> MnnResult<()>

Copy data from this tensor (potentially on device) to a host tensor.

§Arguments
  • host_tensor - The host tensor to copy to
§Returns

Ok(()) on success, or an error on failure.

Source

pub fn create_device( shape: &[i32], format: DataFormat, dtype: DataType, ) -> MnnResult<Tensor>

Create a device tensor with the given shape and format.

§Arguments
  • shape - The tensor shape
  • format - The data format (NHWC, NCHW, etc.)
  • dtype - The data type
§Returns

A new device tensor on success, or an error on failure.

Source

pub fn clone(&self, deep_copy: bool) -> MnnResult<Tensor>

Clone this tensor.

§Arguments
  • deep_copy - If true, copy data; if false, only copy metadata
§Returns

A cloned tensor on success, or an error on failure.

Source

pub fn device_id(&self) -> u64

Get the device ID for this tensor (for GPU tensors).

§Returns

The device ID, or 0 if not a GPU tensor or unknown.

Source

pub fn backend(&self) -> BackendType

Get the backend type for this tensor.

§Returns

The backend type (CPU, CUDA, OpenCL, etc.).

Trait Implementations§

Source§

impl Debug for Tensor

Source§

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

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

impl Send for Tensor

Source§

impl Sync for Tensor

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 = 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.