pub trait Layout {
type Index<'a>: AsRef<[usize]> + Clone + Debug + PartialEq<Self::Index<'a>>;
type Indices;
Show 15 methods
// Required methods
fn 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_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 offset(&self, index: Self::Index<'_>) -> Option<usize>
fn 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.
Offsets returned by this method must be less than the layout’s minimum
storage length reported by min_data_len
.
If a layout also implements TrustedLayout
then callers can rely
on this to avoid subsequent bounds checks.
Provided Methods§
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 without 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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.