pub struct TypedTensorView<'a, T, R = DynRank>where
R: TensorRank,{ /* private fields */ }Expand description
Read-only borrowed view of typed tensor storage with arbitrary strides.
TypedTensorView is the typed representation for layout-only tensor
transformations. It borrows an existing host or backend allocation and
carries a logical shape, strides, and an offset. Slicing, reshaping when
stride-compatible, and transpose_view
update only metadata and do not copy storage.
Use TypedTensorView::to_contiguous when a compact owned
TypedTensor is required. Use TypedTensorView::as_slice only when the
current view is contiguous in the requested layout.
§Examples
use tenferro_tensor::{Rank, TypedTensorView};
let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::<_, Rank<2>>::from_slice_ranked([2, 2], [1, 2], 0, &data)?;
assert_eq!(view.get(&[1, 1]), Some(&4));Implementations§
Source§impl<'a, T> TypedTensorView<'a, T>where
T: 'static,
impl<'a, T> TypedTensorView<'a, T>where
T: 'static,
Sourcepub fn from_col_major(
shape: &[usize],
data: &'a [T],
) -> Result<TypedTensorView<'a, T>, Error>
pub fn from_col_major( shape: &[usize], data: &'a [T], ) -> Result<TypedTensorView<'a, T>, Error>
Create a borrowed dynamic-rank view over compact column-major host data.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::from_col_major(&[2, 2], &data)?;
assert_eq!(view.strides(), &[1, 2]);Sourcepub fn from_slice(
shape: impl AsRef<[usize]>,
strides: impl AsRef<[isize]>,
offset: isize,
data: &'a [T],
) -> Result<TypedTensorView<'a, T>, Error>
pub fn from_slice( shape: impl AsRef<[usize]>, strides: impl AsRef<[isize]>, offset: isize, data: &'a [T], ) -> Result<TypedTensorView<'a, T>, Error>
Create a borrowed host view from explicit layout metadata.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [1_i32, 2, 3];
let view = TypedTensorView::from_slice(vec![3], vec![-1], 2, &data)?;
assert_eq!(view.get(&[2]), Some(&1));Source§impl<'a, T, R> TypedTensorView<'a, T, R>where
T: 'static,
R: TensorRank,
impl<'a, T, R> TypedTensorView<'a, T, R>where
T: 'static,
R: TensorRank,
Sourcepub fn from_slice_ranked(
shape: impl Into<<R as TensorRank>::Shape>,
strides: impl Into<<R as TensorRank>::Strides>,
offset: isize,
data: &'a [T],
) -> Result<TypedTensorView<'a, T, R>, Error>
pub fn from_slice_ranked( shape: impl Into<<R as TensorRank>::Shape>, strides: impl Into<<R as TensorRank>::Strides>, offset: isize, data: &'a [T], ) -> Result<TypedTensorView<'a, T, R>, Error>
Create a rank-generic borrowed host view from explicit layout metadata.
§Examples
use tenferro_tensor::{Rank, TypedTensorView};
let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::<_, Rank<2>>::from_slice_ranked([2, 2], [1, 2], 0, &data)?;
assert_eq!(view.get(&[1, 1]), Some(&4));Sourcepub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
Return the logical shape.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [0_i32; 2];
let view = TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?;
assert_eq!(view.shape(), &[2]);Sourcepub fn strides(&self) -> &[isize]
pub fn strides(&self) -> &[isize]
Return strides in element units.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [0_i32; 2];
let view = TypedTensorView::from_slice(vec![2], vec![-1], 1, &data)?;
assert_eq!(view.strides(), &[-1]);Sourcepub fn offset(&self) -> isize
pub fn offset(&self) -> isize
Return the physical element offset.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [1_i32, 2];
let view = TypedTensorView::from_slice(vec![1], vec![1], 1, &data)?;
assert_eq!(view.offset(), 1);Sourcepub fn host_storage(&self) -> Result<&'a [T], Error>
pub fn host_storage(&self) -> Result<&'a [T], Error>
Return the borrowed host storage backing this view.
This exposes the entire backing host allocation, not just the logical
slice covered by this view. Use TypedTensorView::as_slice when the
caller needs the contiguous logical region instead.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [1_i32, 2];
let view = TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?;
assert_eq!(view.host_storage()?, &[1, 2]);Sourcepub fn n_elements(&self) -> usize
pub fn n_elements(&self) -> usize
Return the number of logical elements in this view.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [0_i32; 6];
let view = TypedTensorView::from_slice(vec![2, 3], vec![1, 2], 0, &data)?;
assert_eq!(view.n_elements(), 6);Sourcepub fn layout(&self) -> &TensorLayout<R>
pub fn layout(&self) -> &TensorLayout<R>
Return layout metadata for this view.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [1_i32, 2];
let view = TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?;
assert!(view.layout().is_compact_col_major());Sourcepub fn placement(&self) -> &Placement
pub fn placement(&self) -> &Placement
Return placement metadata for this view.
§Examples
use tenferro_tensor::{MemoryKind, TypedTensorView};
let data = [1_i32];
let view = TypedTensorView::from_slice(vec![1], vec![1], 0, &data)?;
assert_eq!(view.placement().memory_kind, MemoryKind::UnpinnedHost);Sourcepub fn linear_offset(&self, indices: &[usize]) -> Option<usize>
pub fn linear_offset(&self, indices: &[usize]) -> Option<usize>
Compute the physical element offset for a logical index.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [1_i32, 2, 3];
let view = TypedTensorView::from_slice(vec![3], vec![-1], 2, &data)?;
assert_eq!(view.linear_offset(&[2]), Some(0));Sourcepub fn get(&self, indices: &[usize]) -> Option<&T>
pub fn get(&self, indices: &[usize]) -> Option<&T>
Borrow one host element by logical index.
Returns None for out-of-bounds indices and backend buffers.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [1_i32, 2];
let view = TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?;
assert_eq!(view.get(&[1]), Some(&2));Sourcepub fn as_slice(&self) -> Result<&'a [T], Error>
pub fn as_slice(&self) -> Result<&'a [T], Error>
Borrow the contiguous host slice covered by this view.
Returns an explicit error for backend buffers and for non-contiguous layouts. This method never downloads or materializes backend data.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [1_i32, 2, 3];
let view = TypedTensorView::from_slice(vec![2], vec![1], 1, &data)?;
assert_eq!(view.as_slice()?, &[2, 3]);Sourcepub fn to_contiguous(&self) -> Result<TypedTensor<T, R>, Error>where
T: Clone,
pub fn to_contiguous(&self) -> Result<TypedTensor<T, R>, Error>where
T: Clone,
Materialize this view as compact column-major host tensor storage.
This is an explicit same-placement copy boundary. Host placement metadata is preserved on the materialized tensor. Backend buffers return an error here instead of being downloaded implicitly; backend-specific compacting paths must stay on that backend.
§Examples
use tenferro_tensor::{Rank, TypedTensor};
let tensor = TypedTensor::<i32, Rank<2>>::from_vec_col_major([2, 2], vec![1, 2, 3, 4]).unwrap();
let transposed = tensor.as_view().transpose_view([1, 0])?;
let compact = transposed.to_contiguous()?;
assert_eq!(compact.as_slice()?, &[1, 3, 2, 4]);Sourcepub fn transpose_view(
&self,
axes: impl AsRef<[usize]>,
) -> Result<TypedTensorView<'a, T, R>, Error>
pub fn transpose_view( &self, axes: impl AsRef<[usize]>, ) -> Result<TypedTensorView<'a, T, R>, Error>
Return a metadata-only axis permutation.
§Examples
use tenferro_tensor::{Rank, TypedTensorView};
let data = [1_i32, 2, 3, 4, 5, 6];
let view = TypedTensorView::<_, Rank<2>>::from_slice_ranked([2, 3], [1, 2], 0, &data)?;
let transposed = view.transpose_view([1, 0])?;
assert_eq!(transposed.shape(), &[3, 2]);Sourcepub fn try_slice(
&self,
slices: &[StridedSliceSpec],
) -> Result<TypedTensorView<'a, T, R>, Error>
pub fn try_slice( &self, slices: &[StridedSliceSpec], ) -> Result<TypedTensorView<'a, T, R>, Error>
Return a metadata-only slice using one StridedSliceSpec per axis.
§Examples
use tenferro_tensor::{StridedSliceSpec, TypedTensorView};
let data = [1_i32, 2, 3];
let view = TypedTensorView::from_slice(vec![3], vec![1], 0, &data)?;
let reversed = view.try_slice(&[StridedSliceSpec::reverse()])?;
assert_eq!(reversed.get(&[0]), Some(&3));Sourcepub fn try_slice_axis(
&self,
axis: usize,
slice: StridedSliceSpec,
) -> Result<TypedTensorView<'a, T, R>, Error>
pub fn try_slice_axis( &self, axis: usize, slice: StridedSliceSpec, ) -> Result<TypedTensorView<'a, T, R>, Error>
Return a metadata-only slice along one axis.
§Examples
use tenferro_tensor::{StridedSliceSpec, TypedTensorView};
let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::from_slice(vec![2, 2], vec![1, 2], 0, &data)?;
assert_eq!(view.try_slice_axis(1, StridedSliceSpec::reverse())?.get(&[0, 0]), Some(&3));Sourcepub fn try_reshape(
&self,
shape: &[usize],
) -> Result<TypedTensorView<'a, T>, Error>
pub fn try_reshape( &self, shape: &[usize], ) -> Result<TypedTensorView<'a, T>, Error>
Return a metadata-only dynamic-rank reshape for contiguous column-major views.
§Examples
use tenferro_tensor::TypedTensorView;
let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::from_slice(vec![2, 2], vec![1, 2], 0, &data)?;
assert_eq!(view.try_reshape(&[4])?.shape(), &[4]);Trait Implementations§
Source§impl<'a, T, R> Clone for TypedTensorView<'a, T, R>
impl<'a, T, R> Clone for TypedTensorView<'a, T, R>
Source§fn clone(&self) -> TypedTensorView<'a, T, R>
fn clone(&self) -> TypedTensorView<'a, T, R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more