Skip to main content

TensorRead

Enum TensorRead 

pub enum TensorRead<'a> {
    Tensor(&'a Tensor),
    View(TensorView<'a>),
}
Expand description

Read-only tensor input accepted by synchronous eager kernels.

TensorRead lets kernels accept either an owned tensor reference or a borrowed TensorView without forcing callers to materialize first. The View variant preserves arbitrary strides and offsets, so kernels that support strided reads can consume transposes, slices, and broadcasts directly.

TensorRead is intentionally borrowed. It is an input-dispatch type, not an owned lazy tensor value. APIs that need to store a lazy layout result should keep an owned base tensor plus layout metadata, then expose a TensorRead only for the duration of kernel dispatch.

§Examples

use tenferro_tensor::{DType, Tensor, TensorRead};

let tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
let read = TensorRead::from_tensor(&tensor);

assert_eq!(read.dtype(), DType::F64);
assert_eq!(read.shape(), &[2]);

Variants§

§

Tensor(&'a Tensor)

§

View(TensorView<'a>)

Implementations§

§

impl<'a> TensorRead<'a>

pub fn from_tensor(tensor: &'a Tensor) -> TensorRead<'a>

pub fn from_view(view: TensorView<'a>) -> TensorRead<'a>

pub fn tensor_view(self) -> TensorView<'a>

Convert this read target into a dtype-erased tensor view.

Owned tensors are borrowed without copying their storage. Existing views preserve their layout and placement metadata.

§Examples
let view = TensorRead::from_tensor(&tensor).tensor_view();
assert_eq!(view.shape(), &[2]);
assert_eq!(view.as_slice::<f64>()?, &[1.0, 2.0]);

pub fn as_slice<T>(&self) -> Result<&'a [T], Error>
where T: TensorScalar,

Borrow this read target as a typed compact host slice without allocation.

This delegates to TensorView::as_slice. Cloning a TensorRead is a shallow clone of its borrowed reference or view metadata, so the returned slice retains the original 'a storage lifetime and never outlives the storage borrowed by this read target. This method does not materialize, transfer, or otherwise canonicalize the input.

§Errors

Returns [ValidationError::DTypeMismatch] when T does not match the input dtype, [ValidationError::InvalidArgument] for a noncompact view, or a typed runtime-state host-access error for backend-owned storage. Backend-owned inputs are never downloaded implicitly.

§Examples
use tenferro_tensor::{Tensor, TensorRead};

let tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let read = TensorRead::from_tensor(&tensor);
assert_eq!(read.as_slice::<f64>()?, &[1.0, 2.0]);

pub fn dtype(&self) -> DType

pub fn shape(&self) -> &[usize]

pub fn placement(&self) -> &Placement

Return the placement metadata carried by this read target.

§Examples
use tenferro_tensor::{MemoryKind, Tensor, TensorRead};

let tensor = Tensor::from_vec_col_major(vec![1], vec![1.0_f64])?;
let read = TensorRead::from_tensor(&tensor);
assert_eq!(read.placement().memory_kind, MemoryKind::UnpinnedHost);

pub fn backend_family(&self) -> Option<&'static str>

Return the physical backend family of this read target, when backend-owned.

§Examples
use tenferro_tensor::{Tensor, TensorRead};

let tensor = Tensor::from_vec_col_major(vec![1], vec![1.0_f64])?;
assert_eq!(TensorRead::from_tensor(&tensor).backend_family(), None);

pub fn allocation_domain(&self) -> Option<AllocationDomainId>

Return the shared allocation domain of this read target, when present.

§Examples
use tenferro_tensor::{Tensor, TensorRead};

let tensor = Tensor::from_vec_col_major(vec![1], vec![1.0_f64])?;
assert_eq!(TensorRead::from_tensor(&tensor).allocation_domain(), None);

pub fn strides(&self) -> Result<Vec<isize>, Error>

§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::IntegerOverflow] when column-major stride arithmetic overflows.

pub fn offset(&self) -> isize

pub fn layout_linear_offset(&self, indices: &[usize]) -> Result<usize, Error>

§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::RankMismatch] when indices has the wrong rank, [tenferro_tensor_core::ValidationError::InvalidArgument] when an index is outside its axis extent, or [tenferro_tensor_core::ValidationError::IntegerOverflow] when offset arithmetic overflows.

pub fn is_col_major_contiguous(&self) -> Result<bool, Error>

§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::IntegerOverflow] when compactness arithmetic overflows.

pub fn layout_summary(&self) -> String

pub fn assert_col_major_contiguous(&self) -> Result<(), Error>

§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::IntegerOverflow] when compactness arithmetic overflows, or [tenferro_tensor_core::ValidationError::InvalidArgument] when the view is not compact column-major.

pub fn as_tensor(&self) -> Option<&'a Tensor>

Trait Implementations§

§

impl<'a> Clone for TensorRead<'a>

§

fn clone(&self) -> TensorRead<'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
§

impl<'a> Debug for TensorRead<'a>

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for TensorRead<'a>

§

impl<'a> !UnwindSafe for TensorRead<'a>

§

impl<'a> Freeze for TensorRead<'a>

§

impl<'a> Send for TensorRead<'a>

§

impl<'a> Sync for TensorRead<'a>

§

impl<'a> Unpin for TensorRead<'a>

§

impl<'a> UnsafeUnpin for TensorRead<'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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.