Crate rten_tensor

Source
Expand description

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

Each tensor is a combination of data and a layout. The data can be owned, borrowed or mutably borrowed. This is analagous to Vec<T>, &[T] and &mut [T] for 1D arrays. The layout determines the number of dimensions (the rank), the size of each dimension, and the strides (gap between successive indices along a given dimension).

§Key types and traits

The base type for all tensors is TensorBase. This is not normally used directly but instead via a type alias, depending on whether the number of dimensions (the rank) of the tensor is known at compile time or only at runtime, as well as whether the tensor owns, borrows or mutably borrows its data.

RankOwned (like Vec<T>)Borrowed (like &[T])Mutably borrowed
StaticNdTensorNdTensorViewNdTensorViewMut
DynamicTensorTensorViewTensorViewMut

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§

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

Macros§

Structs§

  • Iterator over slices of a tensor along an axis. See TensorView::axis_chunks.
  • Iterator over mutable slices of a tensor along an axis. See TensorViewMut::axis_chunks_mut.
  • Iterator over slices of a tensor along an axis. See TensorView::axis_iter.
  • Iterator over mutable slices of a tensor along an axis. See TensorViewMut::axis_iter_mut.
  • Iterator over a range of N-dimensional indices, where N is not known at compile time.
  • 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.
  • Implementation of Alloc which wraps the global allocator.
  • Iterator over a range of N-dimensional indices, where N may be known at compile time (see NdIndices) or only at runtime (DynIndices).
  • Iterator over views of the N innermost dimensions of a tensor with element type T and layout L.
  • Iterator over mutable views of the N innermost dimensions of a tensor.
  • Iterator over elements of a tensor, in their logical order.
  • Mutable iterator over elements of a tensor.
  • Iterator over 1D slices of a tensor along a target dimension of size N.
  • Mutable version of Lanes.
  • Iterator over a range of N-dimensional indices, where N is known at compile time.
  • 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.
  • A range for slicing a Tensor or NdTensor.
  • 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.
  • Storage for an immutable tensor view.
  • Storage for a mutable tensor 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§

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

Traits§

  • Storage allocation trait.
  • 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.
  • Trait for shapes which can be used to create a contiguous layout.
  • Used to convert sequences of indices and/or ranges into a uniform [SliceItem] array that can be used to slice a tensor.
  • Trait for converting owned and borrowed element containers (Vec<T>, slices) into their corresponding Storage type.
  • 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.
  • Provides convenience methods for querying the shape and strides of a matrix.
  • MutLayout extends Layout with methods for creating, modifying and transforming layouts.
  • Trait for sources of random data for tensors, for use with Tensor::rand.
  • Trait for backing storage used by tensors and views.
  • Trait for backing storage used by mutable tensors and views.

Functions§

Type Aliases§

  • Dynamically sized array of SliceItems, which avoids allocating in the common case where the length is small.
  • View of a slice as a matrix.
  • Mutable view of a slice as a matrix.
  • Tensor with a static dimension count.
  • View of a slice of a tensor with a static dimension count.
  • Mutable view of a slice of a tensor with a static dimension count.
  • Tensor with a dynamic dimension count.
  • View of a slice of a tensor with a dynamic dimension count.
  • Mutable view of a slice of a tensor with a dynamic dimension count.