when can you use numpy arrays as dict values in numba?

Question:

I am confused by the type rules for numba dicts. Here is an MWE that works:

import numpy as np
import numba as nb

    @nb.njit
    def foo(a, b, c):
        d = {}
        d[(1,2,3)] = a
        return d
    
    a = np.array([1, 2])
    b = np.array([3, 4])
    t = foo(a, b, c)

But if I change the definition of foo as follows this fails:

    @nb.njit
    def foo(a, b, c):
        d = {}
        d[(1,2,3)] = np.array(a)
        return d
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function array>) found for signature:

 >>> array(array(int64, 1d, C))
 
There are 2 candidate implementations:
      - Of which 2 did not match due to:
      Overload in function 'impl_np_array': File: numba/np/arrayobj.py: Line 5384.
        With argument(s): '(array(int64, 1d, C))':
       Rejected as the implementation raised a specific error:
         TypingError: Failed in nopython mode pipeline (step: nopython frontend)
       No implementation of function Function(<intrinsic np_array>) found for signature:
        
        >>> np_array(array(int64, 1d, C), none)
        
       There are 2 candidate implementations:
             - Of which 2 did not match due to:
             Intrinsic in function 'np_array': File: numba/np/arrayobj.py: Line 5358.
               With argument(s): '(array(int64, 1d, C), none)':
              Rejected as the implementation raised a specific error:
                TypingError: array(int64, 1d, C) not allowed in a homogeneous sequence
         raised from /home/raph/python/mypython3.10/lib/python3.10/site-packages/numba/core/typing/npydecl.py:482
       
       During: resolving callee type: Function(<intrinsic np_array>)
       During: typing of call at /home/raph/python/mypython3.10/lib/python3.10/site-packages/numba/np/arrayobj.py (5395)
       
       
       File "../../python/mypython3.10/lib/python3.10/site-packages/numba/np/arrayobj.py", line 5395:
           def impl(object, dtype=None):
               return np_array(object, dtype)
               ^

  raised from /home/raph/python/mypython3.10/lib/python3.10/site-packages/numba/core/typeinfer.py:1086

During: resolving callee type: Function(<built-in function array>)
During: typing of call at <ipython-input-99-e05437a34ab9> (4)


File "<ipython-input-99-e05437a34ab9>", line 4:
def foo(a, b, c):
    <source elided>
    d = {}
    d[(1,2,3)] = np.array(a)
    ^

Why is this?

Asked By: Simd

||

Answers:

This doesn’t have anything with how numba handles dictionaries. This piece of code fails with the same error:

@nb.njit
def foo2(a, b, c):
    x = np.array(a)
    return x

When you look at the error message you see that numba doesn’t know how to initialize np.array from other np.array:

No implementation of function Function(<built-in function array>) found for signature:
 
 >>> array(array(int64, 1d, C))

If you change the code to:

@nb.njit
def foo2(a, b, c):
    x = np.array([*a])
    return x

the compilation succeeds.

Answered By: Andrej Kesely
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.