Crate rten_tensor

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])
  • Shared / reference-counted (like Arc<[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 borrowedReference counted
StaticNdTensorNdTensorViewNdTensorViewMutCowNdTensorArcNdTensor
DynamicTensorTensorViewTensorViewMutCowTensorArcTensor

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]
}

Re-exports§

pub use layout::DynLayout;
pub use layout::Layout;
pub use layout::MatrixLayout;
pub use layout::NdLayout;
pub use slice_range::SliceItem;
pub use slice_range::SliceRange;
pub use storage::Storage;

Modules§

errors
Error types that are reported by various tensor operations.
iterators
Iterators over tensor elements and sub-views.
layout
Layouts which describe the shape and strides of a tensor.
prelude
This module provides a convenient way to import the most common traits from this library via a glob import.
slice_range
Range types used when slicing tensors.
storage
Data storage types and traits.
type_num
Traits and types for compile-time arithmetic.

Structs§

Contiguous
A tensor wrapper which guarantees that the tensor has a contiguous layout.
DynIndices
Iterator over a range of N-dimensional indices, where N is not known at compile time.
Indices
Iterator over a range of N-dimensional indices, where N may be known at compile time (see NdIndices) or only at runtime (DynIndices).
NdIndices
Iterator over a range of N-dimensional indices, where N is known at compile time.
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.
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.

Traits§

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).
RandomSource
Trait for sources of random data for tensors, for use with Tensor::rand.
Scalar
Trait for scalar (ie. non-array) values.

Type Aliases§

ArcNdTensor
Reference-counted tensor with N dimensions.
ArcTensor
Reference-counted tensor with a dynamic dimension count.
CowNdTensor
Owned or borrowed tensor with N dimensions.
CowTensor
Owned or borrowed tensor with a dynamic dimension count.
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.