TorchLean API

NN.Proofs.Autograd.Tape.Ops.Norm.LayerNormEval

Evaluating the LayerNorm graph #

Closed forms for every saved intermediate of layerNormGraph. Each val* definition is the value the corresponding node computes from the packed input [X, gamma, beta], and each get_* lemma identifies the block of Graph.evalVec at that stage with its closed form.

Two consequences drive the calculus: the per-row variance block is a mean of squares, so it is nonnegative, and therefore var + ε is positive and the clamped square root is nonzero as soon as 0 < ε. Those two facts are exactly the domain hypotheses of the LayerNorm graph theorem.

Flattened size of a length-k vector shape.

Index of X in the bare LayerNorm context.

Instances For

    Index of gamma in the bare LayerNorm context.

    Instances For

      Index of beta in the bare LayerNorm context.

      Instances For

        Closed forms of the intermediates #

        noncomputable def Proofs.Autograd.LayerNorm.valX {m n : } (xV : CtxVec (ΓLN m n)) :

        The input matrix block.

        Instances For
          noncomputable def Proofs.Autograd.LayerNorm.valGamma {m n : } (xV : CtxVec (ΓLN m n)) :
          Vec n

          The scale vector block.

          Instances For
            noncomputable def Proofs.Autograd.LayerNorm.valBeta {m n : } (xV : CtxVec (ΓLN m n)) :
            Vec n

            The shift vector block.

            Instances For
              noncomputable def Proofs.Autograd.LayerNorm.valMean {m n : } (xV : CtxVec (ΓLN m n)) :

              Row means of X.

              Instances For
                noncomputable def Proofs.Autograd.LayerNorm.valMeanB {m n : } (xV : CtxVec (ΓLN m n)) :

                Row means broadcast back to the matrix shape.

                Instances For
                  noncomputable def Proofs.Autograd.LayerNorm.valCentered {m n : } (xV : CtxVec (ΓLN m n)) :

                  Centered input X - mean_b.

                  Instances For
                    noncomputable def Proofs.Autograd.LayerNorm.valCenteredSq {m n : } (xV : CtxVec (ΓLN m n)) :

                    Squared centered input.

                    Instances For
                      noncomputable def Proofs.Autograd.LayerNorm.valVar {m n : } (xV : CtxVec (ΓLN m n)) :

                      Row variances (means of the squared centered entries).

                      Instances For
                        noncomputable def Proofs.Autograd.LayerNorm.valVarEps {m n : } (xV : CtxVec (ΓLN m n)) (ε : ) :

                        Row variances shifted by ε.

                        Instances For
                          noncomputable def Proofs.Autograd.LayerNorm.valStd {m n : } (xV : CtxVec (ΓLN m n)) (ε : ) :

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

                          Instances For
                            noncomputable def Proofs.Autograd.LayerNorm.valInvStd {m n : } (xV : CtxVec (ΓLN m n)) (ε : ) :

                            Inverse standard deviation.

                            Instances For
                              noncomputable def Proofs.Autograd.LayerNorm.valInvStdB {m n : } (xV : CtxVec (ΓLN m n)) (ε : ) :

                              Inverse standard deviation broadcast back to the matrix shape.

                              Instances For
                                noncomputable def Proofs.Autograd.LayerNorm.valNorm {m n : } (xV : CtxVec (ΓLN m n)) (ε : ) :

                                Normalized input centered ⊙ inv_std_b.

                                Instances For
                                  noncomputable def Proofs.Autograd.LayerNorm.valGammaB {m n : } (xV : CtxVec (ΓLN m n)) :

                                  Scale vector broadcast over rows.

                                  Instances For
                                    noncomputable def Proofs.Autograd.LayerNorm.valScaled {m n : } (xV : CtxVec (ΓLN m n)) (ε : ) :

                                    Scaled normalized input.

                                    Instances For
                                      noncomputable def Proofs.Autograd.LayerNorm.valBetaB {m n : } (xV : CtxVec (ΓLN m n)) :

                                      Shift vector broadcast over rows.

                                      Instances For
                                        noncomputable def Proofs.Autograd.LayerNorm.valY {m n : } (xV : CtxVec (ΓLN m n)) (ε : ) :

                                        LayerNorm output scaled + beta_b.

                                        Instances For

                                          Stage evaluations #

                                          Stage g1: the mean block.

                                          Stage g2: the broadcast mean block.

                                          Stage g3: the centered block.

                                          Stage g4: the squared centered block.

                                          Stage g5: the variance block.

                                          Stage layerNormPrefix6: the shifted variance block.

                                          Stage layerNormPrefix7: the standard deviation block.

                                          theorem Proofs.Autograd.LayerNorm.get_g8 {m n : } (ε : ) (xV : CtxVec (ΓLN m n)) :

                                          Stage g8: the inverse standard deviation block.

                                          theorem Proofs.Autograd.LayerNorm.get_g9 {m n : } (ε : ) (xV : CtxVec (ΓLN m n)) :

                                          Stage g9: the broadcast inverse standard deviation block.

                                          The centered block is still available at stage g9.

                                          theorem Proofs.Autograd.LayerNorm.get_g10 {m n : } (ε : ) (xV : CtxVec (ΓLN m n)) :

                                          Stage g10: the normalized block.

                                          theorem Proofs.Autograd.LayerNorm.get_g11 {m n : } (ε : ) (xV : CtxVec (ΓLN m n)) :

                                          Stage g11: the broadcast scale block.

                                          theorem Proofs.Autograd.LayerNorm.get_norm11 {m n : } (ε : ) (xV : CtxVec (ΓLN m n)) :

                                          The normalized block is still available at stage g11.

                                          theorem Proofs.Autograd.LayerNorm.get_g12 {m n : } (ε : ) (xV : CtxVec (ΓLN m n)) :

                                          Stage g12: the scaled block.

                                          theorem Proofs.Autograd.LayerNorm.get_g13 {m n : } (ε : ) (xV : CtxVec (ΓLN m n)) :

                                          Stage g13: the broadcast shift block.

                                          The scaled block is still available at stage g13.

                                          theorem Proofs.Autograd.LayerNorm.get_idxY {m n : } (ε : ) (xV : CtxVec (ΓLN m n)) :

                                          The output block of the full LayerNorm graph.

                                          Positivity of the denominators #

                                          Coordinates of the row-mean map.

                                          theorem Proofs.Autograd.LayerNorm.valVar_nonneg {m n : } (xV : CtxVec (ΓLN m n)) (i : Fin (VecShape m).size) :
                                          0 (valVar xV).ofLp i

                                          The variance block is a mean of squares, hence nonnegative.

                                          theorem Proofs.Autograd.LayerNorm.valVarEps_pos {m n : } {ε : } ( : 0 < ε) (xV : CtxVec (ΓLN m n)) (i : Fin (VecShape m).size) :
                                          0 < (valVarEps xV ε).ofLp i

                                          With positive ε, every shifted variance is positive.

                                          theorem Proofs.Autograd.LayerNorm.valStd_pos {m n : } {ε : } ( : 0 < ε) (xV : CtxVec (ΓLN m n)) (i : Fin (VecShape m).size) :
                                          0 < (valStd xV ε).ofLp i

                                          With positive ε, every clamped standard deviation is positive.

                                          theorem Proofs.Autograd.LayerNorm.varEps_pos_of_eps_pos {m n : } {ε : } ( : 0 < ε) (xV : CtxVec (ΓLN m n)) (i : Fin (VecShape m).size) :

                                          The LayerNorm sqrt domain hypothesis follows from 0 < ε.

                                          theorem Proofs.Autograd.LayerNorm.std_ne_zero_of_eps_pos {m n : } {ε : } ( : 0 < ε) (xV : CtxVec (ΓLN m n)) (i : Fin (VecShape m).size) :

                                          The LayerNorm inv domain hypothesis follows from 0 < ε.