pub struct TensorLayout<R: TensorRank = DynRank> { /* 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§
Source§impl<R: TensorRank> TensorLayout<R>
impl<R: TensorRank> TensorLayout<R>
Sourcepub fn compact(shape: R::Shape) -> Result<Self>
pub fn compact(shape: R::Shape) -> Result<Self>
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]);Sourcepub fn from_parts(
shape: R::Shape,
strides: R::Strides,
offset: isize,
buffer_len: usize,
) -> Result<Self>
pub fn from_parts( shape: R::Shape, strides: R::Strides, offset: isize, buffer_len: usize, ) -> Result<Self>
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());Sourcepub 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]);Sourcepub 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]);Sourcepub 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);Sourcepub fn is_compact_col_major(&self) -> bool
pub fn is_compact_col_major(&self) -> bool
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());Sourcepub fn validate_mutable_no_overlap(&self) -> Result<()>
pub fn validate_mutable_no_overlap(&self) -> Result<()>
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()?;Sourcepub fn transpose_view(&self, axes: impl AsRef<[usize]>) -> Result<Self>
pub fn transpose_view(&self, axes: impl AsRef<[usize]>) -> Result<Self>
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]);Sourcepub fn slice_view(
&self,
spec: impl AsRef<[SliceSpec]>,
buffer_len: usize,
) -> Result<Self>
pub fn slice_view( &self, spec: impl AsRef<[SliceSpec]>, buffer_len: usize, ) -> Result<Self>
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]);Sourcepub fn reshape_view_as<R2: TensorRank>(
&self,
shape: R2::Shape,
buffer_len: usize,
) -> Result<TensorLayout<R2>>
pub fn reshape_view_as<R2: TensorRank>( &self, shape: R2::Shape, buffer_len: usize, ) -> Result<TensorLayout<R2>>
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]);Sourcepub fn broadcast_in_dim_view<R2: TensorRank>(
&self,
shape: R2::Shape,
broadcast_dims: impl AsRef<[usize]>,
buffer_len: usize,
) -> Result<TensorLayout<R2>>
pub fn broadcast_in_dim_view<R2: TensorRank>( &self, shape: R2::Shape, broadcast_dims: impl AsRef<[usize]>, buffer_len: usize, ) -> Result<TensorLayout<R2>>
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]);Trait Implementations§
Source§impl<R: Clone + TensorRank> Clone for TensorLayout<R>
impl<R: Clone + TensorRank> Clone for TensorLayout<R>
Source§fn clone(&self) -> TensorLayout<R>
fn clone(&self) -> TensorLayout<R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more