TorchLean API

NN.Runtime.Autograd.Model.Dual

Dual #

Dual numbers for forward-mode differentiation.

This runtime-oriented Dual α scalar can be used to compute:

The rules follow the primal branch. Absolute value and guarded square root select zero tangent at zero; minimum and maximum split ties equally. ReLU separately selects zero at its kink, and tensor clamp selects zero tangent at either endpoint. These choices agree with the recorded graph rules.

detach removes the tangent and recursively clears differentiation metadata from the primal, so nested dual computations respect the same boundary. The power rule uses $d(x^y)=x^y(y'\log x+yx'/x)$ and requires the logarithm and quotient to be defined. Mathematical derivative theorems state their domain assumptions in the separate fderiv proof layer.

A dual number $x+\varepsilon\,dx$ used for forward-mode automatic differentiation.

re is the primal value and du is the tangent (directional derivative). When you run a computation once on dual inputs, the resulting du component computes a Jacobian-vector product.

  • re : α

    Primal part: the value the ordinary computation would have produced.

  • du : α

    Tangent part, the coefficient of ε. Truncating at ε² = 0 is what makes the arithmetic rules below exactly the chain rule.

Instances For
    @[instance_reducible]
    instance Runtime.Autograd.Model.instReprDual {α✝ : Type} [Repr α✝] :
    Repr (Dual α✝)
    Instances For

      Project the primal component of a dual number.

      Instances For

        Project the tangent component of a dual number.

        Instances For
          def Runtime.Autograd.Model.Dual.ofPrimal {α : Type} [Zero α] (x : α) :
          Dual α

          Embed a primal value as a dual number with zero tangent.

          Instances For
            def Runtime.Autograd.Model.Dual.mk' {α : Type} (x dx : α) :
            Dual α

            Convenience constructor using the (primal, tangent) order.

            Instances For
              @[instance_reducible]

              Dual is inhabited by default primal value with zero tangent.

              @[instance_reducible]

              Zero dual number: $0+\varepsilon\cdot0$.

              @[instance_reducible]

              One dual number: $1+\varepsilon\cdot0$.

              @[instance_reducible]

              Negation is componentwise: $-(x+\varepsilon\,dx)=-x+\varepsilon(-dx)$.

              @[instance_reducible]

              Addition is componentwise, so tangents add linearly.

              @[instance_reducible]

              Subtraction is componentwise, so tangents subtract linearly.

              @[instance_reducible]

              Multiplication uses the product rule: $(xy)'=x'y+xy'$.

              @[inline]

              Evaluate a complete quotient layer while retaining all enclosed range flags.

              Instances For
                @[instance_reducible, inline]

                Lift checked arithmetic through both coefficients without discarding inner range flags.

                @[instance_reducible, inline]

                Division uses the quotient rule $(x/y)'=(x'y-xy')/y^2$ at every nesting depth.

                For native binary32/64 coefficient trees, ordinary operations retain this evaluation order. If any enclosed operation exposes a nonfinite or subnormal value, including a nonzero product or quotient rounded to zero, we replay the complete quotient over exact rational coefficients. Every final coefficient is then rounded independently to its native format. A representable mixed derivative can therefore survive an intermediate overflow or underflow at an inner depth. Replay requires all input coefficients finite and the denominator's scalar primal nonzero. Otherwise the native quotient-rule result is retained. An unrepresentable exact coefficient still rounds to infinity or zero; coefficients are never clamped or deleted.

                The scope is one quotient of the given coefficient trees. Rounding before entry is irreversible; ordinary rounding and cancellation outside a detected range event retain their native semantics. Carriers without checked coefficient arithmetic retain the previous operational rearrangement.

                @[instance_reducible]

                Boolean equality compares primals only (tangents are treated as metadata).

                @[instance_reducible]
                instance Runtime.Autograd.Model.Dual.instLT {α : Type} [LT α] :
                LT (Dual α)

                Strict order compares primals only.

                @[instance_reducible]
                instance Runtime.Autograd.Model.Dual.instLE {α : Type} [LE α] :
                LE (Dual α)

                Non-strict order compares primals only.

                @[instance_reducible]

                Take the larger primal and its tangent. Equal primals split the tangent equally between the two arguments, matching the graph's elementwise maximum rule. Each tangent is scaled before the sum: adding two large finite tangents first can overflow even when their average is representable.

                @[instance_reducible]

                Elementwise minimum uses the same equal split at a tie as the graph's minimum rule.

                @[instance_reducible]

                Coerce naturals into dual numbers with zero tangent.

                @[instance_reducible]

                Forward-mode chain rule implementations for MathFunctions over dual numbers.

                @[instance_reducible]

                Power rule for x^y over dual numbers.

                We use the standard identity $d(x^y)=x^y(y'\log x+yx'/x)$, which is mathematically justified only on domains where the right-hand side is defined.

                @[instance_reducible]

                Lift a scalar Context to dual numbers by deciding comparisons on primals.

                @[instance_reducible]

                Differentiate the polar angle in real coordinates away from the origin and branch cut.

                Scaling both coordinates avoids squaring a large magnitude. At the origin we select zero tangent, where the angle has no classical derivative. The primal retains the component backend's signed-zero and branch-cut convention.

                Map a tensor of primals to a tensor of duals with zero tangents.

                Instances For

                  Lift a tensor pack to dual numbers with zero tangents.

                  Instances For

                    Combine a primal tensor and a tangent tensor into a dual tensor.

                    This is the tensor-level analogue of Dual.mk'.

                    Instances For

                      Project the tangent part of a dual tensor.

                      Instances For

                        Extract every tangent in a tensor pack.

                        Instances For