Repository navigation
Performance issue with np_compat.clip #444
Copy link
Copy link
Closed
Milestone
Description
Hello,
While benchmarking a port of some NumPy code to this library, I observed a noticeable performance drop in my simple tests, with runtimes up to roughly 2x slower than the original NumPy implementation.
Before investigating further, I'd like to check whether this is expected, whether my usage is suboptimal, or whether it might be an issue worth looking into. I've attached a minimal reproducible example below.
Thanks!
Python 3.11
Numpy version: 2.4.6
array_api_compat version: 1.15.0
Output:
4000 250 3 Numpy inplace 0.50s
4000 250 3 Numpy standard 0.66s
4000 250 3 Array_compat 1.44s
4000 250 3 Array_compat inplace 1.32s
import numpy as np
import time
from array_api_compat import array_namespace
from copy import deepcopy
rng=np.random.default_rng(0)
N=4000
s=250
c=3
base_imgs=[]
for i in range(N):
base_imgs.append(rng.random((s,s,c),dtype=np.float32))
#### Test numpy speed vs arraycompat speed
minimum = 0.001
maximum = 0.997
print("Made img list")
# copy images to allow inplace modification
imgs=deepcopy(base_imgs)
start_time=time.perf_counter()
for data in imgs:
np.clip(data, minimum, maximum, out=data)
np.subtract(data, minimum, out=data)
np.true_divide(data, maximum - minimum, out=data)
np.log(data,out=data)
end_time= time.perf_counter()
print(N,s,c, f"Numpy inplace {end_time-start_time:.2f}s")
del imgs
imgs=deepcopy(base_imgs)
start_time=time.perf_counter()
for data in imgs:
#xp=array_namespace(data)
data = np.clip(data, minimum, maximum)
data = data - minimum
data = data / (maximum - minimum)
data = np.log(data)
end_time= time.perf_counter()
print(N,s,c, f"Numpy standard {end_time-start_time:.2f}s")
del imgs
imgs=deepcopy(base_imgs)
xp=array_namespace(imgs[0])
data=deepcopy(imgs[0])
data = xp.clip(data, minimum, maximum)
data = data - minimum
data = data / (maximum - minimum)
data = xp.log(data)
del data
start_time=time.perf_counter()
for data in imgs:
data = xp.clip(data, minimum, maximum)
data = data - minimum
data = data / (maximum - minimum)
data = xp.log(data)
end_time= time.perf_counter()
print(N,s,c, f"Array_compat {end_time-start_time:.2f}s")
start_time=time.perf_counter()
for data in imgs:
xp.clip(data, minimum, maximum,out=data)
xp.subtract(data, minimum, out=data)
xp.true_divide(data, maximum - minimum, out=data)
xp.log(data,out=data)
end_time= time.perf_counter()
print(N,s,c, f"Array_compat inplace {end_time-start_time:.2f}s")
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]Perfarmance Issues with Numpy[/-][+]Performance Issues with Numpy[/+]ev-br commentedon Jun 29, 2026
A regular way to check performance would be to use
line_profiler, or more conveniently via%lprunmagic in ipython.It might be that
xp.clipdoes a bit of work for compatibility with the Array API spec. Don't take my word for it though, do check: any second-guessing performance bottlenecks is almost always off.Lasloruhberg commentedon Jul 1, 2026
Yes that seems to be happening, checking with lprun i find
That arraycopmpat does inded spend 70% of the time on the clip, almost 10x as much as numpy.
Is this to be expected/something that is planned to be improved?
Do you have a suggestion around this?
(Numpy: 26 872.1 time units per hit vs
ArrayCompat: 266 455.0 )
Interestingly is the "inplace" clipping is almost as time consuming at the 'normal' one for arraycompat
ev-br commentedon Jul 1, 2026
np_compat.clipgoes through the "generic" code path,array-api-compat/src/array_api_compat/common/_aliases.py
Line 375 in 2865123
np.clipfor numpy-specific version, similar to howtorch_compat.clipgoes throughtorch.clampfor some inputs,array-api-compat/src/array_api_compat/torch/_aliases.py
Line 861 in 2865123
I don't think addressing the overhead is on anybody's radar at the moment, so it's up to a motivated contributor to investigate, improve and send a PR (hint hint).
What is the optimization budget, given the constraint of being spec-compliant, I do not know off-hand either. Likely no way to tell without trying.
[-]Performance Issues with Numpy[/-][+]Performance issue with `np.clip`[/+][-]Performance issue with `np.clip`[/-][+]Performance issue with `np_compat.clip`[/+]ev-br commentedon Jul 7, 2026
fixed by #447