-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create EmptyArray with unknown type. (#21)
* Start work. * EmptyArray compiles. * Fixed linker errors. * Make Windows compiler happy. * Add tests file. * Added RegularType to give all arrays a high-level type. * Added awkward1.typeof to test EmptyArray type. * EmptyArray::getitem works. * EmptyArray::getitem works in Numba; done with EmptyArray.
- Loading branch information
Showing
26 changed files
with
803 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.20 | ||
0.1.21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# BSD 3-Clause License; see https://github.com/jpivarski/awkward-1.0/blob/master/LICENSE | ||
|
||
import operator | ||
|
||
import numpy | ||
import numba | ||
|
||
import awkward1.layout | ||
from ..._numba import cpu, util, content | ||
|
||
@numba.extending.typeof_impl.register(awkward1.layout.EmptyArray) | ||
def typeof(val, c): | ||
return EmptyArrayType(numba.typeof(val.id)) | ||
|
||
class EmptyArrayType(content.ContentType): | ||
def __init__(self, idtpe): | ||
super(EmptyArrayType, self).__init__(name="EmptyArrayType(id={})".format(idtpe.name)) | ||
self.idtpe = idtpe | ||
|
||
@property | ||
def ndim(self): | ||
return 1 | ||
|
||
def getitem_int(self): | ||
raise ValueError("cannot compile getitem for EmptyArray, which has unknown element type") | ||
|
||
def getitem_range(self): | ||
return self | ||
|
||
def getitem_tuple(self, wheretpe): | ||
if len(wheretpe.types) == 0: | ||
return self | ||
elif len(wheretpe.types) == 1 and isinstance(wheretpe.types[0], numba.types.SliceType): | ||
return self | ||
else: | ||
raise ValueError("cannot compile getitem for EmptyArray, which has unknown element type") | ||
|
||
def getitem_next(self, wheretpe, isadvanced): | ||
if len(wheretpe.types) == 0: | ||
return self | ||
else: | ||
raise ValueError("cannot compile getitem for EmptyArray, which has unknown element type") | ||
|
||
def carry(self): | ||
return self | ||
|
||
@property | ||
def lower_len(self): | ||
return lower_len | ||
|
||
@property | ||
def lower_getitem_range(self): | ||
return lower_getitem_range | ||
|
||
@property | ||
def lower_getitem_next(self): | ||
return lower_getitem_next | ||
|
||
@property | ||
def lower_carry(self): | ||
return lower_carry | ||
|
||
@numba.extending.register_model(EmptyArrayType) | ||
class EmptyArrayModel(numba.datamodel.models.StructModel): | ||
def __init__(self, dmm, fe_type): | ||
members = [] | ||
if fe_type.idtpe != numba.none: | ||
members.append(("id", fe_type.idtpe)) | ||
super(EmptyArrayModel, self).__init__(dmm, fe_type, members) | ||
|
||
@numba.extending.unbox(EmptyArrayType) | ||
def unbox(tpe, obj, c): | ||
proxyout = numba.cgutils.create_struct_proxy(tpe)(c.context, c.builder) | ||
if tpe.idtpe != numba.none: | ||
id_obj = c.pyapi.obj_getattr_string(obj, "id") | ||
proxyout.id = c.pyapi.to_native_value(tpe.idtpe, id_obj).value | ||
c.pyapi.decref(id_obj) | ||
is_error = numba.cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) | ||
return numba.extending.NativeValue(proxyout._getvalue(), is_error) | ||
|
||
@numba.extending.box(EmptyArrayType) | ||
def box(tpe, val, c): | ||
EmptyArray_obj = c.pyapi.unserialize(c.pyapi.serialize_object(awkward1.layout.EmptyArray)) | ||
proxyin = numba.cgutils.create_struct_proxy(tpe)(c.context, c.builder, value=val) | ||
if tpe.idtpe != numba.none: | ||
id_obj = c.pyapi.from_native_value(tpe.idtpe, proxyin.id, c.env_manager) | ||
out = c.pyapi.call_function_objargs(EmptyArray_obj, (id_obj,)) | ||
c.pyapi.decref(id_obj) | ||
else: | ||
out = c.pyapi.call_function_objargs(EmptyArray_obj, ()) | ||
c.pyapi.decref(EmptyArray_obj) | ||
return out | ||
|
||
@numba.extending.lower_builtin(len, EmptyArrayType) | ||
def lower_len(context, builder, sig, args): | ||
return context.get_constant(numba.intp, 0) | ||
|
||
@numba.extending.lower_builtin(operator.getitem, EmptyArrayType, numba.types.slice2_type) | ||
def lower_getitem_range(context, builder, sig, args): | ||
rettpe, (tpe, wheretpe) = sig.return_type, sig.args | ||
val, whereval = args | ||
if context.enable_nrt: | ||
context.nrt.incref(builder, rettpe, val) | ||
return val | ||
|
||
@numba.extending.lower_builtin(operator.getitem, EmptyArrayType, numba.types.BaseTuple) | ||
def lower_getitem_tuple(context, builder, sig, args): | ||
rettpe, (tpe, wheretpe) = sig.return_type, sig.args | ||
val, whereval = args | ||
if context.enable_nrt: | ||
context.nrt.incref(builder, rettpe, val) | ||
return val | ||
|
||
def lower_getitem_next(context, builder, arraytpe, wheretpe, arrayval, whereval, advanced): | ||
if context.enable_nrt: | ||
context.nrt.incref(builder, arraytpe, arrayval) | ||
return arrayval | ||
|
||
def lower_carry(context, builder, arraytpe, carrytpe, arrayval, carryval): | ||
if context.enable_nrt: | ||
context.nrt.incref(builder, arraytpe, arrayval) | ||
return arrayval | ||
|
||
@numba.typing.templates.infer_getattr | ||
class type_methods(numba.typing.templates.AttributeTemplate): | ||
key = EmptyArrayType | ||
|
||
def generic_resolve(self, tpe, attr): | ||
if attr == "id": | ||
if tpe.idtpe == numba.none: | ||
return numba.optional(identity.IdentityType(numba.int32[:, :])) | ||
else: | ||
return tpe.idtpe | ||
|
||
@numba.extending.lower_getattr(EmptyArrayType, "id") | ||
def lower_id(context, builder, tpe, val): | ||
proxyin = numba.cgutils.create_struct_proxy(tpe)(context, builder, value=val) | ||
if tpe.idtpe == numba.none: | ||
return context.make_optional_none(builder, identity.IdentityType(numba.int32[:, :])) | ||
else: | ||
if context.enable_nrt: | ||
context.nrt.incref(builder, tpe.idtpe, proxyin.id) | ||
return proxyin.id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# BSD 3-Clause License; see https://github.com/jpivarski/awkward-1.0/blob/master/LICENSE | ||
|
||
import numbers | ||
|
||
import numpy | ||
|
||
import awkward1.layout | ||
|
||
def typeof(array): | ||
if array is None: | ||
return awkward1.layout.UnknownType() | ||
|
||
elif isinstance(array, (bool, numpy.bool, numpy.bool_)): | ||
return awkward1.layout.PrimitiveType("bool") | ||
|
||
elif isinstance(array, numbers.Integral): | ||
return awkward1.layout.PrimitiveType("int64") | ||
|
||
elif isinstance(array, numbers.Real): | ||
return awkward1.layout.PrimitiveType("float64") | ||
|
||
elif isinstance(array, (numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, numpy.float32, numpy.float64)): | ||
return awkward1.layout.PrimitiveType(typeof.dtype2primitive[array.dtype.type]) | ||
|
||
elif isinstance(array, numpy.generic): | ||
raise ValueError("cannot describe {0} as a PrimitiveType".format(type(array))) | ||
|
||
elif isinstance(array, numpy.ndarray): | ||
if len(array.shape) == 0: | ||
return typeof(array.reshape((1,))[0]) | ||
elif len(array.shape) == 1: | ||
return awkward1.layout.ArrayType(array.shape[0], awkward1.layout.PrimitiveType(typeof.dtype2primitive[array.dtype.type])) | ||
else: | ||
return awkward1.layout.ArrayType(array.shape[0], awkward1.layout.RegularType(array.shape[1:], awkward1.layout.PrimitiveType(typeof.dtype2primitive[array.dtype.type]))) | ||
|
||
elif isinstance(array, awkward1.layout.FillableArray): | ||
return array.type | ||
|
||
elif isinstance(array, awkward1.layout.Content): | ||
return array.type | ||
|
||
else: | ||
raise TypeError("unrecognized array type: {0}".format(repr(array))) | ||
|
||
typeof.dtype2primitive = { | ||
numpy.int8: "int8", | ||
numpy.int16: "int16", | ||
numpy.int32: "int32", | ||
numpy.int64: "int64", | ||
numpy.uint8: "uint8", | ||
numpy.uint16: "uint16", | ||
numpy.uint32: "uint32", | ||
numpy.uint64: "uint64", | ||
numpy.float32: "float32", | ||
numpy.float64: "float64", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// BSD 3-Clause License; see https://github.com/jpivarski/awkward-1.0/blob/master/LICENSE | ||
|
||
#ifndef AWKWARD_EMPTYARRAY_H_ | ||
#define AWKWARD_EMPTYARRAY_H_ | ||
|
||
#include <cassert> | ||
#include <string> | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "awkward/cpu-kernels/util.h" | ||
#include "awkward/Slice.h" | ||
#include "awkward/Content.h" | ||
|
||
namespace awkward { | ||
class EmptyArray: public Content { | ||
public: | ||
EmptyArray(const std::shared_ptr<Identity> id): id_(id) { } | ||
|
||
virtual const std::string classname() const; | ||
virtual const std::shared_ptr<Identity> id() const { return id_; } | ||
virtual void setid(); | ||
virtual void setid(const std::shared_ptr<Identity> id); | ||
virtual const std::string tostring_part(const std::string indent, const std::string pre, const std::string post) const; | ||
virtual void tojson_part(ToJson& builder) const; | ||
virtual std::shared_ptr<Type> type_part() const; | ||
virtual int64_t length() const; | ||
virtual const std::shared_ptr<Content> shallow_copy() const; | ||
virtual void checksafe() const; | ||
virtual const std::shared_ptr<Content> getitem_at(int64_t at) const; | ||
virtual const std::shared_ptr<Content> getitem_at_unsafe(int64_t at) const; | ||
virtual const std::shared_ptr<Content> getitem_range(int64_t start, int64_t stop) const; | ||
virtual const std::shared_ptr<Content> getitem_range_unsafe(int64_t start, int64_t stop) const; | ||
virtual const std::shared_ptr<Content> getitem_next(const std::shared_ptr<SliceItem> head, const Slice& tail, const Index64& advanced) const; | ||
virtual const std::shared_ptr<Content> carry(const Index64& carry) const; | ||
virtual const std::pair<int64_t, int64_t> minmax_depth() const; | ||
|
||
private: | ||
std::shared_ptr<Identity> id_; | ||
}; | ||
} | ||
|
||
#endif // AWKWARD_EMPTYARRAY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.