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.

Dict in njitted function

See original GitHub issue

Minimum 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:open
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mix-protocolcommented, Oct 7, 2021

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.

0reactions
pengchaoandycommented, Dec 23, 2020

It is a deviation from Python semantics in Numba that globals are treated as constants: https://numba.readthedocs.io/en/stable/reference/pysemantics.html?highlight=global#global-and-closure-variables - however, I think it ought to be possible to support your use cases, since they don’t mutate the Dict inside the jitted function.

I’ll labelling this as a feature request, to support read-only access to global typed dicts in nopython mode.

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.

Read more comments on GitHub >

github_iconTop 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 >

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