pub struct Tensor<Element> { /* private fields */ }Expand description
A dense tensor with an immutable, runtime-defined Shape and a shared
element buffer read through a strided layout.
The elements are held behind a Storage representation: an
Arc-shared row-major buffer addressed by strides and an offset, or a
non-allocating constant. Cloning shares the buffer and clones only the
metadata; it does not clone the elements. Because tensors are immutable
and buffer-shared, view operations that alias a buffer (transpose and
broadcast) are always safe: no operation ever writes through an alias.
Arithmetic and the elementwise maps operate in logical row-major
order. Binary elementwise operations require identical shapes and
never broadcast implicitly. Broadcasting is available only through
broadcast_like and
broadcast_along, and it produces a
view rather than copying.
matmul requires rank-2 or batched higher-rank
operands, and transpose accepts ranks 0
through 2, returning a view. Reductions, explicit broadcasts, and
reshaping are rank-general.
Implementations§
Source§impl<Element> Tensor<Element>
impl<Element> Tensor<Element>
Source§impl<Element: Clone> Tensor<Element>
impl<Element: Clone> Tensor<Element>
Sourcepub fn iter(&self) -> impl Iterator<Item = Element> + '_
pub fn iter(&self) -> impl Iterator<Item = Element> + '_
Returns the elements in logical row-major order.
A contiguous dense buffer iterates its slice directly; a strided view walks its layout with an odometer; a constant repeats its single value across the shape’s volume.
It yields owned elements rather than references because a representation that computes its elements has nothing to lend, and for the numeric payloads a clone is the same load a borrow-then-copy compiles to.
Sourcepub fn to_vec(&self) -> Vec<Element>
pub fn to_vec(&self) -> Vec<Element>
Returns the elements in logical row-major order as an owned vector.
Sourcepub fn convert<Target: From<Element>>(&self) -> Tensor<Target>
pub fn convert<Target: From<Element>>(&self) -> Tensor<Target>
Returns this tensor with every element converted into Target
through its From conversion, preserving the storage
representation: a constant stays a constant, a selection stays
a selection, and a dense view keeps its layout, so a broadcast
converts only its distinct buffer elements.
It is the precision boundary for mixed-precision work —
loading an f32 checkpoint into a Tensor<Bf16> model, or
widening bf16 results back — priced at one conversion per
stored element.
Sourcepub fn scalar(&self) -> Element
pub fn scalar(&self) -> Element
Returns the rank-0 tensor’s single element: the scalar projection, and the read-back the teaching examples end on.
It is deliberately loud — one call, rank-checked — rather than
a Deref to the element: a silent projection is the kind of
magic the explicitness rule forbids. A rank-1 tensor of one
element does not qualify; reshape it first.
§Panics
Panics if this tensor is not rank 0.
Source§impl<Element: Differentiable> Tensor<Element>
impl<Element: Differentiable> Tensor<Element>
Sourcepub fn new(shape: impl Into<Shape>, elements: impl Into<Vec<Element>>) -> Self
pub fn new(shape: impl Into<Shape>, elements: impl Into<Vec<Element>>) -> Self
Creates a tensor of shape from elements in row-major order.
§Panics
Panics if the shape’s volume overflows usize, the number of elements
differs from that volume, or the shape holds no elements. Empty tensors
are unsupported because reductions initialize their accumulator from
an existing element.
Sourcepub fn filled(shape: impl Into<Shape>, element: Element) -> Self
pub fn filled(shape: impl Into<Shape>, element: Element) -> Self
Creates a tensor of shape with every element set to element,
stored as a non-allocating constant.
§Panics
Panics if the shape’s volume overflows usize or the shape holds no
elements, as documented on Tensor::new.
Sourcepub fn selection(
indices: impl Into<Vec<usize>>,
vocab: usize,
one: Element,
) -> Self
pub fn selection( indices: impl Into<Vec<usize>>, vocab: usize, one: Element, ) -> Self
Creates the one-hot [indices.len(), vocab] selection matrix whose
row i is one at column indices[i] and zero elsewhere, stored as
its indices rather than a dense buffer.
It carries the token indices of an embedding lookup: feed it as a
per-run input and read it with Recordable::gather.
one is the value placed at each selected position (the
multiplicative identity, e.g. 1.0); the zero is derived from it.
§Panics
Panics if vocab is zero, indices is empty, any index is not
below vocab, or the [indices.len(), vocab] volume overflows
usize.
Source§impl<Element: Differentiable> Tensor<Element>
impl<Element: Differentiable> Tensor<Element>
Sourcepub fn zero_like(&self) -> Self
pub fn zero_like(&self) -> Self
Returns a zero shaped like self, stored as a constant: the
seed of gradient accumulators.
Sourcepub fn one_like(&self) -> Self
pub fn one_like(&self) -> Self
Returns a one shaped like self, stored as a constant: the
seed of the output gradient.
Sourcepub fn counted(shape: Shape, count: usize) -> Self
pub fn counted(shape: Shape, count: usize) -> Self
Returns the count spread across shape, stored as a constant;
the element value comes from the element type’s own
from_count.
It is the constructor behind size-derived constants: a composed formula that divides by an axis extent (a mean, a normalization) mints that extent here. Counts convert exactly as long as the element type can represent them.
§Panics
Panics if the shape’s volume overflows usize or the shape holds
no elements, as documented on Tensor::new.
Source§impl<Element: Elementary> Tensor<Element>
impl<Element: Elementary> Tensor<Element>
Sourcepub fn log1p(&self) -> Self
pub fn log1p(&self) -> Self
Returns the natural logarithm of one plus each element, accurate near zero.
Sourcepub fn erf_derivative(&self) -> Self
pub fn erf_derivative(&self) -> Self
Returns the derivative of the error function of each element:
the scaled Gaussian (2/sqrt(pi)) * e^(-x^2).
Sourcepub fn powf(&self, exponent: Self) -> Self
pub fn powf(&self, exponent: Self) -> Self
Returns each element raised to the matching element of
exponent.
§Panics
Panics if the tensors have different shapes.
Source§impl<Element: Elementary> Tensor<Element>
impl<Element: Elementary> Tensor<Element>
Sourcepub fn batch_normalized(
&self,
scale: &Self,
shift: &Self,
epsilon: &Self,
) -> (Self, Self, Self)
pub fn batch_normalized( &self, scale: &Self, shift: &Self, epsilon: &Self, ) -> (Self, Self, Self)
Returns the batch normalization through the payload seam: when every operand is a contiguous dense buffer the whole group is offered to the backend chain as one task, and the composed bitwise reference computes when the chain declines or an operand is a view or constant.
§Panics
Panics if self is not rank 2 [batch, features], the affine
operands do not hold features values, or epsilon holds
more than one value.
Sourcepub fn max_pooled(&self, size: usize, stride: usize) -> Self
pub fn max_pooled(&self, size: usize, stride: usize) -> Self
Returns the max pool through a direct window walk: each
output element folds its window with maximum in the same
row-major lane order as the recorded formula, so the walk is
bit-identical to the composed fold while materializing no
lane views. Non-contiguous inputs and non-dense storages take
the composed reference path.
§Panics
Panics if self is not rank 4, size or stride is zero,
or a window does not fit the spatial extents.
Sourcepub fn matmul(&self, rhs: &Self) -> Self
pub fn matmul(&self, rhs: &Self) -> Self
Returns the matrix product of two rank-2 tensors.
Dense operands, including strided views (a transposed operand, most often), multiply on a slice path that reads their buffers through the layout strides directly; other storages read through logical access. Both paths accumulate every output element in the same order, so their results are bit-identical.
§Panics
Panics if either operand is not rank 2, the inner dimensions do not agree, or any dimension is empty.
Sourcepub fn transpose(&self) -> Self
pub fn transpose(&self) -> Self
Returns the tensor with its two axes swapped as a view over the same buffer.
Rank-0 and rank-1 tensors are returned unchanged.
§Panics
Panics if the tensor’s rank exceeds 2.
Sourcepub fn sum(&self) -> Self
pub fn sum(&self) -> Self
Returns the sum of every element as a rank-0 constant.
Elements are accumulated in logical order from left to right without pairwise or compensated summation.
Sourcepub fn sum_along(&self, axis: usize) -> Self
pub fn sum_along(&self, axis: usize) -> Self
Returns the tensor with axis reduced by summation.
The reduction is rank-general: the elements are viewed as
[outer, axis, inner] in logical order and summed over the middle
extent.
§Panics
Panics if axis is out of rank.
Sourcepub fn max_along(&self, axis: usize) -> Self
pub fn max_along(&self, axis: usize) -> Self
Returns the tensor with axis reduced to its largest element by the
elementwise Elementary::maximum.
The reduction is rank-general and mirrors Recordable::sum_along:
the elements are viewed as [outer, axis, inner] in logical order
and folded over the middle extent.
§Panics
Panics if axis is out of rank.
Sourcepub fn broadcast(&self, shape: Shape) -> Self
pub fn broadcast(&self, shape: Shape) -> Self
Returns this tensor’s single element spread across shape as a
constant: the whole-shape form of explicit broadcasting.
§Panics
Panics if self holds more than one element.
Sourcepub fn broadcast_like(&self, reference: &Self) -> Self
pub fn broadcast_like(&self, reference: &Self) -> Self
Sourcepub fn broadcast_along(&self, axis: usize, extent: usize) -> Self
pub fn broadcast_along(&self, axis: usize, extent: usize) -> Self
Returns the tensor repeated along a new axis of extent
inserted at axis, as a stride-0 view: the named-axis form of
explicit broadcasting.
§Panics
Panics if axis exceeds this tensor’s rank or extent is zero.
Sourcepub fn broadcast_along_like(&self, axis: usize, reference: &Self) -> Self
pub fn broadcast_along_like(&self, axis: usize, reference: &Self) -> Self
Returns the tensor repeated along axis to match reference’s
shape: broadcast_along reading the
reference for its shape alone.
§Panics
Panics if axis is out of reference’s rank or self’s shape
differs from reference’s with that axis removed.
Sourcepub fn reshape(&self, shape: Shape) -> Self
pub fn reshape(&self, shape: Shape) -> Self
Returns self reinterpreted with shape in logical row-major
order.
A contiguous dense tensor and a constant reshape into an O(1) view over the same buffer, and so does a strided view when only extent-1 axes are inserted or removed; any other strided reshape is first materialized.
§Panics
Panics if shape’s volume differs from self’s.
Sourcepub fn permute(&self, order: &[usize]) -> Self
pub fn permute(&self, order: &[usize]) -> Self
Returns self with its axes reordered by order as a view over the
same buffer.
§Panics
Panics if order is not a permutation of 0..rank.
Sourcepub fn narrow(&self, axis: usize, start: usize, len: usize) -> Self
pub fn narrow(&self, axis: usize, start: usize, len: usize) -> Self
Returns the window of len elements from start along axis as a
view over the same buffer.
§Panics
Panics if axis is out of rank, len is zero (tensors cannot be
empty), or start + len overflows or exceeds the axis extent.
Sourcepub fn pad(&self, axis: usize, start: usize, full_extent: usize) -> Self
pub fn pad(&self, axis: usize, start: usize, full_extent: usize) -> Self
Returns self placed at start .. along axis inside a tensor
whose axis has extent full_extent, with zeros elsewhere.
§Panics
Panics if axis is out of rank or the window overflows or exceeds
full_extent.
Sourcepub fn unfold(
&self,
axis: usize,
size: usize,
step: usize,
dilation: usize,
) -> Self
pub fn unfold( &self, axis: usize, size: usize, step: usize, dilation: usize, ) -> Self
Returns the sliding windows of self along axis as a strided
view over the same buffer: the axis becomes a (count, size)
pair where window w starts at w * step and takes every
dilation-th element. Overlapping windows alias elements
read-only, which immutability makes safe.
§Panics
Panics if axis is out of rank, size, step, or dilation
is zero, or the dilated window span dilation * (size - 1) + 1
overflows or exceeds the axis extent.
Sourcepub fn fold(
&self,
axis: usize,
size: usize,
step: usize,
dilation: usize,
extent: usize,
) -> Self
pub fn fold( &self, axis: usize, size: usize, step: usize, dilation: usize, extent: usize, ) -> Self
Returns the (count, size) window pair at axis, axis + 1
folded back onto an axis of extent: the adjoint of
unfold and its gradient rule.
The accumulation is output-centric: each source position sums, in window order, the window elements that were read from it, so the result is deterministic under any evaluation strategy. Positions no window reaches fold to zero.
§Panics
Panics if axis + 1 is out of rank, size, step, or
dilation is zero, the dilated window span overflows or exceeds
extent, or the shape at axis, axis + 1 disagrees with the
windows unfold would produce for extent.
Sourcepub fn windowed_patches(
&self,
kernel_height: usize,
kernel_width: usize,
stride: usize,
padding: usize,
) -> Self
pub fn windowed_patches( &self, kernel_height: usize, kernel_width: usize, stride: usize, padding: usize, ) -> Self
Returns the im2col matrix through a specialized patch fill:
contiguous runs of kernel_width copied per channel and kernel
row, zero runs where the padding window leaves the input, and no
per-element odometer arithmetic — the measured cost the fused
window-GEMM pattern exists to remove. Non-contiguous inputs and
non-dense storages take the composed reference path.
§Panics
Panics if self is not rank 4, stride is zero, or a kernel
window does not fit the padded extents.
Sourcepub fn gather(&self, selection: &Self) -> Self
pub fn gather(&self, selection: &Self) -> Self
Returns the rows of self selected by selection, a one-hot
[count, vocab] whose vocabulary must equal self’s first axis; the
result is [count, ...self.shape[1..]] with row i equal to
self’s row selection_index(i).
§Panics
Panics if selection is not a [count, vocab] selection, self has
no axes, or the vocabulary does not match self’s first axis.
Source§impl<Element: Elementary> Tensor<Element>
impl<Element: Elementary> Tensor<Element>
Sourcepub fn windowed_product(
&self,
kernel: &Self,
kernel_height: usize,
kernel_width: usize,
stride: usize,
padding: usize,
) -> Self
pub fn windowed_product( &self, kernel: &Self, kernel_height: usize, kernel_width: usize, stride: usize, padding: usize, ) -> Self
Returns the im2col product of self with the GEMM-shaped
kernel: the window rows of the padded, strided sliding
windows, matrix-multiplied in one call. It is the fused
executor behind the plan tier’s window-GEMM pattern; the
composed reference is
composed_windowed_patches
followed by the plain product.
Source§impl<E: Element + Emittable> Tensor<E>
impl<E: Element + Emittable> Tensor<E>
Sourcepub fn to_html(&self, theme: Theme) -> String
pub fn to_html(&self, theme: Theme) -> String
Renders the tensor as a self-contained HTML card: shape, element type, and extremes, then the values — an exact table while they are few, a chart once they are many.
Rendering is pure and deterministic for a given tensor and theme.
Sourcepub fn evcxr_display(&self)
pub fn evcxr_display(&self)
Displays the tensor when it is the last expression in an Evcxr cell.
Trait Implementations§
Source§impl<Element: Differentiable> Add for Tensor<Element>
impl<Element: Differentiable> Add for Tensor<Element>
Source§impl<Element: Clone + Display> Display for Tensor<Element>
Renders rank 0 as the bare element and higher ranks as nested
row-major bracket lists, so a scalar read prints as the number it
is.
impl<Element: Clone + Display> Display for Tensor<Element>
Renders rank 0 as the bare element and higher ranks as nested row-major bracket lists, so a scalar read prints as the number it is.
Source§impl<Element: Differentiable> Div for Tensor<Element>
impl<Element: Differentiable> Div for Tensor<Element>
Source§impl<E: Element> From<E> for Tensor<E>
The rank-0 conversion: one element becomes the tensor of shape
[]. It is what lets tape.parameter(0.0_f64) and payload
literals in operator position stay scalar-looking while the graph
is always tensors. The bound is the Element seam marker rather
than Differentiable so a tensor can never be mistaken for an
element of a deeper tensor by inference.
impl<E: Element> From<E> for Tensor<E>
The rank-0 conversion: one element becomes the tensor of shape
[]. It is what lets tape.parameter(0.0_f64) and payload
literals in operator position stay scalar-looking while the graph
is always tensors. The bound is the Element seam marker rather
than Differentiable so a tensor can never be mistaken for an
element of a deeper tensor by inference.
Source§impl<Element: Differentiable> Mul for Tensor<Element>
impl<Element: Differentiable> Mul for Tensor<Element>
Source§impl<Element: Differentiable> Neg for Tensor<Element>
impl<Element: Differentiable> Neg for Tensor<Element>
Source§impl<Element: PartialEq + Clone> PartialEq for Tensor<Element>
impl<Element: PartialEq + Clone> PartialEq for Tensor<Element>
Source§impl<E: Element> Recordable for Tensor<E>
The compute interpretation of the recordable vocabulary: every
rule operation is the inherent tensor method of the same name, so
running a derivative rule over tensors is running it over the
engine’s one payload.
impl<E: Element> Recordable for Tensor<E>
The compute interpretation of the recordable vocabulary: every rule operation is the inherent tensor method of the same name, so running a derivative rule over tensors is running it over the engine’s one payload.
Source§fn log1p(&self) -> Self
fn log1p(&self) -> Self
self,
accurate near zero.Source§fn erf_derivative(&self) -> Self
fn erf_derivative(&self) -> Self
self: the scaled Gaussian (2/sqrt(pi)) * e^(-x^2), the
operation erf’s derivative rule speaks.Source§fn powf(&self, exponent: Self) -> Self
fn powf(&self, exponent: Self) -> Self
self raised elementwise to the power of exponent.Source§fn step(&self, threshold: &Self) -> Self
fn step(&self, threshold: &Self) -> Self
self >= threshold:
the Heaviside step, ties answering one. It carries the
derivative of the maximum family.Source§fn matmul(&self, rhs: &Self) -> Self
fn matmul(&self, rhs: &Self) -> Self
self and rhs; ranks above
two multiply batched over identical leading axes.Source§fn broadcast(&self, shape: Shape) -> Self
fn broadcast(&self, shape: Shape) -> Self
shape.Source§fn broadcast_along(&self, axis: usize, extent: usize) -> Self
fn broadcast_along(&self, axis: usize, extent: usize) -> Self
self repeated along a new axis of extent inserted
at axis.Source§fn reshape(&self, shape: Shape) -> Self
fn reshape(&self, shape: Shape) -> Self
self reinterpreted with shape, preserving logical
row-major order.Source§fn permute(&self, order: &[usize]) -> Self
fn permute(&self, order: &[usize]) -> Self
self with its axes reordered so that axis i of the
result takes axis order[i] of self.Source§fn narrow(&self, axis: usize, start: usize, len: usize) -> Self
fn narrow(&self, axis: usize, start: usize, len: usize) -> Self
len elements from start along axis.Source§fn pad(&self, axis: usize, start: usize, full_extent: usize) -> Self
fn pad(&self, axis: usize, start: usize, full_extent: usize) -> Self
self placed into zeros whose axis has extent
full_extent, at start ..: the adjoint of
narrow.Source§fn unfold(&self, axis: usize, size: usize, step: usize, dilation: usize) -> Self
fn unfold(&self, axis: usize, size: usize, step: usize, dilation: usize) -> Self
self along axis: the axis
becomes a (count, size) pair.Source§fn fold(
&self,
axis: usize,
size: usize,
step: usize,
dilation: usize,
extent: usize,
) -> Self
fn fold( &self, axis: usize, size: usize, step: usize, dilation: usize, extent: usize, ) -> Self
(count, size) window pair at axis, axis + 1
folded back onto an axis of extent: the adjoint of
unfold.