Trait rten_tensor::Layout
source · pub trait Layout {
type Index<'a>: AsRef<[usize]> + Clone + Debug + PartialEq<Self::Index<'a>>;
type Indices;
Show 16 methods
// Required methods
fn try_offset(&self, index: Self::Index<'_>) -> Option<usize>;
fn ndim(&self) -> usize;
fn len(&self) -> usize;
fn shape(&self) -> Self::Index<'_>;
fn strides(&self) -> Self::Index<'_>;
fn indices(&self) -> Self::Indices;
// Provided methods
fn offset(&self, index: Self::Index<'_>) -> usize { ... }
fn offset_unchecked(&self, index: Self::Index<'_>) -> usize { ... }
fn is_contiguous(&self) -> bool { ... }
fn is_broadcast(&self) -> bool { ... }
fn is_empty(&self) -> bool { ... }
fn size(&self, dim: usize) -> usize { ... }
fn stride(&self, dim: usize) -> usize { ... }
fn can_broadcast_to(&self, target_shape: &[usize]) -> bool { ... }
fn can_broadcast_with(&self, shape: &[usize]) -> bool { ... }
fn min_data_len(&self) -> usize { ... }
}Expand description
Layouts describe the shape of a tensor, ie. the number of dimensions and size of each, and the mapping between indices and offsets in the data storage.
The main implementations are NdLayout, where the dimension count is known statically, and DynLayout, where the dimension count is only known at runtime.
Required Associated Types§
Required Methods§
sourcefn try_offset(&self, index: Self::Index<'_>) -> Option<usize>
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.
Provided Methods§
sourcefn offset(&self, index: Self::Index<'_>) -> usize
fn offset(&self, index: Self::Index<'_>) -> usize
Map an index to a storage offset.
Panics if any dimension of the index is out of bounds.
sourcefn offset_unchecked(&self, index: Self::Index<'_>) -> usize
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.
This method is not itself unsafe, because it only computes a storage offset but does not access any data. Using the offset to index into storage with a bounds check is unsafe however.
sourcefn is_contiguous(&self) -> bool
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.
sourcefn is_broadcast(&self) -> bool
fn is_broadcast(&self) -> bool
Return true if iterating over elements in this layout will visit elements multiple times.
sourcefn stride(&self, dim: usize) -> usize
fn stride(&self, dim: usize) -> usize
Returns the offset between adjacent indices along dimension dim.
sourcefn can_broadcast_to(&self, target_shape: &[usize]) -> bool
fn can_broadcast_to(&self, target_shape: &[usize]) -> bool
Return true if this layout’s shape can be broadcast to the given shape.
sourcefn can_broadcast_with(&self, shape: &[usize]) -> bool
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.
The shape of the result may be larger than either the current shape
or shape. eg. If a tensor of shape [1, 5] is broadcast with one
of size [2, 1, 1] the result has shape [2, 1, 5].
See https://github.com/onnx/onnx/blob/main/docs/Broadcasting.md for conditions in which broadcasting is allowed.
sourcefn min_data_len(&self) -> usize
fn min_data_len(&self) -> usize
Return the minimum length required for the element data buffer used with this layout.