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

Document that using plain np.float (and maybe others) is not supported yet. #3068

Open
2 tasks
frstyang opened this issue Jun 29, 2018 · 8 comments
Open
2 tasks

Comments

@frstyang
Copy link

frstyang commented Jun 29, 2018

Feature request

Reporting a bug

I ran into this weird error when trying to use np.empty in a function definition compiled with numba, and turning on nopython=True to make sure optimized typing is in effect. It's weird because numba claims to support np.empty with the first two arguments, and I am only using the first two arguments (correctly I think?), so I don't know why it's not typing correctly.

@jit(nopython=True)
def empty():
    return np.empty(5, np.float)

After defining the above function in an ipython notebook,

empty()

Gives the following error message:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-88-927345c8757f> in <module>()
----> 1 empty()

~/.../lib/python3.5/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
    342                 raise e
    343             else:
--> 344                 reraise(type(e), e, None)
    345         except errors.UnsupportedError as e:
    346             # Something unsupported is present in the user code, add help info

~/.../lib/python3.5/site-packages/numba/six.py in reraise(tp, value, tb)
    656             value = tp()
    657         if value.__traceback__ is not tb:
--> 658             raise value.with_traceback(tb)
    659         raise value
    660 
TypingError: Failed at nopython (nopython frontend)
Invalid usage of Function(<built-in function empty>) with parameters (int64, Function(<class 'float'>))
 * parameterized
In definition 0:
    All templates rejected
[1] During: resolving callee type: Function(<built-in function empty>)
[2] During: typing of call at <ipython-input-87-8c7e8fa4c6eb> (3)


File "<ipython-input-87-8c7e8fa4c6eb>", line 3:
def empty():
    return np.empty(5, np.float)
    ^

This is not usually a problem with Numba itself but instead often caused by
the use of unsupported features or an issue in resolving types.

To see Python/NumPy features supported by the latest release of Numba visit:
http://numba.pydata.org/numba-doc/dev/reference/pysupported.html
and
http://numba.pydata.org/numba-doc/dev/reference/numpysupported.html

For more information about typing errors and how to debug them visit:
http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile

If you think your code should work with Numba, please report the error message
and traceback, along with a minimal reproducer at:
https://github.com/numba/numba/issues/new
@seibert
Copy link
Contributor

seibert commented Jun 29, 2018

This is a problem because Numba doesn't recognize np.float (which is not a NumPy type object, but rather the global Python float type) as a valid dtype. If you switch float for float64, this example works.

I think it is an easy fix to make sure we handle the expected aliasing of float => float64 and int => int64.

@sklam
Copy link
Member

sklam commented Jul 5, 2018

At developer meeting today, we mentioned that we probably want to handle dtpye=<str>; i.e. dtype='f8'. The change shouldn't be difficult but may involve many places.

@seibert seibert added the numpy label Aug 27, 2018
@seibert seibert added this to On Deck in Minor Features Aug 27, 2018
@hsaliak
Copy link

hsaliak commented Nov 11, 2019

This tripped me up, and the workaround is easy enough. Could I please request that it be documented?

A general mention that numba does not yet recognize np.float in the Supported Numpy Features section would be very helpful.

@esc
Copy link
Member

esc commented Sep 30, 2020

@hsaliak we recently merged the feature that @sklam mentions above:

#6262

Beyond that, I'll modify the title and labels of this PR to make it a feature request. It may also be worthwhile looking through the issue tracker for an issue that tracks implementing support for using np.float as a type as that is a fairly common idiom when using Numpy.

@esc esc changed the title Trying to initialize np.empty gives "All templates rejected" error Document that using plain np.float (and maybe others) is not supported yet. Sep 30, 2020
@gmarkall
Copy link
Member

Additionally:

from numba import njit
import numpy as np

@njit
def make_array():
    return np.array((1, 2), dtype=float)

make_array()

(using a plain float as the dtype) is not supported and produces a confusing error:

       numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
       No implementation of function Function(<intrinsic stub>) found for signature:
        
        >>> stub(Tuple(Literal[int](1), Literal[int](2)), Function(<class 'float'>))
        
       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): '(UniTuple(int64 x 2), Function(<class 'float'>))':
          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 /home/gmarkall/numbadev/numba/numba/core/typeinfer.py:1086

During: resolving callee type: Function(<built-in function array>)
During: typing of call at /home/gmarkall/numbadev/issues/joel135/minimal.py (7)


File "minimal.py", line 7:
def make_array():
    return np.array((1, 2), dtype=float)
    ^

@stuartarchibald
Copy link
Contributor

Note np.float has been deprecated (as have a number of the cpython type aliases under np.) since NumPy 1.20.

@gmarkall
Copy link
Member

Note np.float has been deprecated (as have a number of the cpython type aliases under np.) since NumPy 1.20.

Just for clarity (I'm not sure if your comment pertains to mine just prior or not) the issue with the example I posted is using the Python float.

@stuartarchibald
Copy link
Contributor

Note np.float has been deprecated (as have a number of the cpython type aliases under np.) since NumPy 1.20.

Just for clarity (I'm not sure if your comment pertains to mine just prior or not) the issue with the example I posted is using the Python float.

Apologies, I should have been more clear, my comment is WRT the OP. Python float is a related problem that is derived from the OP as np.float is an alias of float.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Minor Features
Solution Understood
Development

No branches or pull requests

7 participants