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:
IEEE 754: Binary16, Binary32
Brain floating point: BFloat16
IEEE P3109 : P3109_{K}p{P}{s}{d} for K > 2, and 1 <= P <= K, Signedness, and Domain
OCP Float8 : E5M2, E4M3
Types from the OCP MX spec: E8M0, INT8, and FP4, FP6 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
- Notebooks
- GFloat Basics
- Collect value statistics for formats
- Making value tables
- Timing tests
- Stochastic rounding
- API
- Scalar Functions
- Array Functions
- Block format functions
- Classes
FormatInfoFormatInfo.nameFormatInfo.kFormatInfo.precisionFormatInfo.biasFormatInfo.is_signedFormatInfo.domainFormatInfo.has_nzFormatInfo.num_high_nansFormatInfo.has_subnormalsFormatInfo.is_twos_complementFormatInfo.tSignificandBitsFormatInfo.expBitsFormatInfo.signBitsFormatInfo.emaxFormatInfo.bitsFormatInfo.epsFormatInfo.epsnegFormatInfo.iexpFormatInfo.machepFormatInfo.maxFormatInfo.maxexpFormatInfo.minFormatInfo.num_nansFormatInfo.code_of_nanFormatInfo.code_of_posinfFormatInfo.code_of_neginfFormatInfo.code_of_zeroFormatInfo.has_zeroFormatInfo.code_of_negzeroFormatInfo.num_posinfsFormatInfo.num_neginfsFormatInfo.num_infsFormatInfo.code_of_maxFormatInfo.code_of_minFormatInfo.smallest_normalFormatInfo.smallest_subnormalFormatInfo.smallestFormatInfo.is_all_subnormal
FloatClassRoundModeFloatValueBlockFormatInfo
- Pretty printers
- Defined Formats