Skip to content
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

Support np.testing.assert_array_max_ulp #56

Open
mrahtz opened this issue Apr 6, 2023 · 1 comment
Open

Support np.testing.assert_array_max_ulp #56

mrahtz opened this issue Apr 6, 2023 · 1 comment
Labels
contributions welcome We welcome community contributions for this issue! enhancement New feature or request

Comments

@mrahtz
Copy link

mrahtz commented Apr 6, 2023

When working with bfloat16, given how variable the precision is, it would be super useful to be able to make assertions based on the number of ULPs between numbers rather than traditional measures of tolerance like atol/rtol.

However, np.testing.assert_array_max_ulp does not currently work with the bfloat16 type from ml_dtypes:

>>> a = np.array(0.0, dtype=ml_dtypes.bfloat16)
>>> np.testing.assert_array_max_ulp(a, a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "numpy/testing/_private/utils.py", line 1638, in assert_array_max_ulp
    ret = nulp_diff(a, b, dtype)
  File "numpy/testing/_private/utils.py", line 1686, in nulp_diff
    t = np.common_type(x, y)
  File "<__array_function__ internals>", line 180, in common_type
  File "numpy/lib/type_check.py", line 730, in common_type
    raise TypeError("can't get common type for non-numeric array")
TypeError: can't get common type for non-numeric array

Given how ml_dtypes integrates with NumPy/the existence of NumPy's own float16 dtype, I'm not sure whether this is possible - but I'll open this issue on the off chance that it might be :)

Thanks!

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 6, 2023

Thanks for the request – it looks like numpy would have to provide some sort of hook in order for us to do this: the supported dtypes are hard-coded here: https://github.com/numpy/numpy/blob/073d1f39f6ed2735a7fca029a34648c258a80550/numpy/lib/type_check.py#L669

Similar to ml_dtypes.finfo, we could provide a version of this and related functions that would support the dtype extensions here, or we could advocate for numpy to make its code more extensible.

@hawkinsp hawkinsp added enhancement New feature or request contributions welcome We welcome community contributions for this issue! labels Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributions welcome We welcome community contributions for this issue! enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants