Skip to main content

Module tensor

Module tensor 

Source
Expand description

Arrays that Rust and the host language share without a copy.

This module defines two types, a bridge between numeric arrays in Rust and in the host language (Python, C, Wasm/JS). Both store a raw data pointer together with shape, strides (in bytes) and scalar type metadata.

TensorView and TensorViewMut have the C layout of tensor::RawLayout (the fields ptr, dtype, ndim, shape and strides), so a C callback reads their fields directly.

§Batches

A diffsol vector or dense matrix can have more than one lane (nbatch). The arrays use the column-major order of diffsol, and the last axis of an array of lanes is always the lane, also when nbatch is 1. For a diffsol vector, the shape is [n, nbatch]. For a dense matrix, the shape is [nrows, ncols, nbatch]. If an input array has one lane, all the lanes get its values.

§Key methods

Structs§

RawLayout
The data pointer, the scalar type, the shape and the byte strides of a Tensor, a TensorView or a TensorViewMut. A RawLayout always obeys the layout rules (the Tensor docs give the rules): each constructor checks them. It does not own or borrow the data, so the reads are unsafe.
Tensor
An N-D array of f32 or f64 values that Rust owns, and that the host language (C, Python) reads without a copy. The outputs of this crate are Tensor values. The inputs are TensorView values.
TensorView
A view of an N-D array of f32 or f64 values that the view does not own. The inputs of this crate are TensorView values.
TensorViewMut
A mutable view of an N-D array of f32 or f64 values that the view does not own.

Constants§

MAX_NDIM
The maximum number of axes of a Tensor or a TensorView.

Traits§

TensorLayout
A value that a Tensor can show with no copy of the data.
TensorLayoutMut
A diffsol vector that a TensorViewMut can show, so that a C callback can write its values with no copy. The layout is [n, nbatch], the same as TensorLayout::tensor_layout.