提交 ead4f23e authored 作者: David Warde-Farley's avatar David Warde-Farley

Make tests PEP 3113 compliant (remove tuple parameter unpacking).

上级 77c20a3b
......@@ -99,11 +99,15 @@ class OutputGuard(gof.Op):
return type(self) == type(other)
def __hash__(self):
return hash(type(self))
def perform(self, node, (x,), (z,)):
def perform(self, node, inp, out):
x, = inp
z, = out
z[0] = x
def __str__(self):
return '%s' % self.__class__.__name__
def c_code(self, node, nodename, (x,), (z,), sub):
def c_code(self, node, nodename, inp, out, sub):
x, = inp
z, = out
return """
Py_XDECREF(%(z)s);
%(z)s = %(x)s;
......@@ -209,7 +213,8 @@ class Mode(object):
def __getstate__(self):
return (self.provided_linker, self.provided_optimizer)
def __setstate__(self, (linker, optimizer)):
def __setstate__(self, state):
linker, optimizer = state
self.provided_linker = linker
self.provided_optimizer = optimizer
if isinstance(linker, str) or linker is None:
......
......@@ -59,9 +59,9 @@ class ProfileMode(Mode):
self.op_cimpl, self.compile_time, self.fct_call_time,
self.fct_call, self.message, self.outputs_size)
def __setstate__(self, (linker, optimizer, apply_time, op_cimpl,
compile_time, fct_call_time, fct_call, message, outputs_size)):
def __setstate__(self, state):
linker, optimizer, apply_time, op_cimpl, compile_time, \
fct_call_time, fct_call, message, outputs_size = state
self.apply_time = apply_time
self.op_cimpl = op_cimpl
self.compile_time = compile_time
......
......@@ -31,14 +31,18 @@ class BROKEN_ON_PURPOSE_Add(gof.Op):
r = gof.Apply(self, [a, b], [a.type()])
return r
def perform(self, node, (a, b), (out,)):
def perform(self, node, inp, out_):
a, b = inp
out, = out_
z = a+b
#ERROR TO ADD THIS CRAPPY OFFSET
if self.py_offset:
out[0] = z+0.5
else: out[0] = z
def c_code(self, node, name, (a, b), (z,), sub):
def c_code(self, node, name, inp, out, sub):
a, b = inp
z, = out
return """
if (%(a)s->nd != 1) {PyErr_SetString(PyExc_NotImplementedError, "rank(a) != 1"); %(fail)s;}
if (%(b)s->nd != 1) {PyErr_SetString(PyExc_NotImplementedError, "rank(b) != 1"); %(fail)s;}
......@@ -100,7 +104,9 @@ class WeirdBrokenOp(gof.Op):
r = gof.Apply(self, [a_], [a_.type()])
return r
def dontuse_perform(self, node, (a,), (out,)):
def dontuse_perform(self, node, inp, out_):
a, = inp
out, = out_
if self.behaviour == 'times2':
out[0] = a * 2
elif self.behaviour == 'times2_inplace':
......@@ -113,7 +119,9 @@ class WeirdBrokenOp(gof.Op):
else:
raise ValueError(self.behaviour)
def c_code(self, node, name, (a,), (z,), sub):
def c_code(self, node, name, inp, out, sub):
a, = inp
z, = out
if "inplace" in self.behaviour:
z_code = """
{Py_XDECREF(%(z)s);}
......@@ -253,7 +261,9 @@ def test_baddestroymap():
def make_node(self, a, b):
c = a.type()
return gof.Apply(self, [a,b], [c])
def perform(self, node, (a,b), (c,)):
def perform(self, node, inp, out):
a, b = inp
c, = out
c[0] = a
c[0] += b
......@@ -283,14 +293,18 @@ class Test_ViewMap(unittest.TestCase):
def make_node(self, a, b):
c = b.type()
return gof.Apply(self, [a,b], [c])
def perform(self, node, (a,b), (c,)):
def perform(self, node, inp, out):
a, b = inp
c, = out
c[0] = b
class BadAddSlice(gof.Op):
def make_node(self, a, b):
c = b.type()
return gof.Apply(self, [a,b], [c])
def perform(self, node, (a,b), (c,)):
def perform(self, node, inp, out):
a, b = inp
c, = out
c[0] = b[1:3]
def test_badviewmap_ref(self):
......@@ -343,7 +357,9 @@ class Test_ViewMap(unittest.TestCase):
c = a.type()
d = a.type()
return gof.Apply(self, [a,b], [c,d])
def perform(self, node, (a,b), (c,d)):
def perform(self, node, inp, out):
a, b = inp
c, d = out
c[0] = a
d[0] = a[1:]
......@@ -364,7 +380,9 @@ class Test_ViewMap(unittest.TestCase):
c = a.type()
d = a.type()
return gof.Apply(self, [a,b], [c,d])
def perform(self, node, (a,b), (c,d)):
def perform(self, node, inp, out):
a, b = inp
c, d = out
r = a * 2
c[0] = r
d[0] = r[1:]
......@@ -387,7 +405,9 @@ class Test_ViewMap(unittest.TestCase):
c = a.type()
d = a.type()
return gof.Apply(self, [a,b], [c,d])
def perform(self, node, (a,b), (c,d)):
def perform(self, node, inp, out):
a, b = inp
c, d = out
r = a * 1
c[0] = r
d[0] = r[1:]
......@@ -409,7 +429,9 @@ class Test_ViewMap(unittest.TestCase):
c = a.type()
d = a.type()
return gof.Apply(self, [a,b], [c,d])
def perform(self, node, (a,b), (c,d)):
def perform(self, node, inp, out):
a, b = inp
c, d = out
r = a * 1
c[0] = r[:-1]
d[0] = r[1:]
......
......@@ -104,7 +104,8 @@ class TanhRnn(Op):
z = x.type() #make a new symbolic variable with the same type as x
return Apply(self, [x, z0, A], [z])
def perform(self, node, (x,z0,A), out):
def perform(self, node, inp, out):
x, z0, A = inp
assert x is not None
assert z0 is not None
assert A is not None
......@@ -115,7 +116,9 @@ class TanhRnn(Op):
z[i+1] = N.tanh(N.dot(z[i], A) + x[i])
out[0][0] = z
def grad(self, (x, z0, A), (gz,)):
def grad(self, inp, grads):
x, z0, A = inp
gz, = grads
z = tanh_rnn(x, z0, A)
gz_incl_rnn, gx = tanh_rnn_grad(A, z, gz)
return [gx, gz_incl_rnn[0], (T.dot(z[:-1].T, gx))]
......@@ -136,7 +139,8 @@ class TanhRnnGrad(Op):
def make_node(self, A, z, gz):
return Apply(self, [A,z,gz], (z.type(), gz.type()))
def perform(self, node, (A, z, gz), out):
def perform(self, node, inp, out):
A, z, gz = inp
Tp1,M = z.shape
T = Tp1 - 1
gx = N.zeros((T, M))
......
......@@ -84,7 +84,8 @@ class MyOp(Op):
def __str__(self):
return self.name
def perform(self, node, inputs, (out, )):
def perform(self, node, inputs, out_):
out, = out_
out[0] = self.impl(*inputs)
def c_code_cache_version(self):
return ()
......@@ -100,28 +101,36 @@ class Binary(MyOp):
class Add(Binary):
def c_code(self, node, name, (x, y), (z, ), sub):
def c_code(self, node, name, inp, out, sub):
x, y = inp
z, = out
return "%(z)s = %(x)s + %(y)s;" % locals()
def impl(self, x, y):
return x + y
add = Add()
class Sub(Binary):
def c_code(self, node, name, (x, y), (z, ), sub):
def c_code(self, node, name, inp, out, sub):
x, y = inp
z, = out
return "%(z)s = %(x)s - %(y)s;" % locals()
def impl(self, x, y):
return -10 # erroneous (most of the time)
sub = Sub()
class Mul(Binary):
def c_code(self, node, name, (x, y), (z, ), sub):
def c_code(self, node, name, inp, out, sub):
x, y = inp
z, = out
return "%(z)s = %(x)s * %(y)s;" % locals()
def impl(self, x, y):
return x * y
mul = Mul()
class Div(Binary):
def c_code(self, node, name, (x, y), (z, ), sub):
def c_code(self, node, name, inp, out, sub):
x, y = inp
z, = out
return "%(z)s = %(x)s / %(y)s;" % locals()
def impl(self, x, y):
return x / y
......@@ -256,7 +265,9 @@ def test_duallinker_mismatch():
################################
class AddFail(Binary):
def c_code(self, node, name, (x, y), (z, ), sub):
def c_code(self, node, name, inp, out, sub):
x, y = inp
z, = out
fail=sub['fail']
return """%(z)s = %(x)s + %(y)s;
PyErr_SetString(PyExc_RuntimeError, "failing here");
......
......@@ -45,7 +45,8 @@ class MyOp(Op):
def __str__(self):
return self.name
def perform(self, node, inputs, (out, )):
def perform(self, node, inputs, out_):
out, = out_
out[0] = self.impl(*inputs)
add = MyOp(2, 'Add', lambda x, y: x + y)
......
......@@ -2513,7 +2513,9 @@ class test_grad(unittest.TestCase):
inputs = [scalar('a'),scalar('c')]
outputs = [scalar('b'),scalar('d')]
return gof.Apply(self, inputs, outputs)
def grad(self, (x0,x1), (gz0,gz1)):
def grad(self, inp, grads):
x0, x1 = inp
gz0, gz1 = grads
return self.gval0, self.gval1
def test_1param(self):
......
......@@ -1303,7 +1303,9 @@ class test_shapeoptimizer(unittest.TestCase):
def make_node(self, x):
x = as_tensor_variable(x)
return Apply(self, [x], [x.type()])
def perform(self, node, (x,), (out,)):
def perform(self, node, inp, out_):
x, = inp
out, = out_
out[0] = x.copy()
#def infer_shape(self, node, (xshp,)):
#return [tuple([self.shape_i(i)(r) for i in xrange(r.ndim)])]
......@@ -1314,9 +1316,13 @@ class test_shapeoptimizer(unittest.TestCase):
def make_node(self, x):
x = as_tensor_variable(x)
return Apply(self, [x], [x.type()])
def perform(self, node, (x,), (out,)):
def perform(self, node, inp, out_):
x, = inp
out, = out_
out[0] = x.copy()
def infer_shape(self, node, (xshp,)):
def infer_shape(self, node, xshp_):
# Could also just return.
xshp, = xshp_
return (xshp,)
identity_shape = IdentityShape()
......
......@@ -22,7 +22,9 @@ class test_grad_sources_inputs(unittest.TestCase):
inputs = [gof.generic()]
outputs = [gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, (x, ), (gz, )):
def grad(self, inp, grads):
x, = inp
gz, = grads
pass
a = retNone().make_node()
try:
......@@ -37,7 +39,7 @@ class test_grad_sources_inputs(unittest.TestCase):
def make_node(self, *inputs):
outputs = [gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, (x, ), (gz, )):
def grad(self, inp, grads):
return [None]
i = gof.generic()
a = retNone().make_node(i)
......@@ -50,7 +52,7 @@ class test_grad_sources_inputs(unittest.TestCase):
def make_node(self, *inputs):
outputs = [gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, inputs, (gz, )):
def grad(self, inputs, grads):
return [None]
i = gof.generic()
......@@ -74,7 +76,7 @@ class test_grad_sources_inputs(unittest.TestCase):
def make_node(self, *inputs):
outputs = [gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, inputs, (gz, )):
def grad(self, inputs, grads):
self.tst.fail()
i = gof.generic()
......@@ -89,7 +91,7 @@ class test_grad_sources_inputs(unittest.TestCase):
inputs = [gof.generic()]
outputs = [gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, (x, ), (gz, )):
def grad(self, inp, grads):
return gval,
a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
......@@ -103,7 +105,9 @@ class test_grad_sources_inputs(unittest.TestCase):
inputs = [gof.generic()]
outputs = [gof.generic(),gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, (x, ), (gz1, gz2)):
def grad(self, inp, grads):
inp, = x
gz1, gz2 = grads
return gval,
a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
......@@ -117,7 +121,9 @@ class test_grad_sources_inputs(unittest.TestCase):
inputs = [gof.generic(),gof.generic()]
outputs = [gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, (x0,x1), (gz, )):
def grad(self, inp, grads):
x0, x1 = inp
gz, = grads
return (gval0, gval1)
a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
......@@ -132,7 +138,7 @@ class test_grad_sources_inputs(unittest.TestCase):
inputs = [gof.generic(),gof.generic()]
outputs = [gof.generic(),gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, (x0,x1), (gz0,gz1)):
def grad(self, inp, grads):
return gval0, gval1
a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
......@@ -189,7 +195,8 @@ class test_grad_sources_inputs(unittest.TestCase):
def make_node(self, *inputs):
outputs = [gof.generic(),gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, inputs, (g0,g1)):
def grad(self, inputs, grads):
g0, g1 = grads
if not self.grad_ok:
self.tst.fail()
else:
......@@ -220,7 +227,8 @@ class test_grad_sources_inputs(unittest.TestCase):
def make_node(self, *inputs):
outputs = [gof.generic(),gof.generic()]
return gof.Apply(self, inputs, outputs)
def grad(self, inputs, (g0,g1)):
def grad(self, inputs, grads):
g0, g1 = grads
if not self.grad_ok:
self.tst.fail()
else:
......
......@@ -147,7 +147,9 @@ class T_extending(unittest.TestCase):
raise TypeError('%s only works on doubles' % self.name)
return gof.Apply(self, [x, y], [double()])
def perform(self, node, (x, y), (z, )):
def perform(self, node, inp, out):
x, y = inp
z, = out
z[0] = self.fn(x, y)
def __str__(self):
......@@ -214,7 +216,9 @@ class T_extending(unittest.TestCase):
raise TypeError('%s only works on doubles' % self.name)
return gof.Apply(self, [x, y], [double()])
def perform(self, node, (x, y), (z, )):
def perform(self, node, inp, out):
x, y = inp
z, = out
z[0] = self.fn(x, y)
def __str__(self):
......@@ -360,13 +364,17 @@ class T_extending(unittest.TestCase):
raise TypeError('%s only works on doubles' % self.name)
return gof.Apply(self, [x, y], [double()])
def perform(self, node, (x, y), (z, )):
def perform(self, node, inp, out):
x, y = inp
z, = out
z[0] = self.fn(x, y)
def __str__(self):
return self.name
def c_code(self, node, name, (x, y), (z, ), sub):
def c_code(self, node, name, inp, out, sub):
x, y = inp
z, = out
return self.ccode % locals()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论