Performance regression for integer dtypes for `np.sum`
Description
The execution performance for np.sum shows a regression.
Reproducer
import json
import timeit
import numpy as np
from numba import jit
@jit
def nb_sum(a):
return np.sum(a)
def bench(func, arr):
func(arr)
t = timeit.Timer(lambda: func(arr))
number, total = t.autorange()
return total / number * 1e6
shape = (1000, 1000)
rng = np.random.default_rng(0)
dtypes = ["int8", "int16", "int32", "int64",
"uint8", "uint16", "uint32", "uint64",
"float32", "float64"]
results = {}
for dt_name in dtypes:
dt = np.dtype(dt_name)
if np.issubdtype(dt, np.integer):
arr = rng.integers(1, 3, size=shape).astype(dt)
else:
arr = rng.uniform(0.999, 1.001, size=shape).astype(dt)
results[f"sum_{dt_name}"] = bench(nb_sum, arr)
print(json.dumps(results, indent=2))
And then an analysis script to visualize the results, called diff_result.py:
import json
import sys
before = json.load(open(sys.argv[1]))
after = json.load(open(sys.argv[2]))
print(f"{'benchmark':20s} {'before':>10s} {'after':>10s} {'ratio':>7s}")
print("-" * 50)
for name in before:
b = before[name]
a = after[name]
ratio = a / b
flag = " <<<" if ratio > 1.15 else ""
print(f"{name:20s} {b:8.2f}us {a:8.2f}us {ratio:7.2f}x{flag}")Results
Running on main at 4b3e8fb92f886b199434a74c7aec9fecfeb834b8 on an MacBook Pro M4:
$ numba -s | grep "Numba Version"
Numba Version : 0.69.0dev0+41.g4b3e8fb92f
$ python bug.py > after
$ cat after
{
"sum_int8": 385.5852501001209,
"sum_int16": 385.45641698874533,
"sum_int32": 387.3103749938309,
"sum_int64": 386.3897090777755,
"sum_uint8": 386.23075000941753,
"sum_uint16": 385.3641669265926,
"sum_uint32": 385.5649590259418,
"sum_uint64": 385.099416016601,
"sum_float32": 942.9383319802582,
"sum_float64": 943.3093338739127
}Running on faf66df31b44231f99fcf231b251ddd5314bf3bf (the commit before #10625) same hardware
$ numba -s | grep "Numba Version"
Numba Version : 0.67.0dev1+6.gfaf66df31b
$ python bug.py > before
cat beforepython bug.py > before 7.88s user 1.03s system 110% cpu 8.089 total
$ cat before
{
"sum_int8": 48.32790840882808,
"sum_int16": 99.03942500241101,
"sum_int32": 96.52875841129571,
"sum_int64": 112.04410449136049,
"sum_uint8": 48.29318339470774,
"sum_uint16": 99.22900840174407,
"sum_uint32": 96.63220839574933,
"sum_uint64": 112.2581249801442,
"sum_float32": 827.186250127852,
"sum_float64": 837.6109160017222
}Now the analysis:
$ python diff_result.py before after
benchmark before after ratio
--------------------------------------------------
sum_int8 48.33us 385.59us 7.98x <<<
sum_int16 99.04us 385.46us 3.89x <<<
sum_int32 96.53us 387.31us 4.01x <<<
sum_int64 112.04us 386.39us 3.45x <<<
sum_uint8 48.29us 386.23us 8.00x <<<
sum_uint16 99.23us 385.36us 3.88x <<<
sum_uint32 96.63us 385.56us 3.99x <<<
sum_uint64 112.26us 385.10us 3.43x <<<
sum_float32 827.19us 942.94us 1.14x
sum_float64 837.61us 943.31us 1.13xAs you can see, the performance hit is between 3.5x and 8x for my given hardware depending on int bit-width.
Affected versions.
As in #10846 -- 0.67.0 and main are affected and 0.66 is not.
$ numba -s | grep "Numba Version"
Numba Version : 0.66.0
$ python bug.py > 0.66.0
python bug.py > 0.66.0 7.85s user 0.82s system 108% cpu 7.988 total
$ cat 0.66.0
{
"sum_int8": 48.23163340333849,
"sum_int16": 99.01475000660866,
"sum_int32": 96.4279918000102,
"sum_int64": 113.01210400415584,
"sum_uint8": 48.30458341166377,
"sum_uint16": 99.00942500680685,
"sum_uint32": 96.51825001928955,
"sum_uint64": 113.69945801561698,
"sum_float32": 827.3863338399678,
"sum_float64": 834.9223341792822
}$ numba -s | grep "Numba Version"
Numba Version : 0.67.0
$ python bug.py > 0.67.0
python bug.py > 0.67.0 8.73s user 0.80s system 108% cpu 8.747 total
$ cat 0.67.0
{
"sum_int8": 384.87512501887977,
"sum_int16": 384.87654097843915,
"sum_int32": 385.2822500048205,
"sum_int64": 384.8951250547543,
"sum_uint8": 385.18600002862513,
"sum_uint16": 384.8943749908358,
"sum_uint32": 386.07479096390307,
"sum_uint64": 384.91729204542935,
"sum_float32": 941.723583964631,
"sum_float64": 941.4368320722133
}$ python diff_result.py 0.66.0 0.67.0
benchmark before after ratio
--------------------------------------------------
sum_int8 48.23us 384.88us 7.98x <<<
sum_int16 99.01us 384.88us 3.89x <<<
sum_int32 96.43us 385.28us 4.00x <<<
sum_int64 113.01us 384.90us 3.41x <<<
sum_uint8 48.30us 385.19us 7.97x <<<
sum_uint16 99.01us 384.89us 3.89x <<<
sum_uint32 96.52us 386.07us 4.00x <<<
sum_uint64 113.70us 384.92us 3.39x <<<
sum_float32 827.39us 941.72us 1.14x
sum_float64 834.92us 941.44us 1.13xRoot cause and origin
The root cause is unknown at present, but the origin can be traced back to #10625 (as with ref: #10846).
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