Skip to content

Instantly share code, notes, and snippets.

@samaid
Created October 18, 2019 17:29
Show Gist options
  • Save samaid/c0852268d017aa207e44a94d241807ab to your computer and use it in GitHub Desktop.
Save samaid/c0852268d017aa207e44a94d241807ab to your computer and use it in GitHub Desktop.
Illustrates Numba error diagnostics
from numba.typed import Dict
from numba import types, njit
import numpy as np
# Using Numba dictionary is more efficient for passing into compiled region due to lower unboxing overheads
# The Dict.empty() constructs a typed dictionary.
# The key and value typed must be explicitly declared.
nd = Dict.empty(
key_type=types.int64,
value_type=types.float64[:]
)
# Assigning key-values using Numpy arrays
nd[1] = np.asarray([1.1, 1.2], dtype='f8')
nd[2] = np.asarray([2.2], dtype='f8')
nd[3] = np.asarray([3.3, 3.4, 3.5], dtype='f8')
print(nd)
@njit
def foo(d):
d[4] = np.asarray([4.4, 4.5], dtype='f8')
return d
print(foo(nd))
@samaid
Copy link
Author

samaid commented Oct 18, 2019

{1: [1.1 1.2], 2: [2.2], 3: [3.3 3.4 3.5]}
Traceback (most recent call last):
  File "C:/Users/smaidano/PycharmProjects/Numba-Examples/numba_dict_njit.py", line 26, in <module>
    print(foo(nd))
  File "C:\Users\smaidano\AppData\Local\Continuum\miniconda3\envs\hpat37\lib\site-packages\numba\dispatcher.py", line 401, in _compile_for_args
    error_rewrite(e, 'typing')
  File "C:\Users\smaidano\AppData\Local\Continuum\miniconda3\envs\hpat37\lib\site-packages\numba\dispatcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "C:\Users\smaidano\AppData\Local\Continuum\miniconda3\envs\hpat37\lib\site-packages\numba\six.py", line 668, in reraise
    raise value.with_traceback(tb)
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<function asarray at 0x0000026414EBFA68>) with argument(s) of type(s): (list(float64), dtype=Literal[str](f8))
 * parameterized
In definition 0:
    TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function array>) with argument(s) of type(s): (list(float64), Literal[str](f8))
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<built-in function array>)
[2] During: typing of call at C:\Users\smaidano\AppData\Local\Continuum\miniconda3\envs\hpat37\lib\site-packages\numba\targets\arraymath.py (3860)


File "..\..\AppData\Local\Continuum\miniconda3\envs\hpat37\lib\site-packages\numba\targets\arraymath.py", line 3860:
            def impl(a, dtype=None):
                return np.array(a, dtype)
                ^

    raised from C:\Users\smaidano\AppData\Local\Continuum\miniconda3\envs\hpat37\lib\site-packages\numba\typeinfer.py:951
In definition 1:
    TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function array>) with argument(s) of type(s): (list(float64), unicode_type)
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<built-in function array>)
[2] During: typing of call at C:\Users\smaidano\AppData\Local\Continuum\miniconda3\envs\hpat37\lib\site-packages\numba\targets\arraymath.py (3860)


File "..\..\AppData\Local\Continuum\miniconda3\envs\hpat37\lib\site-packages\numba\targets\arraymath.py", line 3860:
            def impl(a, dtype=None):
                return np.array(a, dtype)
                ^

    raised from C:\Users\smaidano\AppData\Local\Continuum\miniconda3\envs\hpat37\lib\site-packages\numba\typeinfer.py:951
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<function asarray at 0x0000026414EBFA68>)
[2] During: typing of call at C:/Users/smaidano/PycharmProjects/Numba-Examples/numba_dict_njit.py (22)


File "numba_dict_njit.py", line 22:
def foo(d):
    d[4] = np.asarray([4.4, 4.5], dtype='f8')
    ^

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/latest/reference/pysupported.html
and
http://numba.pydata.org/numba-doc/latest/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


Process finished with exit code 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment