Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError in stencil when cval is np.nan or np.inf #7286

Closed
fbunt opened this issue Aug 6, 2021 · 6 comments · Fixed by #7288
Closed

NameError in stencil when cval is np.nan or np.inf #7286

fbunt opened this issue Aug 6, 2021 · 6 comments · Fixed by #7288
Labels
bug - failure to compile Bugs: failed to compile valid code stencil Issue involving stencils

Comments

@fbunt
Copy link

fbunt commented Aug 6, 2021

I was playing around with the stencil decorator and tried supplying np.nan to the cval argument. This results in a NameError. It looks like the type inference is not able to infer a type for np.nan. The docs state that any value can be supplied to cval so I assume this is a bug. The same thing happens for np.inf. The below code triggers the error on my machine with version 0.53.1. If this is intended, perhaps the documentation could be updated to indicate that NaNs and infs are not valid.

import numpy as np
from numba import stencil


@stencil(neighborhood=((-1, 1), (-1, 1)), func_or_mode="constant", cval=np.nan)
def kern(a):
    return (
        a[-1, -1]
        + a[-1, 0]
        + a[-1, 1]
        + a[0, -1]
        + a[0, 0]
        + a[0, 1]
        + a[1, -1]
        + a[1, 0]
        + a[1, 1]
    )


x = np.random.randint(100, size=(100, 100)).astype(float)
y = kern(x)

Here is the stack trace:

Traceback (most recent call last):
  File "stencil.py", line 21, in <module>
    y = kern(x)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/stencils/stencil.py", line 763, in __call__
    new_func = self._stencil_wrapper(result, None, real_ret, typemap,
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/stencils/stencil.py", line 728, in _stencil_wrapper
    new_func = compiler.compile_ir(
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler.py", line 669, in compile_ir
    return pipeline.compile_ir(func_ir=func_ir, lifted=lifted,
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler.py", line 363, in compile_ir
    return self._compile_ir()
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler.py", line 422, in _compile_ir
    return self._compile_core()
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler.py", line 395, in _compile_core
    raise e
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler.py", line 386, in _compile_core
    pm.run(self.state)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 339, in run
    raise patched_exception
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 330, in run
    self._runPass(idx, pass_inst, state)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler_lock.py", line 35, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 289, in _runPass
    mutated |= check(pss.run_pass, internal_state)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/compiler_machinery.py", line 262, in check
    mangled = func(compiler_state)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/typed_passes.py", line 104, in run_pass
    typemap, return_type, calltypes, errs = type_inference_stage(
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/typed_passes.py", line 80, in type_inference_stage
    infer.build_constraint()
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/typeinfer.py", line 1024, in build_constraint
    self.constrain_statement(inst)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/typeinfer.py", line 1371, in constrain_statement
    self.typeof_assign(inst)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/typeinfer.py", line 1442, in typeof_assign
    self.typeof_global(inst, inst.target, value)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/typeinfer.py", line 1542, in typeof_global
    typ = self.resolve_value_type(inst, gvar.value)
  File "/home/$USER/anaconda3/envs/main/lib/python3.8/site-packages/numba/core/typeinfer.py", line 1463, in resolve_value_type
    raise TypingError(msg, loc=inst.loc)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
NameError: name 'nan' is not defined
@stuartarchibald
Copy link
Contributor

@fbunt Thanks for the report, I can reproduce this on mainline. The issue appears to be in the rendering of the constant np.nan in the stencil output template string, it's writing str(np.nan) in as the constant, which is "nan", but it actually needs the np module in the string for it to resolve.

@stuartarchibald stuartarchibald added bug - failure to compile Bugs: failed to compile valid code stencil Issue involving stencils labels Aug 6, 2021
stuartarchibald added a commit to stuartarchibald/numba that referenced this issue Aug 6, 2021
stuartarchibald added a commit to stuartarchibald/numba that referenced this issue Aug 6, 2021
stuartarchibald added a commit to stuartarchibald/numba that referenced this issue Aug 6, 2021
@stuartarchibald
Copy link
Contributor

Fixed in #7288

@fbunt
Copy link
Author

fbunt commented Aug 6, 2021

I think you might have to check for -np.inf as well.

@stuartarchibald
Copy link
Contributor

I think you might have to check for -np.inf as well.

Good point, I guess this can just specialise for inf and nan. I think that's it for string-like numerical constants from NumPy, are you aware of any others?

@fbunt
Copy link
Author

fbunt commented Aug 6, 2021

I think those are it. Thanks for looking into this!

@stuartarchibald
Copy link
Contributor

I think those are it. Thanks for looking into this!

Thanks, and no problem. 619b278 should fix up the case of negative str based const.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug - failure to compile Bugs: failed to compile valid code stencil Issue involving stencils
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants