TorchLean API

NN.Floats.NeuralFloat.Format.Formats

Flocq-style formats (FIX / FLX / FLT) #

We are not defining the executable IEEE-754 layer here.

What we do here is the same separation used by Flocq:

Those fexps let us talk about “a fixed-point grid”, “an unbounded float grid”, or “a float grid with a lower exponent bound and gradual underflow” without committing to a concrete bit encoding.

In particular:

If you want NaN/Inf/signed-zero and an executable kernel, that is NN/Floats/IEEEExec/. For TorchLean-specific “which precision do we use in each phase?” configuration helpers, see NN/Floats/NeuralFloat/Metadata.lean.

References:

A positive number of radix digits for FLX, FLT, and FTZ formats.

Raw exponent functions remain available for algebraic proofs, but callers accepting an integer configuration should first use NeuralFormatPrecision.ofInt?. This prevents a zero or negative precision from being silently reinterpreted through an absolute-value conversion.

  • digits :

    Number of radix digits retained by the format.

  • digits_pos : 0 < self.digits

    A floating-point precision has at least one radix digit.

Instances For

    Convert a proof-carrying format precision to the integer parameter used by exponent formulas.

    Instances For

      Check a natural-number precision at a configuration boundary.

      Instances For

        Check an integer precision, rejecting zero and every negative value.

        Instances For
          @[simp]

          A checked precision remains positive after conversion to the integer exponent parameter.

          @[simp]

          Natural precision validation fails exactly at zero.

          @[simp]

          Integer precision validation fails exactly for nonpositive inputs.

          FIX_exp emin is the simplest exponent-selection function: it always returns the same exponent.

          This is the Flocq “FIX” family. It is useful when you want to reason about values living on a single, fixed grid $\beta^{\mathtt{emin}}\mathbb{Z}$ (think: fixed-point arithmetic or quantization).

          Instances For

            FIX_exp satisfies the standard Flocq-style Valid_exp axioms (here: NeuralValidExp).

            Even though the proof is trivial, having the instance is what lets later theorems reuse the same generic lemmas for FIX/FLX/FLT.

            theorem TorchLean.Floats.neuralUlp_zero_FIX {β : NeuralRadix} (emin : ) :
            neuralUlp β (FIXExp emin) 0 = neuralBpow β emin

            The ULP at zero for a fixed-point grid is its fixed grid step.

            FIX_format emin x says “x is exactly representable on the fixed grid”.

            This is phrased via an existential NeuralFloat β so that it composes smoothly with the rest of the rounding model (neural_to_real, ULP bounds, etc.).

            Instances For

              FLX_exp prec is the unbounded-exponent family.

              This is Flocq’s “FLX” family: it models a floating-point format with no exponent bounds but with a mantissa precision parameter prec. It is a convenient intermediate model for proofs because it removes underflow/overflow corner cases while still tracking mantissa rounding.

              Instances For
                @[reducible, inline]
                abbrev TorchLean.Floats.flxValidExp (prec : ) (h : 0 < prec) :

                FLX_exp satisfies NeuralValidExp.

                The side-condition 0 < prec matches the standard assumption that “precision is positive”.

                Instances For

                  FLXExp prec satisfies the generic exponent axioms exactly when prec is positive.

                  The unbounded exponent selector associated with a checked precision.

                  Instances For

                    A checked precision automatically discharges the FLX exponent-validity obligation.

                    @[reducible, inline]
                    Instances For
                      @[reducible, inline]
                      Instances For

                        Exact representability predicate for FLX.

                        Heuristically, there exists a mantissa/exponent pair with mantissa bounded by the precision, and $x=m\beta^e$.

                        Instances For
                          theorem TorchLean.Floats.not_FLXFormat_of_nonpos {β : NeuralRadix} (prec : ) (hprec : prec 0) (x : ) :

                          Nonpositive precision is rejected by the explicit FLX format predicate.

                          The unbounded FLX exponent function has no negligible exponent.

                          theorem TorchLean.Floats.neuralUlp_zero_FLX {β : NeuralRadix} (prec : ) (hprec : 0 < prec) :
                          neuralUlp β (FLXExp prec) 0 = 0

                          Consequently, the generic ULP of zero is zero for FLX.

                          def TorchLean.Floats.FLTExp (emin prec : ) :

                          FLT_exp emin prec is the lower-exponent-bounded family with gradual underflow.

                          This is Flocq’s “FLT” family. The exponent is bounded below by emin, but it has no upper bound, so this rounded-real format models gradual underflow but not overflow, infinities, or NaNs. Gradual underflow is captured by taking max (e - prec) emin.

                          Instances For
                            @[reducible, inline]
                            abbrev TorchLean.Floats.fltValidExp (emin prec : ) (h : 0 < prec) :

                            FLT_exp satisfies NeuralValidExp.

                            This is where most format-bridge lemmas live when connecting proofs to float32-style bounds (e.g. via NN/Floats/FP32).

                            Instances For
                              theorem TorchLean.Floats.neuralValidExp_FLT_iff (emin prec : ) :
                              NeuralValidExp (FLTExp emin prec) 0 < prec

                              FLTExp emin prec satisfies the exponent axioms exactly for positive precision.

                              The gradual-underflow exponent selector associated with a checked precision.

                              Instances For

                                A checked precision automatically discharges the gradual-underflow validity obligation.

                                @[reducible, inline]
                                Instances For
                                  @[reducible, inline]
                                  Instances For
                                    def TorchLean.Floats.FLTFormat {β : NeuralRadix} (emin prec : ) (x : ) :

                                    Exact representability predicate for FLT.

                                    This version includes:

                                    • a mantissa size bound (precision),
                                    • and the lower exponent bound $\mathtt{emin}\le\mathtt{exponent}$ (no values smaller than the minimum normal/subnormal scale, depending on the choice of emin and rounding).
                                    Instances For
                                      theorem TorchLean.Floats.not_FLTFormat_of_nonpos {β : NeuralRadix} (emin prec : ) (hprec : prec 0) (x : ) :
                                      ¬FLTFormat emin prec x

                                      Nonpositive precision is rejected by the explicit FLT format predicate.

                                      FLT has a negligible-exponent witness at emin.

                                      theorem TorchLean.Floats.neuralUlp_zero_FLT {β : NeuralRadix} (emin prec : ) (hprec : 0 < prec) :
                                      neuralUlp β (FLTExp emin prec) 0 = neuralBpow β emin

                                      The ULP at zero for FLT is the smallest grid step $\beta^{\mathtt{emin}}$.