提交 5c16f72f authored 作者: Frederic Bastien's avatar Frederic Bastien

Add test for inplace operation on shared variable when the shared constructor…

Add test for inplace operation on shared variable when the shared constructor input is casted to the same value as the internal type.
上级 14a5a805
......@@ -826,6 +826,7 @@ def test_shared_cudandarray():
import theano.tensor.tests.test_sharedvar
#This test the case when the shared constructor view an CudaNdarray as input
test_shared_options = theano.tensor.tests.test_sharedvar.makeSharedTester(
shared_constructor_ = tcn.shared_constructor,
dtype_ = 'float32',
......@@ -833,6 +834,7 @@ test_shared_options = theano.tensor.tests.test_sharedvar.makeSharedTester(
shared_borrow_true_alias_ = True,#True when the original value is already a CudaNdarray!
set_value_borrow_true_alias_ = False,
set_value_inplace_ = True,
set_casted_value_inplace_ = False,
internal_type_ = cuda_ndarray.CudaNdarray,
test_internal_type_ = lambda a: isinstance(a,cuda_ndarray.CudaNdarray),
theano_fct_ = theano.tensor.exp,
......
......@@ -507,6 +507,7 @@ test_shared_options=theano.tensor.tests.test_sharedvar.makeSharedTester(
shared_borrow_true_alias_ = True,
set_value_borrow_true_alias_ = True,
set_value_inplace_ = False,
set_casted_value_inplace_ = False,
internal_type_ = scipy.sparse.csc_matrix,
test_internal_type_ = scipy.sparse.issparse,
theano_fct_ = lambda a: dense_from_sparse(a*2.),
......
......@@ -14,6 +14,7 @@ def makeSharedTester(shared_constructor_,
shared_borrow_true_alias_,
set_value_borrow_true_alias_,
set_value_inplace_,
set_casted_value_inplace_,
internal_type_,
test_internal_type_,
theano_fct_,
......@@ -24,7 +25,28 @@ def makeSharedTester(shared_constructor_,
This is a generic fct to allow reusing the same test function
for many shared variable of many types.
We must use /= as sparse type don't support other inplace operation.
:param shared_constructor_: The shared variable constructor to use
:param dtype_: The dtype of the data to test
:param get_value_borrow_true_alias_: Should a get_value(borrow=True) return the internal object
:param shared_borrow_true_alias_: Should shared(val,borrow=True) reuse the val memory space
:param set_value_borrow_true_alias_: Should set_value(val,borrow=True) reuse the val memory space
:param set_value_inplace_: Should this shared variable overwrite the current
memory when the new value is an ndarray
:param set_casted_value_inplace_: Should this shared variable overwrite the
current memory when the new value is of the same
type as the internal type.
:param internal_type_: The internal type used.
:param test_internal_type_: A function that tell if its input is of the same
type as this shared variable internal type.
:param theano_fct_: A theano op that will be used to do some computation on the shared variable
:param ref_fct_: A reference function that should return the same value as the theano_fct_
:param cast_value_: A callable that cast an ndarray into the internal shared variable representation
:param op_by_matrix_: When we do inplace operation on the an internal type object, should we do it with a scalar or a matrix of the same value.
:note:
We must use /= as sparse type don't support other inplace operation.
"""
class SharedTester(unittest.TestCase):
shared_constructor = staticmethod(shared_constructor_)
......@@ -37,6 +59,7 @@ def makeSharedTester(shared_constructor_,
ref_fct = staticmethod(ref_fct_)
set_value_borrow_true_alias = set_value_borrow_true_alias_
set_value_inplace = set_value_inplace_
set_casted_value_inplace = set_casted_value_inplace_
cast_value = staticmethod(cast_value_)
op_by_matrix = op_by_matrix_
......@@ -303,6 +326,13 @@ def makeSharedTester(shared_constructor_,
assert numpy.allclose(self.ref_fct(x_shared.value), self.ref_fct(self.cast_value(nd)))
assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_value_inplace
# test when the data is already of the same type as the destination
# specificaly usefull for gpu data
nd += 1
x_shared.set_value(self.cast_value(nd), borrow=False)
assert numpy.allclose(self.ref_fct(x_shared.value), self.ref_fct(self.cast_value(nd)))
assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_casted_value_inplace
def test_specify_shape(self):
dtype = self.dtype
if dtype is None:
......@@ -480,6 +510,7 @@ test_shared_options=makeSharedTester(
shared_borrow_true_alias_ = True,
set_value_borrow_true_alias_ = True,
set_value_inplace_ = False,
set_casted_value_inplace_ = False,
internal_type_ = numpy.ndarray,
test_internal_type_ = lambda a: isinstance(a,numpy.ndarray),
theano_fct_ = theano.tensor.sum,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论