TorchLean API

NN.Floats.IEEEExec.Exec32.Core

Executable IEEE-754 binary32 (IEEE32Exec) #

TorchLean uses two complementary ways to talk about "float32":

We implement IEEE32Exec as raw UInt32 bits and provide:

We also provide a Context IEEE32Exec instance so the spec layer can run modules with an executable scalar. That is why we import NN.Spec.Core.Context here.

About transcendentals #

IEEE-754 does not specify implementations for transcendental functions (exp, tanh, ...). In practice those are provided by libm (or vendor math libraries) and vary across platforms.

We provide deterministic implementations for a few transcendentals in Lean so examples can run without delegating to the host runtime. For the remaining ones, we may still delegate to Lean's Float (binary64) and round back to binary32. These functions are executable and stable, but they are not claimed to be correctly rounded or to match any particular hardware/libm.

References #

Executable IEEE-754 binary32 value, stored as raw bits.

Instances For
    @[inline]

    Wrap raw binary32 bits as an IEEE32Exec.

    Instances For
      @[inline]

      Extract the raw binary32 bits of an IEEE32Exec.

      Instances For
        @[implicit_reducible]

        Default inhabitant: all bits zero, i.e. +0.0.

        Binary32 bit layout #

        IEEE-754 binary32 is stored as:

        For NaNs, the "quiet" bit is the top fraction bit.

        Mask selecting the sign bit (bit 31).

        Instances For

          Mask selecting the 8-bit exponent field (bits 30..23).

          Instances For

            Mask selecting the 23-bit fraction field (bits 22..0).

            Instances For

              The IEEE-754 "quiet NaN" indicator bit (top fraction bit).

              Instances For

                8-bit value 0xFF, used to test the “all ones” exponent field.

                Instances For
                  @[inline]

                  True iff the sign bit (bit 31) is set.

                  Instances For
                    @[inline]

                    Extract the 8-bit exponent field (bits 30..23).

                    Instances For
                      @[inline]

                      Extract the 23-bit fraction field (bits 22..0).

                      Instances For
                        @[inline]

                        Predicate for NaN: exponent all ones and fraction nonzero.

                        Instances For
                          @[inline]

                          Predicate for quiet NaN (NaN with the quiet bit set).

                          Instances For
                            @[inline]

                            Predicate for signaling NaN (NaN with the quiet bit clear).

                            Instances For
                              @[inline]

                              Predicate for infinity: exponent all ones and fraction zero.

                              Instances For
                                @[inline]

                                Predicate for signed zero (both +0 and -0).

                                Instances For
                                  @[inline]

                                  Predicate for finiteness: exponent field is not all ones (excludes NaN/Inf).

                                  Instances For
                                    @[inline]

                                    +0.0 as an executable binary32 constant.

                                    Instances For
                                      @[inline]

                                      -0.0 as an executable binary32 constant.

                                      Instances For
                                        @[inline]

                                        +1.0 as an IEEE-754 binary32 constant.

                                        Instances For
                                          @[inline]

                                          -1.0 as an IEEE-754 binary32 constant.

                                          Instances For
                                            @[inline]

                                            +∞ as an executable binary32 constant.

                                            Instances For
                                              @[inline]

                                              -∞ as an executable binary32 constant.

                                              Instances For
                                                @[inline]

                                                A canonical quiet NaN payload used by the executable kernel.

                                                Instances For

                                                  NaN selection / payload propagation #

                                                  IEEE-754 leaves some freedom in how NaNs are "chosen" when multiple NaNs appear. For reproducibility (and nicer debugging), we make the choice deterministic (left-to-right) and we quiet signaling NaNs by setting the quiet bit.

                                                  @[inline]

                                                  Quiet a NaN by setting the quiet bit (and leave non-NaNs unchanged).

                                                  Instances For

                                                    If x is a NaN, return it (quieted).

                                                    Instances For

                                                      Choose a NaN from two operands.

                                                      This is the "NaN propagation" policy used by most binary ops in this file:

                                                      • if any operand is a signaling NaN, return that operand (quieted), left-to-right,
                                                      • otherwise if any operand is a quiet NaN, return that operand, left-to-right,
                                                      • otherwise return none.
                                                      Instances For

                                                        Like chooseNaN2, but for ternary ops (used for fma).

                                                        Instances For

                                                          Adjacent floats (nextUp/nextDown) #

                                                          @[inline]

                                                          Smallest positive subnormal (bit pattern 0x00000001, value 2^-149).

                                                          Instances For
                                                            @[inline]

                                                            Smallest negative subnormal (bit pattern 0x80000001, value -2^-149).

                                                            Instances For
                                                              @[inline]

                                                              Largest finite positive float32 (bit pattern 0x7F7FFFFF).

                                                              Instances For
                                                                @[inline]

                                                                Largest finite negative float32 (bit pattern 0xFF7FFFFF).

                                                                Instances For
                                                                  @[inline]

                                                                  nextUp x is the next representable float32 strictly greater than x.

                                                                  IEEE-754 special cases:

                                                                  • NaN propagates (quieted).
                                                                  • nextUp (+∞) = +∞.
                                                                  • nextUp (-0) = +minSubnormal (since +0 is not strictly greater than -0).
                                                                  Instances For
                                                                    @[inline]

                                                                    nextDown x is the next representable float32 strictly less than x.

                                                                    IEEE-754 special cases:

                                                                    • NaN propagates (quieted).
                                                                    • nextDown (-∞) = -∞.
                                                                    • nextDown (+0) = -minSubnormal (since -0 is not strictly less than +0).
                                                                    Instances For
                                                                      @[inline]

                                                                      Flip the sign bit (works for finite/Inf/NaN, and distinguishes ±0).

                                                                      Instances For
                                                                        @[inline]

                                                                        Clear the sign bit.

                                                                        Instances For