Note

Check the version number of this documentation against the gfloat version you are using. “Latest” refers to the head on https://github.com/graphcore-research/gfloat, while pypi versions installed using pip install will have corresponding vX.Y.Z tags.

GFloat: Generic floating point formats in Python

GFloat is designed to allow experimentation with a variety of floating-point formats in Python. Headline features:

  • A wide variety of floating point formats defined in gfloat.formats

    • IEEE 754 and P3109, BFloat, OCP FP8 and MX

  • Conversion between floats under numerous rounding modes

    • Scalar code is optimized for readability

    • Array code is faster, and can operate on Numpy, JAX, or PyTorch arrays.

  • Notebooks useful for teaching and exploring float formats

Provided Formats

Formats are parameterized by the primary parameters of:

  • Width in bits (k)

  • Precision (p)

  • Exponent bias (bias)

with additional fields defining the presence/encoding of:

  • Domain (Finite vs Extended)

  • Signed/unsigned

  • Not-a-number (NaN) values

  • Negative zero

  • Subnormal numbers

  • Two’s complement encoding (of the significand)

This allows an implementation of generic floating point encode/decode logic, handling various current and proposed floating point types:

As well as block formats from OCP MX .

Rounding modes

Various rounding modes:
  • Directed modes: Toward Zero, Toward Positive, Toward Negative

  • Round-to-nearest, with Ties to Even or Ties to Away

  • Stochastic rounding, with specified numbers of random bits

See Also

GFloat, being a pure Python library, favours readability and extensibility over speed (although the *_ndarray functions are reasonably fast for large arrays). For fast implementations of these datatypes see, for example, ml_dtypes, bitstring, MX PyTorch Emulation Library, APyTypes, TorchAO.

To get started with the library, we recommend perusing the notebooks, otherwise you may wish to jump straight into the API.

Contents