提交 ee78a021 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Make optimizations run in tests.

上级 3465a406
...@@ -72,16 +72,12 @@ def make_checks(loop_orders, dtypes, sub): ...@@ -72,16 +72,12 @@ def make_checks(loop_orders, dtypes, sub):
%(var)s_n%(index)s = PyArray_DIMS(%(var)s)[%(index)s]; %(var)s_n%(index)s = PyArray_DIMS(%(var)s)[%(index)s];
%(var)s_stride%(index)s = PyArray_STRIDES(%(var)s)[%(index)s] / sizeof(%(dtype)s); %(var)s_stride%(index)s = PyArray_STRIDES(%(var)s)[%(index)s] / sizeof(%(dtype)s);
%(var)s_jump%(index)s_%(j)s = %(jump)s; %(var)s_jump%(index)s_%(j)s = %(jump)s;
//printf("%(var)s_jump%(index)s_%(j)s is:");
//std::cout << %(var)s_jump%(index)s_%(j)s << std::endl;
""" % locals() """ % locals()
adjust = "%(var)s_n%(index)s*%(var)s_stride%(index)s" % locals() adjust = "%(var)s_n%(index)s*%(var)s_stride%(index)s" % locals()
else: else:
jump = "-(%s)" % adjust jump = "-(%s)" % adjust
init += """ init += """
%(var)s_jump%(index)s_%(j)s = %(jump)s; %(var)s_jump%(index)s_%(j)s = %(jump)s;
//printf("%(var)s_jump%(index)s_%(j)s is:");
//std::cout << %(var)s_jump%(index)s_%(j)s << std::endl;
""" % locals() """ % locals()
adjust = "0" adjust = "0"
check = "" check = ""
......
...@@ -15,7 +15,7 @@ from theano import gof, scalar, config ...@@ -15,7 +15,7 @@ from theano import gof, scalar, config
from theano import tensor from theano import tensor
from theano.tensor import TensorType, as_tensor_variable from theano.tensor import TensorType, as_tensor_variable
from theano.compile.mode import get_default_mode from theano.compile.mode import get_default_mode, Mode
from theano.tensor.elemwise import (CAReduce, Elemwise, DimShuffle, from theano.tensor.elemwise import (CAReduce, Elemwise, DimShuffle,
Prod, ProdWithoutZeros) Prod, ProdWithoutZeros)
from theano.tests import unittest_tools from theano.tests import unittest_tools
...@@ -365,9 +365,9 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -365,9 +365,9 @@ class test_CAReduce(unittest_tools.InferShapeTester):
((), ())] ((), ())]
type = TensorType type = TensorType
def with_linker(self, linker, scalar_op=scalar.add, dtype="floatX", def with_mode(self, mode, scalar_op=scalar.add, dtype="floatX",
pre_scalar_op=None, pre_scalar_op=None,
test_nan=False, tensor_op=None): test_nan=False, tensor_op=None):
for xsh, tosum in self.cases: for xsh, tosum in self.cases:
if dtype == "floatX": if dtype == "floatX":
dtype = theano.config.floatX dtype = theano.config.floatX
...@@ -383,7 +383,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -383,7 +383,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
if tosum is None: if tosum is None:
tosum = list(range(len(xsh))) tosum = list(range(len(xsh)))
f = copy(linker).accept(FunctionGraph([x], [e])).make_function() f = theano.function([x], e, mode=mode)
xv = np.asarray(np.random.rand(*xsh)) xv = np.asarray(np.random.rand(*xsh))
if dtype not in tensor.discrete_dtypes: if dtype not in tensor.discrete_dtypes:
...@@ -495,8 +495,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -495,8 +495,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
e = tensor_op(x, axis=tosum) e = tensor_op(x, axis=tosum)
if tosum is None: if tosum is None:
tosum = list(range(len(xsh))) tosum = list(range(len(xsh)))
f = copy(linker).accept(FunctionGraph([x], f = theano.function([x], e.shape, mode=mode)
[e.shape])).make_function()
if not(scalar_op in [scalar.maximum, scalar.minimum] and if not(scalar_op in [scalar.maximum, scalar.minimum] and
((xsh == () or np.prod(xsh) == 0))): ((xsh == () or np.prod(xsh) == 0))):
try: try:
...@@ -505,70 +504,78 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -505,70 +504,78 @@ class test_CAReduce(unittest_tools.InferShapeTester):
# GpuCAReduce don't implement all cases when size is 0 # GpuCAReduce don't implement all cases when size is 0
assert xv.size == 0 assert xv.size == 0
def test_perform_noopt(self):
self.with_mode(Mode(linker='py', optimizer=None), scalar.add, dtype='floatX')
def test_perform(self): def test_perform(self):
for dtype in ["bool", "floatX", "complex64", "complex128", "int8", "uint8"]: for dtype in ["bool", "floatX", "complex64", "complex128", "int8", "uint8"]:
self.with_linker(gof.PerformLinker(), scalar.add, dtype=dtype) self.with_mode(Mode(linker='py'), scalar.add, dtype=dtype)
self.with_linker(gof.PerformLinker(), scalar.mul, dtype=dtype) self.with_mode(Mode(linker='py'), scalar.mul, dtype=dtype)
self.with_linker(gof.PerformLinker(), scalar.maximum, dtype=dtype) self.with_mode(Mode(linker='py'), scalar.maximum, dtype=dtype)
self.with_linker(gof.PerformLinker(), scalar.minimum, dtype=dtype) self.with_mode(Mode(linker='py'), scalar.minimum, dtype=dtype)
self.with_linker(gof.PerformLinker(), scalar.and_, dtype=dtype, self.with_mode(Mode(linker='py'), scalar.and_, dtype=dtype,
tensor_op=tensor.all) tensor_op=tensor.all)
self.with_linker(gof.PerformLinker(), scalar.or_, dtype=dtype, self.with_mode(Mode(linker='py'), scalar.or_, dtype=dtype,
tensor_op=tensor.any) tensor_op=tensor.any)
for dtype in ["int8", "uint8"]: for dtype in ["int8", "uint8"]:
self.with_linker(gof.PerformLinker(), scalar.or_, dtype=dtype) self.with_mode(Mode(linker='py'), scalar.or_, dtype=dtype)
self.with_linker(gof.PerformLinker(), scalar.and_, dtype=dtype) self.with_mode(Mode(linker='py'), scalar.and_, dtype=dtype)
self.with_linker(gof.PerformLinker(), scalar.xor, dtype=dtype) self.with_mode(Mode(linker='py'), scalar.xor, dtype=dtype)
def test_perform_nan(self): def test_perform_nan(self):
for dtype in ["floatX", "complex64", "complex128"]: for dtype in ["floatX", "complex64", "complex128"]:
self.with_linker(gof.PerformLinker(), scalar.add, dtype=dtype, self.with_mode(Mode(linker='py'), scalar.add, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), scalar.mul, dtype=dtype, self.with_mode(Mode(linker='py'), scalar.mul, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), scalar.maximum, dtype=dtype, self.with_mode(Mode(linker='py'), scalar.maximum, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), scalar.minimum, dtype=dtype, self.with_mode(Mode(linker='py'), scalar.minimum, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.PerformLinker(), scalar.or_, dtype=dtype, self.with_mode(Mode(linker='py'), scalar.or_, dtype=dtype,
test_nan=True, tensor_op=tensor.any) test_nan=True, tensor_op=tensor.any)
self.with_linker(gof.PerformLinker(), scalar.and_, dtype=dtype, self.with_mode(Mode(linker='py'), scalar.and_, dtype=dtype,
test_nan=True, tensor_op=tensor.all) test_nan=True, tensor_op=tensor.all)
def test_c_noopt(self):
# We need to make sure that we cover the corner cases that
# optimizations normally cover
if not theano.config.cxx:
raise SkipTest("G++ not available, so we need to skip this test.")
self.with_mode(Mode(linker='c', optimizer=None), scalar.add, dtype='floatX')
@attr('slow') @attr('slow')
def test_c(self): def test_c(self):
if not theano.config.cxx:
raise SkipTest("G++ not available, so we need to skip this test.")
for dtype in ["bool", "floatX", "complex64", "complex128", "int8", "uint8"]: for dtype in ["bool", "floatX", "complex64", "complex128", "int8", "uint8"]:
self.with_linker(gof.CLinker(), scalar.add, dtype=dtype) self.with_mode(Mode(linker='c'), scalar.add, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.mul, dtype=dtype) self.with_mode(Mode(linker='c'), scalar.mul, dtype=dtype)
for dtype in ["bool", "floatX", "int8", "uint8"]: for dtype in ["bool", "floatX", "int8", "uint8"]:
self.with_linker(gof.CLinker(), scalar.minimum, dtype=dtype) self.with_mode(Mode(linker='c'), scalar.minimum, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.maximum, dtype=dtype) self.with_mode(Mode(linker='c'), scalar.maximum, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.and_, dtype=dtype, self.with_mode(Mode(linker='c'), scalar.and_, dtype=dtype,
tensor_op=tensor.all) tensor_op=tensor.all)
self.with_linker(gof.CLinker(), scalar.or_, dtype=dtype, self.with_mode(Mode(linker='c'), scalar.or_, dtype=dtype,
tensor_op=tensor.any) tensor_op=tensor.any)
for dtype in ["bool", "int8", "uint8"]: for dtype in ["bool", "int8", "uint8"]:
self.with_linker(gof.CLinker(), scalar.or_, dtype=dtype) self.with_mode(Mode(linker='c'), scalar.or_, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.and_, dtype=dtype) self.with_mode(Mode(linker='c'), scalar.and_, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.xor, dtype=dtype) self.with_mode(Mode(linker='c'), scalar.xor, dtype=dtype)
@attr('slow') @attr('slow')
def test_c_nan(self): def test_c_nan(self):
if not theano.config.cxx: if not theano.config.cxx:
raise SkipTest("G++ not available, so we need to skip this test.") raise SkipTest("G++ not available, so we need to skip this test.")
for dtype in ["floatX", "complex64", "complex128"]: for dtype in ["floatX", "complex64", "complex128"]:
self.with_linker(gof.CLinker(), scalar.add, dtype=dtype, self.with_mode(Mode(linker='c'), scalar.add, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.CLinker(), scalar.mul, dtype=dtype, self.with_mode(Mode(linker='c'), scalar.mul, dtype=dtype,
test_nan=True) test_nan=True)
for dtype in ["floatX"]: for dtype in ["floatX"]:
self.with_linker(gof.CLinker(), scalar.minimum, dtype=dtype, self.with_mode(Mode(linker='c'), scalar.minimum, dtype=dtype,
test_nan=True) test_nan=True)
self.with_linker(gof.CLinker(), scalar.maximum, dtype=dtype, self.with_mode(Mode(linker='c'), scalar.maximum, dtype=dtype,
test_nan=True) test_nan=True)
def test_infer_shape(self, dtype=None, pre_scalar_op=None): def test_infer_shape(self, dtype=None, pre_scalar_op=None):
if dtype is None: if dtype is None:
...@@ -1241,16 +1248,6 @@ def test_gt_grad(): ...@@ -1241,16 +1248,6 @@ def test_gt_grad():
cost = (scores * (scores > 0)).sum() cost = (scores * (scores > 0)).sum()
T.grad(cost, input_) T.grad(cost, input_)
"""
if __name__ == '__main__':
#unittest.main()
suite = unittest.TestSuite([test_Prod('test_mul_without_zeros_zeros')])
#suite.addTest(test_Prod('test_verify_grad_with_zeros'))
#suite.addTest(test_Prod('test_prod_without_zeros'))
#suite.addTest(test_Prod('test_other_grad_tests'))
unittest.TextTestRunner().run(suite)
"""
def test_clip_grad(): def test_clip_grad():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论