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:
Rank | Owned | Borrowed | Mutably borrowed | Owned or borrowed |
---|---|---|---|---|
Static | NdTensor | NdTensorView | NdTensorViewMut | CowNdTensor |
Dynamic | Tensor | TensorView | TensorViewMut | CowTensor |
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§
- ndtensor
Deprecated - Construct an
NdTensor
from literal elements. - tensor
Deprecated - Construct a
Tensor
from literal elements.
Structs§
- Axis
Chunks - Iterator over slices of a tensor along an axis. See
TensorView::axis_chunks
. - Axis
Chunks Mut - Iterator over mutable slices of a tensor along an axis. See
TensorViewMut::axis_chunks_mut
. - Axis
Iter - Iterator over slices of a tensor along an axis. See
TensorView::axis_iter
. - Axis
Iter Mut - 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.
- Global
Alloc - 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
). - Inner
Iter - Iterator over views of the N innermost dimensions of a tensor with element
type
T
and layoutL
. - Inner
Iter Mut - 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.
- Lanes
Mut - 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.
- Slice
Range - A range for slicing a
Tensor
orNdTensor
. - Tensor
Base - 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.
- View
Data - Storage for an immutable tensor view.
- View
MutData - Storage for a mutable tensor view.
- Weakly
Checked View - 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.
- Overlap
Policy - Specifies whether a tensor or view may have an overlapping layout.
- Slice
Item - 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 aview
method to get an immutable view of the tensor, plus methods which forward to such a view. - Assume
Init - Trait for converting collections of uninitialized (
MaybeUninit<T>
) values to collections of corresponding initializes values (T
). - Into
Layout - Trait for shapes which can be used to create a contiguous layout.
- Into
Slice Items - Used to convert sequences of indices and/or ranges into a uniform
[SliceItem]
array that can be used to slice a tensor. - Into
Storage - Trait for converting owned and borrowed element containers (
Vec<T>
, slices) into their correspondingStorage
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.
- Matrix
Layout - Provides convenience methods for querying the shape and strides of a matrix.
- MutLayout
- MutLayout extends
Layout
with methods for creating, modifying and transforming layouts. - Random
Source - Trait for sources of random data for tensors, for use with
Tensor::rand
. - Resize
Layout - 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.
- Storage
Mut - 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 rankndim
. - to_
slice_ items - Convert a slice of indices into
SliceItem
s.
Type Aliases§
- CowNd
Tensor - Owned or borrowed tensor with N dimensions.
- CowTensor
- Owned or borrowed tensor with a dynamic dimension count.
- DynSlice
Items - Dynamically sized array of
SliceItem
s, which avoids allocating in the common case where the length is small. - Matrix
- View of a 2D tensor.
- Matrix
Mut - Mutable view of a 2D tensor.
- NdTensor
- Owned tensor with N dimensions.
- NdTensor
View - View of a tensor with N dimensions.
- NdTensor
View Mut - Mutable view of a tensor with N dimensions.
- Tensor
- Owned tensor with a dynamic dimension count.
- Tensor
View - View of a tensor with a dynamic dimension count.
- Tensor
View Mut - Mutable view of a tensor with a dynamic dimension count.