heavyai / rbc

Remote Backend Compiler
https://rbc.readthedocs.io
BSD 3-Clause "New" or "Revised" License
30 stars 10 forks source link

Construct typesystem.Type from numba array type. #30

Open guilhermeleobas opened 4 years ago

guilhermeleobas commented 4 years ago
In [1]: import rbc

In [2]: rbc.typesystem.Type('int32[]')                                                                                                                                                                      
Out[2]: Type('int32[]',)

In [3]: at = rbc.typesystem.Type('int32[]')                                                                                                                                                                 

In [4]: import numba                                                                                                                                                                                        

In [5]: rt = numba.int32[:]              

In [6]: rt(at)

The code above fails with the following error:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-8-f452b777ef27> in <module>
----> 1 rt(at)
~/mconda3/envs/rbc-dev/lib/python3.7/site-packages/numba/types/abstract.py in __call__(self, *args)
    178         from ..typing import signature
    179         if len(args) == 1 and not isinstance(args[0], Type):
--> 180             return self.cast_python_value(args[0])
    181         return signature(self, # return_type
    182                          *args)
~/mconda3/envs/rbc-dev/lib/python3.7/site-packages/numba/types/abstract.py in cast_python_value(self, args)
    214 
    215     def cast_python_value(self, args):
--> 216         raise NotImplementedError
    217 
    218 
pearu commented 4 years ago

The recommended workflow that demonstrates the issue is:

In [1]: import rbc                                                                                                                                                                                          

In [2]: import numba                                                                                                                                                                                        

In [3]: at = rbc.typesystem.Type('int32[]')                                                                                                                                                                 

In [4]: target_info = rbc.targetinfo.TargetInfo.host()                                                                                                                                                      

In [5]: rt = rbc.typesystem.Type.fromnumba(numba.int32[:], target_info)                                                                                                                                     
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-5-9c218fec7bdd> in <module>
----> 1 rt = rbc.typesystem.Type.fromnumba(numba.int32[:], target_info)

~/git/xnd-project/rbc/rbc/typesystem.py in fromnumba(cls, t, target_info)
    680         if isinstance(t, nb.types.misc.CPointer):
    681             return cls(cls.fromnumba(t.dtype, target_info), '*')
--> 682         raise NotImplementedError(repr(t))
    683 
    684     @classmethod

NotImplementedError: array(int32, 1d, A)

If the above is fixed, then continue:

rt(at)

Another problem is that rbc.typesystem.Type("int32[]") and numba.int32[:] produce different array structures. It would be nice to unify these. The problem boils down to how to map numba array(int32, 1d, A) to omniscidb Array.

guilhermeleobas commented 4 years ago

~I believe this was partially fixed on #41.~ Removed to limit the pull request scope. Will add this in a later PR.

In [1]: import rbc

In [2]: import numba

In [3]: target_info = rbc.targetinfo.TargetInfo.host()

In [4]: at = rbc.typesystem.Type.fromstring('int32[:]', target_info)

In [5]: rt = rbc.typesystem.Type.fromnumba(numba.int32[:], target_info)

In [6]: at
Out[6]: Type(Type('int32',), '[:]')

In [7]: rt
Out[7]: Type(Type('int32',), '[:]')
pearu commented 4 years ago

The current state is:

In [19]: rt = rbc.typesystem.Type.fromnumba(numba.int32[:], target_info)                                                                                                                                    
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-19-9c218fec7bdd> in <module>
----> 1 rt = rbc.typesystem.Type.fromnumba(numba.int32[:], target_info)

~/git/xnd-project/rbc/rbc/typesystem.py in fromnumba(cls, t, target_info)
    713         if isinstance(t, nb.types.misc.CPointer):
    714             return cls(cls.fromnumba(t.dtype, target_info), '*')
--> 715         raise NotImplementedError(repr(t))
    716 
    717     @classmethod

NotImplementedError: array(int32, 1d, A)

Thoughts:

pearu commented 4 years ago

Marked as code quality because it would be nice for the completeness of the typesystem if

rt = rbc.typesystem.Type.fromnumba(numba.int32[:], target_info)

would work. Otherwise, this is not required for omnisci and numpy array connection.