numba: instantiating objects within numba jitclass

Viewed 17

I have the following numba classes:

@jitclass
class SomeComp():
    
    def __init__(self):
        return
    
    def go_fast(self,a):
        trace = 0.0
        for i in range(a.shape[0]):  
            trace += np.tanh(a[i, i])
        return a + trace
    
@jitclass
class NumbaClass():
    
    def __init__(self):
        self.comp = SomeComp()
        return 
    
    def go_fast(self,x):
        return self.comp.go_fast(x)
    
numbaclass = NumbaClass()

Instantiating the class gives the following error:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Cannot resolve setattr: (instance.jitclass.NumbaClass#7f40dec9b3a0<>).comp = instance.jitclass.SomeComp#7f40ded5af10<>

File "../../../../../tmp/ipykernel_66918/3597494714.py", line 5:
<source missing, REPL/exec in use?>

During: typing of set attribute 'comp' at /tmp/ipykernel_66918/3597494714.py (5)

File "../../../../../tmp/ipykernel_66918/3597494714.py", line 5:
<source missing, REPL/exec in use?>

During: resolving callee type: jitclass.NumbaClass#7f40dec9b3a0<>
During: typing of call at <string> (3)

During: resolving callee type: jitclass.NumbaClass#7f40dec9b3a0<>
During: typing of call at <string> (3)


File "<string>", line 3:
<source missing, REPL/exec in use?>

When I remove @jitclass from SomeComp, I get

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name 'SomeComp': Cannot determine Numba type of <class 'type'>

File "../../../../../tmp/ipykernel_66918/4052999349.py", line 17:
<source missing, REPL/exec in use?>

During: resolving callee type: jitclass.NumbaClass#7f40dec669a0<>
During: typing of call at <string> (3)

During: resolving callee type: jitclass.NumbaClass#7f40dec669a0<>
During: typing of call at <string> (3)


File "<string>", line 3:
<source missing, REPL/exec in use?>

instead. So, typingError in both cases.

Writing

@jitclass
class NumbaClass():
    
    def __init__(self):
        return 
    
    def go_fast(self,a):
        trace = 0.0
        for i in range(a.shape[0]):  
            trace += np.tanh(a[i, i])
        return a + trace
    
numbaclass = NumbaClass()

obviously goes fine. But i need to define objects (which themselves should all be numba compatible) inside the NumbaClass, so I am a little stuck here, whether it will be possible to use numba or not. Do I need to somehow specify the typehints to numba? And how would I pass them in this case?

Thanks!

0 Answers
Related