0

I am currently implementing a Python class wrapped by the Numba @jitcalss decorator. My problem is about writing recursive methods. I know there are ways of writing recursive methods as iterative methods as well, but in my case, I believe that recursive use helps me to write a more traceable program script. As far as I see, Numba does not directly support recursive methods declared in Numba classes. The code below does not present my case directly but it is equivalent to my problem. When I run the code, the error thrown by the Python is given below as well. Any kind of suggestion/improvement/help is welcome

import numba


spec = []
@numba.experimental.jitclass(spec)
class basicClass(object):
    def __init__(self):
        pass

    def factorial(self,x):
        if x==1:
            return 1
        else:
            return x*self.factorial(x-1)

# MAIN BELOW
class_object = basicClass()
class_object.factorial(5)

The error is given below:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
- Resolution failure for literal arguments:
Failed in nopython mode pipeline (step: nopython frontend)
- Resolution failure for literal arguments:
Failed in nopython mode pipeline (step: nopython frontend)
compiler re-entrant to the same function signature
- Resolution failure for non-literal arguments:
None

During: resolving callee type: BoundFunction((<class 'numba.core.types.misc.ClassInstanceType'>, 'factorial') for instance.jitclass.basicClass#22839d9cf70<>)
During: typing of call at <ipython-input-7-ab7b9737001d> (16)


File "<ipython-input-7-ab7b9737001d>", line 16:
    def factorial(self,x):
        <source elided>
        else:
            return x*self.factorial(x-1)
            ^

- Resolution failure for non-literal arguments:
None

During: resolving callee type: BoundFunction((<class 'numba.core.types.misc.ClassInstanceType'>, 'factorial') for instance.jitclass.basicClass#22839d9cf70<>)
During: typing of call at <string> (3)


File "<string>", line 3:
Okan Erturk
  • 113
  • 4

1 Answers1

0

This is not supported. The documentation states:

Recursion support in numba is currently limited to self-recursion with explicit type annotation for the function. This limitation comes from the inability to determine the return type of a recursive call.

The thing is you cannot specify a type of the function since it depends of self which contains the function...

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59
  • So, you say that there is no way or decorator of Numba that enables recursion in classes? – Okan Erturk Jan 30 '22 at 22:23
  • Well, I say that as long as the function cannot have a user-defined type this is not possible and there is a strong evidences that the function type cannot be user defined yet (although this is not stated in the documentation because feature are experimental). Thus, yes, I think there is no way or decorator of Numba that enables recursion in classes. – Jérôme Richard Jan 31 '22 at 18:34