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>, Error>
pub fn compact( shape: <R as TensorRank>::Shape, ) -> Result<TensorLayout<R>, Error>
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]);pub fn from_parts(
shape: <R as TensorRank>::Shape,
strides: <R as TensorRank>::Strides,
offset: isize,
buffer_len: usize,
) -> Result<TensorLayout<R>, Error>
pub fn from_parts( shape: <R as TensorRank>::Shape, strides: <R as TensorRank>::Strides, offset: isize, buffer_len: usize, ) -> Result<TensorLayout<R>, Error>
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());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) -> 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());pub fn validate_mutable_no_overlap(&self) -> Result<(), Error>
pub fn validate_mutable_no_overlap(&self) -> Result<(), Error>
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()?;pub fn transpose_view(
&self,
axes: impl AsRef<[usize]>,
) -> Result<TensorLayout<R>, Error>
pub fn transpose_view( &self, axes: impl AsRef<[usize]>, ) -> Result<TensorLayout<R>, Error>
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]);pub fn slice_view(
&self,
spec: impl AsRef<[SliceSpec]>,
buffer_len: usize,
) -> Result<TensorLayout<R>, Error>
pub fn slice_view( &self, spec: impl AsRef<[SliceSpec]>, buffer_len: usize, ) -> Result<TensorLayout<R>, Error>
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]);pub fn reshape_view_as<R2>(
&self,
shape: <R2 as TensorRank>::Shape,
buffer_len: usize,
) -> Result<TensorLayout<R2>, Error>where
R2: TensorRank,
pub fn reshape_view_as<R2>(
&self,
shape: <R2 as TensorRank>::Shape,
buffer_len: usize,
) -> Result<TensorLayout<R2>, Error>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]);pub fn broadcast_in_dim_view<R2>(
&self,
shape: <R2 as TensorRank>::Shape,
broadcast_dims: impl AsRef<[usize]>,
buffer_len: usize,
) -> Result<TensorLayout<R2>, Error>where
R2: TensorRank,
pub fn broadcast_in_dim_view<R2>(
&self,
shape: <R2 as TensorRank>::Shape,
broadcast_dims: impl AsRef<[usize]>,
buffer_len: usize,
) -> Result<TensorLayout<R2>, Error>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]);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 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more