На протяжении всего кода, который я создаю, я использую словарь Python, который выглядит так:
my_dict = { (0,0,0): [(0,0,1),(0,1,0),(0,1,1)] }
Я пытаюсь преобразовать его в словарь, который может понять Numba а затем добавить к нему, и для этого мне нужно сначала объявить тип данных ключа словаря и соответствующие значения.
import numba
from numba import jit
from numba.core import types
from numba.typed import Dict
from numba.typed import List
my_dict = { (0,0,0): [(0,0,1),(0,1,0),(0,1,1)] }
# Determine type for key of my_dict
numba.typeof((0,0,0)) # UniTuple(int64 x 3)
numba.typeof([(0,0,1),(0,1,0),(0,1,1)]) # reflected list(UniTuple(int64 x 3))
my_dict_numba = Dict.empty(key_type = types.UniTuple(types.int64, 3),
value_type = <---- ????)
Насколько я понимаю, отраженный тип списка устарел, поэтому вместо него нужно использовать List (), поэтому измененный код должен быть:
my_dict = { (0,0,0): [(0,0,1),(0,1,0),(0,1,1)] }
my_dict_numba = Dict.empty(key_type = types.UniTuple(types.int64, 3),
value_type = types.ListType(types.UniTuple(types.int64,3)))
# Add to the dictionary:
my_dict_numba[(0,0,0)] = [(0,0,1),(0,1,0),(0,1,1)]
---------------------------------------------------------------------------
TypingError Traceback (most recent call last)
<ipython-input-387-f179359e43de> in <module>
13 value_type = types.ListType(types.UniTuple(types.int64,3)))
14
---> 15 neighbour_dict_numba[(0,0,0)] = [(0,0,1),(0,1,0),(0,1,1)]
16
17
c:\users\djgra\appdata\local\programs\python\python36\lib\site-packages\numba\typed\typeddict.py in __setitem__(self, key, value)
154 if not self._typed:
155 self._initialise_dict(key, value)
--> 156 return _setitem(self, key, value)
157
158 def __delitem__(self, key):
c:\users\djgra\appdata\local\programs\python\python36\lib\site-packages\numba\core\dispatcher.py in _compile_for_args(self, *args, **kws)
413 e.patch_message(msg)
414
--> 415 error_rewrite(e, 'typing')
416 except errors.UnsupportedError as e:
417 # Something unsupported is present in the user code, add help info
c:\users\djgra\appdata\local\programs\python\python36\lib\site-packages\numba\core\dispatcher.py in error_rewrite(e, issue_type)
356 raise e
357 else:
--> 358 reraise(type(e), e, None)
359
360 argtypes = []
c:\users\djgra\appdata\local\programs\python\python36\lib\site-packages\numba\core\utils.py in reraise(tp, value, tb)
78 value = tp()
79 if value.__traceback__ is not tb:
---> 80 raise value.with_traceback(tb)
81 raise value
82
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function setitem>) found for signature:
>>> setitem(DictType[UniTuple(int64 x 3),ListType[UniTuple(int64 x 3)]], UniTuple(int64 x 3), reflected list(UniTuple(int64 x 3)))
There are 10 candidate implementations:
- Of which 8 did not match due to:
Overload in function 'setitem': File: <built-in>: Line <N/A>.
With argument(s): '(DictType[UniTuple(int64 x 3),ListType[UniTuple(int64 x 3)]], UniTuple(int64 x 3), reflected list(UniTuple(int64 x 3)))':
No match.
- Of which 2 did not match due to:
Overload in function 'impl_setitem': File: numba\typed\dictobject.py: Line 669.
With argument(s): '(DictType[UniTuple(int64 x 3),ListType[UniTuple(int64 x 3)]], UniTuple(int64 x 3), reflected list(UniTuple(int64 x 3)))':
Rejected as the implementation raised a specific error:
LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)
Cannot cast reflected list(UniTuple(int64 x 3)) to ListType[UniTuple(int64 x 3)]: %".80" = load {i8*, i8*}, {i8*, i8*}* %"value"
File "..\..\appdata\local\programs\python\python36\lib\site-packages\numba\typed\dictobject.py", line 678:
def impl(d, key, value):
<source elided>
castedkey = _cast(key, keyty)
castedval = _cast(value, valty)
^
During: lowering "$0.8 = call $0.5(value, $0.7, func=$0.5, args=[Var(value, dictobject.py:677), Var($0.7, dictobject.py:678)], kws=(), vararg=None)" at c:\users\djgra\appdata\local\programs\python\python36\lib\site-packages\numba\typed\dictobject.py (678)
raised from c:\users\djgra\appdata\local\programs\python\python36\lib\site-packages\numba\core\utils.py:81
During: typing of setitem at c:\users\djgra\appdata\local\programs\python\python36\lib\site-packages\numba\typed\typeddict.py (34)
File "..\..\appdata\local\programs\python\python36\lib\site-packages\numba\typed\typeddict.py", line 34:
def _setitem(d, key, value):
d[key] = value
^
Как я могу создать словарь Numba, подобный приведенному выше, чтобы я мог потом перейти к функции?