提交 d95e8b6c authored 作者: James Bergstra's avatar James Bergstra

renamed _test_ext

上级 213db8fd
...@@ -8,6 +8,7 @@ from op import Op ...@@ -8,6 +8,7 @@ from op import Op
from opt import * from opt import *
from ext import * from ext import *
import destroyhandler
from env import Env, InconsistencyError from env import Env, InconsistencyError
from toolbox import ReplaceValidate from toolbox import ReplaceValidate
...@@ -79,7 +80,9 @@ _Env = Env ...@@ -79,7 +80,9 @@ _Env = Env
def Env(inputs, outputs, validate = True): def Env(inputs, outputs, validate = True):
e = _Env(inputs, outputs) e = _Env(inputs, outputs)
##e.extend(EquivTool(e)) ##e.extend(EquivTool(e))
e.extend(DestroyHandler())
e.extend(destroyhandler.DestroyHandler())
e.extend(ReplaceValidate()) e.extend(ReplaceValidate())
if validate: if validate:
e.validate() e.validate()
...@@ -95,26 +98,65 @@ class FailureWatch: ...@@ -95,26 +98,65 @@ class FailureWatch:
self.failures += 1 self.failures += 1
class _test_all(unittest.TestCase): class _test_protocol(unittest.TestCase):
def test_misc(self):
def test_multi_destroyers(self):
x, y, z = inputs() x, y, z = inputs()
e = add(add_in_place(x, y), add_in_place(x, y)) e = transpose_view(transpose_view(transpose_view(transpose_view(x))))
try: g = Env([x,y,z], [e])
g = Env([x,y,z], [e]) assert g.consistent()
self.fail() chk = g.checkpoint()
except InconsistencyError, e: PatternOptimizer((transpose_view, (transpose_view, 'x')), 'x').optimize(g)
pass assert str(g) == "[x]"
new_e = add(x,y)
g.replace_validate(x, new_e)
assert str(g) == "[Add(x, y)]"
g.replace(new_e, dot(add_in_place(x,y), transpose_view(x)))
assert str(g) == "[Dot(AddInPlace(x, y), TransposeView(x))]"
assert not g.consistent()
g.revert(chk)
assert g.consistent()
assert str(g) == "[TransposeView(TransposeView(TransposeView(TransposeView(x))))]"
def test_multi_destroyers_through_views(self):
class _test_protocol_skip(unittest.TestCase):
def test_aliased_inputs_replacement(self):
x, y, z = inputs() x, y, z = inputs()
e = dot(add(transpose_view(z), y), add(z, x)) tv = transpose_view(x)
g = Env([x,y,z], [e]) tvv = transpose_view(tv)
sx = sigmoid(x)
e = add_in_place(x, tv)
g = Env([x, y], [e], False)
assert not g.consistent()
g.replace(tv, sx)
assert g.consistent() assert g.consistent()
fail = FailureWatch() g.replace(sx, tv)
OpSubOptimizer(add, add_in_place, fail).optimize(g) assert not g.consistent()
g.replace(tv, tvv)
assert not g.consistent()
g.replace(tv, sx)
assert g.consistent() assert g.consistent()
assert fail.failures == 1 # should have succeeded once and failed once def test_indestructible(self):
x, y, z = inputs()
x.tag.indestructible = True
x = copy(x)
assert x.tag.indestructible # checking if indestructible survives the copy!
e = add_in_place(x, y)
g = Env([x,y,z], [e], False)
assert not g.consistent()
g.replace_validate(e, add(x, y))
assert g.consistent()
def test_usage_loop_through_views_2(self):
x, y, z = inputs()
e0 = transpose_view(transpose_view(sigmoid(x)))
e = dot(add_in_place(x,y), transpose_view(e0))
g = Env([x,y,z], [e])
assert g.consistent() # because sigmoid can do the copy
# print g
# print g.destroy_handler.children
g.replace(e0, x)
assert not g.consistent() # we cut off the path to the sigmoid
def test_destroyers_loop(self): def test_destroyers_loop(self):
# AddInPlace(x, y) and AddInPlace(y, x) should not coexist # AddInPlace(x, y) and AddInPlace(y, x) should not coexist
...@@ -142,86 +184,32 @@ class _test_all(unittest.TestCase): ...@@ -142,86 +184,32 @@ class _test_all(unittest.TestCase):
pass pass
assert g.consistent() assert g.consistent()
def test_long_destroyers_loop(self):
x, y, z = inputs()
e = dot(dot(add_in_place(x,y), add_in_place(y,z)), add(z,x))
g = Env([x,y,z], [e])
assert g.consistent()
OpSubOptimizer(add, add_in_place).optimize(g)
assert g.consistent()
assert str(g) != "[Dot(Dot(AddInPlace(x, y), AddInPlace(y, z)), AddInPlace(z, x))]" # we don't want to see that!
e2 = dot(dot(add_in_place(x,y), add_in_place(y,z)), add_in_place(z,x))
try:
g2 = Env(*graph.clone([x,y,z], [e2]))
self.fail()
except InconsistencyError:
pass
def test_usage_loop(self): class _test_0(unittest.TestCase):
x, y, z = inputs()
g = Env([x,y,z], [dot(add_in_place(x, z), x)], False)
assert not g.consistent()
OpSubOptimizer(add_in_place, add).optimize(g) # replace add_in_place with add
assert g.consistent()
def test_usage_loop_through_views(self): def test_aliased_inputs(self):
x, y, z = inputs() x, y, z = inputs()
aip = add_in_place(x, y) e = add_in_place(x, x)
e = dot(aip, transpose_view(x)) g = Env([x], [e], False)
g = Env([x,y,z], [e], False)
assert not g.consistent() assert not g.consistent()
g.replace_validate(aip, add(x, z))
assert g.consistent()
def test_usage_loop_through_views_2(self): def test_aliased_inputs2(self):
x, y, z = inputs()
e0 = transpose_view(transpose_view(sigmoid(x)))
e = dot(add_in_place(x,y), transpose_view(e0))
g = Env([x,y,z], [e])
assert g.consistent() # because sigmoid can do the copy
# print g
# print g.destroy_handler.children
g.replace(e0, x)
assert not g.consistent() # we cut off the path to the sigmoid
def test_usage_loop_insert_views(self):
x, y, z = inputs() x, y, z = inputs()
e = dot(add_in_place(x, add(y, z)), sigmoid(sigmoid(sigmoid(sigmoid(sigmoid(x)))))) e = add_in_place(x, transpose_view(x))
g = Env([x,y,z], [e]) g = Env([x], [e], False)
assert g.consistent() assert not g.consistent()
fail = FailureWatch()
OpSubOptimizer(sigmoid, transpose_view, fail).optimize(g)
assert g.consistent()
assert fail.failures == 1 # it must keep one sigmoid in the long sigmoid chain
def test_misc(self): def test_aliased_inputs_tolerate(self):
x, y, z = inputs() x, y, z = inputs()
e = transpose_view(transpose_view(transpose_view(transpose_view(x)))) e = add_in_place_2(x, x)
g = Env([x,y,z], [e]) g = Env([x], [e], False)
assert g.consistent()
chk = g.checkpoint()
PatternOptimizer((transpose_view, (transpose_view, 'x')), 'x').optimize(g)
assert str(g) == "[x]"
new_e = add(x,y)
g.replace_validate(x, new_e)
assert str(g) == "[Add(x, y)]"
g.replace(new_e, dot(add_in_place(x,y), transpose_view(x)))
assert str(g) == "[Dot(AddInPlace(x, y), TransposeView(x))]"
assert not g.consistent()
g.revert(chk)
assert g.consistent() assert g.consistent()
assert str(g) == "[TransposeView(TransposeView(TransposeView(TransposeView(x))))]"
def test_indestructible(self): def test_aliased_inputs_tolerate2(self):
x, y, z = inputs() x, y, z = inputs()
x.tag.indestructible = True e = add_in_place_2(x, transpose_view(x))
x = copy(x) g = Env([x], [e], False)
assert x.tag.indestructible # checking if indestructible survives the copy!
e = add_in_place(x, y)
g = Env([x,y,z], [e], False)
assert not g.consistent() assert not g.consistent()
g.replace_validate(e, add(x, y))
assert g.consistent()
def test_indestructible_through_views(self): def test_indestructible_through_views(self):
x, y, z = inputs() x, y, z = inputs()
...@@ -233,17 +221,6 @@ class _test_all(unittest.TestCase): ...@@ -233,17 +221,6 @@ class _test_all(unittest.TestCase):
g.replace_validate(tv, sigmoid(x)) g.replace_validate(tv, sigmoid(x))
assert g.consistent() assert g.consistent()
def test_repair_destroy_path(self):
x, y, z = inputs()
e1 = transpose_view(transpose_view(x))
e2 = transpose_view(transpose_view(e1))
e3 = add_in_place(e2, y)
e4 = add_in_place(e1, z)
g = Env([x,y,z], [e3, e4], False)
assert not g.consistent()
g.replace(e2, transpose_view(x))
assert not g.consistent()
def test_indirect(self): def test_indirect(self):
x, y, z = inputs() x, y, z = inputs()
e0 = add_in_place(x, y) e0 = add_in_place(x, y)
...@@ -266,46 +243,86 @@ class _test_all(unittest.TestCase): ...@@ -266,46 +243,86 @@ class _test_all(unittest.TestCase):
g.replace(e0, new_e0) g.replace(e0, new_e0)
assert g.consistent() assert g.consistent()
def test_aliased_inputs(self): def test_long_destroyers_loop(self):
x, y, z = inputs() x, y, z = inputs()
e = add_in_place(x, x) e = dot(dot(add_in_place(x,y), add_in_place(y,z)), add(z,x))
g = Env([x], [e], False) g = Env([x,y,z], [e])
assert not g.consistent() assert g.consistent()
OpSubOptimizer(add, add_in_place).optimize(g)
assert g.consistent()
assert str(g) != "[Dot(Dot(AddInPlace(x, y), AddInPlace(y, z)), AddInPlace(z, x))]" # we don't want to see that!
e2 = dot(dot(add_in_place(x,y), add_in_place(y,z)), add_in_place(z,x))
try:
g2 = Env(*graph.clone([x,y,z], [e2]))
self.fail()
except InconsistencyError:
pass
def test_aliased_inputs2(self): def test_misc_2(self):
x, y, z = inputs() x, y, z = inputs()
e = add_in_place(x, transpose_view(x)) tv = transpose_view(x)
g = Env([x], [e], False) e = add_in_place(x, tv)
g = Env([x,y], [e], False)
assert not g.consistent()
g.replace(tv, x)
assert not g.consistent() assert not g.consistent()
def test_aliased_inputs_tolerate(self): def test_multi_destroyers(self):
x, y, z = inputs() x, y, z = inputs()
e = add_in_place_2(x, x) e = add(add_in_place(x, y), add_in_place(x, y))
g = Env([x], [e], False) try:
g = Env([x,y,z], [e])
self.fail()
except InconsistencyError, e:
pass
def test_multi_destroyers_through_views(self):
x, y, z = inputs()
e = dot(add(transpose_view(z), y), add(z, x))
g = Env([x,y,z], [e])
assert g.consistent()
fail = FailureWatch()
OpSubOptimizer(add, add_in_place, fail).optimize(g)
assert g.consistent() assert g.consistent()
assert fail.failures == 1 # should have succeeded once and failed once
def test_aliased_inputs_tolerate2(self):
def test_repair_destroy_path(self):
x, y, z = inputs() x, y, z = inputs()
e = add_in_place_2(x, transpose_view(x)) e1 = transpose_view(transpose_view(x))
g = Env([x], [e], False) e2 = transpose_view(transpose_view(e1))
e3 = add_in_place(e2, y)
e4 = add_in_place(e1, z)
g = Env([x,y,z], [e3, e4], False)
assert not g.consistent()
g.replace(e2, transpose_view(x))
assert not g.consistent() assert not g.consistent()
def test_aliased_inputs_replacement(self): def test_usage_loop(self):
x, y, z = inputs() x, y, z = inputs()
tv = transpose_view(x) g = Env([x,y,z], [dot(add_in_place(x, z), x)], False)
tvv = transpose_view(tv)
sx = sigmoid(x)
e = add_in_place(x, tv)
g = Env([x, y], [e], False)
assert not g.consistent() assert not g.consistent()
g.replace(tv, sx) OpSubOptimizer(add_in_place, add).optimize(g) # replace add_in_place with add
assert g.consistent() assert g.consistent()
g.replace(sx, tv)
assert not g.consistent() def test_usage_loop_through_views(self):
g.replace(tv, tvv) x, y, z = inputs()
aip = add_in_place(x, y)
e = dot(aip, transpose_view(x))
g = Env([x,y,z], [e], False)
assert not g.consistent() assert not g.consistent()
g.replace(tv, sx) g.replace_validate(aip, add(x, z))
assert g.consistent()
def test_usage_loop_insert_views(self):
x, y, z = inputs()
e = dot(add_in_place(x, add(y, z)), sigmoid(sigmoid(sigmoid(sigmoid(sigmoid(x))))))
g = Env([x,y,z], [e])
assert g.consistent()
fail = FailureWatch()
OpSubOptimizer(sigmoid, transpose_view, fail).optimize(g)
assert g.consistent() assert g.consistent()
assert fail.failures == 1 # it must keep one sigmoid in the long sigmoid chain
def test_value_repl(self): def test_value_repl(self):
x, y, z = inputs() x, y, z = inputs()
...@@ -325,17 +342,17 @@ class _test_all(unittest.TestCase): ...@@ -325,17 +342,17 @@ class _test_all(unittest.TestCase):
g.replace(sy, transpose_view(MyValue("abc"))) g.replace(sy, transpose_view(MyValue("abc")))
assert g.consistent() assert g.consistent()
def test_misc_2(self): pass
x, y, z = inputs()
tv = transpose_view(x)
e = add_in_place(x, tv)
g = Env([x,y], [e], False)
assert not g.consistent()
g.replace(tv, x)
assert not g.consistent()
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() if 1:
unittest.main()
else:
testcase = _test_protocol
suite = unittest.TestLoader()
suite = suite.loadTestsFromTestCase(testcase)
unittest.TextTestRunner(verbosity=2).run(suite)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论