TorchLean API

NN.Proofs.Autograd.Tape.Ops.Norm.MatrixEntries

Entrywise formulas for row-normalization tensors #

The normalization specs are written with reduceMean, reduceVar, broadcastAfterSum, broadcastTo, and pointwise tensor arithmetic on [m, n] matrices. This file records what each of those operations does to a single entry get2 t i j, and how tensorToVec indexes a matrix, so that the LayerNorm and BatchNorm proofs can work with plain real sums.

Matrix vectorization #

tensorToVec of a matrix at the flattened index (i, j) is the matrix entry.

Every flattened matrix index is idxMN of its row and column.

theorem Proofs.Autograd.Norm.vec_ext_idxMN {m n : } {u v : Vec (TapeNodes.Matmul.matSize m n)} (h : ∀ (i : Fin m) (j : Fin n), u.ofLp (TapeNodes.Matmul.idxMN i j) = v.ofLp (TapeNodes.Matmul.idxMN i j)) :
u = v

Two flattened matrices agree once they agree at every idxMN i j.

Vector and matrix entries of pointwise operations #

Entries of a filled vector.

Entries of a filled matrix.

Entries of a row broadcast: every column of row i reads the vector entry i.

Entries of a column broadcast: every row reads the vector entry j.

Scalar entry of a pointwise binary operation on rank-zero tensors.

theorem Proofs.Autograd.Norm.item_reduceVar_zero {n : } (row : TorchLean.Tensor [n]) (h : Spec.Shape.NonemptyAxis 0 (Spec.Shape.dim n Spec.Shape.scalar)) :
(TorchLean.Tensor.reduceVar 0 row h).item = (∑ j : Fin n, (row.getScalar j - (∑ k : Fin n, row.getScalar k) / n) * (row.getScalar j - (∑ k : Fin n, row.getScalar k) / n)) / n

Population variance of a vector, as the scalar entry of reduceVar 0.

theorem Proofs.Autograd.Norm.getScalar_reduceVar_one {m n : } (x : TorchLean.Tensor [m, n]) (h : Spec.Shape.NonemptyAxis 1 (Spec.Shape.dim m (Spec.Shape.dim n Spec.Shape.scalar))) (i : Fin m) :
(TorchLean.Tensor.reduceVar 1 x h).getScalar i = (∑ j : Fin n, (Spec.get2 x i j - (∑ k : Fin n, Spec.get2 x i k) / n) * (Spec.get2 x i j - (∑ k : Fin n, Spec.get2 x i k) / n)) / n

Row variances along the last axis (population variance of each row).

Vectors #

tensorToVec of a rank-one tensor reads the corresponding scalar entry.

Pointwise operations on matrices and vectors #

theorem Proofs.Autograd.Norm.get2_addSpec {m n : } (a b : TorchLean.Tensor [m, n]) (i : Fin m) (j : Fin n) :
Spec.get2 (a.addSpec b) i j = Spec.get2 a i j + Spec.get2 b i j

Entries of a matrix sum.

theorem Proofs.Autograd.Norm.get2_subSpec {m n : } (a b : TorchLean.Tensor [m, n]) (i : Fin m) (j : Fin n) :
Spec.get2 (a.subSpec b) i j = Spec.get2 a i j - Spec.get2 b i j

Entries of a matrix difference.

theorem Proofs.Autograd.Norm.get2_mulSpec {m n : } (a b : TorchLean.Tensor [m, n]) (i : Fin m) (j : Fin n) :
Spec.get2 (a.mulSpec b) i j = Spec.get2 a i j * Spec.get2 b i j

Entries of a pointwise matrix product.

theorem Proofs.Autograd.Norm.get2_divSpec {m n : } (a b : TorchLean.Tensor [m, n]) (i : Fin m) (j : Fin n) :
Spec.get2 (a.divSpec b) i j = Spec.get2 a i j / Spec.get2 b i j

Entries of a pointwise matrix quotient.

Entries of a vector sum.

Entries of a pointwise vector quotient.

Entries of a pointwise vector maximum.

Entries of the clamped square root.

Row statistics of a spec matrix #

noncomputable def Proofs.Autograd.Norm.rowMeanE {m n : } (x : TorchLean.Tensor [m, n]) (i : Fin m) :

Mean of row i.

Instances For
    noncomputable def Proofs.Autograd.Norm.rowVarE {m n : } (x : TorchLean.Tensor [m, n]) (i : Fin m) :

    Population variance of row i.

    Instances For

      The row variance is a mean of squares.

      theorem Proofs.Autograd.Norm.sum_sub_rowMeanE {m n : } (hn : 0 < n) (x : TorchLean.Tensor [m, n]) (i : Fin m) :
      j : Fin n, (Spec.get2 x i j - rowMeanE x i) = 0

      Centered row entries sum to zero.

      Spec.layerNorm entrywise #

      The feature axis of an [m, n] matrix is nonempty when 0 < n.

      Row means of the LayerNorm input.

      Instances For

        Centered LayerNorm input.

        Instances For

          Clamped LayerNorm standard deviation sqrt (max (max var 0 + ε) 0).

          Instances For

            Spec.layerNorm with its axis and broadcast evidence spelled out.

            Instances For
              theorem Proofs.Autograd.Norm.layerNorm_eq_layerNormMat {m n : } (hm : 0 < m) (hn : 0 < n) (ε : ) (x : TorchLean.Tensor [m, n]) (gamma beta : TorchLean.Tensor [n]) :
              Spec.layerNorm x gamma beta hm hn ε = layerNormMat x gamma beta ε

              Spec.layerNorm is layerNormMat by unfolding.

              Entries of the LayerNorm row means.

              Entries of the centered LayerNorm input.

              theorem Proofs.Autograd.Norm.getScalar_lnStd {m n : } (hn : 0 < n) (x : TorchLean.Tensor [m, n]) (h1 : Spec.Shape.NonemptyAxis 1 (Spec.Shape.dim m (Spec.Shape.dim n Spec.Shape.scalar))) (ε : ) (i : Fin m) :
              (lnStd x h1 ε).getScalar i = (max (rowVarE x i + ε) 0)

              Entries of the LayerNorm standard deviation: the clamp on the variance is inactive.

              theorem Proofs.Autograd.Norm.get2_layerNormMat {m n : } (hn : 0 < n) (x : TorchLean.Tensor [m, n]) (gamma beta : TorchLean.Tensor [n]) (h1 : Spec.Shape.NonemptyAxis 1 (Spec.Shape.dim m (Spec.Shape.dim n Spec.Shape.scalar))) (ε : ) (i : Fin m) (j : Fin n) :
              Spec.get2 (layerNormMat x gamma beta h1 ε) i j = (Spec.get2 x i j - rowMeanE x i) / (max (rowVarE x i + ε) 0) * gamma.getScalar j + beta.getScalar j

              Entries of layerNormMat.

              theorem Proofs.Autograd.Norm.get2_layerNorm {m n : } (hm : 0 < m) (hn : 0 < n) (ε : ) (x : TorchLean.Tensor [m, n]) (gamma beta : TorchLean.Tensor [n]) (i : Fin m) (j : Fin n) :
              Spec.get2 (Spec.layerNorm x gamma beta hm hn ε) i j = (Spec.get2 x i j - rowMeanE x i) / (max (rowVarE x i + ε) 0) * gamma.getScalar j + beta.getScalar j

              Entries of Spec.layerNorm: each row is centered, divided by the clamped standard deviation of that row, then scaled and shifted by the per-column parameters.