pub struct Layout<D>where
D: DimBaseAPI,{ /* private fields */ }
Expand description
Layout of tensor.
Layout is a struct that contains shape, stride, and offset of tensor.
- Shape is the size of each dimension of tensor.
- Stride is the number of elements to skip to get to the next element in each dimension.
- Offset is the starting position of tensor.
§Layout API document
Layout defines how elements will be accessed alongwith vector of data.
§Definition
We use notation $n$ as number of dimension, and $0 \leqslant k < n$ as a specific dimension.
Layout $L(\bm{d}, \bm{t}, s)$ contains three components:
- shape $\bm{d} = (d_0, d_1, \cdots, d_{n - 1})$, where $d_k \geqslant 0$, getter function
Layout::shape
. - stride $\bm{t} = (t_0, t_1, \cdots, t_{n - 1})$, where $t_k \neq 0$, getter function
Layout::stride
. - offset $s$, where $s \geqslant 0$, getter function
Layout::offset
.
For index computation, indices will also be involved:
- indices $\bm{i} = (i_0, i_1, \cdots, i_{n - 1})$, where $0 \leqslant i_k < d_k$
For a tensor, we generally use an array ($m_z$, $z \geqslant 0$) to store data in memory. With layout $L(\bm{d}, \bm{t}, s)$, the memory pointer for element at index $\bm{i}$ should be $$ z(\bm{i}) = s + \bm{i} \cdot \bm{t} = s + \sum_{k = 0}^{n - 1} i_k t_k $$
Note that in actual program, it is allowed to have negative indices $\tilde i_k \in [-d_k, d_k)$: $$ i_k = \begin{cases} \tilde i_k & i_k \in [0, d_k) \\ \tilde i_k + d_k & i_k \in [-d_k, 0) \end{cases} $$
Implementations§
Source§impl<D> Layout<D>where
D: DimBaseAPI,
impl<D> Layout<D>where
D: DimBaseAPI,
Sourcepub fn size_non_broadcast(&self) -> usize
pub fn size_non_broadcast(&self) -> usize
Get the size of the non-broadcasted part.
Equivalent to size()
if there is no broadcast (setting axis size = 1
where stride = 0).
Sourcepub fn is_broadcasted(&self) -> bool
pub fn is_broadcasted(&self) -> bool
Check whether current layout has been broadcasted.
This check is done by checking whether any stride of axis is zero.
Source§impl<D> Layout<D>where
D: DimBaseAPI,
Getter/setter functions for layout.
impl<D> Layout<D>where
D: DimBaseAPI,
Getter/setter functions for layout.
Sourcepub fn stride(&self) -> &<D as DimBaseAPI>::Stride
pub fn stride(&self) -> &<D as DimBaseAPI>::Stride
Stride of tensor. Getter function.
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Total number of elements in tensor.
§Note
This function uses cached size, instead of evaluating from shape.
Sourcepub unsafe fn set_offset(&mut self, offset: usize) -> &mut Layout<D>
pub unsafe fn set_offset(&mut self, offset: usize) -> &mut Layout<D>
Manually set offset.
§Safety
We will not check whether this offset is valid or not. In most cases, it is not intended to be used by user.
Source§impl<D> Layout<D>
Properties of layout.
impl<D> Layout<D>
Properties of layout.
Sourcepub fn ndim_of_f_contig(&self) -> usize
pub fn ndim_of_f_contig(&self) -> usize
Least number of dimensions that is f-contiguous for layout.
This function can be useful determining when to iterate by contiguous, and when to iterate by index.
Sourcepub fn ndim_of_c_contig(&self) -> usize
pub fn ndim_of_c_contig(&self) -> usize
Least number of dimensions that is c-contiguous for layout.
This function can be useful determining when to iterate by contiguous, and when to iterate by index.
Sourcepub fn f_contig(&self) -> bool
pub fn f_contig(&self) -> bool
Whether this tensor is f-contiguous.
Special cases
- When length of a dimension is one, then stride to that dimension is not important.
- When length of a dimension is zero, then tensor contains no elements, thus f-contiguous.
Sourcepub fn c_contig(&self) -> bool
pub fn c_contig(&self) -> bool
Whether this tensor is c-contiguous.
Special cases
- When length of a dimension is one, then stride to that dimension is not important.
- When length of a dimension is zero, then tensor contains no elements, thus c-contiguous.
Sourcepub fn index_f(&self, index: &[isize]) -> Result<usize, Error>
pub fn index_f(&self, index: &[isize]) -> Result<usize, Error>
Index of tensor by list of indexes to dimensions.
This function does not optimized for performance.
Sourcepub fn index(&self, index: &[isize]) -> usize
pub fn index(&self, index: &[isize]) -> usize
Index of tensor by list of indexes to dimensions.
This function does not optimized for performance. Negative index allowed.
§Panics
- Index greater than shape
Sourcepub fn bounds_index(&self) -> Result<(usize, usize), Error>
pub fn bounds_index(&self) -> Result<(usize, usize), Error>
Index range bounds of current layout. This bound is [min, max), which could be feed into range (min..max). If min == max, then this layout should not contains any element.
This function will raise error when minimum index is smaller than zero.
Sourcepub fn check_strides(&self) -> Result<(), Error>
pub fn check_strides(&self) -> Result<(), Error>
Check if strides is correct (no elemenets can overlap).
This will check if all number of elements in dimension of small strides is less than larger strides. For example of valid stride:
shape: (3, 2, 6) -> sorted -> ( 3, 6, 2)
stride: (3, -300, 15) -> sorted -> ( 3, 15, 300)
number of elements: 9, 90,
stride of next dimension 15, 300,
number of elem < stride of next dim? +, +,
Special cases
- if length of tensor is zero, then strides will always be correct.
- if certain dimension is one, then check for this stride will be ignored.
§TODO
Correctness of this function is not fully ensured.
pub fn diagonal(
&self,
offset: Option<isize>,
axis1: Option<isize>,
axis2: Option<isize>,
) -> Result<Layout<<D as DimSmallerOneAPI>::SmallerOne>, Error>where
D: DimSmallerOneAPI,
Source§impl<D> Layout<D>where
D: DimBaseAPI,
Constructors of layout. See also DimLayoutContigAPI
layout from shape
directly.
impl<D> Layout<D>where
D: DimBaseAPI,
Constructors of layout. See also DimLayoutContigAPI
layout from shape
directly.
Sourcepub fn new(
shape: D,
stride: <D as DimBaseAPI>::Stride,
offset: usize,
) -> Result<Layout<D>, Error>where
D: DimShapeAPI + DimStrideAPI,
pub fn new(
shape: D,
stride: <D as DimBaseAPI>::Stride,
offset: usize,
) -> Result<Layout<D>, Error>where
D: DimShapeAPI + DimStrideAPI,
Generate new layout by providing everything.
§Error when
- Shape and stride length mismatch
- Strides is correct (no elements can overlap)
- Minimum bound is not negative
Sourcepub unsafe fn new_unchecked(
shape: D,
stride: <D as DimBaseAPI>::Stride,
offset: usize,
) -> Layout<D>
pub unsafe fn new_unchecked( shape: D, stride: <D as DimBaseAPI>::Stride, offset: usize, ) -> Layout<D>
Generate new layout by providing everything, without checking bounds and strides.
§Safety
This function does not check whether layout is valid.
Sourcepub fn new_shape(&self) -> D
pub fn new_shape(&self) -> D
New zero shape, which number of dimensions are the same to current layout.
Sourcepub fn new_stride(&self) -> <D as DimBaseAPI>::Stride
pub fn new_stride(&self) -> <D as DimBaseAPI>::Stride
New zero stride, which number of dimensions are the same to current layout.
Source§impl<D> Layout<D>
Manuplation of layout.
impl<D> Layout<D>
Manuplation of layout.
Source§impl<D> Layout<D>
Fast indexing and utilities of layout.
impl<D> Layout<D>
Fast indexing and utilities of layout.
These functions are mostly internal to this crate.
Sourcepub unsafe fn index_uncheck(&self, index: &[usize]) -> isize
pub unsafe fn index_uncheck(&self, index: &[usize]) -> isize
Index of tensor by list of indexes to dimensions.
§Safety
This function does not check for bounds, including
- Negative index
- Index greater than shape
Due to these reasons, this function may well give index smaller than zero, which may occur in iterator; so this function returns isize.
Source§impl<D> Layout<D>where
D: DimBaseAPI,
impl<D> Layout<D>where
D: DimBaseAPI,
Sourcepub fn into_dim<D2>(self) -> Result<Layout<D2>, Error>where
D2: DimBaseAPI,
D: DimIntoAPI<D2>,
pub fn into_dim<D2>(self) -> Result<Layout<D2>, Error>where
D2: DimBaseAPI,
D: DimIntoAPI<D2>,
Convert layout to another dimension.
Sourcepub fn to_dim<D2>(&self) -> Result<Layout<D2>, Error>where
D2: DimBaseAPI,
D: DimIntoAPI<D2>,
pub fn to_dim<D2>(&self) -> Result<Layout<D2>, Error>where
D2: DimBaseAPI,
D: DimIntoAPI<D2>,
Convert layout to another dimension.
Trait Implementations§
Source§impl<D> IndexerDynamicAPI for Layout<D>where
D: DimDevAPI,
impl<D> IndexerDynamicAPI for Layout<D>where
D: DimDevAPI,
Source§fn dim_slice(&self, indexers: &[Indexer]) -> Result<Layout<Vec<usize>>, Error>
fn dim_slice(&self, indexers: &[Indexer]) -> Result<Layout<Vec<usize>>, Error>
Source§fn dim_split_at(
&self,
axis: isize,
) -> Result<(Layout<Vec<usize>>, Layout<Vec<usize>>), Error>
fn dim_split_at( &self, axis: isize, ) -> Result<(Layout<Vec<usize>>, Layout<Vec<usize>>), Error>
fn dim_split_axes( &self, axes: &[isize], ) -> Result<(Layout<Vec<usize>>, Layout<Vec<usize>>), Error>
Source§impl<D> IndexerLargerOneAPI for Layout<D>
impl<D> IndexerLargerOneAPI for Layout<D>
Source§impl<D> IndexerPreserveAPI for Layout<D>where
D: DimDevAPI,
impl<D> IndexerPreserveAPI for Layout<D>where
D: DimDevAPI,
Source§impl<D> IndexerSmallerOneAPI for Layout<D>
impl<D> IndexerSmallerOneAPI for Layout<D>
type DOut = <D as DimSmallerOneAPI>::SmallerOne
Source§fn dim_select(
&self,
axis: isize,
index: isize,
) -> Result<Layout<<Layout<D> as IndexerSmallerOneAPI>::DOut>, Error>
fn dim_select( &self, axis: isize, index: isize, ) -> Result<Layout<<Layout<D> as IndexerSmallerOneAPI>::DOut>, Error>
Source§fn dim_eliminate(
&self,
axis: isize,
) -> Result<Layout<<Layout<D> as IndexerSmallerOneAPI>::DOut>, Error>
fn dim_eliminate( &self, axis: isize, ) -> Result<Layout<<Layout<D> as IndexerSmallerOneAPI>::DOut>, Error>
Source§impl<D> PartialEq for Layout<D>where
D: DimBaseAPI,
impl<D> PartialEq for Layout<D>where
D: DimBaseAPI,
impl<D> Send for Layout<D>where
D: DimBaseAPI,
impl<D> Sync for Layout<D>where
D: DimBaseAPI,
Auto Trait Implementations§
impl<D> Freeze for Layout<D>
impl<D> RefUnwindSafe for Layout<D>
impl<D> Unpin for Layout<D>
impl<D> UnwindSafe for Layout<D>
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§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more