Skip to content

Commit 49d2fb8

Browse files
authored
Merge pull request #32 from graphcore-research/docs-cleanup
Various documentation improvements
2 parents f24ca8e + 529a611 commit 49d2fb8

File tree

8 files changed

+979
-526
lines changed

8 files changed

+979
-526
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ handling various current and proposed floating point types:
1010
- [IEEE WG P3109](https://github.com/awf/P3109-Public/blob/main/Shared%20Reports/P3109%20WG%20Interim%20report.pdf): P{p} for p in 1..7
1111
- [OCP MX Formats](https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf): E2M1, M2M3, E3M2, E8M0, INT8, and the MX block formats.
1212

13-
The library favours readability and extensibility over speed - for fast implementations of these datatypes see, for example, [ml_dtypes](https://github.com/jax-ml/ml_dtypes),
13+
The library favours readability and extensibility over speed (although the *_ndarray functions are reasonably fast for large arrays, see the [benchmarking notebook](docs/source/04-benchmark.ipynb)).
14+
For other implementations of these datatypes more focused on speed see, for example, [ml_dtypes](https://github.com/jax-ml/ml_dtypes),
1415
[bitstring](https://github.com/scott-griffiths/bitstring),
1516
[MX PyTorch Emulation Library](https://github.com/microsoft/microxcaling).
1617

docs/source/01-decode.ipynb

Lines changed: 63 additions & 3 deletions
Large diffs are not rendered by default.

docs/source/02-value-stats.ipynb

Lines changed: 797 additions & 508 deletions
Large diffs are not rendered by default.

docs/source/index.rst

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,96 @@ handling various current and proposed floating point types:
3131

3232
- `IEEE 754 <https://en.wikipedia.org/wiki/IEEE_754>`_: Binary16, Binary32
3333
- `Brain floating point <https://en.wikipedia.org/wiki/Bfloat16_floating-point_format>`_: BFloat16
34-
- `OCP Float8 <https://www.opencompute.org/documents/ocp-8-bit-floating-point-specification-ofp8-revision-1-0-2023-06-20-pdf>`_: E5M2, E4M3
35-
- `IEEE WG P3109 <https://github.com/awf/P3109-Public/blob/main/Shared%20Reports/P3109%20WG%20Interim%20report.pdf>`_: P{p} for p in 1..7
36-
- Types from the `OCP MX <https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf>`_ spec: E8M0, INT8, and FP4, FP6 types
37-
34+
- |ocp_link|: E5M2, E4M3
35+
- |p3109_link|: P{p} for p in 1..7
36+
- Types from the |ocp_mx_link| spec: E8M0, INT8, and FP4, FP6 types
37+
38+
As well as block formats from |ocp_mx_link|.
39+
40+
.. |ocp_mx_link| raw:: html
41+
42+
<a href="https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf" target="_blank">
43+
OCP MX
44+
</a>
45+
46+
.. |ocp_link| raw:: html
47+
48+
<a href="https://www.opencompute.org/documents/ocp-8-bit-floating-point-specification-ofp8-revision-1-0-2023-06-20-pdf" target="_blank">
49+
OCP Float8
50+
</a>
51+
52+
.. |p3109_link| raw:: html
53+
54+
<a href="https://raw.githubusercontent.com/P3109/Public/main/Shared%20Reports/P3109%20WG%20Interim%20Report.pdf" target="_blank">
55+
IEEE P3109
56+
</a>
57+
58+
Example
59+
-------
60+
This table (from example notebook :doc:`value-stats <02-value-stats>`) shows how
61+
gfloat has been used to tabulate properties of various floating point formats.
62+
63+
- name: Format
64+
- B: Bits in the format
65+
- P: Precision in bits
66+
- E: Exponent field width in bits
67+
- smallest: Smallest positive value
68+
- smallest_normal: Smallest positive normal value, NaN if no finite values are normal
69+
- max: Largest finite normal value, NaN if all finite values are subnormal
70+
- num_nans: Number of NaN values
71+
- num_infs: Number of infinities (2 or 0)
72+
73+
======== === === === =========== ================= ============ =========== ======
74+
name B P E smallest smallest_normal max num_nans infs
75+
======== === === === =========== ================= ============ =========== ======
76+
ocp_e2m1 4 2 2 0.5 1 6 0 0
77+
ocp_e2m3 6 4 2 0.125 1 7.5 0 0
78+
ocp_e3m2 6 3 3 0.0625 0.25 28 0 0
79+
ocp_e4m3 8 4 4 ≈0.0019531 0.015625 448 2 0
80+
ocp_e5m2 8 3 5 ≈1.5259e-05 ≈6.1035e-05 57344 6 2
81+
p3109_p1 8 1 7 ≈2.1684e-19 ≈2.1684e-19 ≈9.2234e+18 1 2
82+
p3109_p2 8 2 6 ≈2.3283e-10 ≈4.6566e-10 ≈2.1475e+09 1 2
83+
p3109_p3 8 3 5 ≈7.6294e-06 ≈3.0518e-05 49152 1 2
84+
p3109_p4 8 4 4 ≈0.00097656 0.0078125 224 1 2
85+
p3109_p5 8 5 3 0.0078125 0.125 15 1 2
86+
p3109_p6 8 6 2 0.015625 0.5 3.875 1 2
87+
binary16 16 11 5 ≈5.9605e-08 ≈6.1035e-05 65504 2046 2
88+
bfloat16 16 8 8 ≈9.1835e-41 ≈1.1755e-38 ≈3.3895e+38 254 2
89+
binary32 32 24 8 ≈1.4013e-45 ≈1.1755e-38 ≈3.4028e+38 ≈1.6777e+07 2
90+
binary64 64 53 11 4.9407e-324 ≈2.2251e-308 ≈1.7977e+308 ≈9.0072e+15 2
91+
ocp_e8m0 8 1 8 ≈5.8775e-39 ≈5.8775e-39 ≈1.7014e+38 1 0
92+
ocp_int8 8 8 0 0.015625 n/a ≈ 1.9844 0 0
93+
======== === === === =========== ================= ============ =========== ======
94+
95+
In the above table, values which are not exact are indicated with the "≈" symbol.
96+
And here's the same table, but with values which don't render exactly as short floats
97+
printed as rationals times powers of 2:
98+
99+
======== === === === =========== ================= ======================================== ====================================== ======
100+
name B P E smallest smallest_normal max num_nans infs
101+
======== === === === =========== ================= ======================================== ====================================== ======
102+
ocp_e2m1 4 2 2 0.5 1 6 0 0
103+
ocp_e2m3 6 4 2 0.125 1 7.5 0 0
104+
ocp_e3m2 6 3 3 0.0625 0.25 28 0 0
105+
ocp_e4m3 8 4 4 2^-9 0.015625 448 2 0
106+
ocp_e5m2 8 3 5 2^-16 2^-14 57344 6 2
107+
p3109_p1 8 1 7 2^-62 2^-62 2^63 1 2
108+
p3109_p2 8 2 6 2^-32 2^-31 2^31 1 2
109+
p3109_p3 8 3 5 2^-17 2^-15 49152 1 2
110+
p3109_p4 8 4 4 2^-10 0.0078125 224 1 2
111+
p3109_p5 8 5 3 0.0078125 0.125 15 1 2
112+
p3109_p6 8 6 2 0.015625 0.5 3.875 1 2
113+
binary16 16 11 5 2^-24 2^-14 65504 2046 2
114+
bfloat16 16 8 8 2^-133 2^-126 255/128*2^127 254 2
115+
binary32 32 24 8 2^-149 2^-126 16777215/8388608*2^127 8388607/4194304*2^23 2
116+
binary64 64 53 11 4.9407e-324 2^-1022 9007199254740991/9007199254740992*2^1024 4503599627370495/4503599627370496*2^53 2
117+
ocp_e8m0 8 1 8 2^-127 2^-127 2^127 1 0
118+
ocp_int8 8 8 0 0.015625 n/a 127/64*2^0 0 0
119+
======== === === === =========== ================= ======================================== ====================================== ======
120+
121+
122+
See Also
123+
--------
38124

39125
GFloat, being a pure Python library, favours readability and extensibility over speed
40126
(although the `*_ndarray` functions are reasonably fast for large arrays).
@@ -47,6 +133,9 @@ For fast implementations of these datatypes see, for example,
47133
To get started with the library, we recommend perusing the notebooks,
48134
otherwise you may wish to jump straight into the API.
49135

136+
Contents
137+
========
138+
50139
.. toctree::
51140
:hidden:
52141

docs/source/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
# Copyright (c) 2024 Graphcore Ltd. All rights reserved.
22

33
import pandas
4+
from typing import Callable
45
from IPython.display import HTML
56

67

7-
def pandas_render(df: pandas.DataFrame) -> HTML:
8+
def pandas_render(df: pandas.DataFrame, **kwargs) -> HTML:
89
"""
910
Render a dataframe, hiding the index,
1011
and set ID to minimize diffs for notebook regression tests
1112
"""
12-
return HTML(df.style.hide().set_uuid("my_id").to_html())
13+
s = df.style.hide().set_uuid("my_id")
14+
for f, v in kwargs.items():
15+
if isinstance(getattr(s, f, None), Callable):
16+
s = getattr(s, f)(v)
17+
else:
18+
s = s.format(**{f: v})
19+
return HTML(s.to_html())

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ jaxlib
66
jax
77
airium
88
pandas
9+
matplotlib
910

1011
# Requirements for development
1112
pre-commit

src/gfloat/formats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,13 @@ def format_info_p3109(precision: int) -> FormatInfo:
212212
]
213213

214214
all_formats = [
215-
format_info_ocp_e8m0,
216-
format_info_ocp_int8,
217215
*_tiny_formats,
218216
*_fp8_formats,
219217
*_fp16_formats,
220218
format_info_binary32,
221219
format_info_binary64,
220+
format_info_ocp_e8m0,
221+
format_info_ocp_int8,
222222
]
223223

224224
# ------

src/gfloat/printing.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,22 @@ def float_pow2str(v: float, min_exponent: float = -np.inf) -> str:
2121
if not np.isfinite(v):
2222
return str(v)
2323

24-
s = np.sign(v)
24+
signstr = "-" if np.signbit(v) else ""
25+
2526
x = np.abs(v)
26-
e = np.floor(np.log2(x))
27-
sig = x * 2.0**-e
27+
e = int(np.floor(np.log2(x)))
28+
sig = np.ldexp(x, -e)
2829
if e < min_exponent:
29-
sig *= 2.0 ** (e - min_exponent)
30-
e = min_exponent
30+
sig = np.ldexp(sig, e - min_exponent)
31+
e = int(min_exponent)
32+
33+
pow2str = f"2^{e:d}"
3134

3235
significand = fractions.Fraction(sig)
33-
return ("-" if s < 0 else "") + f"{significand}*2^{int(e):d}"
36+
if significand == 1:
37+
return signstr + pow2str
38+
else:
39+
return signstr + f"{significand}*{pow2str}"
3440

3541

3642
def float_tilde_unless_roundtrip_str(v: float, width: int = 14, d: int = 8) -> str:

0 commit comments

Comments
 (0)