Dict in njitted function
See original GitHub issueMinimum example:
import numba as nb
from numba.core import types
from numba.typed import Dict
d_test = Dict.empty(
key_type=types.unicode_type,
value_type=types.float64,
)
d_test['a'] = 0.1
d_test['b'] = 0.2
@nb.njit()
def nb_dict_test():
v1 = d_test['a']
v2 = d_test['b']
summed_v = v1 + v2
return summed_v
nb_dict_test()
or
d_test = Dict.empty(
key_type=types.float64,
value_type=types.float64,
)
d_test[0.1] = 0.1
d_test[0.2] = 0.2
@nb.njit()
def nb_dict_test():
v1 = d_test[0.1]
v2 = d_test[0.2]
summed_v = v1 + v2
return summed_v
nb_dict_test()
do not work with the error message
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: ensure IR is legal prior to lowering)
The use of a DictType[unicode_type,float64]<iv=None> type, assigned to variable 'd_test' in globals, is not supported as globals are considered compile-time constants and there is no known way to compile a DictType[unicode_type,float64]<iv=None> type as a constant.
File "<string>", line 12:
<source missing, REPL/exec in use?>
Putting the dict into the function does work, but this is not the desired solution as it is slow for my case:
@nb.njit()
def nb_dict_test():
d_test = Dict.empty(
key_type=types.float64,
value_type=types.float64,
)
d_test[0.1] = 0.1
d_test[0.2] = 0.2
v1 = d_test[0.1]
v2 = d_test[0.2]
summed_v = v1 + v2
return summed_v
nb_dict_test()
Not sure if this is indeed not implemented or if there is an error in my implementation? The goal is to have a lookup table inside of a @nb.njit() function - is there any recommended way to do it?
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Using Dictionaries with numba njit function - Stack Overflow
How do I merge two dictionaries in a single expression? 2625 · How do I sort a list of dictionaries by a value...
Read more >How to use a sequence as dict key? - #23 by ulupo - Support
I need to index a dict with sequences. ... like __getitem__ and __setitem__ # can be proxied through functions that call `hash` on...
Read more >Python dict() Function - W3Schools
The dict() function creates a dictionary. A dictionary is a collection which is unordered, changeable and indexed. Read more about dictionaries in the ......
Read more >Supported Python features - Numba
set, dict and generator comprehensions; generator delegation: yield from. Functions¶. Function calls¶. Numba supports function calls ...
Read more >5. Data Structures — Python 3.11.1 documentation
List comprehensions can contain complex expressions and nested functions: ... Another useful data type built into Python is the dictionary (see Mapping ...
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
Hello! Just seconding this feature request and checking in if you have found a workaround to this? I’m currently facing the same issue (using a fixed dictionary external to njitted function) and this feature would be extremely useful.
If there is a workaround to support this feature in numba 0.52? I tried to hack lower_assign by return llvmlite.ir.values.Constant if value.value is a typed.Dict, but not working.