import numba
import numpy
class Wrapped(numpy.ndarray):
def __new__(cls, original):
return original.view(cls)
def __array_finalize__(self, obj):
pass
class WrappedType(numba.types.Type):
def __init__(self):
super(WrappedType, self).__init__(name="Wrapped")
wrappedtype = WrappedType()
@numba.extending.typeof_impl.register(Wrapped)
def typeof_index(val, c):
return wrappedtype
@numba.extending.lower_builtin("getitem", wrappedtype, numba.types.Integer)
def wrapped_getitem(context, builder, sig, args):
return 999
@numba.njit
def testie(x):
return x[2]
a = Wrapped(numpy.array([1, 2, 3, 4, 5]))
print(testie(a))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 11, in <module>
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/dispatcher.py", line 330, in _compile_for_args
raise e
numba.errors.TypingError: Caused By:
Traceback (most recent call last):
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/compiler.py", line 238, in run
stage()
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/compiler.py", line 452, in stage_nopython_frontend
self.locals)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/compiler.py", line 865, in type_inference_stage
infer.propagate()
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/typeinfer.py", line 844, in propagate
raise errors[0]
TypingError: Invalid usage of getitem with parameters (Wrapped, int64)
* parameterized
File "<string>", line 9
[1] During: typing of intrinsic-call at <string> (9)
[2] During: typing of static-get-item at <string> (9)
Failed at nopython (nopython frontend)
Invalid usage of getitem with parameters (Wrapped, int64)
* parameterized
File "<string>", line 9
[1] During: typing of intrinsic-call at <string> (9)
[2] During: typing of static-get-item at <string> (9)
import numba
import numpy
class Wrapped(numpy.ndarray):
def __new__(cls, original):
return original.view(cls)
def __array_finalize__(self, obj):
pass
class WrappedType(numba.types.Array): pass
wrappedtype = WrappedType(numba.types.int64, 1, "C", readonly=True, name="Wrapped", aligned=True)
@numba.extending.typeof_impl.register(Wrapped)
def typeof_index(val, c):
return wrappedtype
@numba.extending.register_model(WrappedType)
class WrappedModel(numba.datamodel.models.ArrayModel):
pass
@numba.extending.unbox(WrappedType)
def unbox_wrapped(typ, obj, c):
return numba.targets.boxing.unbox_array(typ, obj, c)
@numba.extending.box(WrappedType)
def box_wrapped(typ, val, c):
return numba.targets.boxing.box_array(typ, val, c)
def something(array, index):
return array[index] + 100
@numba.extending.overload(something)
def wrapped_something(array, index):
if isinstance(array, WrappedType) and isinstance(index, numba.types.Integer):
return something
@numba.njit
def test1(x):
return something(x, 2)
a = Wrapped(numpy.array([0, 10, 20, 30]))
result = test1(a)
print(result)
@numba.extending.lower_builtin("getitem", WrappedType, numba.types.Integer)
def wrapped_getitem(context, builder, sig, args):
for key, value in context._defns.items():
if getattr(key, "__name__", None) == "something":
break
imp = value.versions[0][1]
return imp(context, builder, sig, args)
@numba.njit
def test2(x):
return x[3]
result = test2(a)
print(result)
@numba.njit([numba.types.int64(wrappedtype, numba.types.int64)])
def something(array, index):
return array[index] + 100
cres = something.overloads.values()[0]
something_imp = cres.target_context.get_function(cres.entry_point, cres.signature)._impdel cres.target_context._defns[cres.entry_point]
@numba.extending.lower_builtin("getitem", WrappedType, numba.types.Integer)
def wrapped_getitem(context, builder, sig, args):
return something_imp(context, builder, sig, args)
@numba.typing.templates.infer
class GetItemInterval(numba.typing.templates.AbstractTemplate):
key = "getitem"
def generic(self, args, kwds):
tpe, idx = args
if isinstance(tpe, WrappedType):
idx = numba.typing.builtins.normalize_1d_index(idx)
if isinstance(idx, numba.types.Integer):
return numba.typing.templates.signature(numba.types.float64, tpe, idx)
a = JaggedArray.fromlists([1.1, 1.1, 1.1], [], [3.3, 3.3])
# a = JaggedArray(numpy.array([1.1, 1.1, 1.1, 3.3, 3.3]), numpy.array([3, 3, 5]))
@numba.njit
def test1(a, i):
return a[i]
print(test1(a, 0), a[0])
print(test1(a, 1), a[1])
print(test1(a, 2), a[2])
print(test1(a, -1), a[-1])
print(test1(a, -2), a[-2])
print(test1(a, -3), a[-3])
try:
test1(a, -3), a[3]
except IndexError:
print("IndexError")
try:
test1(a, -3), a[-4]
except IndexError:
print("IndexError")
@numba.njit
def test2(a):
out = 0.0
for ai in a:
out += ai.sum()
return out
print(test2(a))
print(test2(a))
print(test2(a))
@numba.njit
def test3(a):
return len(a)
print(test3(a))
[ 1.1 1.1 1.1] [ 1.1 1.1 1.1]
[] []
[ 3.3 3.3] [ 3.3 3.3]
[ 3.3 3.3] [ 3.3 3.3]
[] []
[ 1.1 1.1 1.1] [ 1.1 1.1 1.1]
IndexError
IndexError
9.9
9.9
9.9
3
--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users+unsubscribe@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/93f5c6ef-eb80-4771-b244-2e4c8f7a5240%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.
--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users+unsubscribe@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/a30e07a8-f56d-4c7d-96dc-8bc2bac380db%40continuum.io.
And yes, we are interested in facilitating projects that build upon the Numba core (but don't necessarily have to become part of the Numba code base). Before Numba 1.0, we'll be listing out exactly which things are considered stable for the 1.x series, and suggesting that all projects that depend on Numba fix their dependency to Numba >= 1 and Numba < 2. Until 1.0 comes out next year, we don't expect any major breakage but can't guarantee it.
On Wed, Nov 8, 2017 at 3:15 PM, Stanley Seibert <ssei...@anaconda.com> wrote:
As a former ROOT user myself, I'm excited to see this! I'd like to figure out how to add a more tutorial-oriented section to our docs that capture what you've described here for other extension authors.
On Wed, Nov 8, 2017 at 3:08 PM, Jim Pivarski <jpiv...@gmail.com> wrote:
I suppose what I'm trying to do is rather transparent: this is the ROOT I/O arm of a larger project to perform calculations over Object-Array-Maps (OAM in analogy with ORM). Up to now, I've been computing functions on complicated columnar data by transforming the Python AST and then passing the results to Numba:My AST-to-AST transformation is essentially a compiler that has to do type inference. It would be more robust if I could just express my types and lowering rules for an existing compiler, such as Numba. I had some trouble with this half a year ago, but I'm starting to understand Numba's internals well enough that I think I'll be able to rewrite OAMap as a suite of Numba extensions. Then we'd be able to do things like write functions that take Apache Arrow buffers as input, treat them as nested objects in the source code, but have that compile to direct memory access (compact and usually sequential). If all works well and the Numba team is interested, it could become a submodule in Numba. (That's why I'm keeping the OAMap code separate from ROOT I/O.)-- Jim
--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.
To post to this group, send email to numba...@continuum.io.