Skip to main content

TensorLayout

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,

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>

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]

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]

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

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

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>

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>

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>

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,

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,

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>
where R: Clone + TensorRank, <R as TensorRank>::Shape: Clone, <R as TensorRank>::Strides: Clone,

§

fn clone(&self) -> TensorLayout<R>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<R> Debug for TensorLayout<R>
where R: Debug + TensorRank, <R as TensorRank>::Shape: Debug, <R as TensorRank>::Strides: Debug,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<R> PartialEq for TensorLayout<R>

§

fn eq(&self, other: &TensorLayout<R>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl<R> Eq for TensorLayout<R>
where R: Eq + TensorRank, <R as TensorRank>::Shape: Eq, <R as TensorRank>::Strides: Eq,

§

impl<R> StructuralPartialEq for TensorLayout<R>
where R: TensorRank,

Auto Trait Implementations§

§

impl<R> Freeze for TensorLayout<R>
where <R as TensorRank>::Shape: Freeze, <R as TensorRank>::Strides: Freeze,

§

impl<R> RefUnwindSafe for TensorLayout<R>

§

impl<R> Send for TensorLayout<R>
where <R as TensorRank>::Shape: Send, <R as TensorRank>::Strides: Send,

§

impl<R> Sync for TensorLayout<R>
where <R as TensorRank>::Shape: Sync, <R as TensorRank>::Strides: Sync,

§

impl<R> Unpin for TensorLayout<R>
where <R as TensorRank>::Shape: Unpin, <R as TensorRank>::Strides: Unpin,

§

impl<R> UnsafeUnpin for TensorLayout<R>

§

impl<R> UnwindSafe for TensorLayout<R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.