Reductions #
Fold, sum/product/mean/variance, axis reductions, and last-axis reductions.
Left fold over all tensor elements.
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.
Right fold over all tensor elements.
Instances For
Sum all elements of a tensor.
Instances For
The sum of a scalar tensor is its value.
Product of all elements of a tensor.
Instances For
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
Count the number of scalar entries in a tensor by folding; see countSpec_eq_size.
Instances For
Counting the entries of a tensor returns its static size.
true if any entry satisfies p.
Instances For
true if all entries satisfy p.
Instances For
Dot product: $\sum_i a_i b_i$.
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 has mean zero: the sum is divided by meanDenominator, which is one when the
shape has no entries.
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. Only reductions that
select an element, such as reduceMin and reduceMean, require a nonempty axis.
Instances For
Sum-reduction along a given axis.
The computation does not need the axis to be nonempty (an empty axis sums to zero, see
reduceDim). The evidence is kept so that reduceSum has the same calling convention as
reduceMean, reduceMin, and reduceMax; dropping it would change the signature of every layer
and model that threads nonemptiness evidence through its own arguments. Use reduceDim sumSpec
to sum along an axis without evidence.
Instances For
Product-reduction along a given axis. An empty axis multiplies to one.
Instances For
Mean-reduction along a given axis.
Instances For
Sum of squares reduced along an axis (helper for variance).
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
Min-reduction along a given axis.
Instances For
Instances For
Max-reduction along a given axis.