Skip to content

BUG: np.triu_indices raises OverflowError with np.uint64 input while np.tril_indices does not #29488

@Kairoven

Description

@Kairoven

Describe the issue:

np.triu_indices raises an OverflowError when given a np.uint64 input, while np.tril_indices handles it properly. This is due to an inconsistency in their internal use of the k parameter and integer arithmetic involving unsigned integers.

In the implementation:

tri_ = ~tri(n, m, k=k - 1, dtype=bool)

When k is default (i.e., 0), this becomes k - 1-1. With n as np.uint64, this causes a problem in:

arange(-k, M - k, dtype=_min_int(-k, M - k))

Where -1 is not representable in uint64, triggering an OverflowError.

By contrast, np.tril_indices uses:

tri_ = tri(n, m, k=k, dtype=bool)

which avoids this issue.

It might be worth considering whether this difference is intentional or if np.triu_indices could be made more robust in such cases.

Reproduce the code example:

import numpy as np

n = np.uint64(4)

# This works as expected
print(np.tril_indices(n))

# This raises an OverflowError
print(np.triu_indices(n))

Error message:

File "/home/user/workspace/xxx/test.py", line 72, in <module>
    iu = np.triu_indices(n)
         ^^^^^^^^^^^^^^^^^^
  File "/home/user/miniconda3/envs/xxx/lib/python3.12/site-packages/numpy/lib/_twodim_base_impl.py", line 1135, in triu_indices
    tri_ = ~tri(n, m, k=k - 1, dtype=bool)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/miniconda3/envs/xxx/lib/python3.12/site-packages/numpy/lib/_twodim_base_impl.py", line 439, in tri
    arange(-k, M - k, dtype=_min_int(-k, M - k)))
               ~~^~~
OverflowError: Python integer -1 out of bounds for uint64

Python and NumPy Versions:

2.3.1
3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, 17:29:18) [GCC 11.2.0]

Runtime Environment:

[{'numpy_version': '2.3.1',
'python': '3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, '
'17:29:18) [GCC 11.2.0]',
'uname': uname_result(system='Linux', node='gpu-node5', release='5.4.0-100-generic', version='https://github.com//pull/113-Ubuntu SMP Thu Feb 3 18:43:29 UTC 2022', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2',
'AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL'],
'not_found': ['AVX512_KNL', 'AVX512_KNM', 'AVX512_SPR']}},
{'architecture': 'SkylakeX',
'filepath': '/home/miniconda3/envs/pbt/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so',
'internal_api': 'openblas',
'num_threads': 64,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.29'}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - BugsprintableIssue fits the time-frame and setting of a sprint

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions