Numba 如何在课堂上使用字典

问题描述 投票:0回答:0

如下所述,dict 应该有键 2 元组的 int 和值 ints。

from numba.experimental import jitclass
import numba

@jitclass({'shape': numba.types.Tuple((numba.int32, numba.int32)), 'dict': numba.types.DictType(numba.types.UniTuple(numba.types.int32, 2), numba.int32)})
class BigramCounts:
    def __init__(self, shape: tuple[int, int]):
        self.shape = shape
        self.dict = {}  # this does not work
        
        # the following does not work either
        # self.dict = numba.typed.Dict.empty(key_type=numba.types.UniTuple(numba.types.int32, 2), value_type=numba.int32)

b_c = BigramCounts((2, 3))

不幸的是

self.dict = {}
初始化不起作用:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Cannot infer the type of variable '$8build_map.2' (temporary variable), have imprecise type: DictType[undefined,undefined]<iv={}>. 

File "scratch_3.py", line 8:
    def __init__(self, shape: tuple[int, int]):
        <source elided>
        self.shape = shape
        self.dict = {}  # this does not work
        ^

During: resolving callee type: jitclass.BigramCounts#1055192d0<shape:UniTuple(int32 x 2),dict:DictType[UniTuple(int32 x 2),int32]<iv=None>>
During: typing of call at <string> (3)

During: resolving callee type: jitclass.BigramCounts#1055192d0<shape:UniTuple(int32 x 2),dict:DictType[UniTuple(int32 x 2),int32]<iv=None>>
During: typing of call at <string> (3)


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

二次初始化也不行(numba.types怎么不支持类?):

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend) Failed in nopython mode pipeline (step: nopython frontend) Invalid use of <class 'numba.core.types.containers.UniTuple'> with parameters (class(int32), Literal[int](2)) No type info available for <class 'numba.core.types.containers.UniTuple'> as a callable. During: resolving callee type: typeref[<class 'numba.core.types.containers.UniTuple'>] During: typing of call at /Users/adam/Library/Application Support/JetBrains/PyCharm2022.3/scratches/scratch_3.py (11)


File "scratch_3.py", line 11:
    def __init__(self, shape: tuple[int, int]):
        <source elided>
        # the following does not work either
        self.dict = numba.typed.Dict.empty(key_type=numba.types.UniTuple(numba.types.int32, 2), value_type=numba.int32)
        ^

During: resolving callee type: jitclass.BigramCounts#11270d5a0<shape:UniTuple(int32 x 2),dict:DictType[UniTuple(int32 x 2),int32]<iv=None>> During: typing of call at <string> (3)

During: resolving callee type: jitclass.BigramCounts#11270d5a0<shape:UniTuple(int32 x 2),dict:DictType[UniTuple(int32 x 2),int32]<iv=None>> During: typing of call at <string> (3)


File "<string>", line 3: <source missing, REPL/exec in use?>
python numba
© www.soinside.com 2019 - 2024. All rights reserved.