Replacing dict type for numba as python function parameters

There are many parameters in my code that are constant during operation. For storage, I defined a type variable dict. But I believe that numbaI can not support dict.

What is better to solve this problem?

+4
source share
2 answers

Numba supports namedtuplesmode nopython, which should be a good alternative dictfor passing a large group of parameters to the numba jitted function.

+2
source

Assuming you have such a function, and you're fine, referring to it as an attribute, not an index:

import numba as nb

@nb.njit
def func(config):
    return config.c

collections.namedtuple (, @JoshAdel):

import numpy as np
from collections import namedtuple

conf = namedtuple('conf', ['a', 'b', 'c'])

func(conf(1, 2.0, np.array([1,2,3], dtype=np.int64)))
# array([1, 2, 3], dtype=int64)

jitclass:

spec = [('a', nb.int64),
        ('b', nb.float64),
        ('c', nb.int64[:])]

@nb.jitclass(spec)
class Conf:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

func(Conf(1, 2.0, np.array([1,2,3], dtype=np.int64)))
# array([1, 2, 3], dtype=int64)

, " " .

+2

Source: https://habr.com/ru/post/1684905/


All Articles