Skip to main content

TensorView

Struct TensorView 

Source
pub struct TensorView<'a> { /* private fields */ }
Expand description

A non-owning view of a tensor: a pointer to a DLTensor.

A function argument of this type accepts both a borrowed DLTensor* (type index kTVMFFIDLTensorPtr, how C callers pass tensors) and a Tensor object (type index kTVMFFITensor), and is passed on as a DLTensor*. Prefer it over Tensor for arguments of exported functions that only read a tensor’s metadata and data.

The view does not keep anything alive: its lifetime 'a ties it to what it borrows. For the same reason, a view cannot be moved into an owned Any, which could outlive it; use Tensor instead. Neither misuse compiles:

ⓘ
use tvm_ffi::{Tensor, TensorView};
let view = {
    let tensor = Tensor::from_slice(&[1.0_f32], &[1]).unwrap();
    TensorView::from(&tensor)
};
view.ndim();
ⓘ
use tvm_ffi::{Any, Tensor, TensorView};
let tensor = Tensor::from_slice(&[1.0_f32], &[1]).unwrap();
let _ = Any::from(TensorView::from(&tensor));

A view received as an argument of an exported function is valid only during the call. The function may name any lifetime for it, TensorView<'static> included, so the compiler does not stop it from being kept past the call; doing so is undefined behavior.

Implementations§

Source§

impl<'a> TensorView<'a>

Source

pub unsafe fn from_raw(tensor: *const DLTensor) -> Self

Creates a view of a DLTensor.

§Safety

tensor must be non-null and valid for every use of the view.

Source

pub fn as_raw(&self) -> *const DLTensor

The viewed DLTensor.

Source

pub fn data_ptr(&self) -> *mut c_void

The data pointer, as in the DLTensor; byte_offset is not applied.

Source

pub fn device(&self) -> DLDevice

The device that holds the data.

Source

pub fn dtype(&self) -> DLDataType

The element type.

Source

pub fn ndim(&self) -> usize

The number of dimensions.

Source

pub fn shape(&self) -> &'a [i64]

The extent of each dimension.

Source

pub fn numel(&self) -> usize

The number of elements.

§Panics

If an extent is negative or the product overflows usize.

Source

pub fn strides(&self) -> &'a [i64]

The strides, in elements.

§Panics

If the DLTensor has null strides and at least one dimension, which DLPack forbids since v1.2.

Source

pub fn is_contiguous(&self) -> bool

Whether the elements are laid out in row-major order without gaps: a DLTensor with null strides or no elements is contiguous, and a dimension of extent 1 may have any stride.

Trait Implementations§

Source§

impl<'a> Clone for TensorView<'a>

Source§

fn clone(&self) -> TensorView<'a>

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<'a> Copy for TensorView<'a>

Source§

impl<'a> Debug for TensorView<'a>

Source§

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

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

impl<'a> From<&'a Tensor> for TensorView<'a>

Source§

fn from(tensor: &'a Tensor) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&TensorView<'a>> for AnyView<'a>

Source§

fn from(view: &TensorView<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> PackedCallArg for TensorView<'a>

Source§

type Holder = TensorView<'a>

What keeps the argument alive during the call.
Source§

fn into_packed_arg(self) -> Self

Source§

fn as_packed_arg(holder: &Self::Holder) -> AnyView<'_>

Source§

impl<'a, 'b> PackedCallArg for &'b TensorView<'a>

Source§

type Holder = &'b TensorView<'a>

What keeps the argument alive during the call.
Source§

fn into_packed_arg(self) -> Self

Source§

fn as_packed_arg(holder: &Self::Holder) -> AnyView<'_>

Source§

impl<'a> TryFrom<AnyView<'a>> for TensorView<'a>

Source§

type Error = Error

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

fn try_from(value: AnyView<'a>) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

§

impl<'a> !Send for TensorView<'a>

§

impl<'a> !Sync for TensorView<'a>

§

impl<'a> Freeze for TensorView<'a>

§

impl<'a> RefUnwindSafe for TensorView<'a>

§

impl<'a> Unpin for TensorView<'a>

§

impl<'a> UnsafeUnpin for TensorView<'a>

§

impl<'a> UnwindSafe for TensorView<'a>

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<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> TypeSubsumes<T> for T
where T: ?Sized,