TorchLean API

NN.Proofs.Autograd.Tape.Ops.Norm.RowNormalization

Calculus of row normalization #

LayerNorm normalizes every row of a [seqLen, embedDim] matrix; the flattened form of BatchNorm normalizes every row of a [channels, positions] matrix. Both share the map

x ↦ (x - mean(x)) / sqrt (var(x) + ε)

applied to each row, where mean and var are the population statistics of that row. This file proves that map is differentiable on flattened matrices (for 0 < ε) and identifies its derivative with the closed form

dx ↦ invStd * (dx - mean(dx) - xhat * mean(dx * xhat))

used by the normalization JVPs.

noncomputable def Proofs.Autograd.RowNorm.rowMean {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) :

Mean of row i of a flattened m × n matrix.

Instances For
    noncomputable def Proofs.Autograd.RowNorm.centered {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) (j : Fin n) :

    Centered entry (i, j).

    Instances For
      noncomputable def Proofs.Autograd.RowNorm.rowVar {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) :

      Population variance of row i.

      Instances For
        noncomputable def Proofs.Autograd.RowNorm.invStd {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (ε : ) (i : Fin m) :

        Inverse standard deviation 1 / sqrt (var + ε) of row i.

        Instances For
          noncomputable def Proofs.Autograd.RowNorm.nrm {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (ε : ) (i : Fin m) (j : Fin n) :

          Normalized entry (i, j).

          Instances For
            noncomputable def Proofs.Autograd.RowNorm.nrmJvp {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (ε : ) (dX : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) (j : Fin n) :

            Closed-form differential of the normalized entry (i, j) in the direction dX.

            Instances For

              The row variance is a mean of squares.

              theorem Proofs.Autograd.RowNorm.rowVar_add_pos {m n : } {ε : } ( : 0 < ε) (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) :
              0 < rowVar X i + ε

              var + ε is positive when ε is.

              theorem Proofs.Autograd.RowNorm.sum_centered {m n : } (hn : 0 < n) (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) :
              j : Fin n, centered X i j = 0

              The centered entries of a row sum to zero.

              theorem Proofs.Autograd.RowNorm.sum_centered_mul_sub {m n : } (hn : 0 < n) (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) (d : Fin n) (a : ) :
              j : Fin n, centered X i j * (d j - a) = j : Fin n, centered X i j * d j

              The centered row is orthogonal to constants, so ∑ c_j (d_j - a) = ∑ c_j d_j.

              Derivative building blocks #

              Coordinate projection as a continuous linear functional on flattened matrices.

              Instances For
                @[simp]

                The projection functional reads coordinate k.

                Derivative of the row mean.

                Instances For
                  @[simp]
                  theorem Proofs.Autograd.RowNorm.meanD_apply {m n : } (i : Fin m) (dX : Vec (TapeNodes.Matmul.matSize m n)) :
                  (meanD i) dX = rowMean dX i

                  The row-mean derivative is the row mean of the perturbation, since the mean is already linear.

                  noncomputable def Proofs.Autograd.RowNorm.centD {m n : } (i : Fin m) (j : Fin n) :

                  Derivative of a centered entry.

                  Instances For
                    @[simp]
                    theorem Proofs.Autograd.RowNorm.centD_apply {m n : } (i : Fin m) (j : Fin n) (dX : Vec (TapeNodes.Matmul.matSize m n)) :
                    (centD i j) dX = dX.ofLp (TapeNodes.Matmul.idxMN i j) - rowMean dX i

                    The centering derivative subtracts the perturbation's own row mean.

                    Centering is linear too, so its derivative is itself; the nonlinearity in row normalization enters only through the variance and the square root below.

                    Derivative of the row variance at X.

                    Instances For
                      theorem Proofs.Autograd.RowNorm.varD_apply {m n : } (hn : 0 < n) (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) (dX : Vec (TapeNodes.Matmul.matSize m n)) :
                      (varD X i) dX = (2 * k : Fin n, centered X i k * dX.ofLp (TapeNodes.Matmul.idxMN i k)) / n

                      The variance differential only sees the centered direction.

                      Derivative of sqrt (var + ε) at X.

                      Instances For
                        noncomputable def Proofs.Autograd.RowNorm.invD {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (ε : ) (i : Fin m) :

                        Derivative of the inverse standard deviation at X.

                        Instances For
                          noncomputable def Proofs.Autograd.RowNorm.nrmD {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (ε : ) (i : Fin m) (j : Fin n) :

                          Derivative of the normalized entry (i, j) at X.

                          Instances For

                            Coordinates of a flattened matrix are differentiable.

                            Derivative of the row mean.

                            theorem Proofs.Autograd.RowNorm.hasFDerivAt_centered {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) (j : Fin n) :
                            HasFDerivAt (fun (Y : Vec (TapeNodes.Matmul.matSize m n)) => centered Y i j) (centD i j) X

                            Derivative of a centered entry.

                            Derivative of the row variance.

                            theorem Proofs.Autograd.RowNorm.hasFDerivAt_invStd {m n : } {ε : } ( : 0 < ε) (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) :
                            HasFDerivAt (fun (Y : Vec (TapeNodes.Matmul.matSize m n)) => invStd Y ε i) (invD X ε i) X

                            Derivative of the inverse standard deviation, for positive ε.

                            theorem Proofs.Autograd.RowNorm.hasFDerivAt_nrm {m n : } {ε : } ( : 0 < ε) (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) (j : Fin n) :
                            HasFDerivAt (fun (Y : Vec (TapeNodes.Matmul.matSize m n)) => nrm Y ε i j) (nrmD X ε i j) X

                            The normalized entry is differentiable for positive ε.

                            theorem Proofs.Autograd.RowNorm.nrmD_apply {m n : } (hn : 0 < n) {ε : } ( : 0 < ε) (X : Vec (TapeNodes.Matmul.matSize m n)) (i : Fin m) (j : Fin n) (dX : Vec (TapeNodes.Matmul.matSize m n)) :
                            (nrmD X ε i j) dX = nrmJvp X ε dX i j

                            The derivative of the normalized entry is the closed-form JVP.

                            Whole-matrix normalization #

                            Row index of a flattened matrix coordinate.

                            Instances For

                              Column index of a flattened matrix coordinate.

                              Instances For

                                Every flattened coordinate is idxMN of its row and column.

                                Row of the flattened coordinate idxMN i j.

                                Column of the flattened coordinate idxMN i j.

                                theorem Proofs.Autograd.RowNorm.sum_vecSize {n : } (f : Fin (TapeNodes.Matmul.vecSize n)) :
                                j : Fin (TapeNodes.Matmul.vecSize n), f j = j : Fin n, f (Fin.cast j)

                                Reindex a sum over Fin (vecSize n) as a sum over Fin n.

                                The tape row-mean map computes rowMean.

                                The tape row-broadcast map at idxMN i j reads entry i.

                                The tape column-broadcast map at idxMN i j reads entry j.

                                Normalize every row of a flattened matrix.

                                Instances For
                                  @[simp]
                                  theorem Proofs.Autograd.RowNorm.nrmVec_apply {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (ε : ) (ip : Fin (TapeNodes.Matmul.matSize m n)) :
                                  (nrmVec X ε).ofLp ip = nrm X ε (rowOf ip) (colOf ip)

                                  Coordinate ip of the normalized matrix is the normalized entry at its row and column.

                                  Closed-form JVP of nrmVec at X, packaged as a continuous linear map.

                                  Instances For
                                    @[simp]
                                    theorem Proofs.Autograd.RowNorm.nrmJvpCLM_apply {m n : } (X : Vec (TapeNodes.Matmul.matSize m n)) (ε : ) (dX : Vec (TapeNodes.Matmul.matSize m n)) (ip : Fin (TapeNodes.Matmul.matSize m n)) :
                                    ((nrmJvpCLM X ε) dX).ofLp ip = nrmJvp X ε dX (rowOf ip) (colOf ip)

                                    The bundled JVP agrees with the closed-form nrmJvp coordinatewise.

                                    Writing the derivative down in closed form and then proving HasFDerivAt against it, rather than deriving it compositionally, is what keeps the ε guard visible: the formula is only the derivative because ε > 0 keeps the denominator away from zero.

                                    theorem Proofs.Autograd.RowNorm.hasFDerivAt_nrmVec {m n : } (hn : 0 < n) {ε : } ( : 0 < ε) (X : Vec (TapeNodes.Matmul.matSize m n)) :
                                    HasFDerivAt (fun (Y : Vec (TapeNodes.Matmul.matSize m n)) => nrmVec Y ε) (nrmJvpCLM X ε) X

                                    Row normalization of a flattened matrix is differentiable for positive ε, with the closed-form JVP as derivative.