Reductions #
Fold, sum, mean, variance, and axis reductions.
Left fold over all tensor elements.
Instances For
Largest absolute entry, or zero for an empty tensor. Self-unequal values are propagated:
for IEEE scalars this prevents a NaN from being hidden by the scalar max operation.
Instances For
Proof-facing fold over a family of equal-shaped slices, starting at slice index.
The public foldlSpec traverses the packed buffer directly. This helper retains the
slice-by-slice recursion useful in shape-inductive proofs without changing the executable path.
Instances For
Starting the proof-facing slice fold at zero is the ordinary finite fold.
Folding a tensor with a leading dimension folds its slices in row-major order.
Sum all elements of a tensor.
Instances For
The sum of a scalar tensor is its value.
Flattened row-major index of the first maximal entry in a nonempty tensor.
The explicit nonemptiness hypothesis keeps the result total without inventing an index for an empty tensor. Ties retain the smaller flattened index, matching a left-to-right traversal.
Instances For
Instances For
true if all entries satisfy p.
Instances For
Dot product in row-major order, with a zero-seeded left fold.
Instances For
Denominator of a totalized mean: the element count, or one for an empty shape.
Dividing by the raw size would make the mean of an empty tensor depend on the scalar type's
convention for x / 0. Using one instead makes the empty mean equal to the (zero) sum, the same
convention as the loss layer and TorchLean.Tensor.mean.
Instances For
On a nonempty shape the mean denominator is the element count.
On an empty shape the mean denominator is one.
The mean denominator is never zero.
Mean of all elements (treats nested dims as one big collection).
An empty tensor evaluates as 0 / (1 : α): meanDenominator uses one when the shape has no
entries. This is zero under ordinary scalar arithmetic.
Instances For
Mean squared difference between two equally shaped tensors.
Subtraction and squaring are pointwise. The final mean uses row-major accumulation and the totalized denominator, including for empty shapes.
Instances For
Variance of all scalar leaves (population variance, divides by the total leaf count).
For a higher-rank tensor this centers every entry around the tensor-wide mean. In particular, it
does not collapse each outer slice to its mean before measuring dispersion. An empty tensor has
variance zero, using the same guarded denominator as meanSpec.
Instances For
Output shape after summing along axis (drops that dimension).
Instances For
Shape obtained by replacing the selected axis with a singleton dimension.
Instances For
Keeping the reduced axis as a singleton does not change the element count.
Keeping the reduced axis as a singleton preserves the rank.
Dropping axis zero from .dim n inner yields inner, including when n = 0.
simp lemma: dropping axis k+1 recurses into the tail shape.
A keep-dimension reduction shape broadcasts back to its input shape.
Reinsert an axis dropped by shapeAfterSum, repeating the reduced tensor along that axis.
This is deliberately separate from broadcastTo. Generic broadcasting aligns dimensions from the
right, whereas a reduction backward pass must restore the exact axis that was removed.
Instances For
Reduce a tensor by applying f across its outer axis.
This is the basic “reduce over axis 0” primitive that we reuse to implement broadcast-adjoints and multi-axis reducers.
Instances For
Reducing a vector along its only axis applies the scalar aggregator to that vector.
Reduce a gradient from a broadcast target shape back to the original input shape.
This is the adjoint of broadcastTo for sum-reduction: broadcast duplicates values, so the
backward pass sums contributions across broadcasted dimensions.
PyTorch analogy: this is the logic behind "sum over broadcasted dimensions" that happens in
autograd for expand + elementwise ops.
Adjoint of broadcastTo under sum-reduction: collapse broadcast axes by summing.
Target axes that only raise the rank are summed away. A source axis of extent one that was expanded to a larger target extent is summed and reinserted as a singleton axis. Axes with equal extents pass through slice by slice. The recursion is on the shapes alone, so the result does not depend on how the broadcast relation was proved.
Instances For
Recursive evaluator underlying reduceDim; kept separate so proofs can use its equations.
Instances For
There is no axis to reduce in a scalar tensor, so the tensor is returned unchanged.
Reducing axis zero is the outer-axis reduction.
Reducing a deeper axis pushes the reduction into every outer slice.
These three equations are the whole computation rule for reduceDimCore, and stating them as simp
lemmas is what lets a reduction on a literal shape unfold without ever mentioning the recursion.
Generic reduction along an axis.
reduceDim f axis x applies f to the slices along axis, and returns a tensor whose shape is
shapeAfterSum s axis (that axis is dropped). The axis may be empty: f then receives empty
slices and returns whatever it does on them, such as zero for sumSpec. The typed reduceMax
requires an initial entry, and reduceMean divides by the axis extent; both require nonemptiness.
Instances For
Sum-reduction along a given axis.
The evidence matches IR.OpContracts.checkReductionAxis, which rejects invalid axes and
in-bounds axes of extent zero during both inference and evaluation. The underlying computation
also supports empty sums: use reduceDim sumSpec when that broader domain is intended.
Instances For
Mean-reduction along a given axis.
Instances For
Variance-reduction along a given axis (population variance, divides by n).
The reduced axis is centered first and squared second. This two-pass arrangement avoids the catastrophic cancellation of $\mathbb{E}[X^2]-\mathbb{E}[X]^2$ when values are large but tightly clustered.
Instances For
Max-reduction along a given axis.