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]

        LayerNorm intermediates through the epsilon-shifted variance.

        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.graphMean {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

                            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

                                  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

                                        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.graphVarEps {m n : ℕ} (ε : ℝ) :
                                                    Graph (ΓLN m n) (ssVarEps m n)

                                                    Graph prefix through the epsilon-shifted variance (ssVarEps).

                                                    Instances For

                                                      Index of var_eps in ΓLN ++ ssVarEps.

                                                      Instances For
                                                        noncomputable def Proofs.Autograd.LayerNorm.nodeStd {m n : ℕ} :
                                                        Node (ΓLN m n ++ ssVarEps 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.graphStd {m n : ℕ} (ε : ℝ) :
                                                          Graph (ΓLN m n) (ssStd m n)

                                                          Graph prefix computing ssStd (adds std).

                                                          Instances For

                                                            Index of std in ΓLN ++ ssStd.

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

                                                              Inverse standard deviation: inv_std := 1/std.

                                                              Instances For
                                                                noncomputable def Proofs.Autograd.LayerNorm.graphInvStd {m n : ℕ} (ε : ℝ) :
                                                                Graph (ΓLN m n) (ssStd m n ++ [VecShape m])

                                                                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 ++ (ssStd m n ++ [VecShape m])) (MatShape m n)

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

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

                                                                      Graph prefix adding inv_std_b.

                                                                      Instances For

                                                                        Index of centered in the stage-graphInvStdBroadcast context.

                                                                        Instances For

                                                                          Index of inv_std_b in the stage-graphInvStdBroadcast context.

                                                                          Instances For
                                                                            noncomputable def Proofs.Autograd.LayerNorm.nodeNorm {m n : ℕ} :
                                                                            Node (ΓLN m n ++ (ssStd m n ++ [VecShape m, MatShape m n])) (MatShape m n)

                                                                            Node computing normalized := centered ⊙ inv_std_b.

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

                                                                              Graph prefix producing normalized := centered ⊙ inv_std_b.

                                                                              Instances For
                                                                                noncomputable def Proofs.Autograd.LayerNorm.nodeGammaB {m n : ℕ} :
                                                                                Node (ΓLN m n ++ (ssStd m n ++ [VecShape m, MatShape m n, MatShape m n])) (MatShape m n)

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

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

                                                                                  Graph prefix adding gamma_b.

                                                                                  Instances For

                                                                                    Index of normalized in the context at stage graphGammaBroadcast.

                                                                                    Instances For

                                                                                      Index of gamma_b at stage graphGammaBroadcast.

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

                                                                                        Scale: scaled := normalized ⊙ gamma_b.

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

                                                                                          Graph prefix adding scaled.

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

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

                                                                                            Instances For

                                                                                              Graph prefix adding beta_b.

                                                                                              Instances For

                                                                                                Index of scaled at stage graphBetaBroadcast.

                                                                                                Instances For

                                                                                                  Index of beta_b at stage graphBetaBroadcast.

                                                                                                  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