Crate rten_tensor

Source
Expand description

rten_tensor provides multi-dimensional arrays, commonly referred to as tensors in a machine learning context.

§Storage and layout

A tensor is a combination of data storage and a layout. The data storage determines the element type and how the data is owned. A tensor can be:

  • Owned (like Vec<T>)
  • Borrowed (like &[T] or &mut [T])
  • Maybe-owned (like Cow[T])

The layout determines the number of dimensions (the rank) and size of each (the shape) and how indices map to offsets in the storage. The dimension count can be static (fixed at compile time) or dynamic (variable at runtime).

§Tensor types and traits

The base type for all tensors is TensorBase. This is not normally used directly but instead via a type alias which specifies the data ownership and layout:

RankOwnedBorrowedMutably borrowedOwned or borrowed
StaticNdTensorNdTensorViewNdTensorViewMutCowNdTensor
DynamicTensorTensorViewTensorViewMutCowTensor

All tensors implement the Layout trait, which provide methods to query the shape, dimension count and strides of the tensor. Tensor views provide various methods for indexing, iterating, slicing and transforming them. The AsView trait provides access to these methods for owned and mutably borrowed tensors. Conceptually it is similar to how Deref allows accesing methods for &[T] on a Vec<T>. The preferred way to import the traits is via the prelude:

use rten_tensor::prelude::*;
use rten_tensor::NdTensor;

let tensor = NdTensor::from([[1, 2], [3, 4]]);

let transposed_elems: Vec<_> = tensor.transposed().iter().copied().collect();
assert_eq!(transposed_elems, [1, 3, 2, 4]);

§Serialization

Tensors can be serialized and deserialized using serde if the serde feature is enabled. The serialized representation of a tensor includes its shape and elements in row-major (C) order. The JSON serialization of a matrix (NdTensor<f32, 2>) looks like this for example:

{
  "shape": [2, 2],
  "data": [0.5, 1.0, 1.5, 2.0]
}

Modules§

errors
Error types that are reported by various tensor operations.
prelude
This module provides a convenient way to import the most common traits from this library via a glob import.
type_num
Traits and types for compile-time arithmetic.

Macros§

ndtensorDeprecated
Construct an NdTensor from literal elements.
tensorDeprecated
Construct a Tensor from literal elements.

Structs§

AxisChunks
Iterator over slices of a tensor along an axis. See TensorView::axis_chunks.
AxisChunksMut
Iterator over mutable slices of a tensor along an axis. See TensorViewMut::axis_chunks_mut.
AxisIter
Iterator over slices of a tensor along an axis. See TensorView::axis_iter.
AxisIterMut
Iterator over mutable slices of a tensor along an axis. See TensorViewMut::axis_iter_mut.
DynIndices
Iterator over a range of N-dimensional indices, where N is not known at compile time.
DynLayout
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.
GlobalAlloc
Implementation of Alloc which wraps the global allocator.
Indices
Iterator over a range of N-dimensional indices, where N may be known at compile time (see NdIndices) or only at runtime (DynIndices).
InnerIter
Iterator over views of the N innermost dimensions of a tensor with element type T and layout L.
InnerIterMut
Iterator over mutable views of the N innermost dimensions of a tensor.
Iter
Iterator over elements of a tensor, in their logical order.
IterMut
Mutable iterator over elements of a tensor.
Lanes
Iterator over 1D slices of a tensor along a target dimension of size N.
LanesMut
Mutable version of Lanes.
NdIndices
Iterator over a range of N-dimensional indices, where N is known at compile time.
NdLayout
Defines the valid indices for an N-dimensional array and how to map them to offsets in a linear buffer, where N is known at compile time.
SliceRange
A range for slicing a Tensor or NdTensor.
TensorBase
The base type for multi-dimensional arrays. This consists of storage for elements, plus a layout which maps from a multi-dimensional array index to a storage offset. This base type is not normally used directly but instead through a type alias which selects the storage type and layout.
ViewData
Storage for an immutable tensor view.
ViewMutData
Storage for a mutable tensor view.
WeaklyCheckedView
A view of a tensor which does “weak” checking when indexing via view[<index>]. This means that it does not bounds-check individual dimensions, but does bounds-check the computed offset.

Enums§

CowData
Tensor storage which may be either owned or borrowed.
OverlapPolicy
Specifies whether a tensor or view may have an overlapping layout.
SliceItem
Specifies a subset of a dimension to include when slicing a tensor or view.

Traits§

Alloc
Storage allocation trait.
AsView
Trait implemented by all variants of TensorBase, which provides a view method to get an immutable view of the tensor, plus methods which forward to such a view.
AssumeInit
Trait for converting collections of uninitialized (MaybeUninit<T>) values to collections of corresponding initializes values (T).
IntoLayout
Trait for shapes which can be used to create a contiguous layout.
IntoSliceItems
Used to convert sequences of indices and/or ranges into a uniform [SliceItem] array that can be used to slice a tensor.
IntoStorage
Trait for converting owned and borrowed element containers (Vec<T>, slices) into their corresponding Storage type.
Layout
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.
MatrixLayout
Provides convenience methods for querying the shape and strides of a matrix.
MutLayout
MutLayout extends Layout with methods for creating, modifying and transforming layouts.
RandomSource
Trait for sources of random data for tensors, for use with Tensor::rand.
ResizeLayout
Trait which extends MutLayout with support for changing the number of dimensions in-place.
Scalar
Storage
Trait for backing storage used by tensors and views.
StorageMut
Trait for backing storage used by mutable tensors and views.

Functions§

is_valid_permutation
Return true if permutation is a valid permutation of dimensions for a tensor of rank ndim.
to_slice_items
Convert a slice of indices into SliceItems.

Type Aliases§

CowNdTensor
Owned or borrowed tensor with N dimensions.
CowTensor
Owned or borrowed tensor with a dynamic dimension count.
DynSliceItems
Dynamically sized array of SliceItems, which avoids allocating in the common case where the length is small.
Matrix
View of a 2D tensor.
MatrixMut
Mutable view of a 2D tensor.
NdTensor
Owned tensor with N dimensions.
NdTensorView
View of a tensor with N dimensions.
NdTensorViewMut
Mutable view of a tensor with N dimensions.
Tensor
Owned tensor with a dynamic dimension count.
TensorView
View of a tensor with a dynamic dimension count.
TensorViewMut
Mutable view of a tensor with a dynamic dimension count.