TorchLean API

NN.Proofs.Autograd.Tape.Nodes.Reductions

Reduction and shape tape nodes #

Scalar sums, broadcast-to, reduce-sum, reduce-mean, concatenation, and the linear shape adapters used by larger graph proofs.

Continuous linear map embedding a scalar into the 1D scalar-vector representation.

Instances For
    @[simp]

    The one coordinate of a scalar vector is the scalar.

    @[simp]

    Same through the WithLp wrapper.

    Continuous linear map summing the entries of a vector: x ↦ ∑ i, x i.

    Instances For
      theorem Proofs.Autograd.TapeNodes.sumCLM_apply {n : } (x : Vec n) :
      (sumCLM n) x = i : Fin n, x.ofLp i

      Evaluation lemma for sumCLM.

      noncomputable def Proofs.Autograd.TapeNodes.sum {Γ : List Spec.Shape} {s : Spec.Shape} (idx : Idx Γ s) :

      Sum all entries of a context tensor into a scalar tensor.

      Instances For
        noncomputable def Proofs.Autograd.TapeNodes.sumFderiv {Γ : List Spec.Shape} {s : Spec.Shape} (idx : Idx Γ s) :

        NodeFDerivCorrect for sum: derivative is the composite of context projection and coordinate sum.

        Instances For

          Compute the source index in s₁ that corresponds to a target index in s₂ under broadcasting.

          The recursion is on the shapes. The broadcast relation only rules out the impossible cases and shows that a source extent differing from the target extent is one.

          Instances For
            noncomputable def Proofs.Autograd.TapeNodes.Broadcast.broadcastToVec {s₁ s₂ : Spec.Shape} (cb : s₁.CanBroadcastTo s₂) :
            Vec s₁.sizeVec s₂.size

            Broadcast a vector Vec (size s₁) into Vec (size s₂) using the CanBroadcastTo index map.

            Instances For
              noncomputable def Proofs.Autograd.TapeNodes.Broadcast.broadcastToCLM {s₁ s₂ : Spec.Shape} (cb : s₁.CanBroadcastTo s₂) :

              Continuous-linear-map form of broadcastToVec.

              Instances For
                @[simp]

                The bundled broadcast map computes broadcastToVec. Broadcasting is linear, so nothing about the shape relation cb needs to reappear in the derivative.

                Source index obtained by deleting coordinate axis from an index into s.

                Instances For

                  Repeat a flattened reduced tensor along the axis removed by shapeAfterSum.

                  Instances For

                    Continuous-linear-map form of afterSumVec.

                    Instances For
                      @[simp]

                      The bundled repeat-along-the-summed-axis map computes afterSumVec.

                      This map is the adjoint of summation over axis, which is why the backward pass for a reduction is a broadcast: each output coordinate contributed to exactly one sum.

                      noncomputable def Proofs.Autograd.TapeNodes.broadcastTo {Γ : List Spec.Shape} {s₁ s₂ : Spec.Shape} (idx : Idx Γ s₁) (cb : s₁.CanBroadcastTo s₂) :
                      Node Γ s₂

                      General shape broadcast node s₁ → s₂ (linear).

                      Instances For
                        noncomputable def Proofs.Autograd.TapeNodes.broadcastToFderiv {Γ : List Spec.Shape} {s₁ s₂ : Spec.Shape} (idx : Idx Γ s₁) (cb : s₁.CanBroadcastTo s₂) :

                        NodeFDerivCorrect for broadcastTo (broadcasting is linear).

                        Instances For
                          noncomputable def Proofs.Autograd.TapeNodes.reduceSum {Γ : List Spec.Shape} {s : Spec.Shape} (axis : ) [_valid : Spec.Shape.HasNonemptyAxis axis s] [_wf : s.WellFormed] (idx : Idx Γ s) :

                          Sum reduction along axis (linear; adjoint is broadcast back).

                          Instances For
                            noncomputable def Proofs.Autograd.TapeNodes.reduceSumFderiv {Γ : List Spec.Shape} {s : Spec.Shape} (axis : ) [valid : Spec.Shape.HasNonemptyAxis axis s] [wf : s.WellFormed] (idx : Idx Γ s) :

                            NodeFDerivCorrect for reduceSum.

                            Instances For
                              noncomputable def Proofs.Autograd.TapeNodes.reduceMean {Γ : List Spec.Shape} {s : Spec.Shape} (axis : ) [valid : Spec.Shape.HasNonemptyAxis axis s] [_wf : s.WellFormed] (idx : Idx Γ s) :

                              Mean reduction along axis (linear; adjoint is broadcast+scale).

                              Instances For
                                noncomputable def Proofs.Autograd.TapeNodes.reduceMeanFderiv {Γ : List Spec.Shape} {s : Spec.Shape} (axis : ) [valid : Spec.Shape.HasNonemptyAxis axis s] [wf : s.WellFormed] (idx : Idx Γ s) :

                                NodeFDerivCorrect for reduceMean.

                                Instances For
                                  noncomputable def Proofs.Autograd.TapeNodes.takeLeftVec {m n : } (v : Vec (m + n)) :
                                  Vec m

                                  Take the left m entries of a length m+n vector.

                                  Instances For
                                    noncomputable def Proofs.Autograd.TapeNodes.takeRightVec {m n : } (v : Vec (m + n)) :
                                    Vec n

                                    Take the right n entries of a length m+n vector.

                                    Instances For
                                      noncomputable def Proofs.Autograd.TapeNodes.concatLeadingAxis {Γ : List Spec.Shape} {n m : } {s : Spec.Shape} (a : Idx Γ (Spec.Shape.dim n s)) (b : Idx Γ (Spec.Shape.dim m s)) :
                                      Node Γ (Spec.Shape.dim (n + m) s)

                                      Concatenate two tensors along dimension 0 (dim-0 concat), using flattened vectors internally.

                                      Instances For

                                        NodeFDerivCorrect for concatLeadingAxis (concat is linear).

                                        Instances For