Is it possible to make CPU only reductions with JAX comparable to Numba in terms of computation time?
The compilers come straight from conda:
$ conda install -c conda-forge numba jax
Here is a 1-d NumPy array example
import numpy as np
import numba as nb
import jax as jx
@nb.njit
def reduce_1d_njit_serial(x):
s = 0
for xi in x:
s += xi
return s
@jx.jit
def reduce_1d_jax_serial(x):
s = 0
for xi in x:
s += xi
return s
N = 2**10
a = np.random.randn(N)
Using timeit on the following
np.add.reduce(a)gives1.99 µs ...reduce_1d_njit_serial(a)gives1.43 µs ...reduce_1d_jax_serial(a).item()gives23.5 µs ...
Note that jx.numpy.sum(a) and using jx.lax.fori_loop gives comparable (marginally slower) comp. times to reduce_1d_jax_serial.
It seems there is a better way to craft the reduction for XLA.
EDIT: compile times were not included as a print statement proceeded to check results.