Передача переменных конфигурации в функции, чтобы они вели себя как константы времени компиляции.Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Передача переменных конфигурации в функции, чтобы они вели себя как константы времени компиляции.

Сообщение Anonymous »

В numba я хочу передать переменную конфигурации в функцию как константу времени компиляции. В частности, я хочу сделать следующее:

Код: Выделить всё

    @njit
def physics(config):
flagA = config.flagA
flagB = config.flagB
aNumbaList = List()
for i in range(100):
if flagA:
aNumbaList.append(i)
else:
aNumbaList.append(i/10)
return aNumbaList
Если переменные конфигурации являются константами времени компиляции, это бы прошло, но это не так, поэтому выдает ошибки, говорящие, что есть два кандидата

Код: Выделить всё

There are 2 candidate implementations:
- Of which 2 did not match due to:
...
...

Я просмотрел протокол одного из собраний Numba и обнаружил, что есть способ провести это собрание Numba: 05.03.2024
Я попробовал это, но он все еще поднимается та же ошибка.
Вот код с сообщением об ошибке:

Код: Выделить всё

.. code:: ipython3

from numba import jit, types, njit
from numba.extending import overload
from numba.typed import List
import functools

.. code:: ipython3

class Config():
def __init__(self, flagA, flagB):
self._flagA = flagA
self._flagB = flagB

@property
def flagA(self):
return self._flagA

@property
def flagB(self):
return self._flagB

.. code:: ipython3

@functools.cache
def obj2strkeydict(obj, config_name):

# unpack object to freevars and close over them
tmp_a = obj.flagA
tmp_b = obj.flagB
assert isinstance(config_name, str)
tmp_force_heterogeneous = config_name

@njit
def configurator():
d = {'flagA': tmp_a,
'flagB': tmp_b,
'config_name': tmp_force_heterogeneous}
return d

# return a configuration function that returns a string-key-dict
# representation of the configuration object.
return configurator

.. code:: ipython3

@njit
def physics(cfig_func):
config = cfig_func()
flagA = config['flagA']
flagB = config['flagB']
aNumbaList = List()
for i in range(100):
if flagA:
aNumbaList.append(i)
else:
aNumbaList.append(i/10)
return aNumbaList

.. code:: ipython3

def demo():
configuration1 = Config(True, False)
jit_config1 = obj2strkeydict(configuration1, 'config1')
physics(jit_config1)

..  code:: ipython3

demo()

::

---------------------------------------------------------------------------

TypingError                               Traceback (most recent call last)

Cell In[83], line 1
----> 1 demo()

Cell In[82], line 4, in demo()
2 configuration1 = Config(True, False)
3 jit_config1 = obj2strkeydict(configuration1, 'config1')
----> 4 physics(jit_config1)

File ~/anaconda3/envs/tardis/lib/python3.11/site-packages/numba/core/dispatcher.py:468, in _DispatcherBase._compile_for_args(self, *args, **kws)
464         msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
465                f"by the following argument(s):\n{args_str}\n")
466         e.patch_message(msg)
--> 468     error_rewrite(e, 'typing')
469 except errors.UnsupportedError as e:
470     # Something unsupported is present in the user code, add help info
471     error_rewrite(e, 'unsupported_error')

File ~/anaconda3/envs/tardis/lib/python3.11/site-packages/numba/core/dispatcher.py:409, in _DispatcherBase._compile_for_args..error_rewrite(e, issue_type)
407     raise e
408 else:
--> 409     raise e.with_traceback(None)

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
- Resolution failure for literal arguments:
No implementation of function Function() found for signature:

>>> impl_append(ListType[int64], float64)

There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload in function 'impl_append': File: numba/typed/listobject.py: Line 592.
With argument(s): '(ListType[int64], float64)':
Rejected as the implementation raised a specific error:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function() found for signature:

>>> _cast(float64, class(int64))

There are 2 candidate implementations:
- Of which 2 did not match due to:
Intrinsic in function '_cast': File: numba/typed/typedobjectutils.py: Line 22.
With argument(s): '(float64, class(int64))':
Rejected as the implementation raised a specific error:
TypingError: cannot safely cast float64 to int64. Please cast explicitly.
raised from /home/sam/anaconda3/envs/tardis/lib/python3.11/site-packages/numba/typed/typedobjectutils.py:75

During: resolving callee type: Function()
During: typing of call at /home/sam/anaconda3/envs/tardis/lib/python3.11/site-packages/numba/typed/listobject.py (600)

File "../anaconda3/envs/tardis/lib/python3.11/site-packages/numba/typed/listobject.py", line 600:
def impl(l, item):
casteditem = _cast(item, itemty)
^

raised from /home/sam/anaconda3/envs/tardis/lib/python3.11/site-packages/numba/core/typeinfer.py:1086

- Resolution failure for non-literal arguments:
None

During: resolving callee type: BoundFunction((, 'append') for ListType[int64])
During: typing of call at /tmp/ipykernel_9889/739598600.py (11)

File "../../../tmp/ipykernel_9889/739598600.py", line 11:

Любая помощь или любая ссылка на соответствующий материал действительно мне помогут.
Спасибо.

Подробнее здесь: https://stackoverflow.com/questions/786 ... -constants
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»