Exemple #1
0
def do_take1(shp, idx, offseted):
    c, g = gen_gpuarray(shp, dtype='float32', ctx=ctx, order='c')
    ci = numpy.asarray(idx)
    gi = pygpu.asarray(ci, context=ctx)

    rc = c.take(ci, axis=0)
    rg = g.take1(gi)

    check_content(rg, rc)
def do_take1(shp, idx, offseted):
    c, g = gen_gpuarray(shp, dtype='float32', ctx=ctx, order='c')
    ci = numpy.asarray(idx)
    gi = pygpu.asarray(ci, context=ctx)

    rc = c.take(ci, axis=0)
    rg = g.take1(gi)

    check_content(rg, rc)
Exemple #3
0
    def perform(self, node, inputs, out_):
        out, = out_
        x = inputs[0]
        idx = inputs[1:]

        # detect and transpose array indices
        nidx = []
        nshp = list(x.shape)
        for k, i in enumerate(idx):
            if i is None:
                nidx.append(slice(None))
                nshp.insert(k, 1)
            else:
                nidx.append(i)

        x = x.reshape(nshp)

        narrays = 0
        transp = list(range(x.ndim))
        p = 0
        # ap gives the position of the array in case there is only one.
        # if there are more than one (narray > 1) it should be ignored.
        ap = 0
        for k, i in enumerate(list(nidx)):
            if (isinstance(i, np.ndarray) and
                    i.ndim != 0):
                transp.remove(k)
                transp.insert(p, k)
                ap += k
                i = nidx.pop(k)
                nidx.insert(p, i)
                p += 1
                narrays += 1
            else:
                if narrays == 0:
                    try:
                        i.__index__()
                        # We shift back the position of the array by the
                        # number of dimensions that are removed by
                        # indexing.  If ap is bigger than 0 it means we
                        # have encountered at least one array.
                        if ap >= 0:
                            ap -= 1
                        # If this index is before the first array then
                        # we will not move the array back to its
                        # position.  Mark this by faking that there
                        # are more than two arrays.  This is crazy
                        # numpy behaviour so blame them.
                        narrays = 2
                    except Exception:
                        pass

        x = x.transpose(*transp)

        idx_ = ([slice(None)] * p + nidx[p:])
        x = x.__getitem__(idx_)

        if p == 0:
            # The only indexing was through slices and indices.
            # This can happen with symbolic slices for instance.
            # Since no view_map is set, we need to copy the returned value
            out[0] = x.copy()
            return

        # flatten the array-indexed dimensions
        shape = ((np.prod(x.shape[0: p]),) +
                 x.shape[p:])
        input_flat = x.reshape(shape)

        # build the strides
        strides = [1]
        for i in range(p - 1, 0, -1):
            stride = x.shape[i] * strides[0]
            strides.insert(0, stride)

        # build the indices and use it
        take_idx = sum((i * s for i, s in zip(nidx, strides)))
        out_flat = input_flat.take1(pygpu.asarray(take_idx.flatten(),
                                                  context=x.context))

        # finish up
        out_flat_shp = take_idx.shape + x.shape[p:]
        o = out_flat.reshape(out_flat_shp)

        # If there was only one array we need to move the indexed
        # dimension(s) back to the position of the array, which is
        # stored in ap.  Note that ap is invalid is narrays != 1.
        if narrays == 1:
            ntransp = list(range(take_idx.ndim, o.ndim))
            ntransp[ap:ap] = list(range(take_idx.ndim))
            o = o.transpose(*ntransp)

        out[0] = o
Exemple #4
0
    def perform(self, node, inputs, out_):
        out, = out_
        x = inputs[0]
        idx = inputs[1:]

        # detect and transpose array indices
        nidx = []
        nshp = list(x.shape)
        for k, i in enumerate(idx):
            if i is None:
                nidx.append(slice(None))
                nshp.insert(k, 1)
            else:
                nidx.append(i)

        x = x.reshape(nshp)

        transp = list(range(x.ndim))
        # number of array-indexed dimensions
        p = 0
        # ap represents the axis in the resulting array where the
        # dimensions indexed by arrays and ints will be inserted.
        # For instance, if all such dimensions are grouped together,
        # it corresponds to the index of the first such dimension in the
        # inital array.  If these dimensions are split (with slices
        # inbetween), then the resulting dimensions will be moved to the
        # beginning, and ap will be 0.
        # If no such dimension has been encountered, ap is None.
        ap = None
        # Indicates whether we have already encountered an index (array
        # or number), and then a slice.
        slice_after_idx = False
        for k, i in enumerate(list(nidx)):
            if (isinstance(i, np.ndarray) and i.ndim != 0):
                transp.remove(k)
                transp.insert(p, k)
                i = nidx.pop(k)
                nidx.insert(p, i)
                p += 1
                if ap is None:
                    # first non-slice index
                    ap = k
                elif slice_after_idx:
                    # We already encountered at least an array or int, and then
                    # a slice. Array-indexed axes are not grouped,
                    # moving to the beginning
                    ap = 0
            else:
                try:
                    i.__index__()
                    if ap is None:
                        ap = k
                    # indices do not break the contiguity of
                    # array-indexed axes
                except Exception:
                    # If we already encountered an array/int index, it
                    # means future ones will not be grouped.
                    if ap is not None:
                        slice_after_idx = True

        x = x.transpose(*transp)

        idx_ = ([slice(None)] * p + nidx[p:])
        x = x.__getitem__(idx_)

        if p == 0:
            assert ap is None
            # The only indexing was through slices and indices.
            # This can happen with symbolic slices for instance.
            # Since no view_map is set, we need to copy the returned value
            out[0] = x.copy()
            return

        # At this point, we should have encountered at least one array
        assert ap is not None

        # flatten the array-indexed dimensions
        shape = ((np.prod(x.shape[0: p]),) +
                 x.shape[p:])
        input_flat = x.reshape(shape)

        # build the strides
        strides = [1]
        for i in range(p - 1, 0, -1):
            stride = x.shape[i] * strides[0]
            strides.insert(0, stride)

        # build the indices and use it
        take_idx = sum((i * s for i, s in zip(nidx, strides)))
        out_flat = input_flat.take1(pygpu.asarray(take_idx.flatten(),
                                                  context=x.context))

        # finish up
        out_flat_shp = take_idx.shape + x.shape[p:]
        o = out_flat.reshape(out_flat_shp)

        if ap != 0:
            # Put the resulting indexing at the place that NumPy
            # decided was the right one.
            ntransp = list(range(take_idx.ndim, o.ndim))
            ntransp[ap:ap] = list(range(take_idx.ndim))
            o = o.transpose(*ntransp)

        out[0] = o
def test_bool():
    for data in [numpy.empty((0, 33)), [[1]], [[0]], [], [0], [1], 0, 1]:
        assert (bool(pygpu.asarray(data, context=ctx)) ==
                bool(numpy.asarray(data)))
Exemple #6
0
def set_bhc_data_from_ary(self, ary):
    self.clary[:] = pygpu.asarray(ary)
Exemple #7
0
def test_bool():
    for data in [numpy.empty((0, 33)), [[1]], [[0]], [], [0], [1], 0, 1]:
        assert (bool(pygpu.asarray(data,
                                   context=ctx)) == bool(numpy.asarray(data)))
    def perform(self, node, inputs, out_):
        out, = out_
        x = inputs[0]
        idx = inputs[1:]

        # convert boolean masks to index arrays
        idx = check_and_convert_boolean_masks(x, idx)

        # detect and transpose array indices
        nidx = []
        nshp = list(x.shape)
        for k, i in enumerate(idx):
            if i is None:
                nidx.append(slice(None))
                nshp.insert(k, 1)
            else:
                nidx.append(i)

        x = x.reshape(nshp)

        transp = list(range(x.ndim))
        # number of array-indexed dimensions
        p = 0
        # ap represents the axis in the resulting array where the
        # dimensions indexed by arrays and ints will be inserted.
        # For instance, if all such dimensions are grouped together,
        # it corresponds to the index of the first such dimension in the
        # inital array.  If these dimensions are split (with slices
        # inbetween), then the resulting dimensions will be moved to the
        # beginning, and ap will be 0.
        # If no such dimension has been encountered, ap is None.
        ap = None
        # Indicates whether we have already encountered an index (array
        # or number), and then a slice.
        slice_after_idx = False
        for k, i in enumerate(list(nidx)):
            if (isinstance(i, np.ndarray) and i.ndim != 0):
                transp.remove(k)
                transp.insert(p, k)
                i = nidx.pop(k)
                nidx.insert(p, i)
                p += 1
                if ap is None:
                    # first non-slice index
                    ap = k
                elif slice_after_idx:
                    # We already encountered at least an array or int, and then
                    # a slice. Array-indexed axes are not grouped,
                    # moving to the beginning
                    ap = 0
            else:
                try:
                    i.__index__()
                    if ap is None:
                        ap = k
                    # indices do not break the contiguity of
                    # array-indexed axes
                except Exception:
                    # If we already encountered an array/int index, it
                    # means future ones will not be grouped.
                    if ap is not None:
                        slice_after_idx = True

        x = x.transpose(*transp)

        idx_ = ([slice(None)] * p + nidx[p:])
        x = x.__getitem__(idx_)

        if p == 0:
            assert ap is None
            # The only indexing was through slices and indices.
            # This can happen with symbolic slices for instance.
            # Since no view_map is set, we need to copy the returned value
            out[0] = x.copy()
            return

        # At this point, we should have encountered at least one array
        assert ap is not None

        # flatten the array-indexed dimensions
        shape = ((np.prod(x.shape[0: p]),) +
                 x.shape[p:])
        input_flat = x.reshape(shape)

        # build the strides
        strides = [1]
        for i in range(p - 1, 0, -1):
            stride = x.shape[i] * strides[0]
            strides.insert(0, stride)

        # build the indices and use it
        take_idx = sum((i * s for i, s in zip(nidx, strides)))
        out_flat = input_flat.take1(pygpu.asarray(take_idx.flatten(),
                                                  context=x.context))

        # finish up
        out_flat_shp = take_idx.shape + x.shape[p:]
        o = out_flat.reshape(out_flat_shp)

        if ap != 0:
            # Put the resulting indexing at the place that NumPy
            # decided was the right one.
            ntransp = list(range(take_idx.ndim, o.ndim))
            ntransp[ap:ap] = list(range(take_idx.ndim))
            o = o.transpose(*ntransp)

        out[0] = o
Exemple #9
0
    def perform(self, node, inputs, out_):
        out, = out_
        x = inputs[0]
        idx = inputs[1:]

        # detect and transpose array indices
        nidx = []
        nshp = list(x.shape)
        for k, i in enumerate(idx):
            if i is None:
                nidx.append(slice(None))
                nshp.insert(k, 1)
            else:
                nidx.append(i)

        x = x.reshape(nshp)

        narrays = 0
        transp = list(range(x.ndim))
        p = 0
        # ap gives the position of the array in case there is only one.
        # if there are more than one (narray > 1) it should be ignored.
        ap = 0
        for k, i in enumerate(list(nidx)):
            if (isinstance(i, numpy.ndarray) and
                    i.ndim != 0):
                transp.remove(k)
                transp.insert(p, k)
                ap += k
                i = nidx.pop(k)
                nidx.insert(p, i)
                p += 1
                narrays += 1
            else:
                if narrays == 0:
                    try:
                        i.__index__()
                        # We shift back the position of the array by the
                        # number of dimensions that are removed by
                        # indexing.  If ap is bigger than 0 it means we
                        # have encountered at least one array.
                        if ap >= 0:
                            ap -= 1
                        # If this index is before the first array then
                        # we will not move the array back to its
                        # position.  Mark this by faking that there
                        # are more than two arrays.  This is crazy
                        # numpy behaviour so blame them.
                        narrays = 2
                    except Exception:
                        pass

        x = x.transpose(*transp)

        idx_ = ([slice(None)] * p + nidx[p:])
        x = x.__getitem__(idx_)

        # flatten the array-indexed dimensions
        shape = ((numpy.prod(x.shape[0: p]),) +
                 x.shape[p:])
        input_flat = x.reshape(shape)

        # build the strides
        strides = [1]
        for i in range(p - 1, 0, -1):
            stride = x.shape[i] * strides[-1]
            strides.insert(0, stride)

        # build the indices and use it
        take_idx = sum((i * s for i, s in zip(nidx, strides)))
        out_flat = input_flat.take1(pygpu.asarray(take_idx.flatten(),
                                                  context=x.context))

        # finish up
        out_flat_shp = take_idx.shape + x.shape[p:]
        o = out_flat.reshape(out_flat_shp)

        # If there was only one array we need to move the indexed
        # dimension(s) back to the position of the array, which is
        # stored in ap.  Note that ap is invalid is narrays != 1.
        if narrays == 1:
            ntransp = list(range(take_idx.ndim, o.ndim))
            ntransp[ap:ap] = list(range(take_idx.ndim))
            o = o.transpose(*ntransp)

        out[0] = o
_,ctx,arr,shared_x,shared_xx = init_device(device=device)



float2half = pygpu.elemwise.GpuElemwise(expr="a = b",
                                             args=[pygpu.elemwise.arg("a", 'float16', write=True),\
                                             pygpu.elemwise.arg("b", 'float32', read=True)],
                                             convert_f16=True,
                                             ctx=ctx)



numpy_float = np.array([1.444, 2.555, 7.999], dtype=np.float32)
numpy_half = np.array([0, 0, 0], dtype=np.float16)

ga_float = pygpu.asarray(numpy_float, dtype=np.float32,
                           context=ctx)
                           
ga_half = pygpu.asarray(numpy_half, dtype=np.float16,
                           context=ctx)

print 'before convert', ga_float

float2half(ga_half, ga_float)

print 'after convert', ga_half
                           

                           
                           
                           
                           
Exemple #11
0
def set_bhc_data_from_ary(self, ary):
    self.clary[:] = pygpu.asarray(ary)