Documentation

Hesper.AD.Reverse

Reverse-Mode Automatic Differentiation #

Implementation of reverse-mode AD (backpropagation) similar to Haskell's ad package. This enables efficient gradient computation for machine learning optimization.

Features #

Usage Example #

-- Define a function
def f (x : Float) : Float := x * x + 2.0 * x + 1.0

-- Compute gradient at x = 3.0
let grad := diff f 3.0
-- Result: 8.0 (derivative of x² + 2x + 1 at x=3 is 2x + 2 = 8)

Tape entry for reverse-mode AD. Stores the operation index, parent indices, and local derivatives.

  • idx : Nat

    Index of this operation in the tape

  • parents : List Nat

    Parent indices (inputs to this operation)

  • localGrads : List Float

    Local derivatives with respect to each parent

Instances For
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      Computational tape for reverse-mode AD

      • entries : Array TapeEntry

        Entries in the tape (reverse chronological order)

      • nextIdx : Nat

        Current index counter

      Instances For

        Create an empty tape

        Equations
        Instances For
          def Hesper.AD.Reverse.Tape.add (tape : Tape) (parents : List Nat) (localGrads : List Float) :

          Add an entry to the tape

          Equations
          Instances For

            Dual number for reverse-mode AD. Contains the primal value and a reference to the tape.

            • primal : Float

              Forward (primal) value

            • tapeIdx : Nat

              Index in the computational tape

            Instances For
              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

                Create a constant (no gradient)

                Equations
                Instances For

                  Create a variable (will have gradient)

                  Equations
                  Instances For
                    def Hesper.AD.Reverse.Dual.lift1 (f df : FloatFloat) (x : Dual) (tape : Tape) :

                    Lift a unary function to Dual numbers

                    Equations
                    Instances For
                      def Hesper.AD.Reverse.Dual.lift2 (f df_dx df_dy : FloatFloatFloat) (x y : Dual) (tape : Tape) :

                      Lift a binary function to Dual numbers

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        def Hesper.AD.Reverse.Dual.lift {Op I O : Type} [inst : Core.Differentiable Op I O] (op : Op) (x : I) (tape : Tape) (parentIndices : List Nat) (localGrads : List Float) :

                        Lift a generic Differentiable operation to Dual numbers

                        Equations
                        Instances For

                          AD context for managing the computational tape

                          • tape : Tape

                            The computational tape

                          Instances For

                            Create a variable in this context

                            Equations
                            Instances For

                              Addition

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For

                                Subtraction

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  Multiplication

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For

                                    Division

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For

                                      Exponentiation

                                      Equations
                                      Instances For

                                        Natural logarithm

                                        Equations
                                        Instances For

                                          Square root

                                          Equations
                                          Instances For

                                            Sine

                                            Equations
                                            Instances For

                                              Cosine

                                              Equations
                                              Instances For

                                                Power (x^n)

                                                Equations
                                                Instances For

                                                  Sigmoid activation

                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  Instances For

                                                    Tanh activation

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For

                                                      ReLU activation

                                                      Equations
                                                      Instances For

                                                        Verified op integration #

                                                        The methods above (add, mul, sub, …) hardcode each op's local gradient in the body, which is fine for the built-in arithmetic but forces every new op to be hand-coded. liftBinaryVerified plugs the gap by reading the local gradient straight out of the Differentiable instance — i.e. the same forward/backward pair we'd register for any Hesper verified op.

                                                        The cost is the function-pointer indirection through the typeclass; the benefit is that any Differentiable Op (Float × Float) Float becomes a Dual op with no further wiring. Used by Ch11 for SquaredErrorOp so the MSE training loop is built from a verified loss op rather than ad-hoc mul/sub/pow.

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For
                                                          def Hesper.AD.Reverse.backprop (tape : Tape) (outputIdx : Nat) :

                                                          Reverse-mode gradient computation via backpropagation

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For

                                                            Compute gradient of a univariate function using reverse-mode AD

                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For

                                                              Compute value and gradient of a univariate function

                                                              Equations
                                                              • One or more equations did not get rendered due to their size.
                                                              Instances For

                                                                Compute gradient of a function with respect to multiple variables

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For

                                                                  Compute value and gradient for multiple variables

                                                                  Equations
                                                                  • One or more equations did not get rendered due to their size.
                                                                  Instances For