TorchLean API

FloatLib.Floats.Formats.OCP.MX.Standard.Runtime

Exact destination quantization for concrete OCP MX blocks #

The default conversion implements the recommended algorithm of OCP MX 1.0 §6.3: select the largest input binade, subtract the element's largest power-of-two exponent, and quantize each scaled input with nearest-even rounding and saturation. The all-zero block uses scale 2^0. An exponent below -127 is clamped to -127, and one above 127 produces a NaN block. Non-finite input lanes also produce a NaN block. These choices resolve cases for which §6.3 does not prescribe a scale, and they do not affect how stored non-finite element words decode.

Nearest rounding is relative to the selected scale, not to all possible block encodings. For example, a constant E4M3 block with value 15/8 selects scale 2^-8 and saturates to 7/4, even though the input is representable using another scale.

Element rounding searches at most 256 words with exact rational comparisons. This bounded reference kernel covers floating and two's-complement elements through the same numerical contract. Its cost depends on the size of the exact input rationals, not on an unbounded shift derived from an exponent field. Signed floating zeros are retained.

FP8's required SAT and OVF element-conversion modes (§5.3.1, Table 3) are the (overflow := ...) option, .saturate or .native, of Numerics.OverflowMode. The default is SAT, as recommended in §6.3. FP4, FP6, and INT8 always saturate. Their scalar NaN conversion maps to zero, while block conversion records a NaN scale.

Reference: OCP, Microscaling Formats (MX) Specification, version 1.0, September 2023, §§5.3 and 6.3, https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf.

All finite element words paired with their exact rational values.

Instances For

    Nearest-even ordering key: distance first, then even least-significant bit, then zero sign. The last component only resolves duplicate numerical encodings such as the two floating zeros.

    Instances For

      Finite nearest-even conversion minimizes absolute error. Ties favor an even low bit and then the input's zero sign. This relation is independent of how candidates are enumerated.

      Instances For

        Saturating nearest-even element rounding, using only exact rational comparisons.

        Instances For

          Signed finite endpoint, including the permitted -2 endpoint of this INT8 profile.

          Instances For

            Canonical scalar NaN when the element has one, and zero for the finite-only encodings.

            Instances For

              Scalar FP8 OVF result, or the finite endpoint for a finite-only element encoding.

              Instances For

                FP8 overflow after nearest-even rounding. The E4M3 midpoint 464 rounds to the even finite 448, and the E5M2 midpoint 61440 overflows.

                Instances For

                  Convert a scalar observation to an element, before applying any shared block scale.

                  overflow := .saturate (the default) is the SAT conversion mode of §5.3.1 and .native the OVF mode, which only FP8 elements distinguish: other profiles saturate under both.

                  Instances For

                    Numerical element contract, with the FP8 after-rounding overflow alternative.

                    Instances For

                      Largest absolute finite input, computed before any element rounding.

                      Instances For

                        Recommended shared exponent before handling the finite E8M0 range.

                        Instances For

                          Recommended max-binade scale. An all-zero block gets 2^0, an exponent below -127 clamps to -127, and one above 127 gives the NaN scale, so only finite E8M0 exponents reach rational scaling in the element kernel.

                          Instances For

                            Quantize a finite vector at the given scale, or produce the canonical NaN block.

                            Instances For

                              Recommended scale selection followed by one rounding of each exact scaled lane.

                              Instances For

                                Destination contract: use the chosen shared scale and round each normalized lane according to the numerical element contract, or propagate an unrepresentable scale as NaN.

                                Instances For

                                  Convert a vector of scalar observations. Any non-finite lane produces a NaN shared scale. Finite neighbors are preserved when decoding a stored block, but not during this scale selection.

                                  Instances For