numba / numba

NumPy aware dynamic Python compiler using LLVM
https://numba.pydata.org/
BSD 2-Clause "Simplified" License
9.93k stars 1.13k forks source link

Unary variation of np.round doesn't work as expected. #5939

Closed sangeetayadav916 closed 4 years ago

sangeetayadav916 commented 4 years ago

TypingError: Failed in nopython mode pipeline (step: nopython frontend) Untyped global name 'neighbo ![njit](https://user-images.githubusercontent.com/30195699/86095651-e15a2780-bacf-11ea-99d1-37e5f8f29df4.JPG) ur': cannot determine Numba type of <class 'function'>

esc commented 4 years ago

@sangeetayadav916 thanks for the report. Please could you provide more information about what caused this problem:

  1. The code that you were running (as a minimal working reproducer, if you are unsure how to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).
  2. Any data needed to run the code.
  3. The full trace back of the exception.

Thanks.

sangeetayadav916 commented 4 years ago

[image: image.png]

I am calling this as %time neighbour(8,1) getting error like [image: image.png]

On Tue, Jun 30, 2020 at 12:55 PM Valentin Haenel notifications@github.com wrote:

@sangeetayadav916 https://github.com/sangeetayadav916 thanks for the report. Please could you provide more information about what caused this problem:

  1. The code that you were running (as a minimal working reproducer, if you are unsure how to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).
  2. Any data needed to run the code.
  3. The full trace back of the exception.

Thanks.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/numba/numba/issues/5939#issuecomment-651602182, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHGL7463G6VJAMUSAMH74SDRZGHPHANCNFSM4OL5ZO3A .

-- Regards, Sangeeta Yadav Assistant Professor Department of Electronics Maharaja Agrasen College University of Delhi

esc commented 4 years ago

@sangeetayadav916 thanks for reporting back. Unfortunately, something seems to have gone wrong with the upload and all I can see are strings like [image: image.png] Also, please be advised, that posting screenshots of code and tracebacks is suboptimal as the maintainers will not be able to copy and paste your code to debug it locally. Please follow up and submit a plain text representation of the code you are running any data needed to run it and a plain text representation of the traceback that you see. Thanks!

sangeetayadav916 commented 4 years ago

Please find the code below. I have also attached the same piece of code as .txt file for your reference.

@njit def neighbour(P,R): x = np.arange(0,P, dtype=np.float32) x = np.round(Rnp.cos((2np.pi x)/P),4) y = np.arange(0,P, dtype=np.float32) y = np.round(-Rnp.sin((2np.pi y)/P),4)

return x,y

def LBP_code(image,P,R): im_c=np.pad(image_eg, ((1,1),(1,1)), 'constant')

h, w = im_c.shape for i in range(h): for j in range(w): Xc,Yc = j,i X,Y = neighbour(P,R)

On Tue, Jun 30, 2020 at 3:15 PM Valentin Haenel notifications@github.com wrote:

@sangeetayadav916 https://github.com/sangeetayadav916 thanks for reporting back. Unfortunately, something seems to have gone wrong with the upload and all I can see are strings like [image: image.png] Also, please be advised, that posting screenshots of code and tracebacks is suboptimal as the maintainers will not be able to copy and paste your code to debug it locally. Please follow up and submit a plain text representation of the code you are running any data needed to run it and a plain text representation of the traceback that you see. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/numba/numba/issues/5939#issuecomment-651685965, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHGL74YZKHCY4F5UP6ZZIBDRZGXZ3ANCNFSM4OL5ZO3A .

-- Regards, Sangeeta Yadav Assistant Professor Department of Electronics Maharaja Agrasen College University of Delhi

@njit def neighbour(P,R): x = np.arange(0,P, dtype=np.float32) x = np.round(Rnp.cos((2np.pi x)/P),4) y = np.arange(0,P, dtype=np.float32) y = np.round(-Rnp.sin((2np.pi y)/P),4)

return x,y

@njit def neighbour(P,R): x = np.arange(0,P, dtype=np.float32) x = np.round(Rnp.cos((2np.pi x)/P),4) y = np.arange(0,P, dtype=np.float32) y = np.round(-Rnp.sin((2np.pi y)/P),4)

return x,y

def calling_fn(image, P,R): h, w = im_c.shape for i in range(h): for j in range(w): Xc,Yc = j,i X,Y = neighbour(P,R)

P=8 R=1

esc commented 4 years ago

@sangeetayadav916 thanks for that. Just a note about formatting in GitHub issues, for future reference triple backticks are needed around a code block (with optional language syntax specified), for example: ```python <your code> ```. GitHub also has a basic guide to its markdown syntax available here.

esc commented 4 years ago

I have reformatted this to the following reproducer:

from numba import njit
import numpy as np

@njit
def neighbour(P,R):
    x = np.arange(0,P, dtype=np.float32)
    x = np.round(R*np.cos((2*np.pi *x)/P),4)
    y = np.arange(0,P, dtype=np.float32)
    y = np.round(-R*np.sin((2*np.pi *y)/P),4)

    return x,y

neighbour(8, 1)

The stack trace I receive is:

Traceback (most recent call last):
  File "/Users/vhaenel/git/numba/numba/core/typeinfer.py", line 155, in propagate
    constraint(typeinfer)
  File "/Users/vhaenel/git/numba/numba/core/typeinfer.py", line 495, in __call__
    self.resolve(typeinfer, typevars, fnty)
  File "/Users/vhaenel/git/numba/numba/core/typeinfer.py", line 515, in resolve
    sig = typeinfer.resolve_call(fnty, pos_args, kw_args)
  File "/Users/vhaenel/git/numba/numba/core/typeinfer.py", line 1459, in resolve_call
    return self.context.resolve_function_type(fnty, pos_args, kw_args)
  File "/Users/vhaenel/git/numba/numba/core/typing/context.py", line 211, in resolve_function_type
    raise last_exception
  File "/Users/vhaenel/git/numba/numba/core/typing/context.py", line 194, in resolve_function_type
    res = self._resolve_user_function_type(func, args, kws)
  File "/Users/vhaenel/git/numba/numba/core/typing/context.py", line 246, in _resolve_user_function_type
    return func.get_call_type(self, args, kws)
  File "/Users/vhaenel/git/numba/numba/core/types/functions.py", line 304, in get_call_type
    failures.raise_error()
  File "/Users/vhaenel/git/numba/numba/core/types/functions.py", line 224, in raise_error
    raise errors.TypingError(self.format())
numba.core.errors.TypingError: No implementation of function Function(<function round_ at 0x10b5181f0>) found for signature:

 >>> round_(array(float64, 1d, C), Literal[int](4))

There are 2 candidate implementations:
   - Of which 2 did not match due to:
   Overload of function 'round_': File: numba/core/typing/npydecl.py: Line 1070.
     With argument(s): '(array(float64, 1d, C), Literal[int](4))':
    No match.

During: resolving callee type: Function(<function round_ at 0x10b5181f0>)
During: typing of call at foo092.py (8)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "foo092.py", line 15, in <module>
    neighbour(8, 1)
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 415, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 356, in error_rewrite
    raise e
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 367, in _compile_for_args
    return self.compile(tuple(argtypes))
  File "/Users/vhaenel/git/numba/numba/core/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 808, in compile
    cres = self._compiler.compile(args, return_type)
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 82, in compile
    raise retval
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 92, in _compile_cached
    retval = self._compile_core(args, return_type)
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 105, in _compile_core
    cres = compiler.compile_extra(self.targetdescr.typing_context,
  File "/Users/vhaenel/git/numba/numba/core/compiler.py", line 603, in compile_extra
    return pipeline.compile_extra(func)
  File "/Users/vhaenel/git/numba/numba/core/compiler.py", line 339, in compile_extra
    return self._compile_bytecode()
  File "/Users/vhaenel/git/numba/numba/core/compiler.py", line 401, in _compile_bytecode
    return self._compile_core()
  File "/Users/vhaenel/git/numba/numba/core/compiler.py", line 381, in _compile_core
    raise e
  File "/Users/vhaenel/git/numba/numba/core/compiler.py", line 372, in _compile_core
    pm.run(self.state)
  File "/Users/vhaenel/git/numba/numba/core/compiler_machinery.py", line 341, in run
    raise patched_exception
  File "/Users/vhaenel/git/numba/numba/core/compiler_machinery.py", line 332, in run
    self._runPass(idx, pass_inst, state)
  File "/Users/vhaenel/git/numba/numba/core/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/Users/vhaenel/git/numba/numba/core/compiler_machinery.py", line 291, in _runPass
    mutated |= check(pss.run_pass, internal_state)
  File "/Users/vhaenel/git/numba/numba/core/compiler_machinery.py", line 264, in check
    mangled = func(compiler_state)
  File "/Users/vhaenel/git/numba/numba/core/typed_passes.py", line 92, in run_pass
    typemap, return_type, calltypes = type_inference_stage(
  File "/Users/vhaenel/git/numba/numba/core/typed_passes.py", line 70, in type_inference_stage
    infer.propagate(raise_errors=raise_errors)
  File "/Users/vhaenel/git/numba/numba/core/typeinfer.py", line 994, in propagate
    raise errors[0]
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function round_ at 0x10b5181f0>) found for signature:

 >>> round_(array(float64, 1d, C), Literal[int](4))

There are 2 candidate implementations:
   - Of which 2 did not match due to:
   Overload of function 'round_': File: numba/core/typing/npydecl.py: Line 1070.
     With argument(s): '(array(float64, 1d, C), Literal[int](4))':
    No match.

During: resolving callee type: Function(<function round_ at 0x10b5181f0>)
During: typing of call at foo092.py (8)

File "foo092.py", line 8:
def neighbour(P,R):
    <source elided>
    x = np.arange(0,P, dtype=np.float32)
    x = np.round(R*np.cos((2*np.pi *x)/P),4)
esc commented 4 years ago

So, I can confirm that this is a bug in the Numba implementation of Numpy's round. The signature handling here:

https://github.com/numba/numba/blob/master/numba/core/typing/npydecl.py#L1064-L1095

isn't entirely correct.

Also, here is a workaround, although a bit of a "kludge" and involves using the three argument version of np.round.

from numba import njit
import numpy as np

@njit
def foo():
    in_ = np.ones((2,2)) * 1.23456789
    out_ = np.empty_like(in_)
    result_ = np.round(in_, 4, out_) # <--- three argument case with a legit 'out' array
    return in_, out_, result_

print(foo())

Lastly, I should mention that fixing this would ideally involve re-writing the np.round (and np.round_ and np.around) implementations using the @overload mechanism, as described here: http://numba.pydata.org/numba-doc/latest/extending/overloading-guide.html -- rather than fixing the existing code as it uses a legacy API.

esc commented 4 years ago

Note: I have updated the title to accurately reflect the issue being reported.

esc commented 4 years ago

Lastly, I discovered there is at least one duplicate here https://github.com/numba/numba/issues/5939 so I will close this issue for now. @sangeetayadav916 please be so kind and do check the issue tracker the next time. Thanks!