Search code examples
pythonnumba

Numba - cannot determine Numba type of <class 'builtin_function_or_method'>


I could not find how to do this. Consider this dict:

this_size = numba.typed.Dict.empty(key_type=numba.types.float64, value_type=numba.types.float64)

I'm in @numba.jit(nopython = True). I try to sum the values of this dict. But I get:

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

File "trial.py", line 163:
def strat_1(bid_price, ask_price, deal_size, deal_price, posture, post_update, is_out, num_ticks, tick_size, point_value):
    <source elided>
                BBB[i] = sum(this_size.values())

Solution

  • Edit: sum is now supported as of Numba version 0.54! (release notes, PR)


    Looks like sum isn't on the list of supported built-in functions (as of Numba version 0.51.2). Right now, Numba supports a growing but still fairly limited subset of vanilla Python and NumPy.

    Numba does support np.sum, although I wasn't able to convince it to take your dictionary's values (it might work with a bit more tinkering). The only way I got it to compile was by falling back a vanilla for loop:

    import numba
    import numpy as np
    
    this_size = numba.typed.Dict.empty(key_type=numba.types.float64, value_type=numba.types.float64)
    
    @numba.njit
    def my_sum(d):
      total = 0.0
      for val in d.values():
        total += val
      return total
    
    print(my_sum(this_size))