Reputation: 35
I have some class:
from numba import jitclass, int32, float32, types
from numba.typed import List
_spec = [
('Y_rf', types.List(float32[:, :])),
...
]
@jitclass(_spec)
class DensityRatioEstimation:
def __init__(self, sigma):
self.sigma = sigma
self.Y_rf = [np.array([[0.]], dtype=float32)]
But I can't make it work. It always broke with different errors. For now error is:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.typeinfer.CallConstraint object at 0x00000277CBBBF550>.
Failed in nopython mode pipeline (step: nopython mode backend)
File "src\models\DDRE.py", line 26:
def __init__(self, sigma):
<source elided>
self.sigma = sigma
self.Y_rf = [np.array([[0.]], dtype=float32)]
^
[1] During: lowering "(self).Y_rf = $0.11" at D:\anomaly-detection\src\models\DDRE.py (26)
[2] During: resolving callee type: jitclass.DensityRatioEstimation#277c8a6cdd8<sigma:float32,Y_rf:list(array(float32, 2d, A)),Y_te:list(array(float32, 2d, A)),k:int32,alphas:array(float32, 1d, A),b:array(float32, 1d, A)>
[3] During: typing of call at <string> (3)
Enable logging at debug level for details.
File "<string>", line 3:
<source missing, REPL/exec in use?>
I also tried to use List.empty_list(float32[:, :])
from numba.types.List
instead [np.array([[0.]], dtype=float32)]
. But it is also not working. How to fix that?
Upvotes: 2
Views: 3499
Reputation: 431
One problem with your snippet, you're trying to create a Numpy array using a Numba dtype.
np.array([[1, 2], [3, 4]], dtype=np.float32) # OK
np.array([[1, 2], [3, 4]], dtype=nb.float32) # Not OK
However, the main problem is you need to specify the type of list with a numba.types.npytypes.Array
. This is different to a function signature where you'd specify the array using float32([:,:])
.
import numba as nb
import numpy as np
_spec = [
('Y_rf', nb.types.List(nb.types.Array(nb.types.float32, 2, 'C'))),
('sigma', nb.types.int32)
]
@jitclass(_spec)
class DensityRatioEstimation:
def __init__(self, sigma):
self.sigma = sigma
self.Y_rf = [np.array([[1, 2], [3, 4]], dtype=np.float32)]
dre = DensityRatioEstimation(1)
dre.Y_rf
[array([[1., 2.],
[3., 4.]], dtype=float32)]
Upvotes: 4