I have a static method that I want to speed up with Numba:
@nb.njit
def numba_loop_choice(population, weights, k):
wc = np.cumsum(weights)
m = wc[-1]
sample = np.empty(k, population.dtype)
sample_idx = np.full(k, -1, np.int32)
i = 0
while i < k:
r = m * np.random.rand()
idx = np.searchsorted(wc, r, side="right")
if idx in sample_idx[:i]:
continue
sample[i] = population[idx]
sample_idx[i] = idx
i += 1
return sample
population and weights are numpy arrays, k is an integer. If I run this on my home computer it works without any error. On my server it crashes with an error. Both have Python 3.8.10. Does anyone understand why it works on one machine but crashes on another?
The error:
Traceback (most recent call last):
File "run.py", line 115, in <module>
paa.simulate_streams()
File "run.py", line 40, in simulate_streams
StreamSimulation(
File "xxx/simulate_stream.py", line 63, in __init__
self.simulate()
File "xxx/simulate_stream.py", line 132, in simulate
selected_element = numba_loop_choice(population=elements_dummy[0:latest_element + 1], weights=self.weights[0:latest_element + 1], k=1)[0]
File "/usr/lib/python3/dist-packages/numba/dispatcher.py", line 401, in _compile_for_args
error_rewrite(e, 'typing')
File "/usr/lib/python3/dist-packages/numba/dispatcher.py", line 344, in error_rewrite
reraise(type(e), e, None)
File "/usr/lib/python3/dist-packages/numba/six.py", line 668, in reraise
raise value.with_traceback(tb)
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function contains>) with argument(s) of type(s): (array(int32, 1d, C), int64)
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
In definition 2:
All templates rejected with literals.
In definition 3:
All templates rejected without literals.
In definition 4:
All templates rejected with literals.
All templates rejected with literals.
In definition 5:
All templates rejected without literals.
In definition 6:
All templates rejected with literals.
In definition 7:
All templates rejected without literals.
In definition 8:
All templates rejected with literals.
In definition 9:
All templates rejected without literals.
In definition 10:
All templates rejected with literals.
In definition 11:
All templates rejected without literals.
In definition 12:
All templates rejected with literals.
In definition 13:
All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: typing of intrinsic-call at xxx/simulate_stream.py (24)
File "simulate_stream.py", line 24:
def numba_loop_choice(population, weights, k):
<source elided>
print(idx, sample_idx)
if idx in sample_idx[:i]:
^