提交 09d7318f authored 作者: Marc-Alexandre Cote's avatar Marc-Alexandre Cote

Simplify unit tests using check_preallocated_output

上级 195bee05
...@@ -92,7 +92,7 @@ class CumsumOp(theano.Op): ...@@ -92,7 +92,7 @@ class CumsumOp(theano.Op):
return code return code
def c_code_cache_version(self): def c_code_cache_version(self):
return (3,) return (1,)
def __str__(self): def __str__(self):
return "%s{%s}" % (self.__class__.__name__, self.axis) return "%s{%s}" % (self.__class__.__name__, self.axis)
...@@ -199,7 +199,7 @@ class CumprodOp(theano.Op): ...@@ -199,7 +199,7 @@ class CumprodOp(theano.Op):
return code return code
def c_code_cache_version(self): def c_code_cache_version(self):
return (2,) return (1,)
def __str__(self): def __str__(self):
return "%s{%s}" % (self.__class__.__name__, self.axis) return "%s{%s}" % (self.__class__.__name__, self.axis)
......
...@@ -10,6 +10,7 @@ from theano.tensor.extra_ops import (CumsumOp, cumsum, CumprodOp, cumprod, ...@@ -10,6 +10,7 @@ from theano.tensor.extra_ops import (CumsumOp, cumsum, CumprodOp, cumprod,
FillDiagonal, fill_diagonal) FillDiagonal, fill_diagonal)
from theano import tensor as T from theano import tensor as T
from theano import config, tensor, function from theano import config, tensor, function
from theano.compile import DebugMode
numpy_ver = [int(n) for n in numpy.__version__.split('.')[:2]] numpy_ver = [int(n) for n in numpy.__version__.split('.')[:2]]
...@@ -25,24 +26,14 @@ class TestCumsumOp(utt.InferShapeTester): ...@@ -25,24 +26,14 @@ class TestCumsumOp(utt.InferShapeTester):
def test_cumsumOp(self): def test_cumsumOp(self):
x = T.tensor3('x') x = T.tensor3('x')
a = np.random.random((3, 5, 2)).astype(config.floatX) a = np.random.random((3, 5, 2)).astype(config.floatX)
b = np.random.random((30, 5, 2)).astype(config.floatX)
f = theano.function([x], cumsum(x), mode="DebugMode") f = theano.function([x], cumsum(x), mode=DebugMode(check_preallocated_output="ALL"))
assert np.allclose(np.cumsum(a), f(a)) # Test axis=None assert np.allclose(np.cumsum(a), f(a)) # Test axis=None
# Test without garbage collector
f = theano.function([x], cumsum(x).sum(), mode=theano.compile.Mode(linker="cvm_nogc", optimizer="fast_run") )
assert np.allclose(np.cumsum(a).sum(), f(a)) # Test axis=None
assert np.allclose(np.cumsum(b).sum(), f(b)) # Would fail without re-allocation
for axis in range(len(a.shape)): for axis in range(len(a.shape)):
f = theano.function([x], cumsum(x, axis=axis), mode="DebugMode") f = theano.function([x], cumsum(x, axis=axis), mode=DebugMode(check_preallocated_output="ALL"))
assert np.allclose(np.cumsum(a, axis=axis), f(a)) assert np.allclose(np.cumsum(a, axis=axis), f(a))
# Test without garbage collector
f = theano.function([x], cumsum(x, axis=axis).sum(), mode=theano.compile.Mode(linker="cvm_nogc", optimizer="fast_run"))
assert np.allclose(np.cumsum(a, axis=axis).sum(), f(a))
assert np.allclose(np.cumsum(b, axis=axis).sum(), f(b)) # Would fail without re-allocation
def test_infer_shape(self): def test_infer_shape(self):
x = T.tensor3('x') x = T.tensor3('x')
...@@ -79,25 +70,14 @@ class TestCumprodOp(utt.InferShapeTester): ...@@ -79,25 +70,14 @@ class TestCumprodOp(utt.InferShapeTester):
def test_CumprodOp(self): def test_CumprodOp(self):
x = T.tensor3('x') x = T.tensor3('x')
a = np.random.random((3, 5, 2)).astype(config.floatX) a = np.random.random((3, 5, 2)).astype(config.floatX)
b = np.random.random((30, 5, 2)).astype(config.floatX)
f = theano.function([x], cumprod(x), mode="DebugMode") f = theano.function([x], cumprod(x), mode=DebugMode(check_preallocated_output="ALL"))
assert np.allclose(np.cumprod(a), f(a)) # Test axis=None assert np.allclose(np.cumprod(a), f(a)) # Test axis=None
# Test without garbage collector
f = theano.function([x], cumprod(x).sum(), mode=theano.compile.Mode(linker="cvm_nogc", optimizer="fast_run") )
assert np.allclose(np.cumprod(a).sum(), f(a)) # Test axis=None
assert np.allclose(np.cumprod(b).sum(), f(b)) # Would fail without re-allocation
for axis in range(len(a.shape)): for axis in range(len(a.shape)):
f = theano.function([x], cumprod(x, axis=axis), mode="DebugMode") f = theano.function([x], cumprod(x, axis=axis), mode=DebugMode(check_preallocated_output="ALL"))
assert np.allclose(np.cumprod(a, axis=axis), f(a)) assert np.allclose(np.cumprod(a, axis=axis), f(a))
# Test without garbage collector
f = theano.function([x], cumprod(x, axis=axis).sum(), mode=theano.compile.Mode(linker="cvm_nogc", optimizer="fast_run"))
assert np.allclose(np.cumprod(a, axis=axis).sum(), f(a))
assert np.allclose(np.cumprod(b, axis=axis).sum(), f(b)) # Would fail without re-allocation
def test_infer_shape(self): def test_infer_shape(self):
x = T.tensor3('x') x = T.tensor3('x')
a = np.random.random((3, 5, 2)).astype(config.floatX) a = np.random.random((3, 5, 2)).astype(config.floatX)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论