提交 894668f3 authored 作者: Frederic Bastien's avatar Frederic Bastien

Make SparseVariable test of Theano memory contract use the generic test fct.…

Make SparseVariable test of Theano memory contract use the generic test fct. This make it test more case. I needed to change to idiv as implace op as scipy sparce support only idiv as inplace op.
上级 9264cf11
...@@ -802,7 +802,12 @@ def test_duplicate_arg_elemwise(): ...@@ -802,7 +802,12 @@ def test_duplicate_arg_elemwise():
import theano.tensor.tests.test_basic import theano.tensor.tests.test_basic
test_shared_options = theano.tensor.tests.test_basic.makeSharedTester(tcn.shared_constructor, 'float32', False, False, False, cuda_ndarray.CudaNdarray, theano.tensor.exp, numpy.exp) test_shared_options = theano.tensor.tests.test_basic.makeSharedTester(
tcn.shared_constructor, 'float32', False, False, False,
cuda_ndarray.CudaNdarray,
lambda a: isinstance(a,cuda_ndarray.CudaNdarray),
theano.tensor.exp, numpy.exp,
add_matrix_ = True)
if __name__ == '__main__': if __name__ == '__main__':
test_many_arg_elemwise() test_many_arg_elemwise()
......
...@@ -440,99 +440,13 @@ class test_structureddot(unittest.TestCase): ...@@ -440,99 +440,13 @@ class test_structureddot(unittest.TestCase):
self.failUnless(numpy.allclose(theano_result, scipy_result)) self.failUnless(numpy.allclose(theano_result, scipy_result))
self.failIf(theano_time > overhead_rtol*scipy_time + overhead_tol) self.failIf(theano_time > overhead_rtol*scipy_time + overhead_tol)
import theano.tensor.tests.test_basic
def test_shared_dont_alias(): test_shared_options=theano.tensor.tests.test_basic.makeSharedTester(
rng = numpy.random.RandomState([3,5,17]) theano.sparse.shared, 'float64',
x_lil = random_lil((2,4), 'float64', 5) True, True, True, scipy.sparse.csc_matrix, scipy.sparse.issparse,
x = sp.csr_matrix(x_lil) lambda a: dense_from_sparse(a*2.),
densifier = rng.randn(*x.shape) lambda a: numpy.asarray((a*2).todense()),
scipy.sparse.csr_matrix)
x_shared = theano.shared(x, borrow = False)
prod = theano.sparse.mul_s_d(x_shared,densifier)
prod_func = theano.function([],prod)
prod_val = prod_func().todense()
x_dense_fake = x.todense()
x_dense = numpy.asarray(x_dense_fake)
scipy_prod_val = x_dense * densifier
assert numpy.allclose(scipy_prod_val, prod_val)
for i in xrange(x.shape[0]):
for j in xrange(x.shape[1]):
if x[i,j] != 0:
x[i,j] += 1
prod_val_2 = prod_func().todense()
#value used to construct should not alias with internal
assert numpy.allclose(prod_val_2,prod_val)
x = x_shared.get_value(borrow = False)
for i in xrange(x.shape[0]):
for j in xrange(x.shape[1]):
if x[i,j] != 0:
x[i,j] += 1
prod_val_3 = prod_func().todense()
#value returned by access should not alias with internal
assert numpy.allclose(prod_val, prod_val_3)
#in this case we can alias
x = x_shared.get_value(borrow = True)
for i in xrange(x.shape[0]):
for j in xrange(x.shape[1]):
if x[i,j] != 0:
x[i,j] += 1
x_dense_fake = x.todense()
x_dense = numpy.asarray(x_dense_fake)
scipy_prod_val = x_dense * densifier
#this is not required by the contract but it is a feature we've implemented
assert numpy.allclose(scipy_prod_val, prod_func().todense())
def test_shared_do_alias():
rng = numpy.random.RandomState([2,4,16])
x_lil = random_lil((2,4), 'float64', 5)
x = sp.csr_matrix(x_lil)
x_shared = theano.shared(x, borrow = True)
densifier = rng.randn(*x.shape)
prod = theano.sparse.mul_s_d(x_shared,densifier)
prod_func = theano.function([],prod)
prod_val = prod_func().todense()
x_dense_fake = x.todense()
x_dense = numpy.asarray(x_dense_fake)
scipy_prod_val = x_dense * densifier
assert numpy.allclose(scipy_prod_val, prod_val)
for i in xrange(x.shape[0]):
for j in xrange(x.shape[1]):
if x[i,j] != 0:
x[i,j] += 1
x_dense_fake = x.todense()
x_dense = numpy.asarray(x_dense_fake)
scipy_prod_val = x_dense * densifier
#not required by the contract but it is a feature we've implemented
assert numpy.allclose(scipy_prod_val, prod_func().todense())
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -3390,16 +3390,21 @@ def test_dimshuffle_duplicate(): ...@@ -3390,16 +3390,21 @@ def test_dimshuffle_duplicate():
assert success assert success
def makeSharedTester(shared_constructor_, def makeSharedTester(shared_constructor_,
dtype_, dtype_,
get_value_borrow_true_alias_, get_value_borrow_true_alias_,
shared_borrow_true_alias_, shared_borrow_true_alias_,
set_value_borrow_true_alias_, set_value_borrow_true_alias_,
internal_type_, internal_type_,
theano_fct_, test_internal_type_,
ref_fct_): theano_fct_,
ref_fct_,
cast_value_ = numpy.asarray,
add_matrix_ = False):
""" """
This is a generic fct to allow reusing the same test function This is a generic fct to allow reusing the same test function
for many shared variable of many types. for many shared variable of many types.
We must use /= as sparse type don't support other inplace operation.
""" """
class SharedTester(unittest.TestCase): class SharedTester(unittest.TestCase):
shared_constructor = staticmethod(shared_constructor_) shared_constructor = staticmethod(shared_constructor_)
...@@ -3407,9 +3412,12 @@ def makeSharedTester(shared_constructor_, ...@@ -3407,9 +3412,12 @@ def makeSharedTester(shared_constructor_,
get_value_borrow_true_alias = get_value_borrow_true_alias_ get_value_borrow_true_alias = get_value_borrow_true_alias_
shared_borrow_true_alias = shared_borrow_true_alias_ shared_borrow_true_alias = shared_borrow_true_alias_
internal_type = internal_type_ internal_type = internal_type_
test_internal_type = staticmethod(test_internal_type_)
theano_fct = staticmethod(theano_fct_) theano_fct = staticmethod(theano_fct_)
ref_fct = staticmethod(ref_fct_) ref_fct = staticmethod(ref_fct_)
set_value_borrow_true_alias = set_value_borrow_true_alias_ set_value_borrow_true_alias = set_value_borrow_true_alias_
cast_value = staticmethod(cast_value_)
add_matrix = add_matrix_
def test_shared_dont_alias(self): def test_shared_dont_alias(self):
dtype = self.dtype dtype = self.dtype
...@@ -3418,6 +3426,8 @@ def makeSharedTester(shared_constructor_, ...@@ -3418,6 +3426,8 @@ def makeSharedTester(shared_constructor_,
rng = numpy.random.RandomState([3,5,17]) rng = numpy.random.RandomState([3,5,17])
x = numpy.asarray(rng.uniform(0,1,[2,4]),dtype=dtype) x = numpy.asarray(rng.uniform(0,1,[2,4]),dtype=dtype)
x = self.cast_value(x)
x_ref = self.ref_fct(x) x_ref = self.ref_fct(x)
x_shared = self.shared_constructor(x, borrow = False) x_shared = self.shared_constructor(x, borrow = False)
total = self.theano_fct(x_shared) total = self.theano_fct(x_shared)
...@@ -3428,7 +3438,7 @@ def makeSharedTester(shared_constructor_, ...@@ -3428,7 +3438,7 @@ def makeSharedTester(shared_constructor_,
assert numpy.allclose(self.ref_fct(x), total_val) assert numpy.allclose(self.ref_fct(x), total_val)
x += 1 x /= .5
total_val_2 = total_func() total_val_2 = total_func()
...@@ -3437,7 +3447,7 @@ def makeSharedTester(shared_constructor_, ...@@ -3437,7 +3447,7 @@ def makeSharedTester(shared_constructor_,
x = x_shared.get_value(borrow = False) x = x_shared.get_value(borrow = False)
x += 1 x /= .5
total_val_3 = total_func() total_val_3 = total_func()
...@@ -3446,7 +3456,7 @@ def makeSharedTester(shared_constructor_, ...@@ -3446,7 +3456,7 @@ def makeSharedTester(shared_constructor_,
#in this case we can alias #in this case we can alias
x = x_shared.get_value(borrow = True) x = x_shared.get_value(borrow = True)
x += 1 x /= .5
#this is not required by the contract but it is a feature we've #this is not required by the contract but it is a feature we've
#implemented for some type of SharedVariable. #implemented for some type of SharedVariable.
...@@ -3463,6 +3473,8 @@ def makeSharedTester(shared_constructor_, ...@@ -3463,6 +3473,8 @@ def makeSharedTester(shared_constructor_,
rng = numpy.random.RandomState([3,5,17]) rng = numpy.random.RandomState([3,5,17])
x = numpy.asarray(rng.uniform(0,1,[2,4]),dtype=dtype) x = numpy.asarray(rng.uniform(0,1,[2,4]),dtype=dtype)
x = self.cast_value(x)
x_ref = self.ref_fct(x) x_ref = self.ref_fct(x)
x_shared = self.shared_constructor(x, borrow = False) x_shared = self.shared_constructor(x, borrow = False)
total = self.theano_fct(x_shared) total = self.theano_fct(x_shared)
...@@ -3471,21 +3483,21 @@ def makeSharedTester(shared_constructor_, ...@@ -3471,21 +3483,21 @@ def makeSharedTester(shared_constructor_,
#in this case we can alias with the internal value #in this case we can alias with the internal value
x = x_shared.get_value(borrow = True, return_internal_type = True) x = x_shared.get_value(borrow = True, return_internal_type = True)
assert isinstance(x, self.internal_type) assert self.test_internal_type(x)
values_to_add = numpy.ones(x.shape,dtype=dtype) values_to_add = .5
if not isinstance(values_to_add, self.internal_type): if self.add_matrix:
values_to_add = self.internal_type(values_to_add)#supported for cudandarray, but not ndarray. values_to_add = self.internal_type(numpy.ones(x.shape,dtype=dtype)/2)#supported for cudandarray, but not ndarray.
x += values_to_add#supported by ndarray and CudaNdarray x /= values_to_add#supported by ndarray and CudaNdarray
#this is not required by the contract but it is a feature we can #this is not required by the contract but it is a feature we can
#implement for some type of SharedVariable. #implement for some type of SharedVariable.
assert numpy.allclose(self.ref_fct(x), total_func()) assert numpy.allclose(self.ref_fct(x), total_func())
x = x_shared.get_value(borrow = False, return_internal_type = True) x = x_shared.get_value(borrow = False, return_internal_type = True)
assert isinstance(x, self.internal_type) assert self.test_internal_type(x)
assert x is not x_shared.container.value
x += values_to_add#supported by ndarray and CudaNdarray x /= values_to_add#supported by ndarray and CudaNdarray
#this is required by the contract #this is required by the contract
assert not numpy.allclose(self.ref_fct(x), total_func()) assert not numpy.allclose(self.ref_fct(x), total_func())
...@@ -3497,6 +3509,8 @@ def makeSharedTester(shared_constructor_, ...@@ -3497,6 +3509,8 @@ def makeSharedTester(shared_constructor_,
rng = numpy.random.RandomState([3,5,17]) rng = numpy.random.RandomState([3,5,17])
x = numpy.asarray(rng.uniform(0,1,[2,4]),dtype=dtype) x = numpy.asarray(rng.uniform(0,1,[2,4]),dtype=dtype)
x = self.cast_value(x)
x_orig = x x_orig = x
x_orig_copy = x.copy() x_orig_copy = x.copy()
x_ref = self.ref_fct(x) x_ref = self.ref_fct(x)
...@@ -3508,29 +3522,29 @@ def makeSharedTester(shared_constructor_, ...@@ -3508,29 +3522,29 @@ def makeSharedTester(shared_constructor_,
#test if that theano shared variable optimize set_value(borrow=True) #test if that theano shared variable optimize set_value(borrow=True)
get_x = x_shared.get_value(borrow=True) get_x = x_shared.get_value(borrow=True)
assert get_x is not x_orig#borrow=False to shared_constructor assert get_x is not x_orig#borrow=False to shared_constructor
get_x +=1 get_x /= .5
x_shared.set_value(get_x, borrow=True) x_shared.set_value(get_x, borrow=True)
x = x_shared.get_value(borrow=True) x = x_shared.get_value(borrow=True)
if self.set_value_borrow_true_alias: if self.set_value_borrow_true_alias:
assert x is get_x assert x is get_x
else: else:
assert x is not get_x assert x is not get_x
assert numpy.allclose(self.ref_fct(x_orig+1),self.ref_fct(x)) assert numpy.allclose(self.ref_fct(x_orig/.5),self.ref_fct(x))
#test optimized get set value on the gpu(don't pass data to the cpu) #test optimized get set value on the gpu(don't pass data to the cpu)
get_x = x_shared.get_value(borrow=True, return_internal_type=True) get_x = x_shared.get_value(borrow=True, return_internal_type=True)
assert get_x is not x_orig#borrow=False to shared_constructor assert get_x is not x_orig#borrow=False to shared_constructor
assert isinstance(get_x, self.internal_type) assert self.test_internal_type(get_x)
values_to_add = numpy.ones(x.shape,dtype=dtype) values_to_add = .5
if not isinstance(values_to_add, self.internal_type): if self.add_matrix:
values_to_add = self.internal_type(values_to_add)#supported for cudandarray, but not ndarray. values_to_add = self.internal_type(numpy.ones(x.shape,dtype=dtype)/2)#supported for cudandarray, but not ndarray.
assert isinstance(values_to_add, self.internal_type) assert self.test_internal_type(values_to_add)
get_x += values_to_add#supported by ndarray and CudaNdarray get_x /= values_to_add#supported by ndarray and CudaNdarray
assert isinstance(get_x, self.internal_type) assert self.test_internal_type(get_x)
x_shared.set_value(get_x, borrow=True) x_shared.set_value(get_x, borrow=True)
x = x_shared.get_value(borrow=True, return_internal_type=True) x = x_shared.get_value(borrow=True, return_internal_type=True)
assert isinstance(x, self.internal_type) assert self.test_internal_type(x)
assert x is get_x assert x is get_x
################ TODO test Out. ################ TODO test Out.
...@@ -3541,6 +3555,7 @@ def makeSharedTester(shared_constructor_, ...@@ -3541,6 +3555,7 @@ def makeSharedTester(shared_constructor_,
rng = numpy.random.RandomState([2,4,16]) rng = numpy.random.RandomState([2,4,16])
x = numpy.asarray(rng.uniform(1,2,[4,2]),dtype=dtype) x = numpy.asarray(rng.uniform(1,2,[4,2]),dtype=dtype)
x = self.cast_value(x)
x_ref = self.ref_fct(x) x_ref = self.ref_fct(x)
x_shared = self.shared_constructor(x, borrow = True) x_shared = self.shared_constructor(x, borrow = True)
...@@ -3553,7 +3568,7 @@ def makeSharedTester(shared_constructor_, ...@@ -3553,7 +3568,7 @@ def makeSharedTester(shared_constructor_,
assert numpy.allclose(self.ref_fct(x), total_val) assert numpy.allclose(self.ref_fct(x), total_val)
x += 1 x /= .5
#not required by the contract but it is a feature we've implemented #not required by the contract but it is a feature we've implemented
if self.shared_borrow_true_alias: if self.shared_borrow_true_alias:
...@@ -3564,8 +3579,10 @@ def makeSharedTester(shared_constructor_, ...@@ -3564,8 +3579,10 @@ def makeSharedTester(shared_constructor_,
return SharedTester return SharedTester
test_shared_options=makeSharedTester(tensor.shared, 'float64', test_shared_options=makeSharedTester(tensor.shared, 'float64',
True, True, True, numpy.ndarray, True, True, True,
theano.tensor.sum, numpy.sum) numpy.ndarray,
lambda a: isinstance(a,numpy.ndarray),
theano.tensor.sum, numpy.sum)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论