Layout

Trait Layout 

Source
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§

Source

type Index<'a>: AsRef<[usize]> + Clone + Debug + PartialEq<Self::Index<'a>>

Type used to represent indices.

It is assumed that this type can also represent the shape and strides of the tensor.

Source

type Indices

Iterator over indices in this tensor.

Required Methods§

Source

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.

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 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.

Provided Methods§

Source

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.

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.

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.

Source

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.

Implementations on Foreign Types§

Source§

impl<L: Layout> Layout for &L

Source§

type Index<'b> = <L as Layout>::Index<'b>

Source§

type Indices = <L as Layout>::Indices

Source§

fn ndim(&self) -> usize

Source§

fn len(&self) -> usize

Source§

fn offset(&self, index: Self::Index<'_>) -> Option<usize>

Source§

fn offset_unchecked(&self, index: Self::Index<'_>) -> usize

Source§

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

Source§

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

Source§

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

Implementors§

Source§

impl Layout for DynLayout

Source§

impl<S: Storage, L: Layout> Layout for TensorBase<S, L>

Source§

type Index<'a> = <L as Layout>::Index<'a>

Source§

type Indices = <L as Layout>::Indices

Source§

impl<T, S: Storage<Elem = T>, L: Layout> Layout for WeaklyCheckedView<S, L>

Source§

type Index<'a> = <L as Layout>::Index<'a>

Source§

type Indices = <L as Layout>::Indices

Source§

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