Accumulator bit-width for `np.sum` does not match NumPy
Description
The Numba implementation for np.sum shows an accumulator-bit width for unsigned integers less than 64 bits in width that does not match NumPy. np.cumsum is also likely to be affected.
Reproducer
import numpy as np
from numba import jit
@jit
def f(a):
return np.sum(a)
dtypes = [np.int8, np.int16, np.int32, np.int64,
np.uint8, np.uint16, np.uint32, np.uint64]
print(f"{'input':8s} {'numpy':8s} numba")
for dt in dtypes:
input_array = np.array([23, 42], dtype=dt)
numpy_dtype = np.sum(input_array).dtype
f(input_array)
numba_dtype = f.nopython_signatures[-1].return_type
print(f"{np.dtype(dt).name:8s} {str(numpy_dtype):8s} {numba_dtype}")Results
Running on main at 4b3e8fb92f886b199434a74c7aec9fecfeb834b8 on a Mac Book Pro M4 (osx-arm64):
$ numba -s | grep "Numba Version"
Numba Version : 0.69.0dev0+41.g4b3e8fb92f
$ python bug.py
input numpy numba
int8 int64 int64
int16 int64 int64
int32 int64 int64
int64 int64 int64
uint8 uint64 int64
uint16 uint64 int64
uint32 uint64 int64
uint64 uint64 uint64As you can see the dtypes [int8, int16, int32] are accumulated as int64 by Numba but should be uint64 as in NumPy.
Running on faf66df31b44231f99fcf231b251ddd5314bf3bf (commit before merging #10625) with the same hardware:
$ numba -s | grep "Numba Version"
Numba Version : 0.67.0dev1+6.gfaf66df31b
$ python bug.py
input numpy numba
int8 int64 int64
int16 int64 int64
int32 int64 int64
int64 int64 int64
uint8 uint64 uint64
uint16 uint64 uint64
uint32 uint64 uint64
uint64 uint64 uint64The accumulator bit-width is fine on this commit, hence this is classified as regression.
Affected Versions
Numba 0.67 is affected, 0.66 is fine. The issue still exists on main today as outlined above.
Running on 0.67.0 tag:
$ numba -s | grep "Numba Version"
Numba Version : 0.67.0
$ python bug.py
input numpy numba
int8 int64 int64
int16 int64 int64
int32 int64 int64
int64 int64 int64
uint8 uint64 int64
uint16 uint64 int64
uint32 uint64 int64
uint64 uint64 uint64And on 0.66.0:
$ numba -s | grep "Numba Version"
Numba Version : 0.66.0
$ python bug.py
input numpy numba
int8 int64 int64
int16 int64 int64
int32 int64 int64
int64 int64 int64
uint8 uint64 uint64
uint16 uint64 uint64
uint32 uint64 uint64
uint64 uint64 uint64Root Cause and Origin
The root cause is in the line:
This was first introduced in #10625 in two places:
- https://github.com/numba/numba/blob/5aad44c4/numba/np/arraymath.py#L391-L397
- https://github.com/numba/numba/blob/5aad44c4/numba/np/arraymath.py#L458-L464
And then refactored in #10657
Suggested Fix
A fix was first proposed in #10794:
https://github.com/numba/numba/pull/10794#discussion_r4045956769
But note that the tests for sum and also cumsum (which may also be affected) need to be revisited.
AI Attribution
Assisted-by: Kilo(moonshotai/kimi-k3) for debugging and reproducer Assisted-by: Claude Desktop (anthropic/claude-opus-5) for debugging and reproducer
Checklist
- I have tried using the latest released version of Numba (most recent is visible in the release notes (https://numba.readthedocs.io/en/stable/release-notes-overview.html).
- I have included a self contained code sample to reproduce the problem. i.e. it's possible to run as 'python bug.py'.
- I have read and understood Numba's AI tool use policy (https://numba.readthedocs.io/en/stable/reference/ai_tools_policy.html)
Source: numba/numba