提交 19a6bd40 authored 作者: Chiheb Trabelsi's avatar Chiheb Trabelsi

test_cuda_ndarray.py has been modified in order to respect the flake8 style.

上级 791d4871
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import time, copy, sys, unittest import copy
import unittest
# Skip test if cuda_ndarray is not available. # Skip test if cuda_ndarray is not available.
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
...@@ -32,7 +33,7 @@ def advantage(cpu_dt, gpu_dt): ...@@ -32,7 +33,7 @@ def advantage(cpu_dt, gpu_dt):
def test_host_to_device(): def test_host_to_device():
#print >>sys.stdout, 'starting test_host_to_dev' # print >>sys.stdout, 'starting test_host_to_dev'
for shape in ((), (3,), (2, 3), (3, 4, 5, 6)): for shape in ((), (3,), (2, 3), (3, 4, 5, 6)):
a = theano._asarray(numpy.random.rand(*shape), dtype='float32') a = theano._asarray(numpy.random.rand(*shape), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
...@@ -52,8 +53,7 @@ def test_host_to_device(): ...@@ -52,8 +53,7 @@ def test_host_to_device():
def test_add_iadd_idiv(): def test_add_iadd_idiv():
for shapes in ( for shapes in ([(5, 5), (5, 1)],
[(5, 5), (5, 1)],
[(5, 5), (1, 5)], [(5, 5), (1, 5)],
(), (0,), (3,), (2, 3), (), (0,), (3,), (2, 3),
(1, 10000000), (10000, 1000), (1000000, 10), (1, 10000000), (10000, 1000), (1000000, 10),
...@@ -98,16 +98,10 @@ def test_add_iadd_idiv(): ...@@ -98,16 +98,10 @@ def test_add_iadd_idiv():
# add don't support stride # add don't support stride
if shape == shape2: if shape == shape2:
t0 = time.time()
bsum = b0 + b1 bsum = b0 + b1
bsum = b0 + b1 bsum = b0 + b1
t1 = time.time()
gpu_dt = t1 - t0
t0 = time.time()
asum = a0 + a1 asum = a0 + a1
asum = a0 + a1 asum = a0 + a1
t1 = time.time()
cpu_dt = t1 - t0
# print shape, 'adding ', a0.size, 'cpu', cpu_dt, 'advantage', advantage(cpu_dt, gpu_dt) # print shape, 'adding ', a0.size, 'cpu', cpu_dt, 'advantage', advantage(cpu_dt, gpu_dt)
assert numpy.allclose(asum, numpy.asarray(bsum)) assert numpy.allclose(asum, numpy.asarray(bsum))
...@@ -133,23 +127,9 @@ def test_add_iadd_idiv(): ...@@ -133,23 +127,9 @@ def test_add_iadd_idiv():
raise Exception("You need to modify this case!") raise Exception("You need to modify this case!")
# TODO: b0[...,::-1] don't work # TODO: b0[...,::-1] don't work
if shape == shape2:
t = False
try:
_c = _b+b1
except TypeError:
t = True
assert t
# test inplace version # test inplace version
t0 = time.time()
b0 += b1 b0 += b1
t1 = time.time()
gpu_dt = t1 - t0
t0 = time.time()
a0 += a1 a0 += a1
t1 = time.time()
cpu_dt = t1 - t0
# print shape, 'adding inplace', a0.size, 'cpu', cpu_dt, 'advantage', advantage(cpu_dt, gpu_dt) # print shape, 'adding inplace', a0.size, 'cpu', cpu_dt, 'advantage', advantage(cpu_dt, gpu_dt)
assert numpy.allclose(a0, numpy.asarray(b0)) assert numpy.allclose(a0, numpy.asarray(b0))
assert numpy.allclose(a0, a0_orig + a1) assert numpy.allclose(a0, a0_orig + a1)
...@@ -157,14 +137,14 @@ def test_add_iadd_idiv(): ...@@ -157,14 +137,14 @@ def test_add_iadd_idiv():
b0 /= b1 b0 /= b1
a0 /= a1 a0 /= a1
assert numpy.allclose(a0, numpy.asarray(b0)) assert numpy.allclose(a0, numpy.asarray(b0))
assert numpy.allclose(a0, (a0_orig + a1)/a1) assert numpy.allclose(a0, (a0_orig + a1) / a1)
# test inplace version # test inplace version
# for not contiguous input # for not contiguous input
b0 += _b b0 += _b
a0 += a1[..., ::-1] a0 += a1[..., ::-1]
assert numpy.allclose(a0, numpy.asarray(b0)) assert numpy.allclose(a0, numpy.asarray(b0))
assert numpy.allclose(a0, (a0_orig+a1)/a1+a1[..., ::-1]) assert numpy.allclose(a0, (a0_orig + a1) / a1 + a1[..., ::-1])
b0 /= _b b0 /= _b
a0 /= a1[..., ::-1] a0 /= a1[..., ::-1]
...@@ -174,48 +154,42 @@ def test_add_iadd_idiv(): ...@@ -174,48 +154,42 @@ def test_add_iadd_idiv():
def test_exp(): def test_exp():
#print >>sys.stdout, 'starting test_exp' # print >>sys.stdout, 'starting test_exp'
for shape in ((), (3,), (2, 3), for shape in ((), (3,), (2, 3),
(1, 10000000), (10, 1000000), (1, 10000000), (10, 1000000),
(100, 100000), (1000, 10000), (10000, 1000)): (100, 100000), (1000, 10000), (10000, 1000)):
a0 = theano._asarray(numpy.random.rand(*shape), dtype='float32') a0 = theano._asarray(numpy.random.rand(*shape), dtype='float32')
a1 = a0.copy() a1 = a0.copy()
b0 = cuda_ndarray.CudaNdarray(a0) b0 = cuda_ndarray.CudaNdarray(a0)
b1 = cuda_ndarray.CudaNdarray(a1) cuda_ndarray.CudaNdarray(a1)
t0 = time.time()
bsum = b0.exp() bsum = b0.exp()
t1 = time.time()
gpu_dt = t1 - t0
t0 = time.time()
asum = numpy.exp(a1) asum = numpy.exp(a1)
t1 = time.time()
cpu_dt = t1 - t0
# print shape, 'adding ', a0.size, 'cpu', cpu_dt, 'advantage', advantage(cpu_dt, gpu_dt) # print shape, 'adding ', a0.size, 'cpu', cpu_dt, 'advantage', advantage(cpu_dt, gpu_dt)
#c = numpy.asarray(b0+b1) # c = numpy.asarray(b0+b1)
if asum.shape: if asum.shape:
assert numpy.allclose(asum, numpy.asarray(bsum)) assert numpy.allclose(asum, numpy.asarray(bsum))
def test_copy(): def test_copy():
#print >>sys.stdout, 'starting test_copy' # print >>sys.stdout, 'starting test_copy'
shape = (500, 499) shape = (500, 499)
a = theano._asarray(numpy.random.rand(*shape), dtype='float32') a = theano._asarray(numpy.random.rand(*shape), dtype='float32')
#print >>sys.stdout, '.. creating device object' # print >>sys.stdout, '.. creating device object'
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
#print >>sys.stdout, '.. copy' # print >>sys.stdout, '.. copy'
c = copy.copy(b) c = copy.copy(b)
#print >>sys.stdout, '.. deepcopy' # print >>sys.stdout, '.. deepcopy'
d = copy.deepcopy(b) d = copy.deepcopy(b)
#print >>sys.stdout, '.. comparisons' # print >>sys.stdout, '.. comparisons'
assert numpy.allclose(a, numpy.asarray(b)) assert numpy.allclose(a, numpy.asarray(b))
assert numpy.allclose(a, numpy.asarray(c)) assert numpy.allclose(a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d)) assert numpy.allclose(a, numpy.asarray(d))
b += b b += b
assert numpy.allclose(a+a, numpy.asarray(b)) assert numpy.allclose(a + a, numpy.asarray(b))
assert numpy.allclose(a+a, numpy.asarray(c)) assert numpy.allclose(a + a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d)) assert numpy.allclose(a, numpy.asarray(d))
...@@ -237,8 +211,8 @@ def test_nvcc_bug(): ...@@ -237,8 +211,8 @@ def test_nvcc_bug():
assert numpy.allclose(a, numpy.asarray(c)) assert numpy.allclose(a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d)) assert numpy.allclose(a, numpy.asarray(d))
b += b b += b
assert numpy.allclose(a+a, numpy.asarray(b)) assert numpy.allclose(a + a, numpy.asarray(b))
assert numpy.allclose(a+a, numpy.asarray(c)) assert numpy.allclose(a + a, numpy.asarray(c))
assert numpy.allclose(a, numpy.asarray(d)) assert numpy.allclose(a, numpy.asarray(d))
...@@ -318,7 +292,7 @@ class test_DimShuffle(unittest.TestCase): ...@@ -318,7 +292,7 @@ class test_DimShuffle(unittest.TestCase):
def test_dot(): def test_dot():
#print >>sys.stdout, 'starting test_dot' # print >>sys.stdout, 'starting test_dot'
utt.seed_rng() utt.seed_rng()
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
...@@ -347,12 +321,14 @@ def test_dot(): ...@@ -347,12 +321,14 @@ def test_dot():
b0 = cuda_ndarray.CudaNdarray(a0) b0 = cuda_ndarray.CudaNdarray(a0)
assert _allclose(numpy.dot(a0.T, a1), assert _allclose(numpy.dot(a0.T, a1),
cuda_ndarray.dot(cuda_ndarray.dimshuffle(b0, (1, 0)), b1)) cuda_ndarray.dot(
cuda_ndarray.dimshuffle(b0, (1, 0)), b1))
a1 = theano._asarray(rng.randn(6, 7), dtype='float32') a1 = theano._asarray(rng.randn(6, 7), dtype='float32')
b1 = cuda_ndarray.CudaNdarray(a1) b1 = cuda_ndarray.CudaNdarray(a1)
assert _allclose(numpy.dot(a0.T, a1.T), assert _allclose(
numpy.dot(a0.T, a1.T),
cuda_ndarray.dot(cuda_ndarray.dimshuffle(b0, (1, 0)), cuda_ndarray.dot(cuda_ndarray.dimshuffle(b0, (1, 0)),
cuda_ndarray.dimshuffle(b1, (1, 0)))) cuda_ndarray.dimshuffle(b1, (1, 0))))
...@@ -367,8 +343,8 @@ def test_sum(): ...@@ -367,8 +343,8 @@ def test_sum():
assert numpy.allclose(a0.sum(), assert numpy.allclose(a0.sum(),
numpy.asarray(b0.reduce_sum([1, 1]))) numpy.asarray(b0.reduce_sum([1, 1])))
a0sum = a0.sum(axis=0) a0.sum(axis=0)
b0sum = b0.reduce_sum([1, 0]) b0.reduce_sum([1, 0])
# print 'asum\n',a0sum # print 'asum\n',a0sum
# print 'bsum\n',numpy.asarray(b0sum) # print 'bsum\n',numpy.asarray(b0sum)
...@@ -399,8 +375,7 @@ def test_sum(): ...@@ -399,8 +375,7 @@ def test_sum():
def test_reshape(): def test_reshape():
shapelist = [ shapelist = [((1, 2, 3), (1, 2, 3)),
((1, 2, 3), (1, 2, 3)),
((1,), (1,)), ((1,), (1,)),
((1, 2, 3), (3, 2, 1)), ((1, 2, 3), (3, 2, 1)),
((1, 2, 3), (6,)), ((1, 2, 3), (6,)),
...@@ -423,7 +398,7 @@ def test_reshape(): ...@@ -423,7 +398,7 @@ def test_reshape():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
def subtest(shape_1, shape_2, rng): def subtest(shape_1, shape_2, rng):
#print >> sys.stdout, "INFO: shapes", shape_1, shape_2 # print >> sys.stdout, "INFO: shapes", shape_1, shape_2
a = theano._asarray(rng.randn(*shape_1), dtype='float32') a = theano._asarray(rng.randn(*shape_1), dtype='float32')
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
...@@ -459,8 +434,8 @@ def test_reshape(): ...@@ -459,8 +434,8 @@ def test_reshape():
b = cuda_ndarray.CudaNdarray(a) b = cuda_ndarray.CudaNdarray(a)
try: try:
bb = b.reshape(shape_2) b.reshape(shape_2)
except Exception as ValueError: except Exception:
return return
assert False assert False
...@@ -509,7 +484,7 @@ def test_stride_manipulation(): ...@@ -509,7 +484,7 @@ def test_stride_manipulation():
b_strides = b._strides b_strides = b._strides
for i in xrange(len(b.shape)): for i in xrange(len(b.shape)):
offset += (b.shape[i]-1) * b_strides[i] offset += (b.shape[i] - 1) * b_strides[i]
v._set_stride(i, -b_strides[i]) v._set_stride(i, -b_strides[i])
v._dev_data += offset * sizeof_float v._dev_data += offset * sizeof_float
...@@ -699,8 +674,8 @@ def test_setitem_matrixvector1(): ...@@ -699,8 +674,8 @@ def test_setitem_matrixvector1():
assert numpy.allclose(a, numpy.asarray(_a)) assert numpy.allclose(a, numpy.asarray(_a))
# test direct transfert from numpy # test direct transfert from numpy
_a[:, 1] = b*100 _a[:, 1] = b * 100
a[:, 1] = b*100 a[:, 1] = b * 100
assert numpy.allclose(a, numpy.asarray(_a)) assert numpy.allclose(a, numpy.asarray(_a))
row = theano._asarray([777, 888, 999], dtype='float32') row = theano._asarray([777, 888, 999], dtype='float32')
...@@ -725,8 +700,8 @@ def test_setitem_matrix_tensor3(): ...@@ -725,8 +700,8 @@ def test_setitem_matrix_tensor3():
assert numpy.allclose(a, numpy.asarray(_a)) assert numpy.allclose(a, numpy.asarray(_a))
# test direct transfert from numpy # test direct transfert from numpy
_a[:, 1, 1] = b*100 _a[:, 1, 1] = b * 100
a[:, 1, 1] = b*100 a[:, 1, 1] = b * 100
assert numpy.allclose(a, numpy.asarray(_a)) assert numpy.allclose(a, numpy.asarray(_a))
row = theano._asarray([777, 888, 999], dtype='float32') row = theano._asarray([777, 888, 999], dtype='float32')
...@@ -752,7 +727,7 @@ def test_setitem_matrix_bad_shape(): ...@@ -752,7 +727,7 @@ def test_setitem_matrix_bad_shape():
# attempt to assign the ndarray b with setitem # attempt to assign the ndarray b with setitem
_a[:, 1, 1] = _b _a[:, 1, 1] = _b
assert False assert False
except ValueError as e: except ValueError:
# print e # print e
assert True assert True
...@@ -761,7 +736,7 @@ def test_setitem_matrix_bad_shape(): ...@@ -761,7 +736,7 @@ def test_setitem_matrix_bad_shape():
# attempt to assign the ndarray b with setitem # attempt to assign the ndarray b with setitem
_a[1, 1, :] = b _a[1, 1, :] = b
assert False assert False
except ValueError as e: except ValueError:
# print e # print e
assert True assert True
...@@ -779,7 +754,7 @@ def test_setitem_matrix_bad_ndim(): ...@@ -779,7 +754,7 @@ def test_setitem_matrix_bad_ndim():
# attempt to assign the ndarray b with setitem # attempt to assign the ndarray b with setitem
_a[:, :, 1] = _b _a[:, :, 1] = _b
assert False assert False
except ValueError as e: except ValueError:
# print e # print e
assert True assert True
...@@ -788,7 +763,7 @@ def test_setitem_matrix_bad_ndim(): ...@@ -788,7 +763,7 @@ def test_setitem_matrix_bad_ndim():
# attempt to assign the ndarray b with setitem # attempt to assign the ndarray b with setitem
_a[1, :, :] = b _a[1, :, :] = b
assert False assert False
except ValueError as e: except ValueError:
# print e # print e
assert True assert True
...@@ -806,7 +781,7 @@ def test_setitem_matrix_bad_type(): ...@@ -806,7 +781,7 @@ def test_setitem_matrix_bad_type():
# attempt to assign the ndarray b with setitem # attempt to assign the ndarray b with setitem
_a[1, :, :] = b _a[1, :, :] = b
assert False assert False
except TypeError as e: except TypeError:
# print e # print e
assert True assert True
...@@ -832,8 +807,8 @@ def test_setitem_assign_to_slice(): ...@@ -832,8 +807,8 @@ def test_setitem_assign_to_slice():
# test direct transfert from numpy # test direct transfert from numpy
_d = _a[1, :, :] _d = _a[1, :, :]
_d[1, :] = b*10 _d[1, :] = b * 10
a[1, :, :][1, :] = b*10 a[1, :, :][1, :] = b * 10
assert numpy.allclose(a, numpy.asarray(_a)) assert numpy.allclose(a, numpy.asarray(_a))
...@@ -923,7 +898,7 @@ def test_setitem_rightvalue_ndarray_fails(): ...@@ -923,7 +898,7 @@ def test_setitem_rightvalue_ndarray_fails():
b = theano._asarray([7, 8, 9, 10], dtype='float32') b = theano._asarray([7, 8, 9, 10], dtype='float32')
_b = cuda_ndarray.CudaNdarray(b) _b = cuda_ndarray.CudaNdarray(b)
b5 = theano._asarray([7, 8, 9, 10, 11], dtype='float32') b5 = theano._asarray([7, 8, 9, 10, 11], dtype='float32')
_b5 = cuda_ndarray.CudaNdarray(b) cuda_ndarray.CudaNdarray(b)
# attempt to assign the ndarray b with setitem # attempt to assign the ndarray b with setitem
_a[:, :, 1] = _b _a[:, :, 1] = _b
...@@ -941,9 +916,9 @@ def test_setitem_rightvalue_ndarray_fails(): ...@@ -941,9 +916,9 @@ def test_setitem_rightvalue_ndarray_fails():
# without same number of dim # without same number of dim
try: try:
_a[0, :, :] = mat _a[0, :, :] = mat
#a[0, :, :] = mat # a[0, :, :] = mat
#assert numpy.allclose(numpy.asarray(_a), a) # assert numpy.allclose(numpy.asarray(_a), a)
except ValueError as e: except ValueError:
pass pass
# test direct transfert from numpy with broadcast # test direct transfert from numpy with broadcast
...@@ -964,7 +939,7 @@ def test_zeros_basic(): ...@@ -964,7 +939,7 @@ def test_zeros_basic():
_n = numpy.zeros(shp, dtype="float32") _n = numpy.zeros(shp, dtype="float32")
assert numpy.allclose(numpy.asarray(_a), _n) assert numpy.allclose(numpy.asarray(_a), _n)
assert _a.shape == _n.shape assert _a.shape == _n.shape
assert all(_a._strides == numpy.asarray(_n.strides)/4) assert all(_a._strides == numpy.asarray(_n.strides) / 4)
# TODO:The following don't have the same stride! # TODO:The following don't have the same stride!
# This should be fixed with the new GpuNdArray. # This should be fixed with the new GpuNdArray.
...@@ -1039,10 +1014,7 @@ def test_is_c_contiguous(): ...@@ -1039,10 +1014,7 @@ def test_is_c_contiguous():
assert not a[::2].is_c_contiguous() assert not a[::2].is_c_contiguous()
if __name__ == '__main__': if __name__ == '__main__':
test_zeros_basic_3d_tensor()
test_zeros_basic_vector()
test_setitem_matrixvector1() test_setitem_matrixvector1()
test_setitem_matrix_tensor3() test_setitem_matrix_tensor3()
test_setitem_broadcast_must_fail()
test_setitem_assign_to_slice() test_setitem_assign_to_slice()
test_setitem_rightvalue_ndarray_fails() test_setitem_rightvalue_ndarray_fails()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论