pub enum TensorView<'a, D = Dynamic>where
D: Representation,{
F32(TypedTensorView<'a, f32, DynRank, D>),
F64(TypedTensorView<'a, f64, DynRank, D>),
I32(TypedTensorView<'a, i32, DynRank, D>),
I64(TypedTensorView<'a, i64, DynRank, D>),
Bool(TypedTensorView<'a, bool, DynRank, D>),
C32(TypedTensorView<'a, Complex<f32>, DynRank, D>),
C64(TypedTensorView<'a, Complex<f64>, DynRank, D>),
}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]);D marks the representation the view was proven to borrow, as on
TypedTensorView. The default Dynamic view may borrow host or backend
storage; a TensorView<'a, Host> comes only from
TensorView::try_host or TensorRead::try_host, so every arm borrows
host storage and a consumer reads it without a further placement check.
Variants§
F32(TypedTensorView<'a, f32, DynRank, D>)
F64(TypedTensorView<'a, f64, DynRank, D>)
I32(TypedTensorView<'a, i32, DynRank, D>)
I64(TypedTensorView<'a, i64, DynRank, D>)
Bool(TypedTensorView<'a, bool, DynRank, D>)
C32(TypedTensorView<'a, Complex<f32>, DynRank, D>)
C64(TypedTensorView<'a, Complex<f64>, DynRank, D>)
Implementations§
Source§impl<'a> TensorView<'a>
impl<'a> TensorView<'a>
Sourcepub fn try_host(self, op: &'static str) -> Result<TensorView<'a, Host>, Error>
pub fn try_host(self, op: &'static str) -> Result<TensorView<'a, Host>, Error>
Checked narrowing of a dtype-erased view to the host representation.
See TypedTensorView::try_host: the narrowing succeeds exactly when
the view borrows host elements, and a consumer of the
TensorView<'a, Host> it returns needs no further placement check.
§Examples
use tenferro_tensor::{DType, TensorView};
let host = TensorView::f64(&[2], &[1.0, 2.0])?.try_host("example")?;
assert_eq!(host.dtype(), DType::F64);
assert_eq!(host.shape(), &[2]);§Errors
Returns crate::Error::RuntimeState attributed to op when the view
borrows backend (device) storage; download the tensor to host first.
Source§impl<'a> TensorView<'a, Host>
impl<'a> TensorView<'a, Host>
Sourcepub fn into_dynamic(self) -> TensorView<'a>
pub fn into_dynamic(self) -> TensorView<'a>
Widen a host-marked view to the runtime union without copying.
§Examples
use tenferro_tensor::{DType, TensorView};
let host = TensorView::i32(&[2], &[3, 4])?.try_host("example")?;
let dynamic = host.into_dynamic();
assert_eq!(dynamic.as_slice::<i32>()?, &[3, 4]);Sourcepub fn dtype(&self) -> DType
pub fn dtype(&self) -> DType
The element dtype of this host view.
§Examples
use tenferro_tensor::{DType, TensorView};
let host = TensorView::i64(&[1], &[7])?.try_host("example")?;
assert_eq!(host.dtype(), DType::I64);Sourcepub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
The logical shape of this host view.
§Examples
use tenferro_tensor::TensorView;
let host = TensorView::f32(&[2], &[1.0, 2.0])?.try_host("example")?;
assert_eq!(host.shape(), &[2]);Sourcepub fn is_owned_layout(&self) -> bool
pub fn is_owned_layout(&self) -> bool
Whether the view covers its whole host buffer in compact column-major order from offset zero, so it reads exactly like an owned tensor.
§Examples
use tenferro_tensor::{Tensor, TensorRead};
let tensor = Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64; 4])?;
let host = TensorRead::from_tensor(&tensor).try_host("example")?;
assert!(host.is_owned_layout());Source§impl<'a> TensorView<'a>
impl<'a> TensorView<'a>
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub fn dtype(&self) -> DType
pub fn dtype(&self) -> DType
Return the element dtype of this borrowed view.
§Examples
use tenferro_tensor::{DType, TensorView};
let view = TensorView::f64(&[2], &[1.0, 2.0])?;
assert_eq!(view.dtype(), DType::F64);Sourcepub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
Return the logical shape of this borrowed view.
§Examples
use tenferro_tensor::TensorView;
let view = TensorView::i32(&[2, 1], &[1, 2])?;
assert_eq!(view.shape(), &[2, 1]);Sourcepub 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.
Sourcepub 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.
§Examples
use tenferro_tensor::{Complex64, DType, TensorView};
let data = [Complex64::new(1.0, 2.0)];
let real = TensorView::c64(&[1], &data)?.as_real_view()?;
assert_eq!(real.dtype(), DType::F64);
assert_eq!(real.as_slice::<f64>()?, &[1.0, 2.0]);§Errors
Returns crate::Error::Unsupported for the wrong dtype pair and
ValidationError::InvalidArgument or
ValidationError::ViewOutOfBounds for invalid layout metadata.
Sourcepub 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.
§Examples
use tenferro_tensor::{Complex64, DType, TensorView};
let data = [1.0_f64, 2.0];
let complex = TensorView::f64(&[2, 1], &data)?.as_complex_view()?;
assert_eq!(complex.dtype(), DType::C64);
assert_eq!(complex.as_slice::<Complex64>()?, &[Complex64::new(1.0, 2.0)]);§Errors
Returns crate::Error::Unsupported for the wrong dtype pair and
ValidationError::InvalidArgument or
ValidationError::ViewOutOfBounds for invalid layout metadata.
Sourcepub 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);Sourcepub 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);Sourcepub fn is_host_memory(&self) -> bool
pub fn is_host_memory(&self) -> bool
Return whether this view borrows host-addressed memory.
See TypedTensorView::is_host_memory; the borrowed payload answers,
not the placement label.
§Examples
use tenferro_tensor::TensorView;
let view = TensorView::f64(&[1], &[1.0])?;
assert!(view.is_host_memory());Sourcepub 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);Sourcepub fn strides(&self) -> &[isize]
pub fn strides(&self) -> &[isize]
Return strides in element units.
§Examples
use tenferro_tensor::TensorView;
let data = [0.0_f64; 6];
let view = TensorView::f64(&[2, 3], &data)?;
assert_eq!(view.strides(), &[1, 2]);Sourcepub 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.
Sourcepub 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.
Sourcepub fn layout_summary(&self) -> String
pub fn layout_summary(&self) -> String
Return a compact string summary of this view’s layout metadata.
Sourcepub 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.
Sourcepub 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§
Source§impl<D> Clone for TensorView<'_, D>where
D: Representation,
impl<D> Clone for TensorView<'_, D>where
D: Representation,
Source§fn clone(&self) -> TensorView<'_, D>
fn clone(&self) -> TensorView<'_, D>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more