TorchLean API

NN.Proofs.Autograd.Tape.Ops.Norm.LayerNormGraph

LayerNorm graph #

The explicit SSA graph used by the LayerNorm proofs: a seqLen × embedDim input is normalized across its last axis, the row statistics are broadcast back over each token, and the affine parameters gamma/beta are broadcast over the sequence axis. The graph is written as an explicit snoc chain so that every prefix has a name; the calculus proofs in NN.Proofs.Autograd.Tape.Ops.Norm.LayerNorm and the evaluation lemmas in NN.Proofs.Autograd.Tape.Ops.Norm.LayerNormEval refer to these prefixes directly.

The nodes are the shape-generic tape nodes: row mean, row and column broadcast, elementwise arithmetic, the clamped square root sqrt (max x 0), and inversion.

@[reducible, inline]

Matrix shape m×n.

Instances For
    @[reducible, inline]

    Rank-one tensor shape k.

    Instances For
      @[reducible, inline]

      Input context shapes: [X, gamma, beta] for layer norm over the last axis.

      Instances For
        @[reducible, inline]

        First 6 intermediates in the LayerNorm computation (up to var_eps).

        Instances For
          @[reducible, inline]

          Prefix intermediates up to std (adds one more vector).

          Instances For
            @[reducible, inline]

            Full list of intermediates for the LayerNorm graph in this file.

            Instances For

              Index of the input matrix X in the base LayerNorm context ΓLN m n ++ ss.

              Instances For

                Index of the scale vector gamma in the base LayerNorm context ΓLN m n ++ ss.

                Instances For

                  Index of the shift vector beta in the base LayerNorm context ΓLN m n ++ ss.

                  Instances For
                    noncomputable def Proofs.Autograd.LayerNorm.nodeMean {m n : } :
                    Node (ΓLN m n) (VecShape m)

                    Mean over the last axis: mean : ℝ^{m×n} → ℝ^{m}.

                    Instances For
                      noncomputable def Proofs.Autograd.LayerNorm.g1 {m n : } :

                      Graph prefix producing [mean].

                      Instances For

                        Index of mean in the extended context ΓLN ++ [mean].

                        Instances For
                          noncomputable def Proofs.Autograd.LayerNorm.nodeMeanB {m n : } :

                          Broadcast mean back to m×n (row-wise).

                          Instances For
                            noncomputable def Proofs.Autograd.LayerNorm.g2 {m n : } :

                            Graph prefix producing [mean, mean_b].

                            Instances For

                              Index of mean_b in ΓLN ++ [mean, mean_b].

                              Instances For
                                noncomputable def Proofs.Autograd.LayerNorm.nodeCentered {m n : } :

                                Center: centered := X - mean_b.

                                Instances For
                                  noncomputable def Proofs.Autograd.LayerNorm.g3 {m n : } :

                                  Graph prefix producing [mean, mean_b, centered].

                                  Instances For

                                    Index of centered in ΓLN ++ [mean, mean_b, centered].

                                    Instances For

                                      Square centered: centered_sq := centered ⊙ centered.

                                      Instances For
                                        noncomputable def Proofs.Autograd.LayerNorm.g4 {m n : } :

                                        Graph prefix producing [mean, mean_b, centered, centered_sq].

                                        Instances For

                                          Index of centered_sq in the extended context.

                                          Instances For

                                            Variance per row: var := mean(centered_sq) producing a length-m vector.

                                            Instances For

                                              Graph prefix producing [mean, mean_b, centered, centered_sq, var].

                                              Instances For

                                                Index of var in the extended context.

                                                Instances For
                                                  noncomputable def Proofs.Autograd.LayerNorm.nodeVarEps {m n : } (ε : ) :

                                                  Add epsilon: var_eps := var + ε.

                                                  Instances For
                                                    noncomputable def Proofs.Autograd.LayerNorm.layerNormPrefix6 {m n : } (ε : ) :
                                                    Graph (ΓLN m n) (ssPrefix6 m n)

                                                    Graph prefix computing the first 6 intermediates (ssPrefix6).

                                                    Instances For

                                                      Index of var_eps in ΓLN ++ ssPrefix6.

                                                      Instances For
                                                        noncomputable def Proofs.Autograd.LayerNorm.nodeStd {m n : } :
                                                        Node (ΓLN m n ++ ssPrefix6 m n) (VecShape m)

                                                        Standard deviation: std := sqrt_clamp(var_eps).

                                                        This is where the development becomes pointwise: differentiability depends on the (clamped) input.

                                                        Instances For
                                                          noncomputable def Proofs.Autograd.LayerNorm.layerNormPrefix7 {m n : } (ε : ) :
                                                          Graph (ΓLN m n) (ssPrefix7 m n)

                                                          Graph prefix computing ssPrefix7 (adds std).

                                                          Instances For

                                                            Index of std in ΓLN ++ ssPrefix7.

                                                            Instances For
                                                              noncomputable def Proofs.Autograd.LayerNorm.nodeInvStd {m n : } :
                                                              Node (ΓLN m n ++ ssPrefix7 m n) (VecShape m)

                                                              Inverse standard deviation: inv_std := 1/std.

                                                              Instances For
                                                                noncomputable def Proofs.Autograd.LayerNorm.g8 {m n : } (ε : ) :

                                                                Graph prefix adding invStd.

                                                                Instances For

                                                                  Index of invStd in the extended context.

                                                                  Instances For
                                                                    noncomputable def Proofs.Autograd.LayerNorm.nodeInvStdB {m n : } :
                                                                    Node (ΓLN m n ++ (ssPrefix7 m n ++ [VecShape m])) (MatShape m n)

                                                                    Broadcast invStd back to m×n (row-wise).

                                                                    Instances For
                                                                      noncomputable def Proofs.Autograd.LayerNorm.g9 {m n : } (ε : ) :

                                                                      Graph prefix adding inv_std_b.

                                                                      Instances For

                                                                        Index of centered in the stage-g9 context.

                                                                        Instances For

                                                                          Index of inv_std_b in the stage-g9 context.

                                                                          Instances For
                                                                            noncomputable def Proofs.Autograd.LayerNorm.nodeNorm {m n : } :

                                                                            Node computing normalized := centered ⊙ inv_std_b.

                                                                            Instances For
                                                                              noncomputable def Proofs.Autograd.LayerNorm.g10 {m n : } (ε : ) :

                                                                              Graph prefix producing normalized := centered ⊙ inv_std_b.

                                                                              Instances For
                                                                                noncomputable def Proofs.Autograd.LayerNorm.nodeGammaB {m n : } :

                                                                                Broadcast gamma to m×n (column-wise).

                                                                                Instances For
                                                                                  noncomputable def Proofs.Autograd.LayerNorm.g11 {m n : } (ε : ) :

                                                                                  Graph prefix adding gamma_b.

                                                                                  Instances For

                                                                                    Index of normalized in the context at stage g11.

                                                                                    Instances For

                                                                                      Index of gamma_b at stage g11.

                                                                                      Instances For
                                                                                        noncomputable def Proofs.Autograd.LayerNorm.nodeScaled {m n : } :

                                                                                        Scale: scaled := normalized ⊙ gamma_b.

                                                                                        Instances For
                                                                                          noncomputable def Proofs.Autograd.LayerNorm.g12 {m n : } (ε : ) :

                                                                                          Graph prefix adding scaled.

                                                                                          Instances For

                                                                                            Broadcast beta to m×n (column-wise).

                                                                                            Instances For
                                                                                              noncomputable def Proofs.Autograd.LayerNorm.g13 {m n : } (ε : ) :

                                                                                              Graph prefix adding beta_b.

                                                                                              Instances For

                                                                                                Index of scaled at stage g13.

                                                                                                Instances For

                                                                                                  Index of beta_b at stage g13.

                                                                                                  Instances For
                                                                                                    noncomputable def Proofs.Autograd.LayerNorm.nodeY {m n : } :

                                                                                                    Output: y := scaled + beta_b.

                                                                                                    Instances For
                                                                                                      noncomputable def Proofs.Autograd.LayerNorm.layerNormGraph {m n : } (ε : ) :
                                                                                                      Graph (ΓLN m n) (ssLayerNorm m n)

                                                                                                      Full LayerNorm graph (as an explicit snoc chain).

                                                                                                      Instances For

                                                                                                        Index of the final LayerNorm output in ΓLN ++ ssLayerNorm.

                                                                                                        Instances For