Skip to content

Commit 1dcc925

Browse files
authored
Merge pull request #122 from bksaiki/refactor
Refactor numbers library
2 parents 3d1374a + 87b5e1e commit 1dcc925

File tree

21 files changed

+1372
-1303
lines changed

21 files changed

+1372
-1303
lines changed

fpy2/number/__init__.py

Lines changed: 28 additions & 307 deletions
Original file line numberDiff line numberDiff line change
@@ -1,316 +1,37 @@
1-
from typing import TypeAlias
1+
22

33
# Numbers
4-
from .number import RealFloat, Float, Real
4+
from .number import Float, RealFloat, Real
55

66
# Contexts
7-
from .context import Context, OrdinalContext, SizedContext, EncodableContext
8-
from .exponential import ExpContext
9-
from .efloat import EFloatContext, EFloatNanKind
10-
from .fixed import FixedContext
11-
from .ieee754 import IEEEContext
12-
from .mp_fixed import MPFixedContext
13-
from .mp_float import MPFloatContext
14-
from .mpb_fixed import MPBFixedContext
15-
from .mpb_float import MPBFloatContext
16-
from .mps_float import MPSFloatContext
17-
from .real import RealContext, REAL
18-
from .sm_fixed import SMFixedContext
7+
from .context import (
8+
# abstract contexts
9+
Context, OrdinalContext, SizedContext, EncodableContext,
10+
# concrete contexts
11+
ExpContext,
12+
EFloatContext, EFloatNanKind,
13+
FixedContext,
14+
IEEEContext,
15+
MPFixedContext,
16+
MPFloatContext,
17+
MPBFixedContext,
18+
MPBFloatContext,
19+
MPSFloatContext,
20+
RealContext,
21+
SMFixedContext,
22+
# context instances
23+
FP256, FP128, FP64, FP32, FP16,
24+
TF32, BF16,
25+
S1E5M2, S1E4M3,
26+
MX_E5M2, MX_E4M3, MX_E3M2, MX_E2M3, MX_E2M1, MX_E8M0, MX_INT8,
27+
FP8P1, FP8P2, FP8P3, FP8P4, FP8P5, FP8P6, FP8P7,
28+
SINT8, SINT16, SINT32, SINT64,
29+
UINT8, UINT16, UINT32, UINT64,
30+
INTEGER, REAL,
31+
)
1932

2033
# Rounding
21-
from .round import RoundingMode, RoundingDirection, OverflowMode
34+
from .round import RoundingMode, RoundingDirection, OverflowMode, RM, OV
2235

2336
# Miscellaneous
2437
from .native import default_float_convert, default_str_convert
25-
26-
###########################################################
27-
# Type aliases
28-
29-
RM: TypeAlias = RoundingMode
30-
"""alias for `RoundingMode`"""
31-
32-
OV: TypeAlias = OverflowMode
33-
"""alias for `OverflowMode`"""
34-
35-
###########################################################
36-
# Format aliases
37-
38-
FP256 = IEEEContext(19, 256, RM.RNE)
39-
"""
40-
Alias for the IEEE 754 octuple precision (256-bit) floating point format
41-
with round nearest, ties-to-even rounding mode.
42-
"""
43-
44-
FP128 = IEEEContext(15, 128, RM.RNE)
45-
"""
46-
Alias for the IEEE 754 quadruple precision (128-bit) floating point format
47-
with round nearest, ties-to-even rounding mode.
48-
"""
49-
50-
FP64 = IEEEContext(11, 64, RM.RNE)
51-
"""
52-
Alias for the IEEE 754 double precision (64-bit) floating point format
53-
with round nearest, ties-to-even rounding mode.
54-
55-
This context is Python's native float type.
56-
"""
57-
58-
FP32 = IEEEContext(8, 32, RM.RNE)
59-
"""
60-
Alias for the IEEE 754 single precision (32-bit) floating point format
61-
with round nearest, ties-to-even rounding mode.
62-
"""
63-
64-
FP16 = IEEEContext(5, 16, RM.RNE)
65-
"""
66-
Alias for the IEEE 754 half precision (16-bit) floating point format
67-
with round nearest, ties-to-even rounding mode.
68-
"""
69-
70-
TF32 = IEEEContext(8, 19, RM.RNE)
71-
"""
72-
Alias for Nvidia's TensorFloat-32 (TF32) floating point format
73-
with round nearest, ties-to-even rounding mode.
74-
"""
75-
76-
BF16 = IEEEContext(8, 16, RM.RNE)
77-
"""
78-
Alias for Google's Brain Floating Point (BF16) floating point format
79-
with round nearest, ties-to-even rounding mode.
80-
"""
81-
82-
S1E5M2 = EFloatContext(5, 8, False, EFloatNanKind.NEG_ZERO, -1, RM.RNE)
83-
"""
84-
Alias for Graphcore's FP8 format with 5 bits of exponent
85-
with round nearest, ties-to-even rounding mode.
86-
87-
See Graphcore's FP8 proposal for more information: https://arxiv.org/pdf/2206.02915.
88-
"""
89-
90-
S1E4M3 = EFloatContext(4, 8, False, EFloatNanKind.NEG_ZERO, -1, RM.RNE)
91-
"""
92-
Alias for Graphcore's FP8 format with 4 bits of exponent
93-
with round nearest, ties-to-even rounding mode.
94-
95-
See Graphcore's FP8 proposal for more information: https://arxiv.org/pdf/2206.02915.
96-
"""
97-
98-
MX_E5M2 = IEEEContext(5, 8, RM.RNE)
99-
"""
100-
Alias for the FP8 format with 5 bits of exponent in
101-
the Open Compute Project (OCP) Microscaling Formats (MX) specification
102-
with round nearest, ties-to-even rounding mode.
103-
104-
See the OCP MX specification for more information:
105-
https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
106-
"""
107-
108-
MX_E4M3 = EFloatContext(4, 8, False, EFloatNanKind.MAX_VAL, 0, RM.RNE)
109-
"""
110-
Alias for the FP8 format with 4 bits of exponent in
111-
the Open Compute Project (OCP) Microscaling Formats (MX) specification
112-
with round nearest, ties-to-even rounding mode.
113-
114-
See the OCP MX specification for more information:
115-
https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
116-
"""
117-
118-
MX_E3M2 = EFloatContext(3, 6, False, EFloatNanKind.NONE, 0, RM.RNE)
119-
"""
120-
Alias for the FP6 format with 3 bits of exponent in
121-
the Open Compute Project (OCP) Microscaling Formats (MX) specification
122-
with round nearest, ties-to-even rounding mode.
123-
124-
See the OCP MX specification for more information:
125-
https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
126-
"""
127-
128-
MX_E2M3 = EFloatContext(2, 6, False, EFloatNanKind.NONE, 0, RM.RNE)
129-
"""
130-
Alias for the FP6 format with 2 bits of exponent in
131-
the Open Compute Project (OCP) Microscaling Formats (MX) specification
132-
with round nearest, ties-to-even rounding mode.
133-
134-
See the OCP MX specification for more information:
135-
https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
136-
"""
137-
138-
MX_E2M1 = EFloatContext(2, 4, False, EFloatNanKind.NONE, 0, RM.RNE)
139-
"""
140-
Alias for the FP4 format with 2 bits of exponent in
141-
the Open Compute Project (OCP) Microscaling Formats (MX) specification
142-
with round nearest, ties-to-even rounding mode.
143-
144-
See the OCP MX specification for more information:
145-
https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
146-
"""
147-
148-
MX_E8M0 = ExpContext(8, 0)
149-
"""
150-
Alias for the MX scaling format in the Open Compute Project (OCP)
151-
Microscaling Formats (MX) specification with round nearest, ties-to-even
152-
rounding mode.
153-
154-
This is just the exponent field of a single-precision floating-point value (`FP32`)
155-
with `encode(x) == 0xFF` representing NaN and `encode(x) == 0x00` representing
156-
the exponent one below the minimum normal exponent value.
157-
"""
158-
159-
MX_INT8 = FixedContext(True, -6, 8, RM.RNE)
160-
"""
161-
Alias for the MX 8-bit integer format in the Open Compute Project (OCP)
162-
Microscaling Formats (MX) specification with round nearest, ties-to-even
163-
rounding mode.
164-
165-
This implementation uses the standard asymmetric encoding inherited
166-
from fixed-point formats, with `+MAX_VAL = +1 63/64` and `-MAX_VAL = -2`.
167-
"""
168-
169-
FP8P1 = EFloatContext(7, 8, True, EFloatNanKind.NEG_ZERO, 0, RM.RNE)
170-
"""
171-
Alias for the FP8 format with 7 bits of exponent found in
172-
a draft proposal by the IEEE P3109 working group
173-
with round nearest, ties-to-even rounding mode.
174-
Format subject to change.
175-
176-
See the IEEE P3109 working group for more information:
177-
https://github.com/P3109/Public/blob/main/IEEE%20WG%20P3109%20Interim%20Report.pdf
178-
"""
179-
180-
FP8P2 = EFloatContext(6, 8, True, EFloatNanKind.NEG_ZERO, -1, RM.RNE)
181-
"""
182-
Alias for the FP8 format with 6 bits of exponent found in
183-
a draft proposal by the IEEE P3109 working group
184-
with round nearest, ties-to-even rounding mode.
185-
Format subject to change.
186-
187-
See the IEEE P3109 working group for more information:
188-
https://github.com/P3109/Public/blob/main/IEEE%20WG%20P3109%20Interim%20Report.pdf
189-
"""
190-
191-
FP8P3 = EFloatContext(5, 8, True, EFloatNanKind.NEG_ZERO, -1, RM.RNE)
192-
"""
193-
Alias for the FP8 format with 5 bits of exponent found in
194-
a draft proposal by the IEEE P3109 working group
195-
with round nearest, ties-to-even rounding mode.
196-
Format subject to change.
197-
198-
See the IEEE P3109 working group for more information:
199-
https://github.com/P3109/Public/blob/main/IEEE%20WG%20P3109%20Interim%20Report.pdf
200-
"""
201-
202-
FP8P4 = EFloatContext(4, 8, True, EFloatNanKind.NEG_ZERO, -1, RM.RNE)
203-
"""
204-
Alias for the FP8 format with 4 bits of exponent found in
205-
a draft proposal by the IEEE P3109 working group
206-
with round nearest, ties-to-even rounding mode.
207-
Format subject to change.
208-
209-
See the IEEE P3109 working group for more information:
210-
https://github.com/P3109/Public/blob/main/IEEE%20WG%20P3109%20Interim%20Report.pdf
211-
"""
212-
213-
FP8P5 = EFloatContext(3, 8, True, EFloatNanKind.NEG_ZERO, -1, RM.RNE)
214-
"""
215-
Alias for the FP8 format with 3 bits of exponent found in
216-
a draft proposal by the IEEE P3109 working group
217-
with round nearest, ties-to-even rounding mode.
218-
Format subject to change.
219-
220-
See the IEEE P3109 working group for more information:
221-
https://github.com/P3109/Public/blob/main/IEEE%20WG%20P3109%20Interim%20Report.pdf
222-
"""
223-
224-
FP8P6 = EFloatContext(2, 8, True, EFloatNanKind.NEG_ZERO, -1, RM.RNE)
225-
"""
226-
Alias for the FP8 format with 2 bits of exponent found in
227-
a draft proposal by the IEEE P3109 working group
228-
with round nearest, ties-to-even rounding mode.
229-
Format subject to change.
230-
231-
See the IEEE P3109 working group for more information:
232-
https://github.com/P3109/Public/blob/main/IEEE%20WG%20P3109%20Interim%20Report.pdf
233-
"""
234-
235-
FP8P7 = EFloatContext(1, 8, True, EFloatNanKind.NEG_ZERO, -1, RM.RNE)
236-
"""
237-
Alias for the FP8 format with 1 bit of exponent found in
238-
a draft proposal by the IEEE P3109 working group
239-
with round nearest, ties-to-even rounding mode.
240-
Format subject to change.
241-
242-
See the IEEE P3109 working group for more information:
243-
https://github.com/P3109/Public/blob/main/IEEE%20WG%20P3109%20Interim%20Report.pdf
244-
"""
245-
246-
INTEGER = MPFixedContext(-1, RoundingMode.RTZ)
247-
"""
248-
Alias for an arbitrary-precision integer context with
249-
round towards zero rounding mode.
250-
251-
Numbers rounded under this context behave like Python's native `int` type.
252-
"""
253-
254-
SINT8 = FixedContext(True, 0, 8, RM.RTZ, OV.WRAP)
255-
"""
256-
Alias for a signed 8-bit integer context with
257-
round towards zero rounding mode and wrapping overflow behavior.
258-
259-
Rounding infinity or NaN under this context produces an OverflowError.
260-
"""
261-
262-
SINT16 = FixedContext(True, 0, 16, RM.RTZ, OV.WRAP)
263-
"""
264-
Alias for a signed 16-bit integer context with
265-
round towards zero rounding mode and wrapping overflow behavior.
266-
267-
Rounding infinity or NaN under this context produces an OverflowError.
268-
"""
269-
270-
SINT32 = FixedContext(True, 0, 32, RM.RTZ, OV.WRAP)
271-
"""
272-
Alias for a signed 32-bit integer context with
273-
round towards zero rounding mode and wrapping overflow behavior.
274-
275-
Rounding infinity or NaN under this context produces an OverflowError.
276-
"""
277-
278-
SINT64 = FixedContext(True, 0, 64, RM.RTZ, OV.WRAP)
279-
"""
280-
Alias for a signed 64-bit integer context with
281-
round towards zero rounding mode and wrapping overflow behavior.
282-
283-
Rounding infinity or NaN under this context produces an OverflowError.
284-
"""
285-
286-
UINT8 = FixedContext(False, 0, 8, RM.RTZ, OV.WRAP)
287-
"""
288-
Alias for an unsigned 8-bit integer context with
289-
round towards zero rounding mode and wrapping overflow behavior.
290-
291-
Rounding infinity or NaN under this context produces an OverflowError.
292-
"""
293-
294-
UINT16 = FixedContext(False, 0, 16, RM.RTZ, OV.WRAP)
295-
"""
296-
Alias for an unsigned 16-bit integer context with
297-
round towards zero rounding mode and wrapping overflow behavior.
298-
299-
Rounding infinity or NaN under this context produces an OverflowError.
300-
"""
301-
302-
UINT32 = FixedContext(False, 0, 32, RM.RTZ, OV.WRAP)
303-
"""
304-
Alias for an unsigned 32-bit integer context with
305-
round towards zero rounding mode and wrapping overflow behavior.
306-
307-
Rounding infinity or NaN under this context produces an OverflowError.
308-
"""
309-
310-
UINT64 = FixedContext(False, 0, 64, RM.RTZ, OV.WRAP)
311-
"""
312-
Alias for an unsigned 64-bit integer context with
313-
round towards zero rounding mode and wrapping overflow behavior.
314-
315-
Rounding infinity or NaN under this context produces an OverflowError.
316-
"""

0 commit comments

Comments
 (0)