pub struct Tensor<S, M, A = <S as TensorType>::H>{ /* 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>
impl<H, T, M> Tensor<T, M>
Sourcepub unsafe fn from_ptr(tensor: *mut Tensor) -> Self
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
Sourcepub unsafe fn halide_buffer(&self) -> *const halide_buffer_t
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
Sourcepub unsafe fn halide_buffer_mut(&self) -> *mut halide_buffer_t
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
Sourcepub fn get_type(&self) -> halide_type_t
pub fn get_type(&self) -> halide_type_t
Get the data type of the tensor
Sourcepub unsafe fn as_any_tensor(&self) -> &AnyTensorRef
pub unsafe fn as_any_tensor(&self) -> &AnyTensorRef
§Safety
Type erase the tensor
Sourcepub unsafe fn as_any_tensor_mut(&mut self) -> &mut AnyTensorRef
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,
impl<H> Tensor<Owned<H>, Host>where
H: HalideType + Copy,
Sourcepub fn from_shape_data(
shape: impl AsTensorShape,
dm_type: DimensionType,
data: &[H],
) -> Self
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>where
H: HalideType,
M: TensorMachine,
impl<H, M> Tensor<Owned<H>, M>where
H: HalideType,
M: TensorMachine,
Sourcepub fn new(shape: impl AsTensorShape, dm_type: DimensionType) -> Self
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,
impl<'a, A> Tensor<View<&'a A>, Host, A>where
A: HalideType,
Sourcepub fn borrowed(
shape: impl AsTensorShape,
dm_type: DimensionType,
input: &'a [A],
) -> Self
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,
impl<'a, A> Tensor<View<&'a mut A>, Host, A>where
A: HalideType,
Sourcepub fn borrowed_mut(
shape: impl AsTensorShape,
dm_type: DimensionType,
input: &'a mut [A],
) -> Self
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>>§
Sourcepub fn copy_from_host_tensor(
&mut self,
tensor: &TensorRef<H, Host>,
) -> Result<()>
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.
Sourcepub fn copy_to_host_tensor(&self, tensor: &mut TensorRef<H, Host>) -> Result<()>
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.
Methods from Deref<Target = AnyTensorRef>§
Sourcepub fn shape(&self) -> TensorShape
pub fn shape(&self) -> TensorShape
Get the shape of the tensor
Sourcepub fn dimensions(&self) -> usize
pub fn dimensions(&self) -> usize
Get the dimensions of the tensor
Sourcepub fn size(&self) -> usize
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.
Sourcepub fn element_size(&self) -> usize
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.
Sourcepub fn get_type(&self) -> halide_type_t
pub fn get_type(&self) -> halide_type_t
Get the halide data type of the tensor
Sourcepub fn is_type_of<Ha: HalideType>(&self) -> bool
pub fn is_type_of<Ha: HalideType>(&self) -> bool
Check if the tensor is of the specified data type
Sourcepub fn is_same_type(&self, other: &AnyTensorRef) -> bool
pub fn is_same_type(&self, other: &AnyTensorRef) -> bool
Check if both tensors have the same halide data type
Sourcepub fn get_dimension_type(&self) -> DimensionType
pub fn get_dimension_type(&self) -> DimensionType
Get the dimension type of the tensor
Sourcepub fn is_dynamic_unsized(&self) -> bool
pub fn is_dynamic_unsized(&self) -> bool
Check if the tensor is dynamic and needs resizing
Sourcepub fn copy_from_host_tensor(&mut self, tensor: &AnyTensorRef) -> Result<()>
pub fn copy_from_host_tensor(&mut self, tensor: &AnyTensorRef) -> Result<()>
Copies the data from a host tensor to the self.as_ptr()
Sourcepub fn copy_to_host_tensor(&self, tensor: &mut AnyTensorRef) -> Result<()>
pub fn copy_to_host_tensor(&self, tensor: &mut AnyTensorRef) -> Result<()>
Copies the data from the self.as_ptr() to a host tensor
Sourcepub fn create_host_tensor_from_device<T: HalideType + Copy>(
&self,
copy_data: bool,
) -> Tensor<Owned<T>, Host>
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<S, M, H> DerefMut for Tensor<S, M, H>
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.
impl<S, M, H> DerefMut for Tensor<S, M, H>
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.