pub struct Tensor { /* private fields */ }Expand description
A reference-counted n-dimensional array, passed across languages as
ffi.Tensor and exchanged with other frameworks through DLPack.
§Examples
use tvm_ffi::Tensor;
let tensor = Tensor::from_slice(&[1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3])?;
assert_eq!(tensor.shape(), &[2, 3]);
assert_eq!(tensor.numel(), 6);
assert_eq!(tensor.data_as_slice::<f32>()?[4], 5.0);Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn data_ptr(&self) -> *mut c_void
pub fn data_ptr(&self) -> *mut c_void
The data pointer, as in the DLTensor; byte_offset is not applied.
A tensor is a shared handle, so &mut self would not make writes
exclusive; getting the pointer is safe, and writing through it is the
caller’s unsafe responsibility, as for
data_as_slice_mut.
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Whether the elements are laid out in row-major order without gaps; see
TensorView::is_contiguous.
Sourcepub fn data_as_slice<T: AsDLDataType>(&self) -> Result<&[T]>
pub fn data_as_slice<T: AsDLDataType>(&self) -> Result<&[T]>
The elements as a slice, starting byte_offset bytes into the data.
§Errors
A TypeError if T is not the tensor’s dtype, and a RuntimeError
if the tensor is not on the CPU, not contiguous, or its data is not
aligned for T.
Sourcepub unsafe fn data_as_slice_mut<T: AsDLDataType>(&self) -> Result<&mut [T]>
pub unsafe fn data_as_slice_mut<T: AsDLDataType>(&self) -> Result<&mut [T]>
The elements as a mutable slice, starting byte_offset bytes into the
data.
It takes &self: a tensor is a shared handle, and kernels write the
data of tensors they receive by shared reference, so &mut self would
not make the write exclusive.
§Errors
As data_as_slice.
§Safety
While the returned slice is alive, nothing else may access the data:
no other slice from this method or data_as_slice
on this tensor or a clone of it, and no kernel or foreign caller writing
the buffer. Rust cannot check this because the method takes &self, as
std::slice::from_raw_parts_mut cannot check it for a raw pointer.
Sourcepub fn strides(&self) -> &[i64]
pub fn strides(&self) -> &[i64]
The strides, in elements.
§Panics
If the tensor has null strides and at least one dimension, as
TensorView::strides does.
Sourcepub fn dtype(&self) -> DLDataType
pub fn dtype(&self) -> DLDataType
The element type.
Source§impl Tensor
impl Tensor
Sourcepub fn from_nd_alloc<TNDAlloc>(
alloc: TNDAlloc,
shape: &[i64],
dtype: DLDataType,
device: DLDevice,
) -> Selfwhere
TNDAlloc: NDAllocator,
pub fn from_nd_alloc<TNDAlloc>(
alloc: TNDAlloc,
shape: &[i64],
dtype: DLDataType,
device: DLDevice,
) -> Selfwhere
TNDAlloc: NDAllocator,
Create a contiguous tensor whose data alloc allocates and frees.
§Arguments
alloc- The allocator, kept by the tensor until it is dropped.shape- The shape of the tensor.dtype- The element type.device- The device of the data.
§Returns
The new tensor, with row-major strides.
§Panics
If an extent of shape is negative or the byte size overflows isize.
Sourcepub fn from_env_alloc(
shape: &[i64],
dtype: DLDataType,
device: DLDevice,
) -> Result<Self>
pub fn from_env_alloc( shape: &[i64], dtype: DLDataType, device: DLDevice, ) -> Result<Self>
Create a contiguous tensor with this thread’s environment allocator.
Kernel libraries allocate intermediate tensors this way, so that they
come from the caller’s allocator, such as the one a framework sets
with TVMFFIEnvSetDLPackManagedTensorAllocator.
§Arguments
shape- The shape of the tensor.dtype- The element type.device- The device of the data.
§Returns
The new tensor.
§Errors
The error the allocator raises, or a RuntimeError when no allocator
is set.
Sourcepub fn from_slice<T: AsDLDataType>(slice: &[T], shape: &[i64]) -> Result<Self>
pub fn from_slice<T: AsDLDataType>(slice: &[T], shape: &[i64]) -> Result<Self>
Create a CPU tensor holding a copy of slice.
§Arguments
slice- The elements, in row-major order.shape- The shape of the tensor.
§Returns
The new tensor, allocated with CPUNDAlloc.
§Errors
A ValueError if slice does not have as many elements as shape,
or shape has a negative extent.
Trait Implementations§
Source§impl DowncastRefFromAny for Tensor
impl DowncastRefFromAny for Tensor
Source§unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self>
unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self>
Source§impl<'a> From<&'a Tensor> for TensorView<'a>
impl<'a> From<&'a Tensor> for TensorView<'a>
Source§impl ObjectRefAnyCompatible for Tensor
impl ObjectRefAnyCompatible for Tensor
Source§fn extra_check(obj: &Self::ContainerType) -> bool
fn extra_check(obj: &Self::ContainerType) -> bool
obj, an instance of ContainerType, is a value of Self.
Every strict and converting cast checks it, after the object type.Source§fn convert_fallback(value: AnyView<'_>) -> Option<Self>
fn convert_fallback(value: AnyView<'_>) -> Option<Self>
Self object, such as an integer that
stands for a constant; None rejects it. Only converting casts (TryFrom<Any>,
typed function arguments) call it; downcast_ref, downcast and
match_any! never do.Source§fn custom_type_str() -> String
fn custom_type_str() -> String
AnyCompatible::type_str.Source§fn custom_mismatch_type_info(value: AnyView<'_>) -> String
fn custom_mismatch_type_info(value: AnyView<'_>) -> String
value in conversion errors; by default its
runtime type key. See AnyCompatible::get_mismatch_type_info.Source§impl ObjectRefCore for Tensor
impl ObjectRefCore for Tensor
Source§type ContainerType = TensorObj
type ContainerType = TensorObj
Source§fn data(this: &Self) -> &ObjectArc<Self::ContainerType>
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>
Source§fn into_data(this: Self) -> ObjectArc<Self::ContainerType>
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>
Source§unsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self
unsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self
Source§fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool
fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool
Source§fn type_key(&self) -> &'static str
fn type_key(&self) -> &'static str
"ffi.Array", as C++ ObjectRef::GetTypeKey.Source§fn is<T: AnyCompatible>(&self) -> bool
fn is<T: AnyCompatible>(&self) -> bool
T as is, such as a subtype or a container
whose elements are checked too, by the strict check of
downcast, without a reference count change.
See also crate::Any::is.Source§impl<'a> PackedCallArg for &'a Tensor
impl<'a> PackedCallArg for &'a Tensor
Source§impl PackedCallArg for Tensor
impl PackedCallArg for Tensor
impl TypeSubsumes<Tensor> for ObjectRef
Auto Trait Implementations§
impl Freeze for Tensor
impl RefUnwindSafe for Tensor
impl !Send for Tensor
impl !Sync for Tensor
impl Unpin for Tensor
impl UnsafeUnpin for Tensor
impl UnwindSafe for Tensor
Blanket Implementations§
Source§impl<T> AnyCompatible for Twhere
T: ObjectRefAnyCompatible,
impl<T> AnyCompatible for Twhere
T: ObjectRefAnyCompatible,
const MATCH_ANY_EXACT: bool = const MATCH_ANY_EXACT: bool = <T as ObjectRefAnyCompatible>::MATCH_ANY_EXACT && <T::ContainerType as ObjectCore>::TYPE_FINAL;
fn match_any_exact_type_index() -> i32
Source§unsafe fn is_undefined(field: *const T) -> bool
unsafe fn is_undefined(field: *const T) -> bool
field, which holds a value of this type or all-zero bytes,
holds all-zero bytes that are no value of this type, as a null object
reference, a string of type index None or a zero device type: a field
that a creator left undefined for reflection to set. An undefined field
is no value; a nullable reference is Option<T>, whose None is a
defined value. Read moreSource§unsafe fn copy_to_any_view(src: &T, data: &mut TVMFFIAny)
unsafe fn copy_to_any_view(src: &T, data: &mut TVMFFIAny)
Source§unsafe fn move_to_any(src: T, data: &mut TVMFFIAny)
unsafe fn move_to_any(src: T, data: &mut TVMFFIAny)
Source§unsafe fn check_any_strict(data: &TVMFFIAny) -> bool
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool
Source§unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> T
unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> T
Self out of a cell that holds one, retaining any object. Read moreSource§unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> T
unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> T
Self out of an owning cell that holds one. Read moreSource§unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<T, ()>
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<T, ()>
data into a Self by the FFI rules, such as an integer into a
float; Err if it does not convert. Read moreSource§fn get_mismatch_type_info(data: &TVMFFIAny) -> String
fn get_mismatch_type_info(data: &TVMFFIAny) -> String
data in conversion errors; by
default its runtime type key.Source§fn type_str() -> String
fn type_str() -> String
int,
ffi.String or Array<int>.