TorchLean API

NN.Floats.NeuralFloat.Rounding.Core

Rounding modes and a half-ULP error bound #

We use this file as the “rounding” half of the Flocq-style rounding-on- model:

These definitions are used by NF (the rounded scalar type), by the FP32 model, and by the bridge layer that connects proofs to executable IEEE-754 behavior.

References #

Round toward negative infinity (floor)

Instances For

    Round toward positive infinity (ceiling)

    Instances For

      Round toward zero (truncation)

      Instances For

        Toward-zero integer rounding agrees with floor on nonnegative inputs.

        Toward-zero integer rounding agrees with ceiling on nonpositive inputs.

        Round to nearest, ties to even

        Instances For

          Valid rounding mode predicate.

          • monotone (x y : ) : x yrnd x rnd y

            The rounding rule preserves the order of its real inputs.

          • id (n : ) : rnd n = n

            Every integer is fixed by the rounding rule.

          Instances

            Rounding modes with a half-unit error bound on the rounded integer.

            This matches "round-to-nearest" style roundings (ties can be resolved arbitrarily): |rnd x - x| ≤ 1/2 for all x.

            • monotone (x y : ) : x yrnd x rnd y
            • id (n : ) : rnd n = n
            • abs_sub_le_half (x : ) : |(rnd x) - x| 2⁻¹

              Rounding changes a real input by at most one half on the integer grid.

            Instances
              noncomputable def TorchLean.Floats.neuralRoundAtScale (rnd : ) (step : ) (_hstep : 0 < step) (x : ) :

              Round x to the integer grid with spacing step.

              This fixed-scale operation is the common core of fixed-point arithmetic and affine quantization. Unlike neuralRound, the scale is supplied explicitly rather than chosen from the magnitude of x by an exponent format. Requiring positivity prevents a zero scale from silently turning the operation into the constant-zero map through totalized real division.

              Instances For

                neural_floor_round is a valid rounding mode (monotone and fixes integers).

                neural_ceil_round is a valid rounding mode (monotone and fixes integers).

                Toward-zero integer rounding is monotone and fixes integers.

                Basic bounds for nearest-even rounding.

                In words: neural_nearest_even x always lands in {⌊x⌋, ⌊x⌋ + 1}. This is the key fact used in the monotonicity proof and in IEEE-style interval bounds.

                Nearest-even rounds down when the fractional part is strictly less than 1/2.

                In words: if x is closer to ⌊x⌋ than to ⌊x⌋+1, then it rounds to ⌊x⌋.

                Nearest-even rounds up when the fractional part is strictly greater than 1/2.

                In words: if x is closer to ⌊x⌋+1 than to ⌊x⌋, then it rounds to ⌊x⌋+1.

                Nearest-even tie-breaking: when the fractional part is exactly 1/2 and the floor is even, round down to the even integer.

                Nearest-even tie-breaking: when the fractional part is exactly 1/2 and the floor is odd, round up to the even integer.

                neural_nearest_even is a valid rounding mode (monotone and fixes integers).

                Nearest-even is a “round-to-nearest” mode in the integer sense: |rnd(x) - x| ≤ 1/2.

                This is the key property required by NeuralValidRndToNearest.

                Nearest-even has the same distance from the input as Mathlib's ties-up nearest integer.

                Nearest-even minimizes distance to the input among all integers.

                neural_nearest_even satisfies the half-unit error bound |rnd x - x| ≤ 1/2.

                noncomputable def TorchLean.Floats.neuralRound {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (rnd : ) (x : ) :

                Core rounding operator (“compute in , then round back to the grid”).

                We build a pure NeuralFloat mantissa/exponent pair and interpret it with neuralToReal.

                Instances For

                  The scaled mantissa is the input divided by its canonical radix power.

                  theorem TorchLean.Floats.neural_scaled_mantissa_int_of_generic {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (x : ) (hx : neuralGenericFormat β fexp x) :
                  ∃ (n : ), neuralScaledMantissa β fexp x = n

                  If x is already in the generic format grid, then neural_scaled_mantissa β fexp x is an integer.

                  In words: exact representability means “no fractional bits at the chosen exponent scale”.

                  @[simp]
                  theorem TorchLean.Floats.neural_round_preserves_generic {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (rnd : ) [NeuralValidRnd rnd] (x : ) (hx : neuralGenericFormat β fexp x) :
                  neuralRound rnd x = x

                  Rounding preserves exactly-representable numbers.

                  In words: if x lies on the grid described by (β,fexp) (neural_generic_format), then rounding it with any valid rnd is a no-op.

                  This is the Flocq-style “round_generic” lemma.

                  Scaled mantissa times base power equals the original value.

                  In words: scaled_mantissa(x) * β^{cexp(x)} = x. This is the algebraic identity that justifies the scaling used before rounding.

                  An integral canonical scaled mantissa is sufficient for exact representability.

                  Exact representability is equivalent to integrality of the canonical scaled mantissa.

                  theorem TorchLean.Floats.neural_error_bound_ulp {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (rnd : ) [NeuralValidRndToNearest rnd] (x : ) :
                  |neuralRound rnd x - x| neuralUlp β fexp x / 2

                  Half-ULP error bound for neural_round under round-to-nearest.

                  This is the basic “one-step” bound used by most error propagation arguments: neural_round deviates from x by at most half an ulp at the chosen exponent scale.

                  Named standard modes #

                  The four standard rounding-direction attributes used by the generic float model.

                  • nearestEven : NeuralRoundingMode

                    Round to the nearest value, resolving a tie toward an even integer at the selected scale.

                    Under the usual Flocq conditions (in particular, excluding the precision-one even-radix corner case), this agrees with the familiar even canonical-significand description.

                  • towardZero : NeuralRoundingMode

                    Round toward zero.

                  • towardPositive : NeuralRoundingMode

                    Round toward positive infinity.

                  • towardNegative : NeuralRoundingMode

                    Round toward negative infinity.

                  Instances For

                    Interpret a named mode as the integer-rounding function used by neuralRound.

                    Instances For

                      Every named standard mode satisfies the generic rounding-function laws.

                      noncomputable def TorchLean.Floats.NeuralRoundingMode.round (mode : NeuralRoundingMode) {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (x : ) :

                      Round a real value into a generic format using a named standard mode.

                      Instances For

                        Package round as a mantissa/exponent value in the selected format.

                        Instances For
                          @[simp]

                          roundedFloat represents exactly the value returned by round.

                          Every named standard mode fixes values already in the generic format.