Support List as a value type in njit dictionaries
See original GitHub issueIt doesn’t appear to be possible to pass a list as a value inside a dictionary. Here is a simple reproducer.
import numba
@numba.njit
def f():
d = {}
for i in range(10):
d[str(i)] = [i]
return d
print(f())
Produces: numba.core.errors.TypingError: list(int64)<iv=None> as value is forbidden
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Using Dictionaries with numba njit function - Stack Overflow
Now I have a function that accepts and returns dictionaries. How can I apply numba here? def collocation(aeolus_data,val_data): ... return ...
Read more >Supported Python features - Numba
Numba supports function calls using positional and named arguments, as well as arguments with default values and *args (note the argument for *args...
Read more >How to convert a non numba dictionary to a nb.typed.Dict?
Hi, using numba 0.54, I would like to convert a normal python dictionary to a numba compatible dictionary. My code is the following:...
Read more >numba/numba - Gitter
jit_cache 0.00012982199999989064 njit 4.040996092 njit_cache 0. ... I guess a dict type memoisation would work as long as the arguments are hashable (which ......
Read more >Data Dictionary for Enrollment Report. - NJIT
Items are listed in order of column in E-Report. ... Student Classification BOAP or when appropriate be the value to begin the next...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I suppose it’s fine to ask here, thank you! The reasons are quite technical, but I will try to shed some light on this:
It is important to note that Numba has two list implementations, a reflected list and a typed list. See also: https://numba.readthedocs.io/en/stable/reference/pysupported.html#list The reflected list can not be used inside a typed dictionary whereas the typed List can (perhaps the docs need an update here?).
The two list variants – reflected and typed – differ in how they are implemented and consequently differ in what features they currently support. The reflected list is a type of proxy object for a Python list. When a regular Python list transitions the jit-boundary, for example by being used as an argument to a
@njit
dec. function, Numba will setup a reflected list as a sort-of-copy of the original Python list. This reflected list then serves as a buffer to hold all the operations to be done on the list inside the function. Lastly, when the function completes, the reflected list will then “reflect” all the changes to the original Python list. As a consequence, a reflected list can’t really exist outside of an@njit
decorated function. The typed list on the other hand is implemented differently and can exist outside of a@njit
dec. function and be operated on as first-class object in Python.Now the typed dictionary is similar to a typed List, which means it can also exist as a first-class object and additionally, the values must also support this. For example, if you created a typed dict inside a
@njit
dec. function and added a reflected list, how should that list behave, when the dictionary is returned from the function as a Python object?Lastly the only set implementation currently available in Numba is the reflected set implementation and as a result it’s not possible to have sets as values in typed dicts. In order to support sets in dicts in such a context a proper implementation of a typed Set (similar to typed List and typed Dict) would be the best solution. As a rule of thumb I usually remember: reflected: no – typed: yes.
I hope this clarifies some of the nuances involved.
I do hope that at some point in the not too distant future
[]
in JIT-ed code will result in a typed List, instead of a reflected one.