Reductions #
Fold, sum/product/mean/variance, axis reductions, and last-axis reductions.
Left fold over all tensor elements.
Instances For
Right fold over all tensor elements.
Instances For
Count the number of scalar entries in a tensor (= Spec.Shape.size).
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.
Instances For
Output shape after summing along axis (drops that dimension).
Instances For
simp lemma: dropping axis 1 from a 2D (nQ+1)×(nK+1) shape yields (nQ+1).
simp lemma: dropping axis 1 from a 2D nQ×nK shape yields nQ.
simp lemma: dropping axis 3 from a 4D b×h×w×c shape yields b×h×w.
simp lemma: dropping axis 0 from a positive .dim (n+1) s yields s.
simp lemma: dropping axis k+1 recurses into the tail shape.
simp lemma: dropping axis 0 from a 2D (kH+1)×(kW+1) yields (kW+1).
simp lemma: dropping axis 0 from .dim n inner yields inner (even when n=0).
Reflexive broadcast proof (s can broadcast to itself).
Instances For
Build a broadcast proof from the reduced shape back to the original shape.
We use this when a backward pass computes something in the reduced shape (e.g. a mean/variance) and we need to broadcast it back to match the original tensor shape.
Instances For
Reduce a tensor of shape (n, innerShape) by applying f across the first axis.
This is the basic “reduce over axis 0” primitive that we reuse to implement broadcast-adjoints and multi-axis reducers.
Instances For
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 broadcasted dimensions by summing.
Instances For
Generic reduction along a (provably reducible) axis.
reduce_dim f axis x applies f to the slices along axis, and returns a tensor whose shape is
shape_after_sum s axis (i.e. that axis is dropped).
Instances For
Sum-reduction along a given axis.
Instances For
Sum-reduction along axis, with axis validity inferred via valid_axis_inst.
Instances For
Product-reduction along a given axis.
Instances For
Product-reduction along axis when you already have a valid_axis proof.
Instances For
Mean-reduction along a given axis.
Instances For
Mean-reduction along axis, with axis validity provided as a typeclass argument.
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
Variance-reduction along axis, with axis validity provided as a typeclass argument.
Instances For
Min-reduction along a given axis.
Instances For
Max-reduction along a given axis.
Instances For
Max-reduction along axis, with axis validity inferred via valid_axis_inst.
Instances For
Reduce along the last axis of s (i.e. axis rank s - 1).
Instances For
Like reduce_last_dim, but infers axis validity via valid_axis_inst.
Instances For
Mean-reduce along the last axis.
Instances For
Sum-reduce along the last axis of a 2D tensor (seqLen, embedDim).
Instances For
Product-reduce along the last axis of a 2D tensor (seqLen, embedDim).
Instances For
Max-reduce along the last axis.
Instances For
Min-reduce along the last axis.
Instances For
Mean-reduce along the last axis (with axis validity as a typeclass argument).
Instances For
Mean-reduce along the last axis, specialized for proofs that assume well-formedness.
Instances For
Sum-reduce along the last axis (with axis validity inferred via valid_axis_inst).