提交 51bb1168 authored 作者: James Bergstra's avatar James Bergstra

s/OptCheck/DebugMode

上级 5f8a6a62
""" Provides `OptCheck` """ Provides `DebugMode`
""" """
...@@ -25,17 +25,10 @@ from ..compile.function_module import (convert_function_input, ...@@ -25,17 +25,10 @@ from ..compile.function_module import (convert_function_input,
SymbolicOutput, SymbolicOutput,
Supervisor) Supervisor)
def values_eq_enough_warn(a, b, r): class DebugModeError(Exception):
# TODO: use the result variables r to retrieve a Type, and then use the Type's value_cmp
# function: see ticket on value_cmp
print >> sys.stderr, 'WARNING: OptCheck comparison of', type(new_r_val), 'NotImplementedError'
return True
class OptCheckError(Exception):
pass pass
class BadClinkerOutput(OptCheckError): class BadClinkerOutput(DebugModeError):
"""Exception: a c implementation and python implementation don't agree""" """Exception: a c implementation and python implementation don't agree"""
r = None r = None
...@@ -54,7 +47,7 @@ class BadClinkerOutput(OptCheckError): ...@@ -54,7 +47,7 @@ class BadClinkerOutput(OptCheckError):
self.a = a self.a = a
self.b = b self.b = b
class BadOptimization(OptCheckError): class BadOptimization(DebugModeError):
"""Exception: some result and its substitute take different runtime values.""" """Exception: some result and its substitute take different runtime values."""
new_r = None new_r = None
...@@ -272,7 +265,7 @@ def optcheck_env(input_specs, output_specs, accept_inplace = False): ...@@ -272,7 +265,7 @@ def optcheck_env(input_specs, output_specs, accept_inplace = False):
return env, map(SymbolicOutput, updates), equivalence_tracker return env, map(SymbolicOutput, updates), equivalence_tracker
class OptCheckLinker(gof.link.LocalLinker): class DebugModeLinker(gof.link.LocalLinker):
def __init__(self, maker): def __init__(self, maker):
super(gof.LocalLinker, self).__init__() super(gof.LocalLinker, self).__init__()
self.env = None self.env = None
...@@ -280,7 +273,7 @@ class OptCheckLinker(gof.link.LocalLinker): ...@@ -280,7 +273,7 @@ class OptCheckLinker(gof.link.LocalLinker):
def accept(self, env, no_recycling = []): def accept(self, env, no_recycling = []):
if self.env is not None and self.env is not env: if self.env is not None and self.env is not env:
assert type(self) is OptCheckLinker assert type(self) is DebugModeLinker
return type(self)(self.env, self.maker).accept(env, no_recycling) return type(self)(self.env, self.maker).accept(env, no_recycling)
self.env = env self.env = env
self.no_recycling = no_recycling self.no_recycling = no_recycling
...@@ -449,7 +442,7 @@ class OptCheckLinker(gof.link.LocalLinker): ...@@ -449,7 +442,7 @@ class OptCheckLinker(gof.link.LocalLinker):
thunks_py, order thunks_py, order
NODEFAULT = ['NODEFAULT'] NODEFAULT = ['NODEFAULT']
class OptCheckFunctionMaker(FunctionMaker): #inheritance buys a few helper functions class DebugModeFunctionMaker(FunctionMaker): #inheritance buys a few helper functions
verbose = 0 verbose = 0
"""Verbosity level of compile-time and run-time checks. (Default 0: silent)""" """Verbosity level of compile-time and run-time checks. (Default 0: silent)"""
...@@ -516,7 +509,7 @@ class OptCheckFunctionMaker(FunctionMaker): #inheritance buys a few helper funct ...@@ -516,7 +509,7 @@ class OptCheckFunctionMaker(FunctionMaker): #inheritance buys a few helper funct
self.env = env self.env = env
#equivalence_tracker.printstuff() #equivalence_tracker.printstuff()
linker = OptCheckLinker(self) linker = DebugModeLinker(self)
#the 'no_borrow' outputs are the ones for which that we can't return the internal storage pointer. #the 'no_borrow' outputs are the ones for which that we can't return the internal storage pointer.
...@@ -629,7 +622,7 @@ class OptCheckFunctionMaker(FunctionMaker): #inheritance buys a few helper funct ...@@ -629,7 +622,7 @@ class OptCheckFunctionMaker(FunctionMaker): #inheritance buys a few helper funct
fn = self.function_builder(_fn, _i, _o, self.indices, self.outputs, defaults, self.unpack_single, self) fn = self.function_builder(_fn, _i, _o, self.indices, self.outputs, defaults, self.unpack_single, self)
return fn return fn
class OptCheck(Mode): class DebugMode(Mode):
"""Evaluation Mode that detects optimization errors. """Evaluation Mode that detects optimization errors.
A basic premise of how theano works is that every node that is replaced during optimization should compute the same thing as its replacement. A basic premise of how theano works is that every node that is replaced during optimization should compute the same thing as its replacement.
...@@ -645,14 +638,14 @@ class OptCheck(Mode): ...@@ -645,14 +638,14 @@ class OptCheck(Mode):
# function_module.function # function_module.function
def function_maker(self, i,o,m, *args, **kwargs): def function_maker(self, i,o,m, *args, **kwargs):
assert m is self assert m is self
return OptCheckFunctionMaker(i, o, self.optimizer, self, *args, **kwargs) return DebugModeFunctionMaker(i, o, self.optimizer, self, *args, **kwargs)
def __init__(self, def __init__(self,
optimizer='fast_run', optimizer='fast_run',
stability_patience=10, stability_patience=10,
check_c_code=True): check_c_code=True):
super(OptCheck, self).__init__( super(DebugMode, self).__init__(
optimizer=optimizer, optimizer=optimizer,
linker=OptCheckLinker) linker=DebugModeLinker)
self.stability_patience = stability_patience self.stability_patience = stability_patience
self.check_c_code = check_c_code self.check_c_code = check_c_code
......
...@@ -10,7 +10,7 @@ import theano.compile ...@@ -10,7 +10,7 @@ import theano.compile
def test0(): def test0():
x = theano.tensor.dvector() x = theano.tensor.dvector()
f = theano.function([x], (2.*x + 7) / 2., mode=debugmode.OptCheck()) f = theano.function([x], (2.*x + 7) / 2., mode=debugmode.DebugMode())
print f([1,2]) print f([1,2])
class BROKEN_ON_PURPOSE_StructuredDotCSC(gof.Op): class BROKEN_ON_PURPOSE_StructuredDotCSC(gof.Op):
...@@ -161,10 +161,10 @@ def test_badclinkeroutput(): ...@@ -161,10 +161,10 @@ def test_badclinkeroutput():
f_good = theano.function([vals, inds, ptrs, nrows, b], f_good = theano.function([vals, inds, ptrs, nrows, b],
theano.sparse.StructuredDotCSC()(vals, inds, ptrs, nrows, b), theano.sparse.StructuredDotCSC()(vals, inds, ptrs, nrows, b),
mode=debugmode.OptCheck(check_c_code=True)) mode=debugmode.DebugMode(check_c_code=True))
f_inconsistent = theano.function([vals, inds, ptrs, nrows, b], f_inconsistent = theano.function([vals, inds, ptrs, nrows, b],
inconsistent(vals, inds, ptrs, nrows, b), inconsistent(vals, inds, ptrs, nrows, b),
mode=debugmode.OptCheck(check_c_code=True)) mode=debugmode.DebugMode(check_c_code=True))
#this should evaluate with no error #this should evaluate with no error
rval_good = f_good([1.0, 2.0, 3.0], rval_good = f_good([1.0, 2.0, 3.0],
...@@ -205,7 +205,7 @@ def test_badoptimization(): ...@@ -205,7 +205,7 @@ def test_badoptimization():
f = theano.function([vals, inds, ptrs, nrows, b], f = theano.function([vals, inds, ptrs, nrows, b],
theano.sparse.sd_csc(vals, inds, ptrs, nrows, b), theano.sparse.sd_csc(vals, inds, ptrs, nrows, b),
mode=debugmode.OptCheck(optimizer=opt, check_c_code=True)) mode=debugmode.DebugMode(optimizer=opt, check_c_code=True))
try: try:
rval = f([1.0, 2.0, 3.0], rval = f([1.0, 2.0, 3.0],
......
...@@ -13,7 +13,7 @@ from theano import pprint ...@@ -13,7 +13,7 @@ from theano import pprint
import numpy import numpy
#import scalar_opt #import scalar_opt
from theano.sandbox.debugmode import OptCheck from theano.sandbox.debugmode import DebugMode
from theano import function from theano import function
...@@ -136,7 +136,7 @@ class test_greedy_distribute(unittest.TestCase): ...@@ -136,7 +136,7 @@ class test_greedy_distribute(unittest.TestCase):
, eps + y/s , eps + y/s
, s) , s)
f = function([s, eps, x,y], r**2, mode=OptCheck()) f = function([s, eps, x,y], r**2, mode=DebugMode())
r0 = f(4,1.e-6, [1.5,2], [2.3,3.1]) r0 = f(4,1.e-6, [1.5,2], [2.3,3.1])
r1 = f(4,1.e-6, [1.5,2], [2.3,3.1]) r1 = f(4,1.e-6, [1.5,2], [2.3,3.1])
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论