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
impl DynLayout
sourcepub fn from_shape(shape: &[usize]) -> DynLayout
pub fn from_shape(shape: &[usize]) -> DynLayout
Construct a layout with dimension sizes given by shape and default
(contiguous) strides.
sourcepub fn try_from_shape_and_strides(
shape: &[usize],
strides: &[usize],
overlap: OverlapPolicy,
) -> Result<DynLayout, FromDataError>
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.
sourcepub fn from_layout<L: Layout>(layout: &L) -> DynLayout
pub fn from_layout<L: Layout>(layout: &L) -> DynLayout
Create a new DynLayout with the same shape and strides as layout.
sourcepub fn broadcast(&self, to_shape: &[usize]) -> DynLayout
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.
sourcepub fn move_axis(&mut self, from: usize, to: usize)
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.
sourcepub fn slice(&self, range: &[SliceItem]) -> (Range<usize>, DynLayout)
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.
sourcepub fn try_slice(
&self,
range: &[SliceItem],
) -> Result<(Range<usize>, DynLayout), SliceError>
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.
pub fn resize_dim(&mut self, dim: usize, new_size: usize)
pub fn make_contiguous(&mut self)
sourcepub fn permute(&mut self, dims: &[usize])
pub fn permute(&mut self, dims: &[usize])
Swap the order of dimensions in this layout to the order described by
dims.
sourcepub fn permuted(&self, dims: &[usize]) -> DynLayout
pub fn permuted(&self, dims: &[usize]) -> DynLayout
Return a copy of this layout with dimensions re-ordered according to
dims.
sourcepub fn transposed(&self) -> DynLayout
pub fn transposed(&self) -> DynLayout
Return a copy of this layout with the order of dimensions reversed.
sourcepub fn insert_dim(&mut self, dim: usize)
pub fn insert_dim(&mut self, dim: usize)
Insert a dimension of size one at index dim.
sourcepub fn slice_offset<Idx: AsRef<[usize]>>(&self, index: Idx) -> usize
pub fn slice_offset<Idx: AsRef<[usize]>>(&self, index: Idx) -> usize
Return the offset of the slice that begins at the given index.
sourcepub fn squeezed(&self) -> DynLayout
pub fn squeezed(&self) -> DynLayout
Return a copy of this layout with dimensions of size 1 removed.
pub fn dims<const N: usize>(&self) -> [usize; N]
Trait Implementations§
source§impl Layout for DynLayout
impl Layout for DynLayout
source§fn len(&self) -> usize
fn len(&self) -> usize
Return the number of elements in the tensor shape described by this layout.
source§fn strides(&self) -> &[usize]
fn strides(&self) -> &[usize]
Return the stride (offset between elements) in the tensor’s element array.
§type Indices = DynIndices
type Indices = DynIndices
source§fn try_offset(&self, index: Self::Index<'_>) -> Option<usize>
fn try_offset(&self, index: Self::Index<'_>) -> Option<usize>
None if the index is out
of bounds along any dimension.source§fn indices(&self) -> DynIndices ⓘ
fn indices(&self) -> DynIndices ⓘ
source§fn offset_unchecked(&self, index: Self::Index<'_>) -> usize
fn offset_unchecked(&self, index: Self::Index<'_>) -> usize
source§fn is_contiguous(&self) -> bool
fn is_contiguous(&self) -> bool
source§fn is_broadcast(&self) -> bool
fn is_broadcast(&self) -> bool
source§fn can_broadcast_to(&self, target_shape: &[usize]) -> bool
fn can_broadcast_to(&self, target_shape: &[usize]) -> bool
source§fn can_broadcast_with(&self, shape: &[usize]) -> bool
fn can_broadcast_with(&self, shape: &[usize]) -> bool
shape as part of a binary operation. Read moresource§fn min_data_len(&self) -> usize
fn min_data_len(&self) -> usize
source§impl MutLayout for DynLayout
impl MutLayout for DynLayout
source§fn from_shape(shape: &[usize]) -> Self
fn from_shape(shape: &[usize]) -> Self
source§fn from_shape_and_strides(
shape: &[usize],
strides: &[usize],
overlap: OverlapPolicy,
) -> Result<Self, FromDataError>
fn from_shape_and_strides( shape: &[usize], strides: &[usize], overlap: OverlapPolicy, ) -> Result<Self, FromDataError>
source§fn move_axis(&mut self, from: usize, to: usize)
fn move_axis(&mut self, from: usize, to: usize)
from to to by swapping their strides.source§fn permuted(&self, order: &[usize]) -> DynLayout
fn permuted(&self, order: &[usize]) -> DynLayout
fn resize_dim(&mut self, dim: usize, size: usize)
source§fn transposed(&self) -> DynLayout
fn transposed(&self) -> DynLayout
self.permuted([N-1, N-2, ... 0]).source§fn slice<const M: usize>(
&self,
range: &[SliceItem],
) -> (Range<usize>, NdLayout<M>)
fn slice<const M: usize>( &self, range: &[SliceItem], ) -> (Range<usize>, NdLayout<M>)
source§fn slice_dyn(&self, range: &[SliceItem]) -> (Range<usize>, DynLayout)
fn slice_dyn(&self, range: &[SliceItem]) -> (Range<usize>, DynLayout)
source§fn split(
&self,
axis: usize,
mid: usize,
) -> ((Range<usize>, Self), (Range<usize>, Self))
fn split( &self, axis: usize, mid: usize, ) -> ((Range<usize>, Self), (Range<usize>, Self))
source§fn try_slice<R: IntoSliceItems>(
&self,
range: R,
) -> Result<(Range<usize>, DynLayout), SliceError>
fn try_slice<R: IntoSliceItems>( &self, range: R, ) -> Result<(Range<usize>, DynLayout), SliceError>
source§fn index_axis(
&self,
axis: usize,
index: usize,
) -> (Range<usize>, <Self as RemoveDim>::Output)where
Self: RemoveDim,
fn index_axis(
&self,
axis: usize,
index: usize,
) -> (Range<usize>, <Self as RemoveDim>::Output)where
Self: RemoveDim,
source§fn reshaped_for_view<S: IntoLayout>(
&self,
shape: S,
) -> Result<S::Layout, ReshapeError>
fn reshaped_for_view<S: IntoLayout>( &self, shape: S, ) -> Result<S::Layout, ReshapeError>
shape. Read moresource§fn reshaped_for_copy<S: IntoLayout>(
&self,
shape: S,
) -> Result<S::Layout, ReshapeError>
fn reshaped_for_copy<S: IntoLayout>( &self, shape: S, ) -> Result<S::Layout, ReshapeError>
shape.source§impl<'a, const N: usize> TryFrom<&'a DynLayout> for NdLayout<N>
impl<'a, const N: usize> TryFrom<&'a DynLayout> for NdLayout<N>
source§fn try_from(value: &'a DynLayout) -> Result<NdLayout<N>, DimensionError>
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
type Error = DimensionError
impl StructuralPartialEq for DynLayout
Auto Trait Implementations§
impl Freeze for DynLayout
impl RefUnwindSafe for DynLayout
impl Send for DynLayout
impl Sync for DynLayout
impl Unpin for DynLayout
impl UnwindSafe for DynLayout
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)