question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using jitclass with types.List of array results in TypingError

See original GitHub issue

Issue

Using jitclass() on a class with an attribute of type ListType[array(int64, 1d, A)] leads to a TypingError. However, this type is correctly printed when calling without a decorator.

Minimal example

from numba import types, typed, typeof
from numba.experimental import jitclass
import numpy as np

spec = [
    ("b", types.ListType(types.int64[:])),
]

@jitclass(spec)
class ListAttribute:
    def __init__(self):
        self.b = typed.List.empty_list(types.int64[:])
        self.b.append(np.zeros(2, dtype=np.int64))


x = ListAttribute()
print(typeof(x.b))

Trace

Traceback (most recent call last):
  File "/home/suthep/.config/JetBrains/PyCharm2020.1/scratches/scratch_1.py", line 16, in <module>
    x = ListAttribute()
  File "/home/suthep/git/uas/venv/lib/python3.8/site-packages/numba/experimental/jitclass/base.py", line 122, in __call__
    return cls._ctor(*bind.args[1:], **bind.kwargs)
  File "/home/suthep/git/uas/venv/lib/python3.8/site-packages/numba/core/dispatcher.py", line 401, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/suthep/git/uas/venv/lib/python3.8/site-packages/numba/core/dispatcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "/home/suthep/git/uas/venv/lib/python3.8/site-packages/numba/core/utils.py", line 80, in reraise
    raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function getitem>) with argument(s) of type(s): (class(int64), slice<a:b>)
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.
In definition 2:
    All templates rejected with literals.
In definition 3:
    All templates rejected without literals.
In definition 4:
    All templates rejected with literals.
In definition 5:
    All templates rejected without literals.
In definition 6:
    All templates rejected with literals.
In definition 7:
    All templates rejected without literals.
In definition 8:
    All templates rejected with literals.
In definition 9:
    All templates rejected without literals.
In definition 10:
    All templates rejected with literals.
In definition 11:
    All templates rejected without literals.
In definition 12:
    All templates rejected with literals.
In definition 13:
    All templates rejected without literals.
In definition 14:
    All templates rejected with literals.
In definition 15:
    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: typing of intrinsic-call at /home/suthep/.config/JetBrains/PyCharm2020.1/scratches/scratch_1.py (12)
[2] During: typing of static-get-item at /home/suthep/.config/JetBrains/PyCharm2020.1/scratches/scratch_1.py (12)

File "scratch_1.py", line 12:
    def __init__(self):
        self.b = typed.List.empty_list(types.int64[:])
        ^

[1] During: resolving callee type: jitclass.ListAttribute#7f7dfdb8b910<b:ListType[array(int64, 1d, A)]>
[2] During: typing of call at <string> (3)

[3] During: resolving callee type: jitclass.ListAttribute#7f7dfdb8b910<b:ListType[array(int64, 1d, A)]>
[4] During: typing of call at <string> (3)


File "<string>", line 3:
<source missing, REPL/exec in use?>

Environment:

python==3.8.2

attrs==19.3.0
coverage==5.1
cycler==0.10.0
decorator==4.4.2
imagecodecs==2020.2.18
imageio==2.8.0
joblib==0.14.1
kiwisolver==1.2.0
llvmlite==0.32.1
matplotlib==3.2.1
more-itertools==8.2.0
networkx==2.4
numba==0.49.1
numpy==1.18.4
packaging==20.3
Pillow==7.1.2
pluggy==0.13.1
py==1.8.1
pyparsing==2.4.7
pytest==5.4.2
python-dateutil==2.8.1
PyWavelets==1.1.1
scikit-image==0.17.2
scikit-learn==0.23.0
scipy==1.4.1
six==1.14.0
threadpoolctl==2.0.0
tifffile==2020.5.11

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
esccommented, May 19, 2020

@spomjaksilp thanks for asking about this on the Numba issue tracker. Specifying types involving jitclasses, typed-lists and arrays can be a bit quirky at times. Here is a snippet that should get you what you want:

from numba import types, typed, typeof
from numba.experimental import jitclass
import numpy as np

array_type = types.int64[:]

spec = [
    ("b", types.ListType(array_type)),
]


@jitclass(spec)
class ListAttribute:
    def __init__(self):
        self.b = typed.List.empty_list(array_type)
        self.b.append(np.zeros(2, dtype=np.int64))


x = ListAttribute()
print(typeof(x.b))
0reactions
esccommented, Jun 22, 2020

@andreapiso thanks, that’s some very helpful input, we appreciate it!

Perhaps the following could be used to make the type spec for the Array:

In [14]: numba.types.Array(numba.types.int64, ndim=1, layout="C")
Out[14]: array(int64, 1d, C)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to do nested jitclass array instantiation - Stack Overflow
As the docs say, typed.List is an experimental feature, and it seems that inferring the type from a normal Python list doesn't work....
Read more >
Types and signatures - Numba documentation
You will encounter Numba types mainly when trying to inspect the results of Numba's type inference, for debugging or educational purposes. However, you...
Read more >
Compiling Python classes with @jitclass - Numba
List as a class member in a jitclass . Methods for using these types and various common patterns are presented in the following:...
Read more >
numba/numba - Gitter
@guilhermeleobas I just reviewed the __getitem__ fix for the typed list, ... I can't just use a dictionary because I have values of...
Read more >
Typed list of jitted functions in jitclass - Numba Discussion
I am trying to create a jitted class that contains a typed list of jitted functions. I have tried using the following in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found