Struct rten_tensor::NdLayout

source ·
pub struct NdLayout<const N: usize> { /* 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 is known at compile time.

Implementations§

source§

impl<const N: usize> NdLayout<N>

source

pub fn from_dyn(l: DynLayout) -> Self

Convert a layout with dynamic rank to a layout with a static rank.

Panics if l does not have N dimensions.

source

pub fn as_dyn(&self) -> DynLayout

Convert this layout to one with a dynamic rank.

source

pub fn index_valid(&self, index: [usize; N]) -> bool

Return true if all components of index are in-bounds.

source

pub fn contiguous_strides(shape: [usize; N]) -> [usize; N]

Return the strides that a contiguous layout with a given shape would have.

source

pub fn from_shape(shape: [usize; N]) -> Self

Create a layout with a given shape and a contiguous layout.

source

pub fn try_from_shape_and_strides( shape: [usize; N], strides: [usize; N], overlap: OverlapPolicy, ) -> Result<NdLayout<N>, FromDataError>

Create a layout with given shape and strides, intended for use with data storage of length data_len.

overlap determines whether this method will fail if the layout may have internal overlap.

source

pub fn broadcast<const M: usize>(&self, to_shape: [usize; M]) -> NdLayout<M>

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

source

pub fn permuted(&self, dims: [usize; N]) -> Self

Swap strides of this layout to put axes in the given order.

Values in dims must be < N.

source

pub fn transposed(&self) -> Self

Reverse the order of dimensions in this layout.

source

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

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.

source

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

Trait Implementations§

source§

impl<const N: usize> Clone for NdLayout<N>

source§

fn clone(&self) -> NdLayout<N>

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<const N: usize> Debug for NdLayout<N>

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<const N: usize> Layout for NdLayout<N>

§

type Index<'a> = [usize; N]

Type used to represent indices. Read more
§

type Indices = NdIndices<N>

Iterator over indices in this tensor.
source§

fn ndim(&self) -> usize

Return the number of dimensions.
source§

fn len(&self) -> usize

Returns the number of elements in the array.
source§

fn try_offset(&self, index: [usize; N]) -> Option<usize>

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

fn offset_unchecked(&self, index: [usize; N]) -> usize

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

fn shape(&self) -> Self::Index<'_>

Returns an array of the sizes of each dimension.
source§

fn strides(&self) -> Self::Index<'_>

Returns an array of the strides of each dimension.
source§

fn indices(&self) -> Self::Indices

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 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 is_empty(&self) -> bool

Returns true if the array has no elements.
source§

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

Returns the size of the dimension dim.
source§

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

Returns the offset between adjacent indices along dimension dim.
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 MatrixLayout for NdLayout<2>

source§

fn rows(&self) -> usize

source§

fn cols(&self) -> usize

source§

fn row_stride(&self) -> usize

source§

fn col_stride(&self) -> usize

source§

impl<const N: usize> MutLayout for NdLayout<N>

source§

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

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

fn from_shape_and_strides( shape: Self::Index<'_>, strides: Self::Index<'_>, 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; N]) -> NdLayout<N>

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) -> NdLayout<N>

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 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<const N: usize> PartialEq for NdLayout<N>

source§

fn eq(&self, other: &NdLayout<N>) -> 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<const N: usize> Copy for NdLayout<N>

source§

impl<const N: usize> StructuralPartialEq for NdLayout<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for NdLayout<N>

§

impl<const N: usize> RefUnwindSafe for NdLayout<N>

§

impl<const N: usize> Send for NdLayout<N>

§

impl<const N: usize> Sync for NdLayout<N>

§

impl<const N: usize> Unpin for NdLayout<N>

§

impl<const N: usize> UnwindSafe for NdLayout<N>

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.