TorchLean API

NN.Proofs.RuntimeApprox.NF.Attention

Rounded scaled dot-product attention #

This module connects TorchLean's stable last-axis softmax theorem to the matrix operations used by attention. The forward theorem follows the actual unmasked computation

softmax(c * (Q Kᵀ)) V,

where c is normally 1 / sqrt(d). Both the matrices and c may be approximate. The result is a single infinity-norm budget assembled from the existing transpose, matrix-multiplication, coefficient-aware scaling, and row-softmax contracts.

The theorem keeps the rounded coefficient error explicit. A caller may obtain it from the concrete construction of 1 / sqrt(d), from an interval certificate, or from a backend capsule. Hiding that error would incorrectly treat a rounded normalization factor as an exact real constant.

References:

The canonical 1 / sqrt(d) coefficient #

Rounding budget for embedding the positive feature dimension into NF.

Instances For

    Error budget for the rounded square root of the embedded feature dimension.

    Instances For

      End-to-end error budget for constructing 1 / sqrt(d) in the rounded backend.

      Instances For

        The canonical rounded attention scale approximates the exact real 1 / sqrt(d).

        The side condition is format-sensitive and executable: the accumulated square-root error must be smaller than the exact lower bound one. For IEEE binary32 and ordinary transformer dimensions it is tiny; keeping it explicit also makes the theorem valid for low-precision experimental formats.

        Error after the rounded score product Q Kᵀ.

        Instances For
          noncomputable def Proofs.RuntimeApprox.Attention.scaledScoreErrorBound {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } {nQ nK d : } (epsQ epsK epsScale : ) (scaleR : FloatLib.Floats.Formats.Flocq.NF β fexp rnd) (qR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nQ, d]) (kR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nK, d]) :

          Error after multiplying scores by an approximate runtime scale.

          Instances For

            Shared numerical certificate for the scaled score matrix scale * (Q Kᵀ).

            Both masked and unmasked attention use this prefix. Keeping it as one theorem also identifies the natural contract boundary for fused score kernels: a provider may replace the implementation as long as it supplies this same approximation statement.

            noncomputable def Proofs.RuntimeApprox.Attention.weightErrorBound {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } {nQ nK d : } (epsQ epsK epsScale : ) (scaleR : FloatLib.Floats.Formats.Flocq.NF β fexp rnd) (qR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nQ, d]) (kR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nK.succ, d]) :

            Error in the row-wise attention weights.

            Instances For
              noncomputable def Proofs.RuntimeApprox.Attention.outputErrorBound {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } {nQ nK d : } (epsQ epsK epsV epsScale : ) (scaleR : FloatLib.Floats.Formats.Flocq.NF β fexp rnd) (qR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nQ, d]) (kR vR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nK.succ, d]) :

              End-to-end output error for unmasked scaled dot-product attention.

              Instances For
                noncomputable def Proofs.RuntimeApprox.Attention.maskedWeightErrorBound {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } {nQ nK d : } (η epsMax : Fin nQ) (rowMaxR : Fin nQFloatLib.Floats.Formats.Flocq.NF β fexp rnd) (epsQ epsK epsScale : ) (scaleR : FloatLib.Floats.Formats.Flocq.NF β fexp rnd) (qR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nQ, d]) (kR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nK.succ, d]) (mask : TorchLean.Tensor Bool [nQ, nK.succ]) :

                Error in hard-masked attention weights, preserving one certificate record per query row.

                Instances For
                  noncomputable def Proofs.RuntimeApprox.Attention.maskedOutputErrorBound {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } {nQ nK d : } (η epsMax : Fin nQ) (rowMaxR : Fin nQFloatLib.Floats.Formats.Flocq.NF β fexp rnd) (epsQ epsK epsV epsScale : ) (scaleR : FloatLib.Floats.Formats.Flocq.NF β fexp rnd) (qR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nQ, d]) (kR vR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nK.succ, d]) (mask : TorchLean.Tensor Bool [nQ, nK.succ]) :

                  End-to-end output error for hard-masked scaled dot-product attention.

                  Instances For
                    theorem Proofs.RuntimeApprox.Attention.approxTensor_hardMaskedScaledDotProductAttentionCore {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } [FloatLib.Floats.Formats.Flocq.ValidRndToNearest rnd] {nQ nK d : } {qS : Spec.SpecTensor [nQ, d]} {kS vS : Spec.SpecTensor [nK.succ, d]} {qR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nQ, d]} {kR vR : TorchLean.Tensor (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) [nK.succ, d]} (mask : TorchLean.Tensor Bool [nQ, nK.succ]) {scaleS : } {scaleR : FloatLib.Floats.Formats.Flocq.NF β fexp rnd} {epsQ epsK epsV epsScale : } (hq : approxTensor NFBackend.toSpec qS qR epsQ) (hk : approxTensor NFBackend.toSpec kS kR epsK) (hv : approxTensor NFBackend.toSpec vS vR epsV) (hscale : |NFBackend.toSpec scaleR - scaleS| epsScale) (evidence : AxisSoftmax.HardMaskedRowsEvidence ((Spec.matMulSpec qS (TorchLean.Tensor.swapAdjacentAxes kS 0)).scaleSpec scaleS) ((Spec.matMulSpec qR (kR.swapAdjacentAxes 0)).scaleSpec scaleR) mask (scaledScoreErrorBound epsQ epsK epsScale scaleR qR kR)) :

                    Numerical forward theorem for hard-masked scaled dot-product attention.

                    Every query row must have an allowed key. This covers causal masks, where the diagonal is allowed; the all-blocked vector theorem remains available for APIs that intentionally permit empty rows. The selected allowed-row maxima and denominator margins are explicit certificate data, so no finite value is ever substituted for negative infinity.

                    Numerical forward theorem for the unmasked scaled-dot-product attention core.

                    hdenom is checked on each rounded score row after matrix multiplication and scaling. This is the only nonlocal side condition introduced by stable softmax; it certifies that the accumulated denominator error remains below the exact lower bound one.

                    theorem Proofs.RuntimeApprox.Attention.approxTensor_scaledDotProductAttention_unmasked {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } [FloatLib.Floats.Formats.Flocq.ValidRndToNearest rnd] {nQ nK d : } {hQ : nQ 0} {hK : nK.succ 0} (ctxS : Spec.AttentionContext nQ nK.succ d hQ hK) (ctxR : Spec.AttentionContext (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) nQ nK.succ d hQ hK) {epsQ epsK epsV epsScale : } (hmaskS : ctxS.mask = none) (hmaskR : ctxR.mask = none) (hq : approxTensor NFBackend.toSpec ctxS.Q ctxR.Q epsQ) (hk : approxTensor NFBackend.toSpec ctxS.K ctxR.K epsK) (hv : approxTensor NFBackend.toSpec ctxS.V ctxR.V epsV) (hscale : |NFBackend.toSpec (1 / Spec.attentionScaleDenom d) - 1 / Spec.attentionScaleDenom d| epsScale) (hdenom : ∀ (i : Fin nQ), AxisSoftmax.denominatorErrorBound (scaledScoreErrorBound epsQ epsK epsScale (1 / Spec.attentionScaleDenom d) ctxR.Q ctxR.K) (Spec.get ((Spec.matMulSpec ctxR.Q (ctxR.K.swapAdjacentAxes 0)).scaleSpec (1 / Spec.attentionScaleDenom d)) i) < 1) :

                    Canonical TorchLean attention corollary for the unmasked branch.

                    This theorem is stated directly over Spec.AttentionContext, so model proofs do not need to reconstruct the core expression by hand. The scale approximation and row-denominator checks are the numerical evidence attached to the runtime context.

                    theorem Proofs.RuntimeApprox.Attention.approxTensor_scaledDotProductAttention_masked {β : FloatLib.Numerics.Radix} {fexp : } [FloatLib.Floats.Formats.Flocq.ValidExp fexp] {rnd : } [FloatLib.Floats.Formats.Flocq.ValidRndToNearest rnd] {nQ nK d : } {hQ : nQ 0} {hK : nK.succ 0} (ctxS : Spec.AttentionContext nQ nK.succ d hQ hK) (ctxR : Spec.AttentionContext (FloatLib.Floats.Formats.Flocq.NF β fexp rnd) nQ nK.succ d hQ hK) (mask : TorchLean.Tensor Bool [nQ, nK.succ]) {epsQ epsK epsV epsScale : } (hmaskS : ctxS.mask = some mask) (hmaskR : ctxR.mask = some mask) (hq : approxTensor NFBackend.toSpec ctxS.Q ctxR.Q epsQ) (hk : approxTensor NFBackend.toSpec ctxS.K ctxR.K epsK) (hv : approxTensor NFBackend.toSpec ctxS.V ctxR.V epsV) (hscale : |NFBackend.toSpec (1 / Spec.attentionScaleDenom d) - 1 / Spec.attentionScaleDenom d| epsScale) (evidence : AxisSoftmax.HardMaskedRowsEvidence ((Spec.matMulSpec ctxS.Q (ctxS.K.swapAdjacentAxes 0)).scaleSpec (1 / Spec.attentionScaleDenom d)) ((Spec.matMulSpec ctxR.Q (ctxR.K.swapAdjacentAxes 0)).scaleSpec (1 / Spec.attentionScaleDenom d)) mask (scaledScoreErrorBound epsQ epsK epsScale (1 / Spec.attentionScaleDenom d) ctxR.Q ctxR.K)) :
                    approxTensor NFBackend.toSpec (Spec.scaledDotProductAttention ctxS) (Spec.scaledDotProductAttention ctxR) (maskedOutputErrorBound evidence.eta evidence.epsMax evidence.rowMaxR epsQ epsK epsV epsScale (1 / Spec.attentionScaleDenom d) ctxR.Q ctxR.K ctxR.V mask)

                    Canonical TorchLean attention corollary for a shared hard mask.

                    The row certificate is tied to the scaled score matrices in the two contexts. Thus a certificate cannot be replayed against a different mask, scale, or set of parameters merely because the tensor shapes happen to agree.

                    Fully instantiated unmasked attention theorem for a positive feature dimension.

                    This corollary discharges the scale-coefficient approximation with approx_canonicalAttentionScale; callers provide only tensor approximation hypotheses and the two checkable safety margins for square root and row normalization.