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

BUG: Infinite Loop in numpy.base_repr #26143

Closed
Stiibu opened this issue Mar 27, 2024 · 2 comments · Fixed by #26162
Closed

BUG: Infinite Loop in numpy.base_repr #26143

Stiibu opened this issue Mar 27, 2024 · 2 comments · Fixed by #26162
Labels

Comments

@Stiibu
Copy link

Stiibu commented Mar 27, 2024

Describe the issue:

A call to numpy.base_repr with a minimal value of the underlying int-type results in an infinite loop, e.g. -128 for np.int8. The problem seems to be a call to abs.

Reproduce the code example:

import numpy as np

np.base_repr(np.int8(-128))
# np.base_repr(np.int16(-2 ** 15)) fails as well

Error message:

RuntimeWarning: overflow encountered in scalar absolute num = abs(number)

Python and NumPy Versions:

Python 3.12.1
numpy 1.26.4

Runtime Environment:

[{'numpy_version': '1.26.4',
'python': '3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 '
'64 bit (AMD64)]',
'uname': uname_result(system='Windows', node='EVK-NB-649', release='11', version='10.0.22621', machine='AMD64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Haswell',
'filepath': 'C:\Users\$username\AppData\Local\Programs\Python\Python312\Lib\site-packages\numpy.libs\libopenblas64__v0.3.23-293-gc2f4bdbb-gcc_10_3_0-2bde3a66a51006b2b53eb373ff767a3f.dll',
'internal_api': 'openblas',
'num_threads': 16,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.23.dev'}]
None

Context for the issue:

The current implementation of numpy.base_repr is not safely usable.

@krisrosengreen
Copy link
Contributor

There seems to be an issue with the implementation of the abs function for int8, int16 ... int64. All corresponding negative edge values raise an overflow error when the absolute value is taken.

I'm not at all familiar with the Numpy codebase, so I couldn't find where the abs function for these is implemented.

@mhvk
Copy link
Contributor

mhvk commented Mar 27, 2024

Thanks for reporting! This still is an issue on current main... np.base_repr? in ipython tells this is implemented as

numpy/numpy/_core/numeric.py

Lines 2139 to 2154 in e1bf1d6

digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
if base > len(digits):
raise ValueError("Bases greater than 36 not handled in base_repr.")
elif base < 2:
raise ValueError("Bases less than 2 not handled in base_repr.")
num = abs(number)
res = []
while num:
res.append(digits[num % base])
num //= base
if padding:
res.append('0' * padding)
if number < 0:
res.append('-')
return ''.join(reversed(res or '0'))

I think the fix is simple: write num = abs(int(number)) to ensure one converts to a python int - but obviously one also has to add a test case. If you are able to do that, that would be great!

p.s. I don't quite like that np.abs(np.int8(-128)) yields -128 but that's probably best dealt with as a separate issue - #26145

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants