Home > other >  Numpy function round throws error using numba jitclass
Numpy function round throws error using numba jitclass

Time:11-22

I want to use the numpy.round_ ina method of a class. Any calculation done by methods of this class I want to accelerate by using numba. In general, it works fine. But I somehow do not get numpy.round_ running. When using numpy.round_ numba throws an error.

Here is my code of a reduced example:

from numba import types    
from numba.experimental import jitclass
import numpy as np

spec = [
    ('arr', types.Array(types.uint8, 1, 'C')),
    ('quot', types.Array(types.float64, 1, 'C')),
]

@jitclass(spec)
class test:
    
    def __init__(self):
        self.arr = np.array((130,190,130),dtype=np.uint8)
        
    def rnd_(self):
        quot = np.zeros(3, dtype=np.float64)
        val = self.arr
        quot = np.round(val/3.0)
        return quot
    
t = test()
a = t.rnd_()

It throws the following error:

TypingError: - Resolution failure for literal arguments:
Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function round_ at 0x0000021B3500D870>) found for signature:

round_(array(float64, 1d, C))

There are 4 candidate implementations:
  - Of which 4 did not match due to:
  Overload in function '_OverloadWrapper._build.<locals>.ol_generated': File: numba\core\overload_glue.py: Line 131.
    With argument(s): '(array(float64, 1d, C))':
   Rejected as the implementation raised a specific error:
     TypingError: Failed in nopython mode pipeline (step: nopython frontend)
   No implementation of function Function(<intrinsic stub>) found for signature:

stub(array(float64, 1d, C))

   There are 2 candidate implementations:
     - Of which 2 did not match due to:
     Intrinsic of function 'stub': File: numba\core\overload_glue.py: Line 35.
       With argument(s): '(array(float64, 1d, C))':
      No match.
   
   During: resolving callee type: Function(<intrinsic stub>)
   During: typing of call at <string> (3)
   
   
   File "<string>", line 3:
   <source missing, REPL/exec in use?>

  raised from C:\ProgramData\Anaconda3\envs\mybase_conda\lib\site-packages\numba\core\typeinfer.py:1086

During: resolving callee type: Function(<function round_ at 0x0000021B3500D870>)
During: typing of call at .......\python\playground\tmp.py (27)


File "tmp.py", line 27:
    def rnd_(self):
        <source elided>
        val = self.arr
        quot = np.round(val/3.0)
        ^

- Resolution failure for non-literal arguments:
None

During: resolving callee type: BoundFunction((<class 'numba.core.types.misc.ClassInstanceType'>, 'rnd_') for instance.jitclass.test#21b3c031930<arr:array(uint8, 1d, C),quot:array(float64, 1d, C)>)
During: typing of call at <string> (3)

What am I doing wrong?

CodePudding user response:

Seems like you need to pass round's optional arguments as well. I could reproduce the error with an even smaller example:

@nb.jit(nopython=True)
def foo(x):
    return np.round(x)

The fix to this is something like:

@nb.jit(nopython=True)
def foo(x):
    out = np.empty_like(x)
    np.round(x, 0, out)
    return out

So for your case, it should be:

def rnd_(self):
    quot = np.zeros(3, dtype=np.float64)
    np.round(self.arr / 3.0, 0, quot)
    return quot
  • Related