提交 59204ff9 authored 作者: Frederic Bastien's avatar Frederic Bastien

added Assert op c_code and test.

上级 ab17c207
...@@ -622,30 +622,32 @@ def local_alloc_unary(node): ...@@ -622,30 +622,32 @@ def local_alloc_unary(node):
class Assert(T.Op): class Assert(T.Op):
view_map={0:[0]} view_map={0:[0]}
def make_node(self, value, *shape): def make_node(self, value, *conds):
sh = [T.as_tensor_variable(s) for s in shape] cond = [T.as_tensor_variable(c) for c in conds]
return gof.Apply(self, [value]+sh, [value.type()]) assert numpy.all([c.type.ndim == 0 for c in cond])
return gof.Apply(self, [value]+cond, [value.type()])
def __str__(self): def __str__(self):
return self.__class__.__name__ return self.__class__.__name__
def perform(self, node, inputs, (out,)): def perform(self, node, inputs, (out,)):
v = inputs[0] v = inputs[0]
if out[0] is None:
out[0]=v out[0]=v
assert all(inputs[1:]) assert numpy.all(inputs[1:])
def __eq__(self, other): def __eq__(self, other):
return type(self)==type(other) return type(self)==type(other)
def __hash__(self): return hash(type(self))
def grad(self,input,output_gradients): def grad(self,input,output_gradients):
return output_gradients return output_gradients
def c_code_old(self, node, name, inames, onames, sub): def c_code(self, node, name, inames, onames, sub):
value = inames[0] value = inames[0]
out = onames[0] out = onames[0]
check = [] check = []
fail = sub['fail']
for idx in range(len(inames)-1): for idx in range(len(inames)-1):
i=inames[idx+1] i=inames[idx+1]
dtype=node.inputs[idx+1].dtype dtype=node.inputs[idx+1].dtype
check.append("assert((%(out)s->dimensions[%(idx)s]));//==(*((*npy_%(dtype)s)PyArray_DATA(%(i)s))));"%locals()) check.append('if(!((npy_%(dtype)s*)PyArray_DATA(%(i)s))[0]){PyErr_SetString(PyExc_AssertionError,"Theano Assert failed!");%(fail)s}'%locals())
check = "\n".join(check) check = "\n".join(check)
return """ return """
%(check)s %(check)s
...@@ -654,7 +656,7 @@ class Assert(T.Op): ...@@ -654,7 +656,7 @@ class Assert(T.Op):
"""%locals() """%locals()
pass pass
def c_code_cache_version(self): def c_code_cache_version(self):
return () return (0,1)
assert_ = Assert() assert_ = Assert()
...@@ -729,10 +731,8 @@ def local_alloc_elemwise(node): ...@@ -729,10 +731,8 @@ def local_alloc_elemwise(node):
#TODO, T.eq if both input are the same, remove! #TODO, T.eq if both input are the same, remove!
#TODO, op that check the condition are all true and remove the Assert. Also remove the constant condition. #TODO, op that check the condition are all true and remove the Assert. Also remove the constant condition.
#TODO, create a test_case where the assert is needed and fail.
#TODO, global optimizer that lift the assert to the beginning of the graph. #TODO, global optimizer that lift the assert to the beginning of the graph.
#TODO, var.tag.shape to propagate the shape and lower the overhead of this op #TODO, var.tag.shape to propagate the shape and lower the overhead of this op
#TODO, Assert.c_code
#TODO, when all can be optimizer do all except one #TODO, when all can be optimizer do all except one
theano.configparser.AddConfigVar('experimental.local_alloc_elemwise', theano.configparser.AddConfigVar('experimental.local_alloc_elemwise',
......
...@@ -1085,6 +1085,14 @@ class test_shapeoptimizer(unittest.TestCase): ...@@ -1085,6 +1085,14 @@ class test_shapeoptimizer(unittest.TestCase):
assert identity_noshape not in h_ops assert identity_noshape not in h_ops
assert identity_shape not in h_ops assert identity_shape not in h_ops
class test_assert(unittest.TestCase):
def test0(self):
x=T.scalar()
y=T.scalar()
f = theano.function([x,y],theano.tensor.opt.assert_(x,T.eq(x,y)))
f(1,1)
self.failUnlessRaises(AssertionError, f, 1,0)
def test_local_mul_specialize(): def test_local_mul_specialize():
# test a few cases to make sure that the basics are covered # test a few cases to make sure that the basics are covered
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论