Struct TensorLayout
pub struct TensorLayout<R = DynRank>where
R: TensorRank,{ /* private fields */ }Expand description
Storage-neutral tensor layout metadata.
§Examples
use tenferro_tensor_core::{Rank, TensorLayout};
let layout = TensorLayout::<Rank<2>>::compact([2, 3])?;
assert_eq!(layout.shape(), &[2, 3]);
assert_eq!(layout.strides(), &[1, 2]);Implementations§
§impl<R> TensorLayout<R>where
R: TensorRank,
impl<R> TensorLayout<R>where
R: TensorRank,
pub fn compact(
shape: <R as TensorRank>::Shape,
) -> Result<TensorLayout<R>, ValidationError>
pub fn compact( shape: <R as TensorRank>::Shape, ) -> Result<TensorLayout<R>, ValidationError>
Create a compact column-major layout with zero offset.
§Examples
use tenferro_tensor_core::{Rank, TensorLayout};
let layout = TensorLayout::<Rank<2>>::compact([2, 3])?;
assert_eq!(layout.strides(), &[1, 2]);§Errors
Returns ValidationError::IntegerOverflow when compact strides
cannot be computed for shape.
pub fn from_parts(
shape: <R as TensorRank>::Shape,
strides: <R as TensorRank>::Strides,
offset: isize,
buffer_len: usize,
) -> Result<TensorLayout<R>, ValidationError>
pub fn from_parts( shape: <R as TensorRank>::Shape, strides: <R as TensorRank>::Strides, offset: isize, buffer_len: usize, ) -> Result<TensorLayout<R>, ValidationError>
Create a layout from shape, strides, element offset, and backing buffer length.
§Examples
use tenferro_tensor_core::{DynRank, TensorLayout};
let layout = TensorLayout::<DynRank>::from_parts(
vec![2, 3].into(),
vec![1, 2].into(),
0,
6,
)?;
assert!(layout.is_compact_col_major()?);§Errors
Returns ValidationError::RankMismatch for incompatible shape and
stride ranks, ValidationError::ViewOutOfBounds when the reachable
range exceeds buffer_len, or ValidationError::IntegerOverflow
when metadata arithmetic overflows.
pub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
Return the layout shape.
§Examples
use tenferro_tensor_core::{Rank, TensorLayout};
let layout = TensorLayout::<Rank<1>>::compact([4])?;
assert_eq!(layout.shape(), &[4]);pub fn strides(&self) -> &[isize]
pub fn strides(&self) -> &[isize]
Return the layout strides in element units.
§Examples
use tenferro_tensor_core::{Rank, TensorLayout};
let layout = TensorLayout::<Rank<2>>::compact([2, 3])?;
assert_eq!(layout.strides(), &[1, 2]);pub fn offset(&self) -> isize
pub fn offset(&self) -> isize
Return the layout element offset.
§Examples
use tenferro_tensor_core::{DynRank, TensorLayout};
let layout = TensorLayout::<DynRank>::from_parts(vec![3].into(), vec![1].into(), 2, 5)?;
assert_eq!(layout.offset(), 2);pub fn is_compact_col_major(&self) -> Result<bool, ValidationError>
pub fn is_compact_col_major(&self) -> Result<bool, ValidationError>
Return whether the layout has compact column-major strides.
§Examples
use tenferro_tensor_core::{Rank, TensorLayout};
let layout = TensorLayout::<Rank<2>>::compact([2, 3])?;
assert!(layout.is_compact_col_major()?);§Errors
Returns ValidationError::IntegerOverflow when compactness
validation overflows metadata arithmetic.
pub fn validate_mutable_no_overlap(&self) -> Result<(), ValidationError>
pub fn validate_mutable_no_overlap(&self) -> Result<(), ValidationError>
Validate that the layout can be used for mutable access without aliasing.
Empty logical views are accepted. Non-empty layouts are accepted when a conservative stride-span proof succeeds, or when exact enumeration of a small bounded view proves that all logical elements map to distinct physical offsets.
§Examples
use tenferro_tensor_core::{DynRank, TensorLayout};
let layout = TensorLayout::<DynRank>::from_parts(vec![3].into(), vec![-1].into(), 2, 3)?;
layout.validate_mutable_no_overlap()?;§Errors
Returns ValidationError::OverlappingMutableLayout when multiple
logical elements can alias, or ValidationError::IntegerOverflow
when overlap validation arithmetic overflows.
pub fn transpose_view(
&self,
axes: impl AsRef<[usize]>,
) -> Result<TensorLayout<R>, ValidationError>
pub fn transpose_view( &self, axes: impl AsRef<[usize]>, ) -> Result<TensorLayout<R>, ValidationError>
Return a metadata-only axis permutation of this layout.
§Examples
use tenferro_tensor_core::{Rank, TensorLayout};
let layout = TensorLayout::<Rank<2>>::compact([2, 3])?;
let transposed = layout.transpose_view([1, 0])?;
assert_eq!(transposed.shape(), &[3, 2]);
assert_eq!(transposed.strides(), &[2, 1]);§Errors
Returns ValidationError::InvalidPermutationLength,
ValidationError::AxisOutOfBounds, or
ValidationError::DuplicateAxis when axes is not a permutation of
the layout rank.
pub fn slice_view(
&self,
spec: impl AsRef<[SliceSpec]>,
buffer_len: usize,
) -> Result<TensorLayout<R>, ValidationError>
pub fn slice_view( &self, spec: impl AsRef<[SliceSpec]>, buffer_len: usize, ) -> Result<TensorLayout<R>, ValidationError>
Return a metadata-only slice of this layout.
§Examples
use tenferro_tensor_core::{Rank, SliceSpec, TensorLayout};
let layout = TensorLayout::<Rank<1>>::compact([4])?;
let view = layout.slice_view([SliceSpec { start: 3, end: -1, step: -2 }], 4)?;
assert_eq!(view.shape(), &[2]);
assert_eq!(view.strides(), &[-2]);§Errors
Returns ValidationError::RankMismatch when spec does not cover
every axis, ValidationError::InvalidSliceStep or
ValidationError::InvalidSliceBounds for invalid slice parameters,
or ValidationError::ViewOutOfBounds when the result is outside the
backing buffer.
pub fn reshape_view_as<R2>(
&self,
shape: impl Into<<R2 as TensorRank>::Shape>,
buffer_len: usize,
) -> Result<TensorLayout<R2>, ValidationError>where
R2: TensorRank,
pub fn reshape_view_as<R2>(
&self,
shape: impl Into<<R2 as TensorRank>::Shape>,
buffer_len: usize,
) -> Result<TensorLayout<R2>, ValidationError>where
R2: TensorRank,
Return a metadata-only reshape of this compact column-major layout.
§Examples
use tenferro_tensor_core::{Rank, TensorLayout};
let layout = TensorLayout::<Rank<2>>::compact([2, 3])?;
let reshaped = layout.reshape_view_as::<Rank<1>>([6], 6)?;
assert_eq!(reshaped.shape(), &[6]);
assert_eq!(reshaped.strides(), &[1]);§Errors
Returns ValidationError::NonContiguousViewAsSlice for a noncompact
source, ValidationError::ShapeMismatch for a different element
count, or ValidationError::IntegerOverflow when shape arithmetic
overflows.
pub fn broadcast_in_dim_view<R2>(
&self,
shape: impl Into<<R2 as TensorRank>::Shape>,
broadcast_dims: impl AsRef<[usize]>,
buffer_len: usize,
) -> Result<TensorLayout<R2>, ValidationError>where
R2: TensorRank,
pub fn broadcast_in_dim_view<R2>(
&self,
shape: impl Into<<R2 as TensorRank>::Shape>,
broadcast_dims: impl AsRef<[usize]>,
buffer_len: usize,
) -> Result<TensorLayout<R2>, ValidationError>where
R2: TensorRank,
Return a metadata-only explicit broadcast of this layout into a target rank.
§Examples
use tenferro_tensor_core::{Rank, TensorLayout};
let layout = TensorLayout::<Rank<1>>::compact([3])?;
let broadcast = layout.broadcast_in_dim_view::<Rank<2>>([2, 3], [1], 3)?;
assert_eq!(broadcast.shape(), &[2, 3]);
assert_eq!(broadcast.strides(), &[0, 1]);§Errors
Returns ValidationError::RankMismatch,
ValidationError::AxisOutOfBounds, or
ValidationError::DuplicateAxis for invalid broadcast axes;
ValidationError::ShapeDataLengthMismatch for incompatible extents;
or ValidationError::ViewOutOfBounds for an invalid result layout.
Trait Implementations§
§impl<R> Clone for TensorLayout<R>
impl<R> Clone for TensorLayout<R>
§fn clone(&self) -> TensorLayout<R>
fn clone(&self) -> TensorLayout<R>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl<R> Debug for TensorLayout<R>
impl<R> Debug for TensorLayout<R>
§impl<R> PartialEq for TensorLayout<R>where
R: PartialEq + TensorRank,
<R as TensorRank>::Shape: PartialEq,
<R as TensorRank>::Strides: PartialEq,
impl<R> PartialEq for TensorLayout<R>where
R: PartialEq + TensorRank,
<R as TensorRank>::Shape: PartialEq,
<R as TensorRank>::Strides: PartialEq,
impl<R> Eq for TensorLayout<R>
impl<R> StructuralPartialEq for TensorLayout<R>where
R: TensorRank,
Auto Trait Implementations§
impl<R> Freeze for TensorLayout<R>
impl<R> RefUnwindSafe for TensorLayout<R>
impl<R> Send for TensorLayout<R>
impl<R> Sync for TensorLayout<R>
impl<R> Unpin for TensorLayout<R>
impl<R> UnsafeUnpin for TensorLayout<R>
impl<R> UnwindSafe for TensorLayout<R>
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