-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: np.percentile fails with internal overflow when using float16 input on large arrays #29003
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
Comments
@friedlerido Could you share the |
For the example above we end up with
and the overflow occurs. The issue might have been introduced in #23912 @seberg. There the dtype of the input array is set on the quantiles, e.g. numpy/numpy/lib/_function_base_impl.py Lines 4273 to 4275 in 2a7a0d0
Here is a small reproducer:
|
Likely NEP 50 itself that changed the dtype, not so much the specific code change? I am not sure immediately if there are some internal calculations that should maybe always use float64 at least (because they are related to the length of an array, so need full float64 mantissa to be pretty correct). To some degree, float16 tends to overflow, but this may also not be ideal for float32. The other thing is that a |
This indeed seems tricky to get right: the output of
We can use We could downcast Several other methods also give incorrect results:
Output:
|
Describe the issue:
When using np.percentile or np.nanpercentile on a large float16 array from real data (≈9 million elements), I get:
ValueError: kth(=-9223372036845518721) out of bounds (9257087)
This happens even for low percentiles like 20 or 50, and is resolved by casting the array to float64.
Either: NumPy automatically upcasts float16 to float64
Or: raises a UserWarning if float16 is passed to percentile
Reproduce the code example:
Error message:
Python and NumPy Versions:
2.2.4
3.11.3 (main, May 23 2023, 13:34:03) [GCC 10.2.1 20210110]
Runtime Environment:
[{'numpy_version': '2.2.4',
'python': '3.11.3 (main, May 23 2023, 13:34:03) [GCC 10.2.1 20210110]',
'uname': uname_result(system='Linux', , release='5.15.0-1039-aws', version='#44~20.04.1-Ubuntu SMP Thu Jun 22 12:21:12 UTC 2023', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Haswell',
'filepath': '/opt/venv/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-6bb31eeb.so',
'internal_api': 'openblas',
'num_threads': 64,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.28'}]
Context for the issue:
No response
The text was updated successfully, but these errors were encountered: