I'm exploring a bit Numba to optimize some signal processing codes. According to Numba's documentation, the function from the numpy.random
package is well supported by the just-in-time compiler. However, when I run
import numpy as np
from numba import jit
@jit(nopython=True)
def numba():
noise = np.random.normal(size=100)
# ...
if __name__ == "__main__":
numba()
I get the following error:
Traceback (most recent call last):
File ".../test.py", line 89, in <module>
numba()
File ".../venv/lib/python3.9/site-packages/numba/core/dispatcher.py", line 468, in _compile_for_args
error_rewrite(e, 'typing')
File ".../venv/lib/python3.9/site-packages/numba/core/dispatcher.py", line 409, in error_rewrite
raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in method normal of numpy.random.mtrand.RandomState object at 0x104355740>) found for signature:
>>> normal(size=Literal[int](100000))
There are 4 candidate implementations:
- Of which 4 did not match due to:
Overload in function '_OverloadWrapper._build.<locals>.ol_generated': File: numba/core/overload_glue.py: Line 129.
With argument(s): '(size=int64)':
Rejected as the implementation raised a specific error:
TypingError: unsupported call signature
raised from .../venv/lib/python3.9/site-packages/numba/core/typing/templates.py:439
During: resolving callee type: Function(<built-in method normal of numpy.random.mtrand.RandomState object at 0x104355740>)
During: typing of call at .../test.py (65)
File "test.py", line 65:
def numba():
noise = np.random.normal(size=SIZE)
^
Am I doing something obviously silly?
Thanks a lot!
CodePudding user response:
if you check the current state of documentation, the size argument is not supported yet.
since numba compiles this to machine code, this is equivalent in terms of speed.
@jit(nopython=True)
def numba():
noise = np.empty(100,dtype=np.float64)
for i in range(100):
noise[i] = np.random.normal()
return noise
Edit: the numba version is actually twice as fast ... probably because it doesn't parse inputs.