Struct rten_tensor::DynLayout

source ·
pub struct DynLayout { /* private fields */ }
Expand description

Defines the valid indices for an N-dimensional array and how to map them to offsets in a linear buffer, where N can be varied at runtime.

The layout specifies the size of each dimension of the tensor (the shape) and the stride (gap) between offsets in each dimension.

§Safety and internal overlap

Rust requires that only one mutable reference can exist for any value. To ensure this, mutable iteration over a tensor must visit each element only once. This means that in the tensor’s Layout, every valid index must map to a unique offset. Verifying this for the general case of arbitrary shape and strides is non-trivial. See notes in mem_overlap.c in the NumPy source. In this library the problem is simplified by limiting the stride patterns that can be constructed. All Layout functions must uphold the invariant:

Every Layout either has one or more strides set to zero, or every valid index must map to a unique offset.

Zero-strides are used for broadcasting, which is widely used and easy to check for.

Implementations§

source§

impl DynLayout

source

pub fn from_shape(shape: &[usize]) -> DynLayout

Construct a layout with dimension sizes given by shape and default (contiguous) strides.

source

pub fn try_from_shape_and_strides( shape: &[usize], strides: &[usize], overlap: OverlapPolicy, ) -> Result<DynLayout, FromDataError>

Construct a layout with dimension sizes given by shape and given strides.

Panics if strides may lead to internal overlap (multiple indices map to the same data offset), unless strides contains a 0. See struct notes.

source

pub fn from_layout<L: Layout>(layout: &L) -> DynLayout

Create a new DynLayout with the same shape and strides as layout.

source

pub fn broadcast(&self, to_shape: &[usize]) -> DynLayout

Construct a layout which broadcasts elements to to_shape by setting the stride to 0 in broadcasted dimensions.

source

pub fn move_axis(&mut self, from: usize, to: usize)

Move the index at axis from to to, keeping the relative order of other dimensions the same. This is like NumPy’s moveaxis function.

source

pub fn slice(&self, range: &[SliceItem]) -> (Range<usize>, DynLayout)

Compute the new layout and offset of the first element for a slice into an existing tensor view.

Returns a tuple of (offset_range, layout) for the sliced view.

Panics if the range is invalid for the current layout.

source

pub fn try_slice( &self, range: &[SliceItem], ) -> Result<(Range<usize>, DynLayout), SliceError>

Compute the new layout and offset of the first element for a slice into an existing tensor view.

Returns a tuple of (offset_range, layout) for the sliced view, or an error if the range is invalid.

source

pub fn resize_dim(&mut self, dim: usize, new_size: usize)

source

pub fn make_contiguous(&mut self)

source

pub fn permute(&mut self, dims: &[usize])

Swap the order of dimensions in this layout to the order described by dims.

source

pub fn permuted(&self, dims: &[usize]) -> DynLayout

Return a copy of this layout with dimensions re-ordered according to dims.

source

pub fn transpose(&mut self)

Reverse the order of dimensions in this layout.

source

pub fn transposed(&self) -> DynLayout

Return a copy of this layout with the order of dimensions reversed.

source

pub fn insert_dim(&mut self, dim: usize)

Insert a dimension of size one at index dim.

source

pub fn slice_offset<Idx: AsRef<[usize]>>(&self, index: Idx) -> usize

Return the offset of the slice that begins at the given index.

source

pub fn squeezed(&self) -> DynLayout

Return a copy of this layout with dimensions of size 1 removed.

source

pub fn dims<const N: usize>(&self) -> [usize; N]

Trait Implementations§

source§

impl Clone for DynLayout

source§

fn clone(&self) -> DynLayout

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for DynLayout

source§

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

Formats the value using the given formatter. Read more
source§

impl<const N: usize> From<&NdLayout<N>> for DynLayout

source§

fn from(value: &NdLayout<N>) -> DynLayout

Converts to this type from the input type.
source§

impl<const N: usize> From<NdLayout<N>> for DynLayout

source§

fn from(value: NdLayout<N>) -> DynLayout

Converts to this type from the input type.
source§

impl Layout for DynLayout

source§

fn len(&self) -> usize

Return the number of elements in the tensor shape described by this layout.

source§

fn ndim(&self) -> usize

Return the number of dimensions.

source§

fn shape(&self) -> &[usize]

Return the sizes of each dimension.

source§

fn size(&self, dim: usize) -> usize

Returns the size of the dimension dim.

source§

fn strides(&self) -> &[usize]

Return the stride (offset between elements) in the tensor’s element array.

source§

fn stride(&self, dim: usize) -> usize

Return the stride for a specific dimension.

§

type Index<'a> = &'a [usize]

Type used to represent indices. Read more
§

type Indices = DynIndices

Iterator over indices in this tensor.
source§

fn try_offset(&self, index: Self::Index<'_>) -> Option<usize>

Map an index to a storage offset, or return None if the index is out of bounds along any dimension.
source§

fn is_empty(&self) -> bool

Returns true if the array has no elements.
source§

fn indices(&self) -> DynIndices

Return an iterator over all valid indices in this tensor.
source§

fn offset(&self, index: Self::Index<'_>) -> usize

Map an index to a storage offset. Read more
source§

fn offset_unchecked(&self, index: Self::Index<'_>) -> usize

Map an index to a storage offset, without checking if it is valid for the tensor’s shape. Read more
source§

fn is_contiguous(&self) -> bool

Return true if this layout describes a contiguous tensor, where the logical order of elements matches the order in which they are stored.
source§

fn is_broadcast(&self) -> bool

Return true if iterating over elements in this layout will visit elements multiple times.
source§

fn can_broadcast_to(&self, target_shape: &[usize]) -> bool

Return true if this layout’s shape can be broadcast to the given shape.
source§

fn can_broadcast_with(&self, shape: &[usize]) -> bool

Return true if the tensor/view can be broadcast with another tensor or view with a given shape as part of a binary operation. Read more
source§

fn min_data_len(&self) -> usize

Return the minimum length required for the element data buffer used with this layout.
source§

impl MutLayout for DynLayout

source§

fn from_shape(shape: &[usize]) -> Self

Create a new contiguous layout with a given shape.
source§

fn from_shape_and_strides( shape: &[usize], strides: &[usize], overlap: OverlapPolicy, ) -> Result<Self, FromDataError>

Create a layout with custom strides. Read more
source§

fn move_axis(&mut self, from: usize, to: usize)

Move the axis at position from to to by swapping their strides.
source§

fn permuted(&self, order: &[usize]) -> DynLayout

Return a layout with the axes permuted according to the given order.
source§

fn resize_dim(&mut self, dim: usize, size: usize)

source§

fn transposed(&self) -> DynLayout

Reverse the order of dimensions. This is equivalent to self.permuted([N-1, N-2, ... 0]).
source§

fn slice<const M: usize>( &self, range: &[SliceItem], ) -> (Range<usize>, NdLayout<M>)

Slice the layout. Read more
source§

fn slice_dyn(&self, range: &[SliceItem]) -> (Range<usize>, DynLayout)

Slice the layout and return a dynamic rank layout. Read more
source§

fn squeezed(&self) -> DynLayout

Return a layout with all size-one dimensions removed.
source§

fn split( &self, axis: usize, mid: usize, ) -> ((Range<usize>, Self), (Range<usize>, Self))

Split the layout along the given axis into two. Read more
source§

fn try_slice<R: IntoSliceItems>( &self, range: R, ) -> Result<(Range<usize>, DynLayout), SliceError>

Attempt to slice the layout or return an error if the range is invalid for the layout’s shape.
source§

fn index_axis( &self, axis: usize, index: usize, ) -> (Range<usize>, <Self as RemoveDim>::Output)
where Self: RemoveDim,

Slice a layout by selecting a single entry from a given axis. Read more
source§

fn reshaped_for_view<S: IntoLayout>( &self, shape: S, ) -> Result<S::Layout, ReshapeError>

Return a new layout formed by reshaping this one to shape. Read more
source§

fn reshaped_for_copy<S: IntoLayout>( &self, shape: S, ) -> Result<S::Layout, ReshapeError>

Return a new layout formed by reshaping this one to shape.
source§

fn slice_axis(&self, axis: usize, range: Range<usize>) -> (Range<usize>, Self)

Slice the layout along a given axis. Read more
source§

impl PartialEq for DynLayout

source§

fn eq(&self, other: &DynLayout) -> 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.
source§

impl<'a, const N: usize> TryFrom<&'a DynLayout> for NdLayout<N>

source§

fn try_from(value: &'a DynLayout) -> Result<NdLayout<N>, DimensionError>

Convert a dynamic layout into a static layout with N dims. Fails if value.ndim() != N.

§

type Error = DimensionError

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

impl StructuralPartialEq for DynLayout

Auto Trait Implementations§

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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>,

§

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>,

§

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.