Numba: Typing jit signature of readonly arrays inputs

Created on 3 Sep 2019  路  3Comments  路  Source: numba/numba

Reporting a bug

  • [x] I am using the latest released version of Numba (most recent is visible in
    the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
  • [x] I have included below a minimal working reproducer (if you are unsure how
    to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

Is there a way to type the signature of readonly arrays? I did not find a way to specify readonly on the documentation page or StackOverflow (or maybe specifying array should work on readonly arrays as well?).

In [1]: import numba

In [2]: arr = np.array([1])

In [3]: arr.setflags(write=False)

In [4]: @numba.jit((numba.int64[:],))
   ...: def f(x):
   ...:     return x
   ...:

In [5]: f(arr)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-0e64fe0f2f72> in <module>
----> 1 f(arr)

/lib/python3.7/site-packages/numba/dispatcher.py in _explain_matching_error(self, *args, **kws)
    498         msg = ("No matching definition for argument type(s) %s"
    499                % ', '.join(map(str, args)))
--> 500         raise TypeError(msg)
    501
    502     def _search_new_conversions(self, *args, **kws):

TypeError: No matching definition for argument type(s) readonly array(int64, 1d, C)

In [6]: numba.__version__
Out[6]: '0.45.1'
no action required question

Most helpful comment

Thanks for the report. Unfortunately there's no short cut for specifying an array that is read only, the full numba.types API calls have to be used. Here's an example:

In [1]: from numba import njit, types                                                                                           

In [2]: import numpy as np                                                                                                      

In [3]: arr = np.array([1])                                                                                                     

In [4]: arr.setflags(write=False)                                                                                               

In [5]: arr.flags                                                                                                               
Out[5]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In [6]: @njit((types.Array(types.int64, 1, 'C', readonly=True),)) # this is an int64, 1D, C order, read only array
   ...: def f(x): 
   ...:     return x 
   ...:                                                                                                                         

In [7]: f(arr)                                                                                                                  
Out[7]: array([1])

In [8]: f.signatures                                                                                                            
Out[8]: [(readonly array(int64, 1d, C),)]

hope this helps.

All 3 comments

Thanks for the report. Unfortunately there's no short cut for specifying an array that is read only, the full numba.types API calls have to be used. Here's an example:

In [1]: from numba import njit, types                                                                                           

In [2]: import numpy as np                                                                                                      

In [3]: arr = np.array([1])                                                                                                     

In [4]: arr.setflags(write=False)                                                                                               

In [5]: arr.flags                                                                                                               
Out[5]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In [6]: @njit((types.Array(types.int64, 1, 'C', readonly=True),)) # this is an int64, 1D, C order, read only array
   ...: def f(x): 
   ...:     return x 
   ...:                                                                                                                         

In [7]: f(arr)                                                                                                                  
Out[7]: array([1])

In [8]: f.signatures                                                                                                            
Out[8]: [(readonly array(int64, 1d, C),)]

hope this helps.

@mroeschke I will close this issue now as I believe this issue has been resolved. Thanks again for asking about this.

Great, thanks for your response!

Was this page helpful?
0 / 5 - 0 ratings