numba icon indicating copy to clipboard operation
numba copied to clipboard

Support `np.any` for scalar input

Open maxnoe opened this issue 3 years ago • 1 comments

Feature request

Not sure if this is a bug or just missing, but numba does not support calling np.any on a single boolean, which numpy does:


In [1]: import numpy as np

In [2]: np.any(True)
Out[2]: True

In [3]: np.any(False)
Out[3]: False

In [4]: import numba

In [5]: @numba.njit
   ...: def numba_any(vals):
   ...:     return np.any(vals)

In [6]: numba_any(True)
---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
Input In [6], in <module>
----> 1 numba_any(True)

File ~/.local/anaconda/envs/astropy-dev/lib/python3.9/site-packages/numba/core/dispatcher.py:468, in _DispatcherBase._compile_for_args(self, *args, **kws)
    464         msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
    465                f"by the following argument(s):\n{args_str}\n")
    466         e.patch_message(msg)
--> 468     error_rewrite(e, 'typing')
    469 except errors.UnsupportedError as e:
    470     # Something unsupported is present in the user code, add help info
    471     error_rewrite(e, 'unsupported_error')

File ~/.local/anaconda/envs/astropy-dev/lib/python3.9/site-packages/numba/core/dispatcher.py:409, in _DispatcherBase._compile_for_args.<locals>.error_rewrite(e, issue_type)
    407     raise e
    408 else:
--> 409     raise e.with_traceback(None)

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function any at 0x7ff4004f5820>) found for signature:
 
 >>> any(bool)
 
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function 'np_any': File: numba/np/arraymath.py: Line 869.
    With argument(s): '(bool)':
   Rejected as the implementation raised a specific error:
     LoweringError: Failed in nopython mode pipeline (step: native lowering)
   Invalid store of i1* to i8* in <numba.core.datamodel.models.NdIter object at 0x7ff3b2bd29a0> (trying to write member #4)
   
   File "../../.local/anaconda/envs/astropy-dev/lib/python3.9/site-packages/numba/np/arraymath.py", line 873:
       def flat_any(a):
           for v in np.nditer(a):
           ^
   
   During: lowering "$8call_method.3 = call $4load_method.1(a, func=$4load_method.1, args=[Var(a, arraymath.py:873)], kws=(), vararg=None, target=None)" at /home/maxnoe/.local/anaconda/envs/astropy-dev/lib/python3.9/site-packages/numba/np/arraymath.py (873)
  raised from /home/maxnoe/.local/anaconda/envs/astropy-dev/lib/python3.9/site-packages/numba/core/errors.py:837

During: resolving callee type: Function(<function any at 0x7ff4004f5820>)
During: typing of call at <ipython-input-5-1baff706b788> (3)


File "<ipython-input-5-1baff706b788>", line 3:
def numba_any(vals):
    return np.any(vals)
    ^

maxnoe avatar Jul 22 '22 08:07 maxnoe

Thanks for the report. First thing is that this shows up a bug, there's a LoweringError in one of the implementations due to an invalid store of an i1* to an i8*. This stems from a bug in np.nditer:

from numba import njit
import numpy as np


@njit
def foo():
    for v in np.nditer(False):
        pass

foo()

were that fixed, this would probably work.

stuartarchibald avatar Jul 22 '22 14:07 stuartarchibald