8 releases (breaking)

0.10.0 May 25, 2024
0.9.0 May 16, 2024
0.8.0 Apr 29, 2024
0.7.0 Apr 12, 2024
0.1.0 Dec 31, 2023

#399 in Machine learning

Download history 99/week @ 2024-02-14 130/week @ 2024-02-21 195/week @ 2024-02-28 130/week @ 2024-03-06 127/week @ 2024-03-13 197/week @ 2024-03-20 701/week @ 2024-03-27 338/week @ 2024-04-03 281/week @ 2024-04-10 67/week @ 2024-04-17 283/week @ 2024-04-24 189/week @ 2024-05-01 124/week @ 2024-05-08 535/week @ 2024-05-15 363/week @ 2024-05-22 194/week @ 2024-05-29

1,231 downloads per month
Used in 7 crates (6 directly)


6.5K SLoC


rten-tensor is the foundational library that provides multi-dimensional arrays used by RTen. It is similar to ndarray but tailored for use in the RTen library.


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.

Rank Owned (like Vec<T>) Borrowed (like &[T]) Mutably borrowed
Static [NdTensor] [NdTensorView] [NdTensorViewMut]
Dynamic [Tensor] [TensorView] [TensorViewMut]

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_data([2, 2], vec![1, 2, 3, 4]);

// Logs 1, 3, 2, 4.
for x in tensor.transposed().iter() {
  println!("{}", x);