Example #1
0
    def op_ckernel(self, op):
        op_ndim = len(op.type.shape)
        result_ndim = self.env.get('result-ndim', 0)
        ckernel, args = op.args
        in_types = [self.get_arg_type(arg) for arg in args[1:]]
        out_type = ndt.type(str(args[0].type))

        if isinstance(ckernel, dict):
            tag = ckernel['tag']
            if tag == 'elwise':
                ck = ckernel['ckernel']
                if op.metadata['rank'] < op_ndim and \
                        self.env.get('stream-outer', False) and result_ndim == op_ndim:
                    # Replace the leading dimension type with 'strided' in each operand
                    # if we're streaming it for processing BLZ
                    # TODO: Add dynd tp.subarray(N) function like datashape has
                    for i, tp in enumerate(in_types):
                        if tp.ndim == result_ndim:
                            in_types[i] = ndt.make_strided_dim(tp.element_type)
                    out_type = ndt.make_strided_dim(out_type.element_type)

                op.args[0] = _lowlevel.lift_ckernel_deferred(ck,
                                                             [out_type] + in_types)
            elif tag == 'reduction':
                ck = ckernel['ckernel']
                assoc = ckernel['assoc']
                comm = ckernel['comm']
                ident = ckernel['ident']
                ident = None if ident is None else nd.asarray(ident)
                axis = ckernel['axis']
                keepdims = ckernel['keepdims']
                op.args[0] = _lowlevel.lift_reduction_ckernel_deferred(
                                ck, in_types[0],
                                axis=axis, keepdims=keepdims,
                                associative=assoc, commutative=comm,
                                reduction_identity=ident)
            elif tag == 'rolling':
                ck = ckernel['ckernel']
                window = ckernel['window']
                minp = ckernel['minp']
                if minp != 0:
                    raise ValueError('rolling window with minp != 0 not supported yet')
                op.args[0] = _lowlevel.make_rolling_ckernel_deferred(out_type,
                                                                     in_types[0],
                                                                     ck, window)
            elif tag == 'ckfactory':
                ckfactory = ckernel['ckernel_factory']
                ck = ckfactory(out_type, *in_types)
                op.args[0] = ck
            else:
                raise RuntimeError('unnrecognized ckernel tag %s' % tag)
        else:
            op.args[0] = ckernel
 def test_empty(self):
     # Constructor from scalar type
     a = nd.empty(ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.int32)
     # Constructor from type with fixed dimension
     a = nd.empty('3 * int32')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3,))
     # Constructor from shape as single integer
     a = nd.empty(3, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_strided_dim(ndt.int32))
     self.assertEqual(a.shape, (3,))
     # Constructor from shape as tuple
     a = nd.empty((3,4), ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('strided * strided * int32'))
     self.assertEqual(a.shape, (3,4))
     # Constructor from shape as variadic arguments
     a = nd.empty(3, 4, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('strided * strided * int32'))
     self.assertEqual(a.shape, (3,4))
 def check_constructor(self, cons, value):
     # Constructor from scalar type
     a = cons(ndt.int32)
     self.assertEqual(a.access_flags, 'immutable')
     self.assertEqual(nd.type_of(a), ndt.int32)
     self.assertEqual(nd.as_py(a), value)
     # Constructor from type with fixed dimension
     a = cons('3 * int32')
     self.assertEqual(a.access_flags, 'immutable')
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [value]*3)
     # Constructor from shape as single integer
     a = cons(3, ndt.int32)
     self.assertEqual(a.access_flags, 'immutable')
     self.assertEqual(nd.type_of(a), ndt.make_strided_dim(ndt.int32))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [value]*3)
     # Constructor from shape as tuple
     a = cons((3,4), ndt.int32)
     self.assertEqual(a.access_flags, 'immutable')
     self.assertEqual(nd.type_of(a), ndt.type('strided * strided * int32'))
     self.assertEqual(a.shape, (3,4))
     self.assertEqual(nd.as_py(a), [[value]*4]*3)
     # Constructor from shape as variadic arguments
     a = cons(3, 4, ndt.int32)
     self.assertEqual(a.access_flags, 'immutable')
     self.assertEqual(nd.type_of(a), ndt.type('strided * strided * int32'))
     self.assertEqual(a.shape, (3,4))
     self.assertEqual(nd.as_py(a), [[value]*4]*3)
     # Constructor of a cstruct type
     a = cons(3, '{x: int32, y: int32}')
     self.assertEqual(a.access_flags, 'immutable')
     self.assertEqual(nd.type_of(a),
                 ndt.type('strided * {x: int32, y: int32}'))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a),
                 [{'x': value, 'y': value}]*3)
     # Constructor of a struct type
     a = cons(3, ndt.make_struct([ndt.int32]*2, ['x', 'y']))
     self.assertEqual(a.access_flags, 'immutable')
     self.assertEqual(nd.type_of(a),
                 ndt.make_strided_dim(
                     ndt.make_struct([ndt.int32]*2, ['x', 'y'])))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a),
                 [{'x': value, 'y': value}]*3)
Example #4
0
    def op_ckernel(self, op):
        op_ndim = len(op.type.shape)
        if op.metadata['rank'] < op_ndim:
            result_ndim = self.env.get('result-ndim', 0)
            ckernel, args = op.args
            in_types = [self.get_arg_type(arg) for arg in args[1:]]
            out_type = ndt.type(str(args[0].type))

            # Replace the leading dimension type with 'strided' in each operand
            # if we're streaming it for processing BLZ
            if self.env.get('stream-outer', False) and result_ndim == op_ndim:
                # TODO: Add dynd tp.subarray(N) function like datashape has
                for i, tp in enumerate(in_types):
                    if tp.ndim == result_ndim:
                        in_types[i] = ndt.make_strided_dim(tp.element_type)
                out_type = ndt.make_strided_dim(out_type.element_type)

            op.args[0] = _lowlevel.lift_ckernel_deferred(ckernel,
                            [out_type] + in_types)
Example #5
0
    def op_ckernel(self, op):
        op_ndim = len(op.type.shape)
        if op.metadata['rank'] < op_ndim:
            result_ndim = self.env.get('result-ndim', 0)
            ckernel, args = op.args
            in_types = [self.get_arg_type(arg) for arg in args[1:]]
            out_type = ndt.type(str(args[0].type))

            # Replace the leading dimension type with 'strided' in each operand
            # if we're streaming it for processing BLZ
            if self.env.get('stream-outer', False) and result_ndim == op_ndim:
                # TODO: Add dynd tp.subarray(N) function like datashape has
                for i, tp in enumerate(in_types):
                    if tp.ndim == result_ndim:
                        in_types[i] = ndt.make_strided_dim(tp.element_type)
                out_type = ndt.make_strided_dim(out_type.element_type)

            op.args[0] = _lowlevel.lift_ckernel_deferred(
                ckernel, [out_type] + in_types)
 def check_constructor_readwrite(self, cons, value):
     # Constructor from scalar type
     a = cons(ndt.int32, access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.int32)
     self.assertEqual(nd.as_py(a), value)
     # Constructor from type with fixed dimension
     a = cons("3 * int32", access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [value] * 3)
     # Constructor from shape as single integer
     a = cons(3, ndt.int32, access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.make_strided_dim(ndt.int32))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [value] * 3)
     # Constructor from shape as tuple
     a = cons((3, 4), ndt.int32, access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.type("strided * strided * int32"))
     self.assertEqual(a.shape, (3, 4))
     self.assertEqual(nd.as_py(a), [[value] * 4] * 3)
     # Constructor from shape as variadic arguments
     a = cons(3, 4, ndt.int32, access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.type("strided * strided * int32"))
     self.assertEqual(a.shape, (3, 4))
     self.assertEqual(nd.as_py(a), [[value] * 4] * 3)
     # Constructor of a cstruct type
     a = cons(3, "{x: int32, y: int32}", access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.type("strided * {x: int32, y: int32}"))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [{"x": value, "y": value}] * 3)
     # Constructor of a struct type
     a = cons(3, ndt.make_struct([ndt.int32] * 2, ["x", "y"]), access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.make_strided_dim(ndt.make_struct([ndt.int32] * 2, ["x", "y"])))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [{"x": value, "y": value}] * 3)
 def test_full_of_struct(self):
     # Constructor of a cstruct type
     a = nd.full(3, "{x: int32, y: int32}", value=[1, 5], access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.type("strided * {x: int32, y: int32}"))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [{"x": 1, "y": 5}] * 3)
     # Constructor of a struct type
     a = nd.full(3, ndt.make_struct([ndt.int32] * 2, ["x", "y"]), value={"x": 3, "y": 10}, access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.make_strided_dim(ndt.make_struct([ndt.int32] * 2, ["x", "y"])))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [{"x": 3, "y": 10}] * 3)
 def test_empty(self):
     # Constructor from scalar type
     a = nd.empty(ndt.int32)
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.int32)
     # Constructor from type with fixed dimension
     a = nd.empty("3 * int32")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3,))
     # Constructor from type with cfixed dimension
     a = nd.empty("cfixed[3] * int32")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.make_cfixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3,))
     # Constructor from type with fixed dimension, accesskwarg
     a = nd.empty("3 * int32", access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3,))
     # Can't create with access as immutable
     self.assertRaises(ValueError, nd.empty, "3 * int32", access="immutable")
     # Constructor from shape as single integer
     a = nd.empty(3, ndt.int32)
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.make_strided_dim(ndt.int32))
     self.assertEqual(a.shape, (3,))
     # Constructor from shape as tuple
     a = nd.empty((3, 4), ndt.int32)
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.type("strided * strided * int32"))
     self.assertEqual(a.shape, (3, 4))
     # Constructor from shape as variadic arguments
     a = nd.empty(3, 4, ndt.int32)
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.type("strided * strided * int32"))
     self.assertEqual(a.shape, (3, 4))
     # Constructor from shape as variadic arguments, access kwarg
     a = nd.empty(3, 4, ndt.int32, access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.type("strided * strided * int32"))
     self.assertEqual(a.shape, (3, 4))
     # Can't create with access as immutable
     self.assertRaises(ValueError, nd.empty, 3, 4, ndt.int32, access="immutable")
 def test_full_of_struct(self):
     # Constructor of a cstruct type
     a = nd.full(3, '{x: int32, y: int32}', value=[1,5], access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a),
                 ndt.type('strided * {x: int32, y: int32}'))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a),
                 [{'x': 1, 'y': 5}]*3)
     # Constructor of a struct type
     a = nd.full(3, ndt.make_struct([ndt.int32]*2, ['x', 'y']),
                 value={'x' : 3, 'y' : 10}, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a),
                 ndt.make_strided_dim(
                     ndt.make_struct([ndt.int32]*2, ['x', 'y'])))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a),
                 [{'x': 3, 'y': 10}]*3)