Enum TensorView
pub enum TensorView<'a> {
F32(TypedTensorView<'a, f32>),
F64(TypedTensorView<'a, f64>),
I32(TypedTensorView<'a, i32>),
I64(TypedTensorView<'a, i64>),
Bool(TypedTensorView<'a, bool>),
C32(TypedTensorView<'a, Complex<f32>>),
C64(TypedTensorView<'a, Complex<f64>>),
}Expand description
Dynamic read-only borrowed tensor view.
TensorView keeps dtype erased while borrowing typed view metadata and
storage. Use TypedTensorView directly when the scalar type is statically
known.
§Examples
use tenferro_tensor::{DType, TensorView, TypedTensorView};
let data = [1_i32, 2, 3, 4];
let typed = TypedTensorView::from_slice([2, 2], [1, 2], 0, &data)?;
let view = TensorView::I32(typed);
assert_eq!(view.dtype(), DType::I32);
assert_eq!(view.shape(), &[2, 2]);Variants§
F32(TypedTensorView<'a, f32>)
F64(TypedTensorView<'a, f64>)
I32(TypedTensorView<'a, i32>)
I64(TypedTensorView<'a, i64>)
Bool(TypedTensorView<'a, bool>)
C32(TypedTensorView<'a, Complex<f32>>)
C64(TypedTensorView<'a, Complex<f64>>)
Implementations§
§impl<'a> TensorView<'a>
impl<'a> TensorView<'a>
pub fn f32(shape: &'a [usize], data: &'a [f32]) -> Result<TensorView<'a>, Error>
pub fn f32(shape: &'a [usize], data: &'a [f32]) -> Result<TensorView<'a>, Error>
Create a dynamic f32 view over compact column-major host data.
§Examples
use tenferro_tensor::{DType, TensorView};
let data = [1.0_f32, 2.0];
let view = TensorView::f32(&[2], &data)?;
assert_eq!(view.dtype(), DType::F32);§Errors
Returns [crate::Error::Validation] with
[tenferro_tensor_core::ValidationError::IntegerOverflow] for compact
shape or offset arithmetic overflow, or
[tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the
compact shape reaches beyond data.
pub fn f64(shape: &'a [usize], data: &'a [f64]) -> Result<TensorView<'a>, Error>
pub fn f64(shape: &'a [usize], data: &'a [f64]) -> Result<TensorView<'a>, Error>
Create a dynamic f64 view over compact column-major host data.
§Examples
use tenferro_tensor::{DType, TensorView};
let data = [1.0_f64, 2.0];
let view = TensorView::f64(&[2], &data)?;
assert_eq!(view.dtype(), DType::F64);§Errors
Returns [crate::Error::Validation] with
[tenferro_tensor_core::ValidationError::IntegerOverflow] for compact
shape or offset arithmetic overflow, or
[tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the
compact shape reaches beyond data.
pub fn i64(shape: &'a [usize], data: &'a [i64]) -> Result<TensorView<'a>, Error>
pub fn i64(shape: &'a [usize], data: &'a [i64]) -> Result<TensorView<'a>, Error>
Create a dynamic i64 view over compact column-major host data.
§Examples
use tenferro_tensor::{DType, TensorView};
let data = [1_i64, 2];
let view = TensorView::i64(&[2], &data)?;
assert_eq!(view.dtype(), DType::I64);§Errors
Returns [crate::Error::Validation] with
[tenferro_tensor_core::ValidationError::IntegerOverflow] for compact
shape or offset arithmetic overflow, or
[tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the
compact shape reaches beyond data.
pub fn i32(shape: &'a [usize], data: &'a [i32]) -> Result<TensorView<'a>, Error>
pub fn i32(shape: &'a [usize], data: &'a [i32]) -> Result<TensorView<'a>, Error>
Create a dynamic i32 view over compact column-major host data.
§Examples
use tenferro_tensor::{DType, TensorView};
let data = [1_i32, 2];
let view = TensorView::i32(&[2], &data)?;
assert_eq!(view.dtype(), DType::I32);§Errors
Returns [crate::Error::Validation] with
[tenferro_tensor_core::ValidationError::IntegerOverflow] for compact
shape or offset arithmetic overflow, or
[tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the
compact shape reaches beyond data.
pub fn bool(
shape: &'a [usize],
data: &'a [bool],
) -> Result<TensorView<'a>, Error>
pub fn bool( shape: &'a [usize], data: &'a [bool], ) -> Result<TensorView<'a>, Error>
Create a dynamic bool view over compact column-major host data.
§Examples
use tenferro_tensor::{DType, TensorView};
let data = [true, false];
let view = TensorView::bool(&[2], &data)?;
assert_eq!(view.dtype(), DType::Bool);§Errors
Returns [crate::Error::Validation] with
[tenferro_tensor_core::ValidationError::IntegerOverflow] for compact
shape or offset arithmetic overflow, or
[tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the
compact shape reaches beyond data.
pub fn c32(
shape: &'a [usize],
data: &'a [Complex<f32>],
) -> Result<TensorView<'a>, Error>
pub fn c32( shape: &'a [usize], data: &'a [Complex<f32>], ) -> Result<TensorView<'a>, Error>
Create a dynamic Complex32 view over compact column-major host data.
§Examples
use num_complex::Complex32;
use tenferro_tensor::{DType, TensorView};
let data = [Complex32::new(1.0, 2.0)];
let view = TensorView::c32(&[1], &data)?;
assert_eq!(view.dtype(), DType::C32);§Errors
Returns [crate::Error::Validation] with
[tenferro_tensor_core::ValidationError::IntegerOverflow] for compact
shape or offset arithmetic overflow, or
[tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the
compact shape reaches beyond data.
pub fn c64(
shape: &'a [usize],
data: &'a [Complex<f64>],
) -> Result<TensorView<'a>, Error>
pub fn c64( shape: &'a [usize], data: &'a [Complex<f64>], ) -> Result<TensorView<'a>, Error>
Create a dynamic Complex64 view over compact column-major host data.
§Examples
use num_complex::Complex64;
use tenferro_tensor::{DType, TensorView};
let data = [Complex64::new(1.0, 2.0)];
let view = TensorView::c64(&[1], &data)?;
assert_eq!(view.dtype(), DType::C64);§Errors
Returns [crate::Error::Validation] with
[tenferro_tensor_core::ValidationError::IntegerOverflow] for compact
shape or offset arithmetic overflow, or
[tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the
compact shape reaches beyond data.
pub fn dtype(&self) -> DType
pub fn shape(&self) -> &[usize]
pub fn as_slice<T>(&self) -> Result<&'a [T], Error>where
T: TensorScalar,
pub fn as_slice<T>(&self) -> Result<&'a [T], Error>where
T: TensorScalar,
Borrow a contiguous host slice when the requested scalar matches this view’s dtype.
Backend buffers and non-contiguous views return an explicit error. No download or materialization is performed.
§Errors
Returns [ValidationError::DTypeMismatch] when T does not match the
view dtype, [ValidationError::NonContiguousViewAsSlice] for a
non-contiguous layout, or [crate::Error::HostAccess] for unavailable
backend host access.
pub fn as_real_view(&self) -> Result<TensorView<'a>, Error>
pub fn as_real_view(&self) -> Result<TensorView<'a>, Error>
Reinterpret a complex view as its sealed real representation.
§Errors
Returns [crate::Error::Unsupported] for the wrong dtype pair and
[ValidationError::InvalidArgument] or
[ValidationError::ViewOutOfBounds] for invalid layout metadata.
pub fn as_complex_view(&self) -> Result<TensorView<'a>, Error>
pub fn as_complex_view(&self) -> Result<TensorView<'a>, Error>
Reinterpret a real view as its sealed complex representation.
§Errors
Returns [crate::Error::Unsupported] for the wrong dtype pair and
[ValidationError::InvalidArgument] or
[ValidationError::ViewOutOfBounds] for invalid layout metadata.
pub fn placement(&self) -> &Placement
pub fn placement(&self) -> &Placement
Return the placement metadata carried by this borrowed view.
§Examples
use tenferro_tensor::{MemoryKind, TensorView};
let view = TensorView::f64(&[1], &[1.0])?;
assert_eq!(view.placement().memory_kind, MemoryKind::UnpinnedHost);pub fn backend_family(&self) -> Option<&'static str>
pub fn backend_family(&self) -> Option<&'static str>
Return the physical backend family, when this view is backend-owned.
§Examples
use tenferro_tensor::TensorView;
let view = TensorView::f64(&[1], &[1.0])?;
assert_eq!(view.backend_family(), None);pub fn allocation_domain(&self) -> Option<AllocationDomainId>
pub fn allocation_domain(&self) -> Option<AllocationDomainId>
Return the shared allocation domain, when this view has one.
§Examples
use tenferro_tensor::TensorView;
let view = TensorView::f64(&[1], &[1.0])?;
assert_eq!(view.allocation_domain(), None);pub fn layout_linear_offset(&self, indices: &[usize]) -> Result<usize, Error>
pub fn layout_linear_offset(&self, indices: &[usize]) -> Result<usize, Error>
Compute the physical element offset for a logical index.
§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>
pub fn is_col_major_contiguous(&self) -> Result<bool, Error>
Return whether this view is compact column-major.
§Errors
Returns [crate::Error::Validation] with
[tenferro_tensor_core::ValidationError::IntegerOverflow] when
compactness arithmetic overflows.
pub fn layout_summary(&self) -> String
pub fn layout_summary(&self) -> String
Return a compact string summary of this view’s layout metadata.
pub fn assert_col_major_contiguous(&self) -> Result<(), Error>
pub fn assert_col_major_contiguous(&self) -> Result<(), Error>
Assert this view is compact column-major.
§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 duplicate(&self) -> Result<Tensor, Error>
pub fn duplicate(&self) -> Result<Tensor, Error>
Explicitly duplicate a compact host view into a fresh tensor.
Backend views and non-contiguous layouts return a typed error; this operation never downloads or silently canonicalizes a view.
§Examples
use tenferro_tensor::{TensorView, TypedTensorView};
let data = [1_i32, 2];
let view = TensorView::I32(TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?);
let copy = view.duplicate()?;
assert_eq!(copy.shape(), &[2]);§Errors
Returns [crate::Error::HostAccess] for backend-owned views,
[ValidationError::NonContiguousViewAsSlice] for non-contiguous views,
or [ValidationError::InvalidArgument] for invalid layout metadata.
Trait Implementations§
§impl<'a> Clone for TensorView<'a>
impl<'a> Clone for TensorView<'a>
§fn clone(&self) -> TensorView<'a>
fn clone(&self) -> TensorView<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<'a> !RefUnwindSafe for TensorView<'a>
impl<'a> !UnwindSafe for TensorView<'a>
impl<'a> Freeze for TensorView<'a>
impl<'a> Send for TensorView<'a>
impl<'a> Sync for TensorView<'a>
impl<'a> Unpin for TensorView<'a>
impl<'a> UnsafeUnpin for TensorView<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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