Can't iterate over heterogeneous tuple of jitted functions

Hi,
I need to be able to iterate over a heterogeneous tuple of jitted functions. I tried using numba.unroll_literal but that didn’t work. Below is my code and the error I get. How can I do this?

@numba.njit
def f1():
    return 4

@numba.njit
def f2():
    return 1

@numba.njit
def combine(func_tuple):
    for i in range(len(func_tuple)):
        print(func_tuple[i]())

Error:

Traceback (most recent call last):
  File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
    exec(code, self.locals)
  File "<pyshell#120>", line 1, in <module>
  File "/home/dyuman/.local/lib/python3.10/site-packages/numba/core/dispatcher.py", line 468, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/dyuman/.local/lib/python3.10/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)
e[1me[1mNo implementation of function Function(<built-in function getitem>) found for signature:
 
 >>> getitem(Tuple(type(CPUDispatcher(<function f2 at 0x7f58de43a710>)), type(CPUDispatcher(<function f3 at 0x7f58a88ccdc0>))), int64)
 
There are 22 candidate implementations:
e[1m      - Of which 22 did not match due to:
      Overload of function 'getitem': File: <numerous>: Line N/A.
        With argument(s): '(Tuple(type(CPUDispatcher(<function f2 at 0x7f58de43a710>)), type(CPUDispatcher(<function f3 at 0x7f58a88ccdc0>))), int64)':e[0m
e[1m       No match.e[0m
e[0m
e[0me[1mDuring: typing of intrinsic-call at <pyshell#119> (4)e[0m
e[1m
File "<pyshell#119>", line 4:e[0m
e[1m<source missing, REPL/exec in use?>e[0m

Hi @dyu

Is it possible that you have used numba.literal_unroll inside combine? If so, Numba cannot handle the numba module inside the jitted function. However, the following works:

import numba as nb 
from numba import literal_unroll

@nb.njit
def f1():
    return 4

@nb.njit
def f2():
    return 1

@nb.njit
def combine(func_tuple):
    for i in range(len(literal_unroll(func_tuple))):
        print(func_tuple[i]())
        
combine((f1, f2))
1 Like

@sschaer Thank you so much for your reply!

You were right, I was using numba.literal_unroll. I made the modification you suggested and it works!

Thanks again,
Dyuman