【问题标题】:Numba: How to avoid <type 'reflected list' found for argument> warning?Numba:如何避免 <type 'reflected list' found for argument> 警告?
【发布时间】:2021-06-24 22:03:51
【问题描述】:

numba 函数中,我传递的参数如下:

sets_one_step(idx,sets=[set1, set2], indexes=[sort_idx1, sort_idx2], probs=[P1, P2])

运行代码后,我收到以下警告:

/.local/lib/python3.6/site-packages/numba/core/ir_utils.py:2031: NumbaPendingDeprecationWarning: 
Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'sets' of function '__numba_parfor_gufunc_0x7f5ebfb88748'.

此特定错误/警告可能源自函数内的以下行:

for k in prange(len(sets)):

这里,set1, set2 是 numpy 数组(integerflaot32)。如果这只是 2 组,那么我可以在函数中将它们设为单独的参数。但是,我的应用程序可以有超过 2 个这样的数组。我想知道将来如何在我的用例中避​​免此警告。

【问题讨论】:

    标签: python numba


    【解决方案1】:

    考虑以下代码:

    import numba as nb
    
    @nb.njit
    def process_some_list(lst):
        return lst[0]
    

    传递标准 Python 列表会产生以下输出:

    >>> process_some_list([1,2,3])
    .../numba/core/ir_utils.py:2119: NumbaPendingDeprecationWarning: 
    Encountered the use of a type that is scheduled for deprecation:
    type 'reflected list' found for argument 'lst' of function 'process_some_list'.
    For more information visit https://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-reflection-for-list-and-set-types
    [... more lines deleted ...]
    1
    

    结果是正确的,但 Numba 说 reflected list 计划弃用。根据docs,您可以使用typed.List,如下例所示:

    >>> process_some_list(nb.typed.List([1,2,3]))
    1
    

    但是,如果您需要集合列表:

    >>> lst = [{1, 2}, {3, 4}]
    >>> process_some_list(nb.typed.List(lst))
    

    您会收到相同的 Set 弃用消息:

    .../numba/typed/typedlist.py:83: NumbaPendingDeprecationWarning: 
    Encountered the use of a type that is scheduled for deprecation: type 'reflected set' found for argument 'item' of function 'impl_append.<locals>.impl'.
    For more information visit https://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-reflection-for-list-and-set-types
    [... more lines deleted ...]
    {1, 2}
    

    结果再次正确,但文档说 typed.Set 尚未实现,因此您可能需要忍受警告。它们仅在编译函数时出现,无论是在第一次执行期间,还是在对参数和返回类型使用签名时声明时。

    【讨论】:

      猜你喜欢
      • 2016-12-25
      • 2021-02-04
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2014-03-21
      • 2014-10-23
      相关资源
      最近更新 更多