提交 b6b797d7 authored 作者: Samira Ebrahimi Kahou's avatar Samira Ebrahimi Kahou

Modified test_opt.py to use check_stack_trace and modified check_stack_trace to…

Modified test_opt.py to use check_stack_trace and modified check_stack_trace to loop over outputs for ops_to_check=last and some refactoring
上级 e3569d12
...@@ -2667,7 +2667,7 @@ def check_stack_trace(f_or_fgraph, ops_to_check='last', bug_print='raise'): ...@@ -2667,7 +2667,7 @@ def check_stack_trace(f_or_fgraph, ops_to_check='last', bug_print='raise'):
# if ops_to_check is a string # if ops_to_check is a string
if isinstance(ops_to_check, string_types): if isinstance(ops_to_check, string_types):
if ops_to_check == 'last': if ops_to_check == 'last':
apply_nodes_to_check = [fgraph.outputs[0].owner] apply_nodes_to_check = [fgraph.outputs[i].owner for i in range(len(fgraph.outputs))]
elif ops_to_check == 'all': elif ops_to_check == 'all':
apply_nodes_to_check = fgraph.apply_nodes apply_nodes_to_check = fgraph.apply_nodes
else: else:
......
...@@ -61,6 +61,7 @@ from theano.tensor.elemwise import DimShuffle ...@@ -61,6 +61,7 @@ from theano.tensor.elemwise import DimShuffle
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
from theano.compile.mode import optdb from theano.compile.mode import optdb
from theano.compile import Mode from theano.compile import Mode
from theano.gof.opt import check_stack_trace
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
mode_opt = theano.config.mode mode_opt = theano.config.mode
...@@ -111,7 +112,7 @@ class test_dimshuffle_lift(unittest.TestCase): ...@@ -111,7 +112,7 @@ class test_dimshuffle_lift(unittest.TestCase):
dimshuffle_lift.optimize(g) dimshuffle_lift.optimize(g)
self.assertTrue(str(g) == "[x]") self.assertTrue(str(g) == "[x]")
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(g.outputs[0].tag, 'trace')) self.assertTrue(check_stack_trace(g, ops_to_check='all'))
def test_merge2(self): def test_merge2(self):
x, y, z = inputs() x, y, z = inputs()
...@@ -123,7 +124,7 @@ class test_dimshuffle_lift(unittest.TestCase): ...@@ -123,7 +124,7 @@ class test_dimshuffle_lift(unittest.TestCase):
dimshuffle_lift.optimize(g) dimshuffle_lift.optimize(g)
self.assertTrue(str(g) == "[DimShuffle{0,1,x,x}(x)]", str(g)) self.assertTrue(str(g) == "[DimShuffle{0,1,x,x}(x)]", str(g))
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(g.outputs[0].tag, 'trace')) self.assertTrue(check_stack_trace(g, ops_to_check='all'))
def test_elim3(self): def test_elim3(self):
x, y, z = inputs() x, y, z = inputs()
...@@ -136,7 +137,7 @@ class test_dimshuffle_lift(unittest.TestCase): ...@@ -136,7 +137,7 @@ class test_dimshuffle_lift(unittest.TestCase):
dimshuffle_lift.optimize(g) dimshuffle_lift.optimize(g)
self.assertTrue(str(g) == "[x]", str(g)) self.assertTrue(str(g) == "[x]", str(g))
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(g.outputs[0].tag, 'trace')) self.assertTrue(check_stack_trace(g, ops_to_check='all'))
def test_lift(self): def test_lift(self):
x, y, z = inputs([False] * 1, [False] * 2, [False] * 3) x, y, z = inputs([False] * 1, [False] * 2, [False] * 3)
...@@ -164,7 +165,7 @@ class test_dimshuffle_lift(unittest.TestCase): ...@@ -164,7 +165,7 @@ class test_dimshuffle_lift(unittest.TestCase):
self.assertTrue(str(g) in (opt_str_g_inplace, opt_str_g_noinplace), self.assertTrue(str(g) in (opt_str_g_inplace, opt_str_g_noinplace),
str(g)) str(g))
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(g.outputs[0].tag, 'trace')) self.assertTrue(check_stack_trace(g, ops_to_check='all'))
def test_recursive_lift(self): def test_recursive_lift(self):
v = T.vector(dtype="float64") v = T.vector(dtype="float64")
...@@ -191,7 +192,7 @@ class test_dimshuffle_lift(unittest.TestCase): ...@@ -191,7 +192,7 @@ class test_dimshuffle_lift(unittest.TestCase):
self.assertTrue(str(new_g) == opt_str_g) self.assertTrue(str(new_g) == opt_str_g)
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(new_g.outputs[0].tag, 'trace')) self.assertTrue(check_stack_trace(new_g, ops_to_check='all'))
def test_useless_dimshuffle(self): def test_useless_dimshuffle(self):
x, _, _ = inputs() x, _, _ = inputs()
...@@ -1649,8 +1650,8 @@ def test_local_useless_slice(): ...@@ -1649,8 +1650,8 @@ def test_local_useless_slice():
# Now test that the stack trace is copied over properly, # Now test that the stack trace is copied over properly,
# before before and after optimization. # before before and after optimization.
assert hasattr(f_unopt.outputs[0].variable.tag, 'trace') assert check_stack_trace(f_unopt, ops_to_check='all')
assert hasattr(f_opt.outputs[0].variable.tag, 'trace') assert check_stack_trace(f_opt, ops_to_check='all')
# test a 4d tensor # test a 4d tensor
z = tensor.tensor4('z') z = tensor.tensor4('z')
...@@ -1670,8 +1671,8 @@ def test_local_useless_slice(): ...@@ -1670,8 +1671,8 @@ def test_local_useless_slice():
# Finally, test that the stack trace is copied over properly, # Finally, test that the stack trace is copied over properly,
# before before and after optimization. # before before and after optimization.
assert hasattr(f_opt_check.outputs[0].variable.tag, 'trace') assert check_stack_trace(f_opt_check, ops_to_check='all')
assert hasattr(f_opt_check_apply.outputs[0].variable.tag, 'trace') assert check_stack_trace(f_opt_check_apply, ops_to_check='all')
def test_local_useless_inc_subtensor(): def test_local_useless_inc_subtensor():
x = tensor.matrix('x') x = tensor.matrix('x')
...@@ -1891,7 +1892,7 @@ class test_local_subtensor_make_vector(unittest.TestCase): ...@@ -1891,7 +1892,7 @@ class test_local_subtensor_make_vector(unittest.TestCase):
mode = theano.compile.mode.Mode(optimizer=None).including('canonicalize_db').including("local_subtensor_make_vector") mode = theano.compile.mode.Mode(optimizer=None).including('canonicalize_db').including("local_subtensor_make_vector")
f = function([x, y, z], v[0], mode=mode) f = function([x, y, z], v[0], mode=mode)
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(f.outputs[0].variable.tag, 'trace')) self.assertTrue(check_stack_trace(f, ops_to_check='all'))
#import ipdb; ipdb.set_trace() #import ipdb; ipdb.set_trace()
...@@ -1900,21 +1901,17 @@ class test_local_subtensor_make_vector(unittest.TestCase): ...@@ -1900,21 +1901,17 @@ class test_local_subtensor_make_vector(unittest.TestCase):
mode = theano.compile.mode.get_mode('FAST_COMPILE').including("local_subtensor_make_vector") mode = theano.compile.mode.get_mode('FAST_COMPILE').including("local_subtensor_make_vector")
f = function([x, y, z], v[0], mode=mode) f = function([x, y, z], v[0], mode=mode)
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(f.outputs[0].variable.tag, 'trace')) self.assertTrue(check_stack_trace(f, ops_to_check='all'))
class test_local_subtensor_lift(unittest.TestCase): class test_local_subtensor_lift(unittest.TestCase):
def _verify_stack_trace(self, f):
for output in f.outputs:
# Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(output.variable.tag, 'trace'))
def test0(self): def test0(self):
# basic test that the Op works # basic test that the Op works
x = tensor.matrix('x') x = tensor.matrix('x')
f = function([x], tensor.exp(x)[0], mode=mode_opt) f = function([x], tensor.exp(x)[0], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
prog = f.maker.fgraph.toposort() prog = f.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.Subtensor) # first subtensor assert isinstance(prog[0].op, tensor.Subtensor) # first subtensor
...@@ -1928,7 +1925,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -1928,7 +1925,8 @@ class test_local_subtensor_lift(unittest.TestCase):
x = tensor.matrix('x') x = tensor.matrix('x')
f = function([x], [tensor.exp(x)[0], tensor.exp(x)], mode=mode_opt) f = function([x], [tensor.exp(x)[0], tensor.exp(x)], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
prog = f.maker.fgraph.toposort() prog = f.maker.fgraph.toposort()
assert prog[0].op == tensor.exp assert prog[0].op == tensor.exp
...@@ -1944,7 +1942,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -1944,7 +1942,8 @@ class test_local_subtensor_lift(unittest.TestCase):
z = tensor.matrix('z') z = tensor.matrix('z')
f = function([x, y, z], tensor.exp(x + y + z)[0], mode=mode_opt) f = function([x, y, z], tensor.exp(x + y + z)[0], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
prog = f.maker.fgraph.toposort() prog = f.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.DimShuffle) assert isinstance(prog[0].op, tensor.DimShuffle)
...@@ -1963,7 +1962,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -1963,7 +1962,8 @@ class test_local_subtensor_lift(unittest.TestCase):
z = tensor.matrix('z') z = tensor.matrix('z')
f = function([x, y, z], tensor.exp(x + y + z)[0:2], mode=mode_opt) f = function([x, y, z], tensor.exp(x + y + z)[0:2], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
prog = f.maker.fgraph.toposort() prog = f.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.DimShuffle) assert isinstance(prog[0].op, tensor.DimShuffle)
...@@ -1981,7 +1981,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -1981,7 +1981,8 @@ class test_local_subtensor_lift(unittest.TestCase):
y = tensor.vector('y') y = tensor.vector('y')
f = function([y], tensor.exp(y.dimshuffle(0, 'x'))[0], mode=mode_opt) f = function([y], tensor.exp(y.dimshuffle(0, 'x'))[0], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
prog = f.maker.fgraph.toposort() prog = f.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.DimShuffle) assert isinstance(prog[0].op, tensor.DimShuffle)
...@@ -1998,7 +1999,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -1998,7 +1999,8 @@ class test_local_subtensor_lift(unittest.TestCase):
y = tensor.vector('y') y = tensor.vector('y')
f = function([x, y], tensor.exp(x + y)[0], mode=mode_opt) f = function([x, y], tensor.exp(x + y)[0], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
prog = f.maker.fgraph.toposort() prog = f.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.DimShuffle) assert isinstance(prog[0].op, tensor.DimShuffle)
...@@ -2016,7 +2018,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -2016,7 +2018,8 @@ class test_local_subtensor_lift(unittest.TestCase):
f = function([x, y], [tensor.exp(x + y)[0], tensor.exp(x + y) + x], f = function([x, y], [tensor.exp(x + y)[0], tensor.exp(x + y) + x],
mode=mode_opt) mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
prog = f.maker.fgraph.toposort() prog = f.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.DimShuffle) assert isinstance(prog[0].op, tensor.DimShuffle)
...@@ -2036,7 +2039,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -2036,7 +2039,8 @@ class test_local_subtensor_lift(unittest.TestCase):
y = tensor.scalar('y') y = tensor.scalar('y')
f = function([x, y], tensor.exp(x + y)[0], mode=mode_opt) f = function([x, y], tensor.exp(x + y)[0], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
prog = f.maker.fgraph.toposort() prog = f.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.Subtensor) assert isinstance(prog[0].op, tensor.Subtensor)
...@@ -2057,7 +2061,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -2057,7 +2061,8 @@ class test_local_subtensor_lift(unittest.TestCase):
assert newx.broadcastable == (True, False) assert newx.broadcastable == (True, False)
f1 = function([x], newx[:2, :5], mode=mode_opt) f1 = function([x], newx[:2, :5], mode=mode_opt)
self._verify_stack_trace(f1) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f1, ops_to_check='all'))
prog = f1.maker.fgraph.toposort() prog = f1.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.Subtensor) assert isinstance(prog[0].op, tensor.Subtensor)
assert isinstance(prog[1].op, tensor.Rebroadcast) assert isinstance(prog[1].op, tensor.Rebroadcast)
...@@ -2071,7 +2076,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -2071,7 +2076,8 @@ class test_local_subtensor_lift(unittest.TestCase):
assert newy.broadcastable == (True, False, True, False) assert newy.broadcastable == (True, False, True, False)
f2 = function([y], newy[:, 3, 0, :], mode=mode_opt) f2 = function([y], newy[:, 3, 0, :], mode=mode_opt)
self._verify_stack_trace(f2) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f2, ops_to_check='all'))
prog = f2.maker.fgraph.toposort() prog = f2.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.Subtensor) assert isinstance(prog[0].op, tensor.Subtensor)
assert isinstance(prog[1].op, tensor.Rebroadcast) assert isinstance(prog[1].op, tensor.Rebroadcast)
...@@ -2079,7 +2085,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -2079,7 +2085,8 @@ class test_local_subtensor_lift(unittest.TestCase):
# corner case 2: subtensor idx_list is shorter than resulting broadcast pattern # corner case 2: subtensor idx_list is shorter than resulting broadcast pattern
f3 = function([y], newy[:, 3, 0], mode=mode_opt) f3 = function([y], newy[:, 3, 0], mode=mode_opt)
self._verify_stack_trace(f3) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f3, ops_to_check='all'))
prog = f3.maker.fgraph.toposort() prog = f3.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.Subtensor) assert isinstance(prog[0].op, tensor.Subtensor)
assert isinstance(prog[1].op, tensor.Rebroadcast) assert isinstance(prog[1].op, tensor.Rebroadcast)
...@@ -2094,7 +2101,8 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -2094,7 +2101,8 @@ class test_local_subtensor_lift(unittest.TestCase):
out = newz[:, 3, 0] out = newz[:, 3, 0]
f4 = function([z], newz[:, 3, 0], mode=mode_opt) f4 = function([z], newz[:, 3, 0], mode=mode_opt)
self._verify_stack_trace(f4) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f4, ops_to_check='all'))
prog = f4.maker.fgraph.toposort() prog = f4.maker.fgraph.toposort()
assert isinstance(prog[0].op, tensor.Subtensor) assert isinstance(prog[0].op, tensor.Subtensor)
assert isinstance(prog[1].op, tensor.Rebroadcast) assert isinstance(prog[1].op, tensor.Rebroadcast)
...@@ -2102,11 +2110,6 @@ class test_local_subtensor_lift(unittest.TestCase): ...@@ -2102,11 +2110,6 @@ class test_local_subtensor_lift(unittest.TestCase):
class test_local_subtensor_merge(unittest.TestCase): class test_local_subtensor_merge(unittest.TestCase):
def _verify_stack_trace(self, f):
for output in f.outputs:
# Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(output.variable.tag, 'trace'))
def setUp(self): def setUp(self):
utt.seed_rng() utt.seed_rng()
self.x_shapes = [(2, 2), (5, 3), (4, 1), (1, 2), self.x_shapes = [(2, 2), (5, 3), (4, 1), (1, 2),
...@@ -2121,7 +2124,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2121,7 +2124,8 @@ class test_local_subtensor_merge(unittest.TestCase):
g = function([x], x[idx::][-1], mode=mode_opt.excluding( g = function([x], x[idx::][-1], mode=mode_opt.excluding(
'local_subtensor_merge')) 'local_subtensor_merge'))
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert len([t for t in topo assert len([t for t in topo
...@@ -2149,7 +2153,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2149,7 +2153,8 @@ class test_local_subtensor_merge(unittest.TestCase):
mode=mode_opt.excluding('local_subtensor_merge')) mode=mode_opt.excluding('local_subtensor_merge'))
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
# print [t for t in topo if isinstance(t.op, tensor.Subtensor)] # print [t for t in topo if isinstance(t.op, tensor.Subtensor)]
...@@ -2178,7 +2183,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2178,7 +2183,8 @@ class test_local_subtensor_merge(unittest.TestCase):
g = function([x], x[::-1][idx], g = function([x], x[::-1][idx],
mode=mode_opt.excluding('local_subtensor_merge')) mode=mode_opt.excluding('local_subtensor_merge'))
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
...@@ -2208,7 +2214,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2208,7 +2214,8 @@ class test_local_subtensor_merge(unittest.TestCase):
mode=mode_opt.excluding('local_subtensor_merge')) mode=mode_opt.excluding('local_subtensor_merge'))
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
# print [t for t in topo if isinstance(t.op, tensor.Subtensor)] # print [t for t in topo if isinstance(t.op, tensor.Subtensor)]
...@@ -2232,7 +2239,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2232,7 +2239,8 @@ class test_local_subtensor_merge(unittest.TestCase):
for idx in xrange(-9, 8): for idx in xrange(-9, 8):
f = function([x], x[::-1][:idx], mode=mode_opt) f = function([x], x[::-1][:idx], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
...@@ -2252,7 +2260,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2252,7 +2260,8 @@ class test_local_subtensor_merge(unittest.TestCase):
y = tensor.iscalar('y') y = tensor.iscalar('y')
f = function([x, y], x[::-1][:y], mode=mode_opt) f = function([x, y], x[::-1][:y], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
...@@ -2275,7 +2284,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2275,7 +2284,8 @@ class test_local_subtensor_merge(unittest.TestCase):
for idx2 in xrange(-7, 7): for idx2 in xrange(-7, 7):
f = function([x], x[idx1:][:idx2], mode=mode_opt) f = function([x], x[idx1:][:idx2], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
...@@ -2296,7 +2306,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2296,7 +2306,8 @@ class test_local_subtensor_merge(unittest.TestCase):
z = tensor.iscalar('y') z = tensor.iscalar('y')
f = function([x, y, z], x[y:][:z], mode=mode_opt) f = function([x, y, z], x[y:][:z], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
...@@ -2327,7 +2338,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2327,7 +2338,8 @@ class test_local_subtensor_merge(unittest.TestCase):
z = x[slice(*sl1)][slice(*sl2)] z = x[slice(*sl1)][slice(*sl2)]
f = function([x], z, mode=mode_opt) f = function([x], z, mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
x_val = self.rng.uniform(size=shape).astype(config.floatX) x_val = self.rng.uniform(size=shape).astype(config.floatX)
...@@ -2346,7 +2358,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2346,7 +2358,8 @@ class test_local_subtensor_merge(unittest.TestCase):
f = function([x, b1, e1, s1, b2, e2, s2], x[b1:e1:s1][b2:e2:s2], f = function([x, b1, e1, s1, b2, e2, s2], x[b1:e1:s1][b2:e2:s2],
mode=mode_opt) mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
...@@ -2433,7 +2446,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2433,7 +2446,8 @@ class test_local_subtensor_merge(unittest.TestCase):
i = tensor.iscalar('i') i = tensor.iscalar('i')
f = function([x, b, e, s, i], x[b:e:s][i], mode=mode_opt) f = function([x, b, e, s, i], x[b:e:s][i], mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
...@@ -2526,8 +2540,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2526,8 +2540,8 @@ class test_local_subtensor_merge(unittest.TestCase):
sub_x = x[slice1][slice2] sub_x = x[slice1][slice2]
f = theano.function([x] + input_vars, sub_x, mode=mode_opt) f = theano.function([x] + input_vars, sub_x, mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
# print [t for t in topo if isinstance(t.op, tensor.Subtensor)] # print [t for t in topo if isinstance(t.op, tensor.Subtensor)]
...@@ -2586,7 +2600,8 @@ class test_local_subtensor_merge(unittest.TestCase): ...@@ -2586,7 +2600,8 @@ class test_local_subtensor_merge(unittest.TestCase):
sub_x = x[symbol_slice][i] sub_x = x[symbol_slice][i]
f = theano.function([x] + input_vars, sub_x, mode=mode_opt) f = theano.function([x] + input_vars, sub_x, mode=mode_opt)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='last'))
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
# print [t for t in topo if isinstance(t.op, tensor.Subtensor)] # print [t for t in topo if isinstance(t.op, tensor.Subtensor)]
...@@ -2718,14 +2733,14 @@ class test_local_adv_sub1_adv_inc_sub1(unittest.TestCase): ...@@ -2718,14 +2733,14 @@ class test_local_adv_sub1_adv_inc_sub1(unittest.TestCase):
mode = theano.compile.mode.Mode(optimizer=None).including('canonicalize').including("local_adv_sub1_adv_inc_sub1") mode = theano.compile.mode.Mode(optimizer=None).including('canonicalize').including("local_adv_sub1_adv_inc_sub1")
f = theano.function([x, y, idx], o, self.mode) f = theano.function([x, y, idx], o, self.mode)
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(f.outputs[0].variable.tag, 'trace')) self.assertTrue(check_stack_trace(f, ops_to_check='all'))
# Compile function using all optimizations in fast_compile mode, # Compile function using all optimizations in fast_compile mode,
# including the 'local_subtensor_make_vector' optimization # including the 'local_subtensor_make_vector' optimization
mode = theano.compile.mode.get_mode('FAST_COMPILE').including("local_adv_sub1_adv_inc_sub1") mode = theano.compile.mode.get_mode('FAST_COMPILE').including("local_adv_sub1_adv_inc_sub1")
f = theano.function([x, y, idx], o, self.mode) f = theano.function([x, y, idx], o, self.mode)
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(f.outputs[0].variable.tag, 'trace')) self.assertTrue(check_stack_trace(f, ops_to_check='all'))
class Test_alloc_zero(unittest.TestCase): class Test_alloc_zero(unittest.TestCase):
...@@ -2930,7 +2945,7 @@ def test_local_IncSubtensor_serialize(): ...@@ -2930,7 +2945,7 @@ def test_local_IncSubtensor_serialize():
# Now test that the stack trace is copied over properly, # Now test that the stack trace is copied over properly,
# if we return the gradients. We need to use same mode as before. # if we return the gradients. We need to use same mode as before.
f = theano.function([i, j, t], dW, mode=mode) f = theano.function([i, j, t], dW, mode=mode)
assert hasattr(f.outputs[0].variable.tag, 'trace') assert check_stack_trace(f, ops_to_check='all')
def test_local_set_to_inc_subtensor(): def test_local_set_to_inc_subtensor():
v = theano.tensor.fmatrix() v = theano.tensor.fmatrix()
...@@ -2962,8 +2977,8 @@ def test_local_set_to_inc_subtensor(): ...@@ -2962,8 +2977,8 @@ def test_local_set_to_inc_subtensor():
# Finally, test that the stack trace is copied over properly, # Finally, test that the stack trace is copied over properly,
# before before and after optimization. # before before and after optimization.
assert hasattr(f1.outputs[0].variable.tag, 'trace') assert check_stack_trace(f1, ops_to_check='all')
assert hasattr(f2.outputs[0].variable.tag, 'trace') assert check_stack_trace(f2, ops_to_check='all')
def test_local_subtensor_of_dot(): def test_local_subtensor_of_dot():
...@@ -2998,14 +3013,14 @@ def test_local_subtensor_of_dot(): ...@@ -2998,14 +3013,14 @@ def test_local_subtensor_of_dot():
f = theano.function([m1, m2, idx], theano.dot(m1, m2)[idx, 1:4, :, idx:], mode=mode) f = theano.function([m1, m2, idx], theano.dot(m1, m2)[idx, 1:4, :, idx:], mode=mode)
assert test_equality(f(d1, d2, 1), numpy.dot(d1, d2)[1, 1:4, :, 1:]) assert test_equality(f(d1, d2, 1), numpy.dot(d1, d2)[1, 1:4, :, 1:])
# if we return the gradients. We need to use same mode as before. # if we return the gradients. We need to use same mode as before.
assert hasattr(f.outputs[0].variable.tag, 'trace') assert check_stack_trace(f, ops_to_check='last')
f = theano.function([m1, m2, idx], theano.dot(m1, m2)[1:4, :, idx:, idx], mode=mode) f = theano.function([m1, m2, idx], theano.dot(m1, m2)[1:4, :, idx:, idx], mode=mode)
assert test_equality(f(d1, d2, 1), numpy.dot(d1, d2)[1:4, :, 1:, 1]) assert test_equality(f(d1, d2, 1), numpy.dot(d1, d2)[1:4, :, 1:, 1])
# Now test that the stack trace is copied over properly, # Now test that the stack trace is copied over properly,
# if we return the gradients. We need to use same mode as before. # if we return the gradients. We need to use same mode as before.
assert hasattr(f.outputs[0].variable.tag, 'trace') assert check_stack_trace(f, ops_to_check='last')
class Test_local_elemwise_alloc(unittest.TestCase): class Test_local_elemwise_alloc(unittest.TestCase):
...@@ -3052,11 +3067,6 @@ class Test_local_elemwise_alloc(unittest.TestCase): ...@@ -3052,11 +3067,6 @@ class Test_local_elemwise_alloc(unittest.TestCase):
if elem.op is not None]) == count if elem.op is not None]) == count
) )
def _verify_stack_trace(self, f):
for output in f.outputs:
# Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(output.variable.tag, 'trace'))
def test_remove_alloc_wo_dimshuffle(self): def test_remove_alloc_wo_dimshuffle(self):
# No optimization on alloc # No optimization on alloc
func = function( func = function(
...@@ -3066,7 +3076,8 @@ class Test_local_elemwise_alloc(unittest.TestCase): ...@@ -3066,7 +3076,8 @@ class Test_local_elemwise_alloc(unittest.TestCase):
) )
self._verify_alloc_count(func, 1) self._verify_alloc_count(func, 1)
self._verify_assert_count(func, 0) self._verify_assert_count(func, 0)
self._verify_stack_trace(func) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(func, ops_to_check='all'))
# Optimization on alloc with assert # Optimization on alloc with assert
func = function( func = function(
...@@ -3509,11 +3520,6 @@ class Test_local_useless_elemwise_comparison(unittest.TestCase): ...@@ -3509,11 +3520,6 @@ class Test_local_useless_elemwise_comparison(unittest.TestCase):
class Test_local_useless_alloc(unittest.TestCase): class Test_local_useless_alloc(unittest.TestCase):
def _verify_stack_trace(self, f):
for output in f.outputs:
# Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(output.variable.tag, 'trace'))
def setUp(self): def setUp(self):
self.rng = numpy.random.RandomState(utt.fetch_seed()) self.rng = numpy.random.RandomState(utt.fetch_seed())
...@@ -3534,7 +3540,8 @@ class Test_local_useless_alloc(unittest.TestCase): ...@@ -3534,7 +3540,8 @@ class Test_local_useless_alloc(unittest.TestCase):
if isinstance(mode_opt, compile.DebugMode): if isinstance(mode_opt, compile.DebugMode):
self.assertRaises(ValueError, f) self.assertRaises(ValueError, f)
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
def test1(self): def test1(self):
# Test that alloc never gets instantiated during optimization # Test that alloc never gets instantiated during optimization
...@@ -3549,7 +3556,8 @@ class Test_local_useless_alloc(unittest.TestCase): ...@@ -3549,7 +3556,8 @@ class Test_local_useless_alloc(unittest.TestCase):
op_classes = [node.op.__class__ for node in f.maker.fgraph.toposort()] op_classes = [node.op.__class__ for node in f.maker.fgraph.toposort()]
assert tensor.Alloc not in op_classes assert tensor.Alloc not in op_classes
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
def test2(self): def test2(self):
# Test that alloc never gets instantiated during optimization # Test that alloc never gets instantiated during optimization
...@@ -3569,17 +3577,13 @@ class Test_local_useless_alloc(unittest.TestCase): ...@@ -3569,17 +3577,13 @@ class Test_local_useless_alloc(unittest.TestCase):
# in op_classes and we have to change the assert. # in op_classes and we have to change the assert.
assert tensor.Alloc in op_classes assert tensor.Alloc in op_classes
self._verify_stack_trace(f) # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(check_stack_trace(f, ops_to_check='all'))
class Test_local_useless_inc_subtensor_alloc(unittest.TestCase): class Test_local_useless_inc_subtensor_alloc(unittest.TestCase):
opt_name = 'local_useless_inc_subtensor_alloc' opt_name = 'local_useless_inc_subtensor_alloc'
def _verify_stack_trace(self, f):
for output in f.outputs:
# Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(output.variable.tag, 'trace'))
def setUp(self): def setUp(self):
# The optimization requires the shape feature so we need to compile in # The optimization requires the shape feature so we need to compile in
# FAST_RUN mode. # FAST_RUN mode.
...@@ -3617,8 +3621,9 @@ class Test_local_useless_inc_subtensor_alloc(unittest.TestCase): ...@@ -3617,8 +3621,9 @@ class Test_local_useless_inc_subtensor_alloc(unittest.TestCase):
utt.assert_allclose(r1, r2) utt.assert_allclose(r1, r2)
self._verify_stack_trace(f1) # Check stacktrace was copied over correctly after opt was applied
self._verify_stack_trace(f2) self.assertTrue(check_stack_trace(f1, ops_to_check='all'))
self.assertTrue(check_stack_trace(f2, ops_to_check='all'))
def test_advanced_inc_subtensor1(self): def test_advanced_inc_subtensor1(self):
...@@ -3650,8 +3655,9 @@ class Test_local_useless_inc_subtensor_alloc(unittest.TestCase): ...@@ -3650,8 +3655,9 @@ class Test_local_useless_inc_subtensor_alloc(unittest.TestCase):
utt.assert_allclose(r1, r2) utt.assert_allclose(r1, r2)
self._verify_stack_trace(f1) # Check stacktrace was copied over correctly after opt was applied
self._verify_stack_trace(f2) self.assertTrue(check_stack_trace(f1, ops_to_check='all'))
self.assertTrue(check_stack_trace(f2, ops_to_check='all'))
def test_incsubtensor(self): def test_incsubtensor(self):
x = tensor.vector('x') x = tensor.vector('x')
...@@ -3679,8 +3685,9 @@ class Test_local_useless_inc_subtensor_alloc(unittest.TestCase): ...@@ -3679,8 +3685,9 @@ class Test_local_useless_inc_subtensor_alloc(unittest.TestCase):
utt.assert_allclose(r1, r2) utt.assert_allclose(r1, r2)
self._verify_stack_trace(f1) # Check stacktrace was copied over correctly after opt was applied
self._verify_stack_trace(f2) self.assertTrue(check_stack_trace(f1, ops_to_check='last'))
self.assertTrue(check_stack_trace(f2, ops_to_check='last'))
class test_shapeoptimizer(unittest.TestCase): class test_shapeoptimizer(unittest.TestCase):
...@@ -4050,7 +4057,7 @@ class T_Tile(unittest.TestCase): ...@@ -4050,7 +4057,7 @@ class T_Tile(unittest.TestCase):
f(data) f(data)
# Check that stacktrace is copied over # Check that stacktrace is copied over
self.assertTrue(hasattr(f.outputs[0].variable.tag, 'trace')) self.assertTrue(check_stack_trace(f, ops_to_check='all'))
self.assertTrue(len(f.outputs[0].variable.tag.trace)>0) self.assertTrue(len(f.outputs[0].variable.tag.trace)>0)
...@@ -4190,7 +4197,7 @@ class T_Rebroadcast(unittest.TestCase): ...@@ -4190,7 +4197,7 @@ class T_Rebroadcast(unittest.TestCase):
e = f.maker.fgraph.toposort() e = f.maker.fgraph.toposort()
assert len([n for n in e if isinstance(n.op, T.Rebroadcast)]) == 0 assert len([n for n in e if isinstance(n.op, T.Rebroadcast)]) == 0
assert hasattr(f.outputs[0].variable.tag, 'trace') assert check_stack_trace(f, ops_to_check='all')
def test_rebroadcast_rebroadcast(self): def test_rebroadcast_rebroadcast(self):
mode = theano.compile.get_default_mode().including('canonicalize') mode = theano.compile.get_default_mode().including('canonicalize')
...@@ -5818,8 +5825,7 @@ def test_local_join_make_vector(): ...@@ -5818,8 +5825,7 @@ def test_local_join_make_vector():
for n in e if isinstance(n.op, Join)]) for n in e if isinstance(n.op, Join)])
assert f.maker.fgraph.outputs[0].dtype == config.floatX assert f.maker.fgraph.outputs[0].dtype == config.floatX
assert hasattr(f.outputs[0].variable, 'tag') assert check_stack_trace(f, ops_to_check='all')
assert hasattr(f.outputs[0].variable.tag, 'trace')
def test_local_add_specialize(): def test_local_add_specialize():
...@@ -5922,9 +5928,9 @@ def test_local_useless_split(): ...@@ -5922,9 +5928,9 @@ def test_local_useless_split():
assert isinstance(graph_nonopt[0].op, tensor.Split) assert isinstance(graph_nonopt[0].op, tensor.Split)
# Check that stacktraces have been copied over properly # Check that stacktraces have been copied over properly
assert hasattr(f_opt.outputs[0].variable.tag, 'trace') assert check_stack_trace(f_opt, ops_to_check='all')
assert len(f_opt.outputs[0].variable.tag.trace) > 0 assert len(f_opt.outputs[0].variable.tag.trace) > 0
assert hasattr(f_nonopt.outputs[0].variable.tag, 'trace') assert check_stack_trace(f_nonopt, ops_to_check='all')
assert len(f_nonopt.outputs[0].variable.tag.trace) > 0 assert len(f_nonopt.outputs[0].variable.tag.trace) > 0
...@@ -5998,7 +6004,7 @@ class Test_lift_transpose_through_dot(unittest.TestCase): ...@@ -5998,7 +6004,7 @@ class Test_lift_transpose_through_dot(unittest.TestCase):
sg = '[dot(DimShuffle{1,0}(b), DimShuffle{1,0}(a))]' sg = '[dot(DimShuffle{1,0}(b), DimShuffle{1,0}(a))]'
assert str(g) == sg, (str(g), sg) assert str(g) == sg, (str(g), sg)
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(g.outputs[0].tag, 'trace')) self.assertTrue(check_stack_trace(g, ops_to_check='all'))
def test_row_matrix(self): def test_row_matrix(self):
a = vector('a') a = vector('a')
...@@ -6010,7 +6016,7 @@ class Test_lift_transpose_through_dot(unittest.TestCase): ...@@ -6010,7 +6016,7 @@ class Test_lift_transpose_through_dot(unittest.TestCase):
sg = '[dot(DimShuffle{1,0}(b), DimShuffle{0,x}(a))]' sg = '[dot(DimShuffle{1,0}(b), DimShuffle{0,x}(a))]'
assert str(g) == sg, (str(g), sg) assert str(g) == sg, (str(g), sg)
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(g.outputs[0].tag, 'trace')) self.assertTrue(check_stack_trace(g, ops_to_check='all'))
def test_matrix_col(self): def test_matrix_col(self):
a = vector('a') a = vector('a')
...@@ -6022,7 +6028,7 @@ class Test_lift_transpose_through_dot(unittest.TestCase): ...@@ -6022,7 +6028,7 @@ class Test_lift_transpose_through_dot(unittest.TestCase):
sg = '[dot(DimShuffle{x,0}(a), DimShuffle{1,0}(b))]' sg = '[dot(DimShuffle{x,0}(a), DimShuffle{1,0}(b))]'
assert str(g) == sg, (str(g), sg) assert str(g) == sg, (str(g), sg)
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
self.assertTrue(hasattr(g.outputs[0].tag, 'trace')) self.assertTrue(check_stack_trace(g, ops_to_check='all'))
def test_local_upcast_elemwise_constant_inputs(): def test_local_upcast_elemwise_constant_inputs():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论