Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python numba fingerprint error

I'm attempting numba to optimise some code. I've worked through the initial examples in section 1.3.1 in the 0.26.0 user guide (http://numba.pydata.org/numba-doc/0.26.0/user/jit.html) and get the expected results, so I don't think the problem is installation.

Here's my code:

import numba
import numpy
import random

a = 8
b = 4

def my_function(a, b):
    all_values = numpy.fromiter(range(a), dtype = int)
    my_array = []
    for n in (range(a)):
        some_values = (all_values[all_values != n]).tolist()
        c = random.sample(some_values, b)
        my_array.append(sorted([n] + c))
    return my_array

print(my_function(a, b))

my_function_numba = numba.jit()(my_function)

print(my_function_numba(a, b))

Which after printing out the expected results from the my_function call returns the following error message:

ValueError                                Traceback (most recent call last)
<ipython-input-8-b5d8983a58f6> in <module>()
     19 my_function_numba = numba.jit()(my_function)
     20 
---> 21 print(my_function_numba(a, b))

ValueError: cannot compute fingerprint of empty list

Fingerprint of empty list?

like image 549
zazizoma Avatar asked Jun 11 '16 21:06

zazizoma


1 Answers

I'm not sure about that error in particular, but in general, to be fast numba requires a particular subset of numpy/python (see here and here for more). So I might rewrite it like this.

@numba.jit(nopython=True)
def fast_my_function(a, b):
    all_values = np.arange(a)
    my_array = np.empty((a, b + 1), dtype=np.int32)
    for n in range(a):
        some = all_values[all_values != n]
        c = np.empty(b + 1, dtype=np.int32)
        c[1:] = np.random.choice(some, b)
        c[0] = n
        c.sort()
        my_array[n, :] = c
    return my_array

Main things to note:

  1. no lists, I'm pre-allocating everything.
  2. no use of generators (in both python 2 & 3 for n in range(a) will get converted to a fast native loop)
  3. adding nopython=True to the decorator makes it so numba will complain if I use something that can't be efficiently JITed.
like image 185
chrisb Avatar answered Oct 12 '22 01:10

chrisb