TorchLean API

NN.Proofs.RuntimeApprox.NF.Optimizers

Rounded Optimizer Steps for NF #

Concrete instances of RuntimeApprox.Optimizer.NumericalStepContract for TorchLean's rounded NF runtime. These proofs use the same tensor equations as the public optimizers and the shared elementwise error transformers; there is no second optimizer implementation in the proof layer.

The first contracts cover SGD and momentum SGD. They already compose with the generic NumericalStepContract.run_approx theorem over arbitrary finite gradient streams and arbitrary tensor ranks. Adaptive optimizers build on the positive-division and square-root rules and are kept in this module so all optimizer numerical contracts share one public home.

The Adam recurrence follows Kingma and Ba, Adam: A Method for Stochastic Optimization, ICLR 2015 (https://arxiv.org/abs/1412.6980). The decoupled decay term follows Loshchilov and Hutter, Decoupled Weight Decay Regularization, ICLR 2019 (https://arxiv.org/abs/1711.05101).

SGD #

@[reducible, inline]

Error in the runtime learning-rate scalar stored by SGD.

Instances For

    Exact/runtime relation for SGD state.

    Instances For
      noncomputable def Proofs.RuntimeApprox.NFBackend.Optimizer.sgdStepError {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } {s : Spec.Shape} (learningRateError parameterError gradientError : ) (runtimeState : Optim.SGD.State (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s) (runtimeParameters runtimeGradients : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s) :

      Parameter error after one SGD update, computed from the actual runtime tensors.

      Instances For
        theorem Proofs.RuntimeApprox.NFBackend.Optimizer.approxTensor_sgd_update {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } [FloatLib.Floats.Formats.Flocq.ValidRndToNearest rnd] {s : Spec.Shape} {stateS : Optim.SGD.State s} {stateR : Optim.SGD.State (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s} {learningRateError : } {exactParameters : TorchLean.Tensor s} {runtimeParameters : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s} {parameterError : } {exactGradients : TorchLean.Tensor s} {runtimeGradients : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s} {gradientError : } (hstate : sgdStateApprox stateS stateR learningRateError) (hparams : approxTensor toSpec exactParameters runtimeParameters parameterError) (hgrads : approxTensor toSpec exactGradients runtimeGradients gradientError) :
        approxTensor toSpec (Optim.SGD.update stateS exactParameters exactGradients).parameters (Optim.SGD.update stateR runtimeParameters runtimeGradients).parameters (sgdStepError learningRateError parameterError gradientError stateR runtimeParameters runtimeGradients).parameterError

        One actual TorchLean SGD parameter update refines its exact-real counterpart.

        Momentum SGD #

        Error budgets for momentum SGD's scalar hyperparameters and momentum buffer.

        • learningRate :

          Learning-rate error.

        • momentum :

          Momentum-coefficient error.

        • momentumBuffer :

          Infinity-norm error in the stored momentum buffer.

        Instances For

          Exact/runtime relation for momentum SGD state.

          Instances For
            noncomputable def Proofs.RuntimeApprox.NFBackend.Optimizer.momentumSGDStepError {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } {s : Spec.Shape} (stateError : MomentumSGDStateError s) (parameterError gradientError : ) (runtimeState : Optim.MomentumSGD.State (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s) (runtimeParameters runtimeGradients : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s) :

            State and parameter bounds for one momentum-SGD update.

            Instances For

              Numerical refinement contract for momentum SGD with arbitrary-rank parameter tensors.

              Instances For
                theorem Proofs.RuntimeApprox.NFBackend.Optimizer.approxTensor_momentumSGD_update {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } [FloatLib.Floats.Formats.Flocq.ValidRndToNearest rnd] {s : Spec.Shape} {stateS : Optim.MomentumSGD.State s} {stateR : Optim.MomentumSGD.State (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s} {stateError : MomentumSGDStateError s} {paramsS : TorchLean.Tensor s} {paramsR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s} {paramsError : } {gradsS : TorchLean.Tensor s} {gradsR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s} {gradsError : } (hstate : momentumSGDStateApprox stateS stateR stateError) (hparams : approxTensor toSpec paramsS paramsR paramsError) (hgrads : approxTensor toSpec gradsS gradsR gradsError) :
                have nextError := momentumSGDStepError stateError paramsError gradsError stateR paramsR gradsR; momentumSGDStateApprox (Optim.MomentumSGD.update stateS paramsS gradsS).optimizerState (Optim.MomentumSGD.update stateR paramsR gradsR).optimizerState nextError.optimizerStateError approxTensor toSpec (Optim.MomentumSGD.update stateS paramsS gradsS).parameters (Optim.MomentumSGD.update stateR paramsR gradsR).parameters nextError.parameterError

                One public momentum-SGD update refines its exact-real counterpart.

                This named corollary exposes the useful one-step statement without duplicating its proof; the generic momentumSGDContract.updateApprox field remains the source used for finite runs and graph-level composition.

                AdamW #

                Error budgets relating exact and rounded AdamW state.

                • learningRate :

                  Error in the stored learning rate.

                • beta1 :

                  Error in the first-moment decay coefficient.

                • beta2 :

                  Error in the second-moment decay coefficient.

                • epsilon :

                  Error in the denominator stabilizer.

                • weightDecay :

                  Error in the decoupled weight-decay coefficient.

                • firstMoment :

                  Infinity-norm error in the first-moment tensor.

                • secondMoment :

                  Infinity-norm error in the second-moment tensor.

                Instances For

                  Exact/runtime relation for the persistent AdamW state.

                  Instances For

                    Errors for scalar expressions derived inside one AdamW step.

                    They are kept separate from persistent state errors because subtraction, powers, reciprocal, and the product lr * weightDecay each round in the runtime scalar model.

                    • oneMinusBeta1 :

                      Error in the rounded scalar expression 1 - beta1.

                    • oneMinusBeta2 :

                      Error in the rounded scalar expression 1 - beta2.

                    • firstMomentBiasInverse :

                      Error in the reciprocal first-moment bias correction.

                    • secondMomentBiasInverse :

                      Error in the reciprocal second-moment bias correction.

                    • decayScale :

                      Error in the rounded product lr * weightDecay.

                    Instances For

                      Composed errors for the intermediate tensors in one AdamW step.

                      • squaredGradient :

                        Error after squaring the gradient.

                      • firstMoment :

                        Error after updating the first moment.

                      • secondMoment :

                        Error after updating the second moment.

                      • correctedFirstMoment :

                        Error after first-moment bias correction.

                      • correctedSecondMoment :

                        Error after second-moment bias correction.

                      • standardDeviation :

                        Error after square root of the corrected second moment.

                      • denominator :

                        Error after adding epsilon to the square-root denominator.

                      • adaptiveLearningRate :

                        Error in the elementwise adaptive learning rate.

                      • adaptiveUpdate :

                        Error in the Adam update before subtraction from parameters.

                      • decayUpdate :

                        Error in the decoupled weight-decay update.

                      • decayedParameters :

                        Error after applying decoupled weight decay.

                      • parameterError :

                        Final parameter error after the full AdamW step.

                      Instances For
                        noncomputable def Proofs.RuntimeApprox.NFBackend.Optimizer.adamWStepErrorTrace {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } {s : Spec.Shape} (stateError : AdamWStateError s) (derivedErrors : AdamWDerivedErrors) (parameterError gradientError minimumSecondMoment : ) (runtimeState : Optim.AdamW.State (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s) (runtimeParameters runtimeGradients : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s) :

                        Compute AdamW's complete one-step error trace from runtime values and scalar subexpression budgets. The reduction to one infinity-norm number per tensor keeps the trace independent of rank.

                        Instances For
                          theorem Proofs.RuntimeApprox.NFBackend.Optimizer.approxTensor_adamW_update {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } [FloatLib.Floats.Formats.Flocq.ValidRndToNearest rnd] {s : Spec.Shape} {stateS : Optim.AdamW.State s} {stateR : Optim.AdamW.State (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s} {stateError : AdamWStateError s} {derivedErrors : AdamWDerivedErrors} {paramsS : TorchLean.Tensor s} {paramsR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s} {paramsError : } {gradsS : TorchLean.Tensor s} {gradsR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) s} {gradsError η : } (hstate : adamWStateApprox stateS stateR stateError) (hparams : approxTensor toSpec paramsS paramsR paramsError) (hgrads : approxTensor toSpec gradsS gradsR gradsError) (honeMinus1 : |toSpec (1 - stateR.beta1) - (1 - stateS.beta1)| derivedErrors.oneMinusBeta1) (honeMinus2 : |toSpec (1 - stateR.beta2) - (1 - stateS.beta2)| derivedErrors.oneMinusBeta2) (hbias1 : |toSpec (1 / (1 - Optim.scalarPowNat stateR.beta1 (stateR.stepCount + 1))) - 1 / (1 - Optim.scalarPowNat stateS.beta1 (stateS.stepCount + 1))| derivedErrors.firstMomentBiasInverse) (hbias2 : |toSpec (1 / (1 - Optim.scalarPowNat stateR.beta2 (stateR.stepCount + 1))) - 1 / (1 - Optim.scalarPowNat stateS.beta2 (stateS.stepCount + 1))| derivedErrors.secondMomentBiasInverse) (hdecay : |toSpec (stateR.learningRate * stateR.weightDecay) - stateS.learningRate * stateS.weightDecay| derivedErrors.decayScale) ( : 0 < η) (hEpsilon : 0 stateS.epsilon) (hMoment2Hat : have nextStepCount := stateS.stepCount + 1; have moment2 := (stateS.secondMoment.scaleSpec stateS.beta2).addSpec (gradsS.squareSpec.scaleSpec (1 - stateS.beta2)); TorchLean.Tensor.Forall (fun (z : ) => η z) (moment2.scaleSpec (1 / (1 - Optim.scalarPowNat stateS.beta2 nextStepCount)))) (hMoment2Margin : (adamWStepErrorTrace stateError derivedErrors paramsError gradsError η stateR paramsR gradsR).correctedSecondMoment < η) (hDenominatorMargin : (adamWStepErrorTrace stateError derivedErrors paramsError gradsError η stateR paramsR gradsR).denominator < η) :
                          have trace := adamWStepErrorTrace stateError derivedErrors paramsError gradsError η stateR paramsR gradsR; adamWStateApprox (Optim.AdamW.update stateS paramsS gradsS).optimizerState (Optim.AdamW.update stateR paramsR gradsR).optimizerState { learningRate := stateError.learningRate, beta1 := stateError.beta1, beta2 := stateError.beta2, epsilon := stateError.epsilon, weightDecay := stateError.weightDecay, firstMoment := trace.firstMoment, secondMoment := trace.secondMoment } approxTensor toSpec (Optim.AdamW.update stateS paramsS gradsS).parameters (Optim.AdamW.update stateR paramsR gradsR).parameters trace.parameterError

                          One AdamW update is numerically sound on a certified positive second-moment domain.

                          The hypotheses for the derived scalar expressions expose rounding in 1-β, bias correction, and the decoupled decay coefficient. η keeps sqrt(vHat) away from its singular derivative at zero; the two margin hypotheses ensure the rounded second moment and final denominator remain positive.

                          AdamW contract instance #

                          Numerical assumptions and positivity margin for one AdamW update.

                          • derivedErrors : AdamWDerivedErrors

                            Bounds for rounded scalar subexpressions used by bias correction and decay.

                          • minimumSecondMoment :

                            Strict lower bound on the exact bias-corrected second moment.

                          Instances For

                            Complete validity predicate for one AdamW contract application.

                            Instances For

                              State and parameter error object produced by one AdamW contract step.

                              Instances For

                                AdamW instance of the generic numerical optimizer contract.

                                Its assumptions are proof data, not a second execution framework. NumericalStepContract.run_approx therefore composes AdamW over finite runs exactly as it does SGD and momentum SGD.

                                Instances For