提交 9f48c753 authored 作者: Frederic's avatar Frederic 提交者: Arnaud Bergeron

Move Flatten to the new GPU back-end by reusing reshape.

上级 2284a814
......@@ -62,7 +62,10 @@ def op_lifter(OP):
new_op = maker(node)
# This is needed as sometimes new_op inherit from OP.
if new_op and new_op != node.op:
return [host_from_gpu(new_op(*node.inputs))]
if isinstance(new_op, theano.Op):
return [host_from_gpu(new_op(*node.inputs))]
else: # suppose it is a variable on the GPU
return [host_from_gpu(new_op)]
return False
local_opt.__name__ = maker.__name__
return local_optimizer([OP])(local_opt)
......@@ -128,12 +131,24 @@ def local_gpureshape(node):
if type(node.op) is not tensor.Reshape:
return None
if name:
name = 'Gpu'+name
name = 'Gpu' + name
res = GpuReshape(op.ndim, op.name)
o = res(*node.inputs)
return res
@register_opt()
@op_lifter(tensor.Flatten)
def local_gpuflatten(node):
op = node.op
if type(node.op) is not tensor.Flatten:
return None
if op.outdim != 1:
return None
res = GpuReshape(op.outdim, None)
o = res(node.inputs[0], theano.tensor.constant([-1]))
return o
@register_opt()
@op_lifter(tensor.Elemwise)
def local_gpu_elemwise(node):
......
import numpy
import theano
from theano.tests import unittest_tools as utt
from theano.sandbox.gpuarray.basic_ops import GpuReshape
import theano.sandbox.gpuarray
if theano.sandbox.gpuarray.pygpu is None:
raise SkipTest("pygpu not installed")
import theano.sandbox.cuda as cuda_ndarray
if cuda_ndarray.cuda_available and not theano.sandbox.gpuarray.pygpu_activated:
if not cuda_ndarray.use.device_number:
cuda_ndarray.use('gpu')
theano.sandbox.gpuarray.init_dev('cuda')
if not theano.sandbox.gpuarray.pygpu_activated:
raise SkipTest("pygpu disabled")
if theano.config.mode == 'FAST_COMPILE':
mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpuarray').excluding('gpu')
mode_without_gpu = theano.compile.mode.get_mode('FAST_RUN').excluding('gpuarray')
else:
mode_with_gpu = theano.compile.mode.get_default_mode().including('gpuarray').excluding('gpu')
mode_without_gpu = theano.compile.mode.get_default_mode().excluding('gpuarray')
def test_flatten():
m = theano.tensor.fmatrix()
f = theano.function([m], m.flatten(), mode=mode_with_gpu)
val = numpy.random.rand(10,11).astype("float32")
res = f(val)
utt.assert_allclose(res, val.flatten())
assert res.shape == val.flatten().shape
assert GpuReshape in [type(node.op)
for node in f.maker.fgraph.toposort()]
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论