Пример #1
0
def gen_gpu_nd_array(shape_orig,
                     dtype='float32',
                     offseted_outer=False,
                     offseted_inner=False,
                     sliced=1,
                     order='c'):
    if sliced is True:
        sliced = 2
    elif sliced is False:
        sliced = 1
    shape = numpy.asarray(shape_orig).copy()
    if sliced != 1 and len(shape) > 0:
        shape[0] *= numpy.absolute(sliced)
    if offseted_outer and len(shape) > 0:
        shape[0] += 1
    if offseted_inner and len(shape) > 0:
        shape[-1] += 1

    a = numpy.random.rand(*shape) * 10
    if dtype.startswith("u"):
        a = numpy.absolute(a)
    a = numpy.asarray(a, dtype=dtype)
    assert order in ['c', 'f']
    if order == 'f' and len(shape) > 0:
        a = numpy.asfortranarray(a)
    b = gpu_ndarray.GpuNdArrayObject(a)
    if order == 'f' and len(shape) > 0 and b.size > 1:
        assert b.flags['F_CONTIGUOUS']

    if offseted_outer and len(shape) > 0:
        b = b[1:]
        a = a[1:]
        assert b.offset != 0
    if offseted_inner and len(shape) > 0:
        # The b[..., 1:] act as the test for this subtensor case.
        b = b[..., 1:]
        a = a[..., 1:]
        assert b.offset != 0
    if sliced != 1 and len(shape) > 0:
        a = a[::sliced]
        b = b[::sliced]

    if False and shape_orig == ():
        assert a.shape == (1, )
        assert b.shape == (1, )
    else:
        assert a.shape == shape_orig, (a.shape, shape_orig)
        assert b.shape == shape_orig, (b.shape, shape_orig)

    assert numpy.allclose(a, numpy.asarray(b))

    return a, b
Пример #2
0
def test_transfer_fortran():
    for shp in [(5, ), (6, 7), (4, 8, 9), (1, 8, 9)]:
        for dtype in dtypes_all:
            a = numpy.random.rand(*shp) * 10
            a_ = numpy.asfortranarray(a)
            if len(shp) > 1:
                assert a_.strides != a.strides
            a = a_
            b = gpu_ndarray.GpuNdArrayObject(a)
            c = numpy.asarray(b)

            assert a.shape == b.shape == c.shape
            assert a.dtype == b.dtype == c.dtype
            assert a.flags.f_contiguous
            assert c.flags.f_contiguous
            assert a.strides == b.strides == c.strides
            assert numpy.allclose(c, a)
Пример #3
0
def test_transfer_not_contiguous():
    """
    Test transfer when the input on the CPU is not contiguous
    TODO: test when the input on the gpu is not contiguous
    """
    for shp in [(5, ), (6, 7), (4, 8, 9), (1, 8, 9)]:
        for dtype in dtypes_all:
            a = numpy.random.rand(*shp) * 10
            a = a[::-1]
            b = gpu_ndarray.GpuNdArrayObject(a)
            c = numpy.asarray(b)

            assert numpy.allclose(c, a)
            assert a.shape == b.shape == c.shape
            # We copy a to a c contiguous array before the transfer
            assert (-a.strides[0], ) + a.strides[1:] == b.strides == c.strides
            assert a.dtype == b.dtype == c.dtype
            assert c.flags.c_contiguous
Пример #4
0
def test_elemwise_collapse():
    """ Test collapsing under many broadcast and strided pattern """

    for dtype1 in ["int16", "float32", "int8"]:
        for dtype2 in ["int16", "float32", "int8"]:

            for shape1_, shape2_, expected in [
                    # 1d to test this special case
                ((40, ), (40, ), 0),
                ((40, ), (1, ), 1),
                    # No broadcastable dimensions
                ((4, 5, 6, 9), (4, 5, 6, 9), 0),
                    # All inputs have one(and the same) broadcastable dimension
                ((1, 4, 5, 9), (1, 4, 5, 9), 0),
                ((4, 1, 5, 9), (4, 1, 5, 9), 0),
                ((4, 5, 1, 9), (4, 5, 1, 9), 0),
                ((4, 5, 9, 1), (4, 5, 9, 1), 0),
                    # One inputs have one broadcastable dimension
                ((1, 5, 6, 9), (4, 5, 6, 9), 2),
                ((4, 1, 6, 9), (4, 5, 6, 9), 3),
                ((4, 5, 1, 9), (4, 5, 6, 9), 3),
                ((4, 5, 6, 1), (4, 5, 6, 9), 2),
                    # One inputs have two broadcastable dimension
                ((1, 1, 6, 9), (4, 5, 6, 9), 2),
                ((1, 5, 1, 9), (4, 5, 6, 9), 4),
                ((1, 5, 6, 1), (4, 5, 6, 9), 3),
                ((4, 1, 1, 9), (4, 5, 6, 9), 3),
                ((4, 1, 6, 1), (4, 5, 6, 9), 4),
                ((4, 5, 1, 1), (4, 5, 6, 9), 2),
                    # One inputs have tree broadcastable dimension
                ((1, 1, 1, 9), (4, 5, 6, 9), 2),
                ((1, 1, 6, 1), (4, 5, 6, 9), 3),
                ((1, 5, 1, 1), (4, 5, 6, 9), 3),
                ((4, 1, 1, 1), (4, 5, 6, 9), 2),
                    # One scalar
                ((1, 1, 1, 1), (4, 5, 6, 9), 1),
                    # One scalar, the other 1 broadcast dims
                ((1, 1, 1, 1), (4, 5, 6, 1), 1),
            ]:
                scalar_cpu = rand((1, ) * len(shape1_), dtype=dtype1)
                scalar_gpu = gpu_ndarray.GpuNdArrayObject(scalar_cpu)
                scalar_gpu1 = MyGpuNdArray(scalar_gpu)
                for shape1, shape2 in [(shape1_, shape2_), (shape2_, shape1_)]:
                    a_cpu = rand(shape1, dtype=dtype1)
                    a = gpu_ndarray.GpuNdArrayObject(a_cpu)
                    a1 = MyGpuNdArray(a)

                    b_cpu = rand(shape2, dtype=dtype2)
                    b = gpu_ndarray.GpuNdArrayObject(b_cpu)
                    b1 = MyGpuNdArray(b)

                    assert len(shape1) == len(shape2)
                    o_shape = []
                    for i in range(len(shape1)):
                        o_shape.append(max(shape1[i], shape2[i]))
                    o = gpu_ndarray.empty(o_shape, dtype=(a_cpu + b_cpu).dtype)

                    # 1.1 Check direct collapse
                    nd_collaps, info = elemwise_collapses([a, b], [o])
                    assert nd_collaps == expected, (shape1, shape2, nd_collaps,
                                                    expected, info)

                    # 1.2 Check computation are still valid
                    f = MyGpuNdArray.gen_fct(theano.tensor.add, [a1, b1],
                                             len(shape1))
                    out = f([a1, b1])
                    out2 = f([a1, b1], out=out)
                    assert out is out2
                    assert numpy.allclose(numpy.asarray(f([a1, b1])),
                                          a_cpu + b_cpu)
                    assert numpy.allclose(
                        numpy.asarray(MyGpuNdArray.adds(a1, b1)),
                        a_cpu + b_cpu)
                    assert numpy.allclose(
                        numpy.asarray(MyGpuNdArray.add(a1, b1)), a_cpu + b_cpu)
                    assert MyGpuNdArray.add(a1, b1, out=out2) is out2

                    # 1.3 Check work without collaping
                    f = MyGpuNdArray.gen_fct(theano.tensor.add, [a1, b1],
                                             len(shape1),
                                             collapse=False)
                    out = f([a1, b1])
                    out2 = f([a1, b1], out=out)
                    assert out is out2
                    assert numpy.allclose(numpy.asarray(f([a1, b1])),
                                          a_cpu + b_cpu)
                    assert numpy.allclose(
                        numpy.asarray(MyGpuNdArray.adds(a1, b1)),
                        a_cpu + b_cpu)
                    assert numpy.allclose(
                        numpy.asarray(MyGpuNdArray.add(a1, b1)), a_cpu + b_cpu)
                    assert MyGpuNdArray.add(a1, b1, out=out2) is out2

                    # 2.1 What if we add a scalar?
                    nd_collaps, info = elemwise_collapses([a, b, scalar_gpu],
                                                          [o])
                    if expected == 0:
                        expected2 = 1
                    else:
                        expected2 = expected
                    assert nd_collaps == expected2, (shape1, shape2,
                                                     nd_collaps, expected,
                                                     info)
                    # 2.2 Check computation
                    assert numpy.allclose(
                        numpy.asarray(MyGpuNdArray.adds(a1, b1, scalar_gpu1)),
                        a_cpu + b_cpu + scalar_cpu)

                    # 3.1 What if one of the dimensions is strided?
                    broadcast = any(
                        [True for i in a.shape + b.shape if i == 1])
                    if expected == 0:
                        expected2 = 2
                    else:
                        expected2 = expected

                    if len(shape1_) != 4:
                        continue

                    if a.shape[0] != 1:
                        shape = list(shape1)
                        shape[0] *= 2
                        c_cpu = rand(shape, dtype='float32')
                        c = gpu_ndarray.GpuNdArrayObject(c_cpu)[::2]
                        c1 = MyGpuNdArray(c)

                        err = ("strided", c.shape, shape2, nd_collaps,
                               expected, info)
                        nd_collaps, info = elemwise_collapses([c, b], [o])
                        if broadcast:
                            assert nd_collaps >= expected, err
                        else:
                            assert nd_collaps == expected2, err
                        assert numpy.allclose(
                            numpy.asarray(MyGpuNdArray.adds(c1, b1)),
                            numpy.asarray(c) + b_cpu)

                    if a.shape[1] != 1:
                        shape = list(shape1)
                        shape[1] *= 2
                        c_cpu = rand(shape, dtype='float32')
                        c = gpu_ndarray.GpuNdArrayObject(c_cpu)[::, ::2]
                        c1 = MyGpuNdArray(c)

                        err = ("strided", c.shape, shape2, nd_collaps,
                               expected, info)
                        nd_collaps, info = elemwise_collapses([c, b], [o])
                        if broadcast:
                            assert nd_collaps >= expected, err
                        else:
                            assert nd_collaps == expected2, err
                            pass
                        assert numpy.allclose(
                            numpy.asarray(MyGpuNdArray.adds(c1, b1)),
                            numpy.asarray(c) + b_cpu)

                    if a.shape[2] != 1:
                        shape = list(shape1)
                        shape[2] *= 2
                        c_cpu = rand(shape, dtype='float32')
                        c = gpu_ndarray.GpuNdArrayObject(c_cpu)[::, ::, ::2]
                        c1 = MyGpuNdArray(c)

                        err = ("strided", c.shape, shape2, nd_collaps,
                               expected, info)
                        nd_collaps, info = elemwise_collapses([c, b], [o])
                        if broadcast:
                            assert nd_collaps >= expected, err
                        else:
                            assert nd_collaps == expected2, err
                            pass
                        assert numpy.allclose(
                            numpy.asarray(MyGpuNdArray.adds(c1, b1)),
                            numpy.asarray(c) + b_cpu)

                    if a.shape[3] != 1:
                        shape = list(shape1)
                        shape[3] *= 2
                        c_cpu = rand(shape, dtype='float32')
                        c = gpu_ndarray.GpuNdArrayObject(
                            c_cpu)[::, ::, ::, ::2]
                        c1 = MyGpuNdArray(c)

                        err = ("strided", c.shape, shape2, nd_collaps,
                               expected, info)
                        nd_collaps, info = elemwise_collapses([c, b], [o])
                        if broadcast:
                            assert nd_collaps >= expected, err
                        else:
                            assert nd_collaps == 1, err
                            pass
                        assert numpy.allclose(
                            numpy.asarray(MyGpuNdArray.adds(c1, b1)),
                            numpy.asarray(c) + b_cpu)
Пример #5
0
def test_elemwise_mixed_dtype():
    to_cpu = numpy.asarray

    for dtype1 in ["int16", "float32", "int8"]:
        for dtype2 in ["int16", "float32", "int8"]:
            dtypeo = str((numpy.zeros(1, dtype=dtype1) +
                          numpy.zeros(1, dtype=dtype2)).dtype)
            #print "dtypes", dtype1, dtype2, "o dtype", dtypeo

            #print "    Test inside a wrapping python object 2 inputs"
            for shape in [(500, ), (50, 5), (5, 6, 7)]:
                input_vals = [rand(shape, dtype) for dtype in [dtype1, dtype2]]
                del dtype
                gpu_vals = [
                    gpu_ndarray.GpuNdArrayObject(i) for i in input_vals
                ]
                assert all([
                    numpy.allclose(to_cpu(ig), i)
                    for ig, i in zip(gpu_vals, input_vals)
                ])

                gpu_vals = [MyGpuNdArray(x) for x in gpu_vals]
                out = gpu_vals[0] + gpu_vals[1]
                assert numpy.allclose(to_cpu(out),
                                      input_vals[0] + input_vals[1])
                out = gpu_vals[0] - gpu_vals[1]
                assert numpy.allclose(to_cpu(out),
                                      input_vals[0] - input_vals[1])
                out = gpu_vals[0] * gpu_vals[1]
                assert all_close(to_cpu(out), input_vals[0] * input_vals[1])
                if dtypeo.startswith("float"):
                    # TODO: execute for all dtype
                    out = gpu_vals[0] / gpu_vals[1]
                    assert numpy.allclose(to_cpu(out),
                                          input_vals[0] / input_vals[1])

            nb_in = 4
            #print "    Test inside a wrapping python object %d inputs"%nb_in
            for shape in [(500, ), (50, 5), (5, 6, 7)]:
                input_vals = [
                    rand(shape, dtype)
                    for dtype in [dtype1, dtype2, dtype1, dtype2]
                ]
                gpu_vals = [
                    gpu_ndarray.GpuNdArrayObject(i) for i in input_vals
                ]
                assert all([
                    numpy.allclose(to_cpu(ig), i)
                    for ig, i in zip(gpu_vals, input_vals)
                ])

                gpu_vals = [MyGpuNdArray(x) for x in gpu_vals]
                out = MyGpuNdArray.adds(*gpu_vals)
                assert numpy.allclose(to_cpu(out),
                                      reduce(numpy.add, input_vals))

                out = MyGpuNdArray.multiplys(*gpu_vals)
                assert all_close(to_cpu(out), reduce(numpy.multiply,
                                                     input_vals))

            #print "    Test broadcasting"
            for shapes in [
                ((1, 5), (4, 5)),
                ((33, 10), (33, 1)),
                ((33, 1, 5), (33, 10, 1)),
                ((33, 1, 5), (33, 10, 1), ((1, 10, 5))),
            ]:
                input_vals = [
                    rand(shape, dtype)
                    for shape, dtype in zip(shapes, [dtype1, dtype2])
                ]
                gpu_vals = [
                    gpu_ndarray.GpuNdArrayObject(i) for i in input_vals
                ]
                assert all([
                    numpy.allclose(to_cpu(ig), i)
                    for ig, i in zip(gpu_vals, input_vals)
                ])

                gpu_vals = [MyGpuNdArray(x) for x in gpu_vals]
                out = MyGpuNdArray.adds(*gpu_vals)
                assert numpy.allclose(to_cpu(out),
                                      reduce(numpy.add, input_vals))

                out = MyGpuNdArray.multiplys(*gpu_vals)
                assert all_close(to_cpu(out), reduce(numpy.multiply,
                                                     input_vals))