-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce IEEE P3109 dtypes #122
Open
awf
wants to merge
4
commits into
jax-ml:main
Choose a base branch
from
awf:p3109
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,10 @@ | |
from ml_dtypes._ml_dtypes_ext import float8_e4m3fnuz | ||
from ml_dtypes._ml_dtypes_ext import float8_e5m2 | ||
from ml_dtypes._ml_dtypes_ext import float8_e5m2fnuz | ||
from ml_dtypes._ml_dtypes_ext import float8_p3109_p3 | ||
from ml_dtypes._ml_dtypes_ext import float8_p3109_p4 | ||
from ml_dtypes._ml_dtypes_ext import float8_p3109_p5 | ||
|
||
import numpy as np | ||
|
||
_bfloat16_dtype = np.dtype(bfloat16) | ||
|
@@ -30,6 +34,9 @@ | |
_float8_e4m3fnuz_dtype = np.dtype(float8_e4m3fnuz) | ||
_float8_e5m2_dtype = np.dtype(float8_e5m2) | ||
_float8_e5m2fnuz_dtype = np.dtype(float8_e5m2fnuz) | ||
_float8_p3109_p3_dtype = np.dtype(float8_p3109_p3) | ||
_float8_p3109_p4_dtype = np.dtype(float8_p3109_p4) | ||
_float8_p3109_p5_dtype = np.dtype(float8_p3109_p5) | ||
|
||
|
||
class _Bfloat16MachArLike: | ||
|
@@ -86,6 +93,29 @@ def __init__(self): | |
self.smallest_subnormal = float8_e5m2fnuz(smallest_subnormal) | ||
|
||
|
||
class _Float8IEEEMachArLike: | ||
|
||
def __init__(self, p): | ||
# These are hard-coded in order to independently test against the computed values in the C++ implementation | ||
if p == 3: | ||
smallest_normal = float.fromhex("0x1p-15") | ||
self.smallest_normal = float8_p3109_p3(smallest_normal) | ||
smallest_subnormal = float.fromhex("0x1p-17") | ||
self.smallest_subnormal = float8_p3109_p3(smallest_subnormal) | ||
|
||
if p == 4: | ||
smallest_normal = float.fromhex("0x1p-7") | ||
self.smallest_normal = float8_p3109_p4(smallest_normal) | ||
smallest_subnormal = float.fromhex("0x1p-10") | ||
self.smallest_subnormal = float8_p3109_p4(smallest_subnormal) | ||
|
||
if p == 5: | ||
smallest_normal = float.fromhex("0x1p-3") | ||
self.smallest_normal = float8_p3109_p5(smallest_normal) | ||
smallest_subnormal = float.fromhex("0x1p-7") | ||
self.smallest_subnormal = float8_p3109_p5(smallest_subnormal) | ||
|
||
|
||
class finfo(np.finfo): # pylint: disable=invalid-name,missing-class-docstring | ||
__doc__ = np.finfo.__doc__ | ||
_finfo_cache: Dict[np.dtype, np.finfo] = {} | ||
|
@@ -360,6 +390,114 @@ def float_to_str(f): | |
# pylint: enable=protected-access | ||
return obj | ||
|
||
@staticmethod | ||
def _float8_p3109_p_finfo(p): | ||
def float_to_str(f): | ||
return "%6.2e" % float(f) | ||
|
||
# pylint: disable=protected-access | ||
obj = object.__new__(np.finfo) | ||
|
||
if p == 3: | ||
dtype = float8_p3109_p3 | ||
obj.dtype = _float8_p3109_p3_dtype | ||
elif p == 4: | ||
dtype = float8_p3109_p4 | ||
obj.dtype = _float8_p3109_p4_dtype | ||
elif p == 5: | ||
dtype = float8_p3109_p5 | ||
obj.dtype = _float8_p3109_p5_dtype | ||
else: | ||
raise NotImplementedError() | ||
|
||
obj._machar = _Float8IEEEMachArLike(p) | ||
|
||
bias = 2 ** (7 - p) | ||
tiny = obj._machar.smallest_normal | ||
machep = 1 - p | ||
eps = 2.0**machep | ||
negep = -p | ||
epsneg = 2.0**negep | ||
max_ = (1 - 2 ** (1 - p)) * 2**bias # 1'0000 - 0'0010 = 0'1110 | ||
|
||
if p == 3: | ||
assert tiny == float.fromhex("0x1p-15") | ||
assert eps == float.fromhex("0x1p-2") | ||
assert epsneg == float.fromhex("0x1p-3") | ||
assert max_ == float.fromhex("0x1.8p15") | ||
elif p == 4: | ||
assert tiny == float.fromhex("0x1p-7") | ||
assert eps == float.fromhex("0x1p-3") | ||
assert epsneg == float.fromhex("0x1p-4") | ||
assert max_ == float.fromhex("0x1.Cp7") | ||
elif p == 5: | ||
assert tiny == float.fromhex("0x1p-3") | ||
assert eps == float.fromhex("0x1p-4") | ||
assert epsneg == float.fromhex("0x1p-5") | ||
assert max_ == float.fromhex("0x1.Ep3") | ||
else: | ||
raise NotImplementedError() | ||
|
||
obj.bits = 8 | ||
|
||
# nextafter(1.0, Inf) - 1.0 | ||
obj.eps = dtype(eps) | ||
|
||
# The exponent that yields eps. | ||
obj.machep = machep | ||
|
||
# 1.0 = nextafter(1.0, -Inf) | ||
obj.epsneg = dtype(epsneg) | ||
|
||
# The exponent that yields epsneg. | ||
obj.negep = negep | ||
|
||
# The largest representable number. | ||
obj.max = dtype(max_) | ||
|
||
# The smallest representable number, typically -max. | ||
obj.min = dtype(-max_) | ||
|
||
obj.nexp = 8 - p | ||
obj.nmant = p - 1 | ||
obj.iexp = obj.nexp | ||
obj.maxexp = bias | ||
obj.minexp = 1 - bias | ||
|
||
# The approximate number of decimal digits to which this kind of float is precise. | ||
obj.precision = 1 if p < 4 else 2 | ||
|
||
# The approximate decimal resolution of this type, i.e., 10**-precision. | ||
obj.resolution = dtype(10**-obj.precision) | ||
|
||
if not hasattr(obj, "tiny"): | ||
obj.tiny = dtype(tiny) | ||
if not hasattr(obj, "smallest_normal"): | ||
obj.smallest_normal = obj._machar.smallest_normal | ||
obj.smallest_subnormal = obj._machar.smallest_subnormal | ||
|
||
obj._str_tiny = float_to_str(tiny) | ||
obj._str_smallest_normal = float_to_str(tiny) | ||
obj._str_smallest_subnormal = float_to_str(obj.smallest_subnormal) | ||
obj._str_max = float_to_str(max_) | ||
obj._str_epsneg = float_to_str(epsneg) | ||
obj._str_eps = float_to_str(eps) | ||
obj._str_resolution = float_to_str(obj.resolution) | ||
# pylint: enable=protected-access | ||
return obj | ||
|
||
@staticmethod | ||
def _float8_p3109_p3_finfo(): | ||
return finfo._float8_p3109_p_finfo(3) | ||
|
||
@staticmethod | ||
def _float8_p3109_p4_finfo(): | ||
return finfo._float8_p3109_p_finfo(4) | ||
|
||
@staticmethod | ||
def _float8_p3109_p5_finfo(): | ||
return finfo._float8_p3109_p_finfo(5) | ||
|
||
def __new__(cls, dtype): | ||
if ( | ||
isinstance(dtype, str) | ||
|
@@ -411,4 +549,14 @@ def __new__(cls, dtype): | |
if _float8_e5m2fnuz_dtype not in cls._finfo_cache: | ||
cls._finfo_cache[_float8_e5m2fnuz_dtype] = cls._float8_e5m2fnuz_finfo() | ||
return cls._finfo_cache[_float8_e5m2fnuz_dtype] | ||
for type_str, test_dtype, finfo in ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's confusing that the local There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally agree, apologies. |
||
("float8_p3109_p3", _float8_p3109_p3_dtype, cls._float8_p3109_p3_finfo), | ||
("float8_p3109_p4", _float8_p3109_p4_dtype, cls._float8_p3109_p4_finfo), | ||
("float8_p3109_p5", _float8_p3109_p5_dtype, cls._float8_p3109_p5_finfo), | ||
): | ||
if isinstance(dtype, str) and dtype == type_str or dtype == test_dtype: | ||
if test_dtype not in cls._finfo_cache: | ||
cls._finfo_cache[test_dtype] = finfo() | ||
return cls._finfo_cache[test_dtype] | ||
|
||
return super().__new__(cls, dtype) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems strange for a static method to refer to its class by name...
Maybe we could remove these three and use
cls._float8_p3109_p_finfo(p)
directly in__new__
belowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
Done. As an aside I put all the tests into the same
for
loop, makes the code rather tidier (and no measurable speed impact in pytest), hope that's reasonable.