TorchLean API

NN.Core.Numeric.Quotient

Arithmetic for nested quotient rules #

Checked operations retain range-loss information until the complete quotient has been evaluated. The cold path evaluates the original finite coefficient trees over Rat, then rounds each coefficient through FloatLib to the requested format. The same algebra can carry partial enclosures for compound expressions without duplicating its product and quotient rules. Quotient replay alone cannot recover information lost before its inputs were formed.

Coefficients in n commuting square-zero variables, with a chosen scalar carrier.

Instances For
    @[instance_reducible]
    instance TorchLean.Numeric.instBEqCoefficients {α✝ : Type} {a✝ : ℕ} [BEq α✝] :
    BEq (Coefficients α✝ a✝)
    def TorchLean.Numeric.instBEqCoefficients.beq {α✝ : Type} {a✝ : ℕ} [BEq α✝] :
    Coefficients α✝ a✝ → Coefficients α✝ a✝ → Bool
    Instances For
      @[reducible, inline]

      Finite rational coefficients by default; enclosure replay can choose another leaf carrier.

      Instances For

        Constant coefficient, obtained by setting all square-zero variables to zero.

        Instances For

          Add corresponding coefficients using the chosen leaf arithmetic.

          Instances For

            Subtract corresponding coefficients using the chosen leaf arithmetic.

            Instances For

              Multiply coefficient trees using the product rule at each square-zero extension.

              Instances For

                Divide coefficient trees. Rational callers check the primal denominator first; partial enclosure arithmetic propagates a failed or zero-crossing divisor at each leaf.

                Instances For

                  Change the leaf representation without changing the differentiation directions.

                  Instances For
                    def TorchLean.Numeric.QuotientCoefficients.mapM {α β : Type} {m : Type → Type} [Monad m] (f : α → m β) {n : ℕ} :

                    Convert every coefficient, propagating a failed conversion.

                    Instances For

                      A constant has zero coefficients in every differentiation direction.

                      Instances For
                        def TorchLean.Numeric.QuotientCoefficients.exp {α : Type} [Add α] [Mul α] (evaluate : α → α) {n : ℕ} :

                        Lift scalar exponential evaluation through the existing coefficient product rule.

                        Instances For

                          Operations used inside one complete quotient, together with finite coefficient conversion.

                          Internal byte flags record nonfinite or subnormal results, including a nonzero product or quotient rounded to zero. Zero means no range loss; recursive carriers combine flags with bitwise OR. The checked-operation entrypoints expose the same Boolean result without converting at each internal node. Unsupported carriers use the ordinary operations and do not supply a conversion.

                          Instances
                            @[instance_reducible, instance 100]
                            @[inline]
                            Instances For
                              @[noinline]
                              def TorchLean.Numeric.QuotientArithmetic.replay? {α : Type} {depth : ℕ} (encode : α → Option (QuotientCoefficients depth)) (decode : QuotientCoefficients depth → Option α) (x y : α) :

                              Replay only finite input trees with an invertible denominator.

                              Only coefficient conversion crosses this cold boundary, leaving the checked arithmetic operations available for specialization in the caller.

                              Instances For

                                Native constants and rational rounding, selected by the scalar type.

                                • zero : α
                                • minNormal : α
                                • negMinNormal : α
                                • round : ℚ → α

                                  Round a rational once to the native scalar type.

                                Instances
                                  @[inline]
                                  Instances For
                                    @[inline]
                                    Instances For
                                      @[inline]
                                      def TorchLean.Numeric.QuotientArithmetic.Native.rangeFlags {α : Type} [NativeFormat α] [BEq α] [LT α] [DecidableLT α] [Sub α] (zero : α → UInt8) (x : α) :
                                      Instances For
                                        @[noinline, specialize #[]]
                                        def TorchLean.Numeric.QuotientArithmetic.Native.addSubFlags {α : Type} [NativeFormat α] [BEq α] [LT α] [DecidableLT α] [Sub α] (zero : α → UInt8) (z : α) :
                                        Instances For
                                          @[noinline, specialize #[]]
                                          def TorchLean.Numeric.QuotientArithmetic.Native.productFlags {α : Type} [NativeFormat α] [BEq α] [LT α] [DecidableLT α] [Sub α] (zero : α → UInt8) (x y z : α) :

                                          Range flags for the actual native product z = x * y, using native zero equality. The open subnormal interval includes zero; nonzero inputs detect a product rounded to zero. This formula requires the actual product, not an independently supplied third value.

                                          Instances For
                                            @[noinline, specialize #[]]
                                            def TorchLean.Numeric.QuotientArithmetic.Native.quotientFlags {α : Type} [NativeFormat α] [BEq α] [LT α] [DecidableLT α] [Sub α] (zero : α → UInt8) (x z : α) :

                                            Range flags for the actual native quotient z = x / y, using native zero equality. A zero denominator already makes the result nonfinite. A zero or subnormal result with a nonzero numerator requires replay. This formula assumes z is the actual division result.

                                            Instances For
                                              @[inline]
                                              Instances For
                                                @[inline]
                                                Instances For
                                                  @[inline]
                                                  Instances For
                                                    @[inline]
                                                    Instances For

                                                      FloatLib exception status plus the stricter range predicate needed by a nested quotient.

                                                      IEEE underflow requires inexactness. An exact subnormal intermediate must also trigger replay, since subsequent multiplication or division can lose coefficients. Ordinary inexact rounding alone does not trigger replay.

                                                      Instances For
                                                        @[instance_reducible]

                                                        Configured values with IEEE exceptional encoding use FloatLib's nearest-even operations and exact finite decoder, including custom exponent biases. Status is computed on the descriptor model and repacked through the codec, independently of the selected arithmetic backend.

                                                        The complete derivative tree is replayed by the recursive Dual instance. Every coefficient is rounded independently to the original descriptor and storage plan, including formats wider than binary64. Finite-only encodings are not enabled: their saturation policy would require a separate derivative contract. True coefficients outside the IEEE range may still round to zero or infinity.