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

PEP3113 compliance for theano.sandbox, theano.sparse.sandbox, and…

PEP3113 compliance for theano.sandbox, theano.sparse.sandbox, and theano/scalar/basic_scipy.py. Now only sparse/basic.py and scalar/basic.py to do.
上级 032c5b14
......@@ -42,9 +42,12 @@ class HostFromGpu(Op):
if not isinstance(x.type, CudaNdarrayType):
raise TypeError(x)
return Apply(self, [x], [tensor.TensorType(dtype=x.dtype, broadcastable=x.broadcastable)()])
def perform(self, node, (x,), (z,)):
def perform(self, node, inp, out):
x, = inp
z, = out
z[0] = numpy.asarray(x)
def grad(self, inputs, (gz,)):
def grad(self, inputs, grads):
gz, = grads
return [gpu_from_host(gz)]
def infer_shape(self, node, xshp):
return xshp
......@@ -61,9 +64,12 @@ class GpuFromHost(Op):
if not isinstance(x.type, tensor.TensorType):
raise TypeError(x)
return Apply(self, [x], [CudaNdarrayType(broadcastable=x.broadcastable, dtype=x.dtype)()])
def perform(self, node, (x,), (z,)):
def perform(self, node, inp, out):
x, = inp
z, = out
z[0] = type_support_filter(theano._asarray(x, dtype='float32'), tuple([0]*x.ndim), 0, z[0])
def grad(self, inputs, (gz,)):
def grad(self, inputs, grads):
gz, = grads
return [host_from_gpu(gz)]
def infer_shape(self, node, xshp):
return xshp
......@@ -245,7 +251,9 @@ class GpuDimShuffle(Op):
def __str__(self):
return "GpuDimShuffle{%s}" % ",".join(str(x) for x in self.new_order)
def c_code(self, node, name, (input,), (res,), sub):
def c_code(self, node, name, inp, out, sub):
input, = inp
res, = out
basename = input + '__view_or_copy'
nd_in = len(self.input_broadcastable)
......@@ -395,10 +403,14 @@ class GpuSum(Op):
o_broadcast = [x.type.broadcastable[i] for i in xrange(x.type.ndim) if not self.reduce_mask[i]]
return Apply(self, [x], [CudaNdarrayType(o_broadcast)()])
def perform(self, node, (x,), (z,)):
def perform(self, node, inp, out):
x, = inp
z, = out
z[0] = x.reduce_sum(self.reduce_mask)
def c_code(self, node, name, (x,), (z,), sub):
def c_code(self, node, name, inp, out, sub):
x, = inp
z, = out
nd_in = node.inputs[0].type.ndim
nd_out = node.outputs[0].type.ndim
......@@ -1672,7 +1684,9 @@ class GpuReshape(tensor.Reshape):
def make_node(self, x, shp):
host_reshaped = host_from_gpu(x).reshape(shp,ndim=self.ndim)
return Apply(self, [x, shp], [CudaNdarrayType(host_reshaped.broadcastable)()])
def perform(self, node, (x, shp), (out,)):
def perform(self, node, inp, out_):
x, shp = inp
out, = out_
if (len(shp) != self.ndim):
raise ValueError('shape argument to Reshape.perform has incorrect length %i'
', should be %i' % (len(shp), self.ndim), shp)
......@@ -1686,7 +1700,8 @@ class GpuSubtensor(tensor.Subtensor):
otype = CudaNdarrayType(rval.outputs[0].type.broadcastable)
return Apply(self, [x]+rval.inputs[1:], [otype()])
def perform(self, node, inputs, (out, )):
def perform(self, node, inputs, out_):
out, = out_
x = inputs[0]
indices = list(reversed(inputs[1:]))
......@@ -1745,7 +1760,8 @@ class GpuJoin(tensor.Join):
axis, tensors,
as_tensor_variable_args, output_maker)
def perform(self, node, axis_and_tensors, (out, )):
def perform(self, node, axis_and_tensors, out_):
out, = out_
axis, cndas = axis_and_tensors[0], axis_and_tensors[1:]
# In case axis is numpy.int8 and has no __index__() method
axis = int(axis)
......@@ -1832,14 +1848,16 @@ class GpuAlloc(Op):
otype = CudaNdarrayType(dtype='float32', broadcastable=bcast)
return Apply(self, [v]+sh, [otype()])
def perform(self, node, inputs, (out,)):
def perform(self, node, inputs, out_):
out, = out_
v = inputs[0]
sh = tuple([int(i) for i in inputs[1:]])
if out[0] is None or out[0].shape != sh:
out[0] = cuda_ndarray.cuda_ndarray.CudaNdarray.zeros(sh)
out[0][...] = v # broadcast v to fill us up
def c_code(self, node, name, inputs, (out,), sub):
def c_code(self, node, name, inputs, out_, sub):
out, = out_
fail = sub['fail']
value = inputs[0]
shps = inputs[1:]
......@@ -1866,7 +1884,8 @@ class GpuAlloc(Op):
def infer_shape(self, node, input_shapes):
return [node.inputs[1:]]
def grad(self, inputs, (gout,)):
def grad(self, inputs, grads):
gout, = grads
return [None for i in inputs]
def c_code_cache_version(self):
......@@ -1888,7 +1907,9 @@ class GpuContiguous(Op):
input = as_cuda_ndarray_variable(input)
return Apply(self, [input], [input.type()])
def c_code(self, node, name, (input,), (z,), sub):
def c_code(self, node, name, inp, out, sub):
input, = inp
z, = out
fail = sub['fail']
str = """
{
......
......@@ -371,7 +371,9 @@ class GpuConv(Op):
open(os.path.join(os.path.split(__file__)[0],'conv_full_kernel.cu')).read()+\
open(os.path.join(os.path.split(__file__)[0],'conv.cu')).read()
def c_code(self, node, nodename, (img, kern), (out,), sub):
def c_code(self, node, nodename, inp, out_, sub):
img, kern = inp
out, = out_
dx = self.subsample[0]
dy = self.subsample[1]
border_mode = self.border_mode
......@@ -435,7 +437,9 @@ class GpuDownsampleFactorMax(Op):
#raise NotImplementedError('only C is implemented')
def c_code_cache_version(self):
return (1)
def c_code(self, node, nodename, (x,), (z,), sub):
def c_code(self, node, nodename, inp, out, sub):
x, = inp
z, = out
fail = sub['fail']
ds0, ds1 = self.ds
ignore_border = int(self.ignore_border)
......@@ -586,7 +590,9 @@ class GpuDownsampleFactorMaxGrad(Op):
#return ()
return (3,)
def c_code(self, node, nodename, (x, z, gz), (gx,), sub):
def c_code(self, node, nodename, inp, out, sub):
x, z, gz = inp
gx, = out
fail = sub['fail']
ds0, ds1 = self.ds
ignore_border = int(self.ignore_border)
......
......@@ -77,7 +77,9 @@ class GpuCrossentropySoftmaxArgmax1HotWithBias (Op):
"""
def c_code(self, node, nodename, (x, b, y_idx), (nll, sm, am), sub):
def c_code(self, node, nodename, inp, out, sub):
x, b, y_idx = inp
nll, sm, am = out
classname=self.__class__.__name__
fail = sub['fail']
sio = StringIO.StringIO()
......@@ -191,7 +193,9 @@ class GpuCrossentropySoftmax1HotWithBiasDx (Op):
def c_code_cache_version(self):
return (3,)
#return ()
def c_code(self, node, nodename, (dnll, sm, y_idx), (dx,), sub):
def c_code(self, node, nodename, inp, out, sub):
dnll, sm, y_idx = inp
dx, = out
fail = sub['fail']
return """
if ((%(dnll)s->nd != 1)
......@@ -306,7 +310,9 @@ class GpuSoftmax (Op):
def c_code_cache_version(self):
#return ()
return (2,) + inline_softmax.code_version
def c_code(self, node, nodename, (x,), (z,), sub):
def c_code(self, node, nodename, inp, out, sub):
x, = inp
z, = out
fail = sub['fail']
return """
if (%(x)s->nd != 2)
......@@ -394,7 +400,9 @@ class GpuSoftmaxWithBias (Op):
#return ()
return (2,) + inline_softmax.code_version
def c_code(self, node, nodename, (x,b), (z,), sub):
def c_code(self, node, nodename, inp, out, sub):
x, b = inp
z, = out
fail = sub['fail']
return """
if (%(x)s->nd != 2)
......
......@@ -67,7 +67,9 @@ class FFT(Op):
rval = Apply(self, [_frames, _n, _axis], [spectrogram, buf])
return rval
def perform(self, node, (frames, n, axis), (spectrogram, buf)):
def perform(self, node, inp, out):
frames, n, axis = inp
spectrogram, buf = out
if self.inverse:
fft_fn = numpy.fft.ifft
else:
......@@ -88,7 +90,9 @@ class FFT(Op):
raise NotImplementedError()
else:
spectrogram[0] = fft
def grad(self, (frames, n, axis), (g_spectrogram, g_buf)):
def grad(self, inp, out):
frames, n, axis = inp
g_spectrogram, g_buf = out
return [grad_todo(frames), None, None]
fft = FFT(half=False, inverse=False)
......
......@@ -28,7 +28,8 @@ class Minimal(gof.Op):
# HERE `args` must be THEANO VARIABLES
return gof.Apply(op=self, inputs=args, outputs=[tensor.lscalar()])
def perform(self, node, inputs, (output, )):
def perform(self, node, inputs, out_):
output, = out_
# HERE `inputs` are PYTHON OBJECTS
# do what you want here,
......
......@@ -23,13 +23,17 @@ class Multinomial(Op):
#assert unis.dtype == 'float32'
return Apply(self, [pvals, unis], [pvals.type()])
def grad(self, (pvals, unis), (gz,)):
def grad(self, inp, grads):
pvals, unis = inp
gz, = grads
return [None, None]
def c_code_cache_version(self):
return (3,)
def c_code(self, node, name, (pvals, unis), (z,), sub):
def c_code(self, node, name, inp, out, sub):
pvals, unis = inp
z, = out
fail = sub['fail']
return """
......@@ -154,7 +158,9 @@ class GpuMultinomial(Multinomial):
""" % locals()
def c_code(self, node, name, (pvals, unis), (z,), sub):
def c_code(self, node, name, inp, out, sub):
pvals, unis = inp
z, = out
fail = sub['fail']
return """
......
......@@ -161,7 +161,9 @@ class NeighbourhoodsFromImages(Op):
raise TypeError()
return gof.Apply(self, [x], [x.type()])
def perform(self, node, (x,), (z,)):
def perform(self, node, inp, out):
x, = inp
z, = out
if self.inverse:
# +1 in the inverse case
if len(x.shape) != (self.n_dims_before + \
......
......@@ -46,7 +46,9 @@ class Images2Neibs(Op):
return Apply(self, [ten4, neib_shape,neib_step], [T.matrix(dtype=ten4.type.dtype)])
def grad(self, (x, neib_shape, neib_step), (gz,)):
def grad(self, inp, grads):
x, neib_shape, neib_step = inp
gz, = grads
if self.mode=='valid':
return [neibs2images(gz, neib_shape, x.shape), None, None]
else:
......@@ -55,7 +57,9 @@ class Images2Neibs(Op):
def c_code_cache_version(self):
return (3,)
def c_code(self, node, name, (ten4, neib_shape, neib_step), (z,), sub):
def c_code(self, node, name, inp, out, sub):
ten4, neib_shape, neib_step = inp
z, = out
fail = sub['fail']
mode=self.mode
......@@ -388,7 +392,9 @@ class GpuImages2Neibs(Images2Neibs):
""" % locals()
def c_code(self, node, name, (ten4, neib_shape, neib_step), (z,), sub):
def c_code(self, node, name, inp, out, sub):
ten4, neib_shape, neib_step = inp
z, = out
fail = sub['fail']
mode = self.mode
return """
......
......@@ -173,7 +173,9 @@ class mrg_uniform(mrg_uniform_base):
op = cls(TensorType(dtype, (False,)*ndim))
return op(rstate, cast(v_size, 'int32'))
def perform(self, node, (rstate, size), (o_rstate, o_sample)):
def perform(self, node, inp, out):
rstate, size = inp
o_rstate, o_sample = out
numpy_version=numpy.__version__.split('.')
if not self.warned_numpy_version and int(numpy_version[0])<=1 and int(numpy_version[1])<3:
print "Warning: you must use numpy version 1.3.0 or higher with the python version of this op. Otherwise numpy leak memory. and numpy"
......@@ -199,7 +201,9 @@ class mrg_uniform(mrg_uniform_base):
o_rstate[0] = node.outputs[0].type.filter(rstate) # send to GPU if necessary
o_sample[0] = node.outputs[1].type.filter(rval.reshape(size))# send to GPU if necessary
def c_code(self, node, name, (rstate, size), (o_rstate, o_sample), sub):
def c_code(self, node, name, inp, out, sub):
rstate, size = inp
o_rstate, o_sample = out
if self.inplace:
o_rstate_requirement = 'NPY_C_CONTIGUOUS|NPY_ALIGNED'
else:
......@@ -455,7 +459,9 @@ class GPU_mrg_uniform(mrg_uniform_base):
""" %locals()
def c_code(self, node, nodename, (rstate, size), (o_rstate, o_sample), sub):
def c_code(self, node, nodename, inp, out, sub):
rstate, size = inp
o_rstate, o_sample = out
inplace = int(self.inplace)
ndim = self.output_type.ndim
o_type_num = numpy.asarray(0, dtype=self.output_type.dtype).dtype.num
......
......@@ -9,13 +9,17 @@ class ScalarSoftsign(theano.scalar.UnaryScalarOp):
return x / (1.0 + abs(x))
def impl(self, x):
return ScalarSoftsign.static_impl(x)
def grad(self, (x, ), (gz, )):
def grad(self, inp, grads):
x, = inp
gz, = grads
if 'float' in x.type.dtype:
d = (1.0 + abs(x))
return [gz / (d * d)]
else:
return NotImplemented
def c_code(self, node, name, (x, ), (z, ), sub):
def c_code(self, node, name, inp, out, sub):
x, = inp
z, = out
if node.inputs[0].type in [theano.scalar.float32, theano.scalar.float64]:
return "%(z)s = %(x)s / (1.0+fabs(%(x)s));" % locals()
raise NotImplementedError('only floating point x is implemented')
......
......@@ -33,7 +33,9 @@ class Solve(gof.Op):
otype = tensor.TensorType(broadcastable=b_.broadcastable, dtype=odtype)
return gof.Apply(op=self, inputs=[A, B], outputs=[otype()])
def perform(self, node, (A, b), (output, )):
def perform(self, node, inp, out):
A, b = inp
output, = out
ret=scipy.linalg.solve(A,b)
if ret.dtype != node.outputs[0].dtype:
print >> sys.stderr, "WARNING: Solve.perform() required cast."
......
......@@ -18,7 +18,9 @@ class Erf(UnaryScalarOp):
return scipy.special.erf(x)
else:
super(Erf,self).impl(x)
def grad(self, (x, ), (gz, )):
def grad(self, inp, grads):
x, = inp
gz, = grads
if x.type in complex_types:
raise NotImplementedError()
elif x.type in float_types:
......@@ -26,7 +28,9 @@ class Erf(UnaryScalarOp):
return gz * cst * exp(-x*x),
else:
return None,
def c_code(self, node, name, (x, ), (z, ), sub):
def c_code(self, node, name, inp, out, sub):
x, = inp
z, = out
if node.inputs[0].type in complex_types:
raise NotImplementedError('type not supported', type)
return "%(z)s = erf(%(x)s);" % locals()
......@@ -38,7 +42,9 @@ class Erfc(UnaryScalarOp):
return scipy.special.erfc(x)
else:
super(Erfc,self).impl(x)
def grad(self, (x, ), (gz, )):
def grad(self, inp, grads):
x, = inp
gz, = grads
if x.type in complex_types:
raise NotImplementedError()
elif x.type in float_types:
......@@ -46,7 +52,9 @@ class Erfc(UnaryScalarOp):
return - gz * cst * exp(-x*x),
else:
return None,
def c_code(self, node, name, (x, ), (z, ), sub):
def c_code(self, node, name, inp, out, sub):
x, = inp
z, = out
if node.inputs[0].type in complex_types:
raise NotImplementedError('type not supported', type)
return "%(z)s = erfc(%(x)s);" % locals()
......
......@@ -45,14 +45,18 @@ class TrueDot(gof.op.Op):
inputs = [x, y] # Need to convert? e.g. assparse
outputs = [Sparse(dtype = x.type.dtype, format = myformat).make_variable()]
return gof.Apply(self, inputs, outputs)
def perform(self, node, (x, y), (out, )):
def perform(self, node, inp, out_):
"""
@todo: Verify that output is sufficiently sparse, and raise a warning if it is not
@todo: Also determine that we are storing the output in the best storage format?
"""
x, y = inp
out, = out_
rval = x.dot(y)
out[0] = rval
def grad(self, (x, y), (gz,)):
def grad(self, inp, grads):
x, y = inp
gz, = grads
assert _is_sparse_variable(gz)
assert _is_sparse_variable(x)
rval = [true_dot(gz, y.T), true_dot(x.T, gz)]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论