提交 aa6c6a91 authored 作者: Chiheb Trabelsi's avatar Chiheb Trabelsi

test_opt.py has been modified in order to respect the flake8 style.

上级 46e11a73
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import operator import operator
import sys import sys
import unittest
import numpy import numpy
# Skip test if cuda_ndarray is not available. # Skip test if cuda_ndarray is not available.
...@@ -9,39 +8,28 @@ from nose.plugins.skip import SkipTest ...@@ -9,39 +8,28 @@ from nose.plugins.skip import SkipTest
from nose.tools import assert_raises from nose.tools import assert_raises
import theano import theano
import theano.sandbox.cuda.cula as cula
from theano.sandbox.cuda import basic_ops
from theano.sandbox.cuda.type import CudaNdarrayType
from theano.scalar.basic_scipy import erfinv
from six.moves import reduce from six.moves import reduce
from theano.compile.pfunc import pfunc from theano.compile.pfunc import pfunc
from theano import config, tensor from theano import config, tensor
import theano.tensor.tests.test_nlinalg import theano.tensor.tests.test_nlinalg
import theano.tensor.tests.test_opt as test_opt import theano.tensor.tests.test_opt as test_opt
from theano.tensor.nnet.blocksparse import sparse_block_dot
from theano.sandbox.cuda.blocksparse import GpuSparseBlockGemv
from theano.sandbox.cuda.blocksparse import GpuSparseBlockOuter
from theano.tests.breakpoint import PdbBreakpoint from theano.tests.breakpoint import PdbBreakpoint
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
import theano.tests.test_ifelse
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
if not cuda.cuda_available: if not cuda.cuda_available:
raise SkipTest('Optional package cuda disabled') raise SkipTest('Optional package cuda disabled')
import theano.sandbox.cuda.cula as cula
from theano.sandbox.cuda import basic_ops
from theano.sandbox.cuda.type import CudaNdarrayType
from theano.scalar.basic_scipy import erfinv
from theano.tensor.nnet.blocksparse import sparse_block_dot
from theano.sandbox.cuda.blocksparse import GpuSparseBlockGemv, GpuSparseBlockOuter
imported_scipy_special = False
try:
import scipy.special
imported_scipy_special = True
# Importing scipy.special may raise ValueError.
# See http://projects.scipy.org/scipy/ticket/1739
except (ImportError, ValueError):
pass
if theano.config.mode == 'FAST_COMPILE': if theano.config.mode == 'FAST_COMPILE':
mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu') mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu')
mode_without_gpu = theano.compile.mode.get_mode('FAST_RUN').excluding('gpu') mode_without_gpu = theano.compile.mode.get_mode('FAST_RUN').excluding('gpu')
...@@ -152,7 +140,7 @@ def test_local_assert_no_cpu_op(): ...@@ -152,7 +140,7 @@ def test_local_assert_no_cpu_op():
def test_int_pow(): def test_int_pow():
a = CudaNdarrayType([False])() a = CudaNdarrayType([False])()
f = theano.function([a], (a*4).sum(), mode=mode_with_gpu) f = theano.function([a], (a * 4).sum(), mode=mode_with_gpu)
op_names = [n.op.__class__.__name__ for n in f.maker.fgraph.toposort()] op_names = [n.op.__class__.__name__ for n in f.maker.fgraph.toposort()]
assert op_names == ['GpuCAReduce', 'GpuElemwise', 'HostFromGpu'] assert op_names == ['GpuCAReduce', 'GpuElemwise', 'HostFromGpu']
...@@ -175,23 +163,30 @@ def test_gpualloc(): ...@@ -175,23 +163,30 @@ def test_gpualloc():
x = theano.shared(numpy.ones(3, dtype='float32'), 'x') x = theano.shared(numpy.ones(3, dtype='float32'), 'x')
m = (x).dimshuffle(['x', 0]) m = (x).dimshuffle(['x', 0])
v = tensor.alloc(1., *m.shape) v = tensor.alloc(1., *m.shape)
f = theano.function([], v + x, f = theano.function([],
mode=mode_with_gpu.excluding("local_elemwise_alloc")) v + x,
mode=mode_with_gpu.excluding(
"local_elemwise_alloc"))
l = f.maker.fgraph.toposort() l = f.maker.fgraph.toposort()
assert numpy.any([isinstance(x.op, cuda.GpuAlloc) for x in l]) assert numpy.any([isinstance(x.op, cuda.GpuAlloc) for y in l])
def test_gpuallocempty(): def test_gpuallocempty():
f_gpu = theano.function([], tensor.AllocEmpty('float32')(2,3), f_gpu = theano.function(
[],
tensor.AllocEmpty('float32')(2, 3),
mode=mode_with_gpu) mode=mode_with_gpu)
l_gpu = f_gpu.maker.fgraph.toposort() l_gpu = f_gpu.maker.fgraph.toposort()
assert numpy.any([isinstance(x.op, basic_ops.GpuAllocEmpty) for x in l_gpu]) assert numpy.any(
[isinstance(x.op, basic_ops.GpuAllocEmpty) for x in l_gpu])
f_cpu = theano.function([], tensor.AllocEmpty('int32')(2,3)) f_cpu = theano.function([], tensor.AllocEmpty('int32')(2, 3))
l_cpu = f_cpu.maker.fgraph.toposort() l_cpu = f_cpu.maker.fgraph.toposort()
assert not numpy.any([isinstance(x.op, basic_ops.GpuAllocEmpty) for x in l_cpu]) assert not numpy.any(
[isinstance(x.op, basic_ops.GpuAllocEmpty) for x in l_cpu])
class Test_local_elemwise_alloc(test_opt.Test_local_elemwise_alloc): class Test_local_elemwise_alloc(test_opt.Test_local_elemwise_alloc):
dtype = 'float32' dtype = 'float32'
...@@ -269,7 +264,8 @@ def test_gpuspecifyshape(): ...@@ -269,7 +264,8 @@ def test_gpuspecifyshape():
f = theano.function([], updates=[(x, m * numpy.float32(2))], f = theano.function([], updates=[(x, m * numpy.float32(2))],
mode=mode_with_gpu) mode=mode_with_gpu)
l = f.maker.fgraph.toposort() l = f.maker.fgraph.toposort()
assert not numpy.any([isinstance(x.op, cuda.HostFromGpu) for x in l]) assert not numpy.any(
[isinstance(x.op, cuda.HostFromGpu) for y in l])
def test_softmax(): def test_softmax():
...@@ -430,7 +426,7 @@ def test_local_gpu_subtensor(): ...@@ -430,7 +426,7 @@ def test_local_gpu_subtensor():
# Test multiple use of the input # Test multiple use of the input
# We want the subtensor to be on the GPU to prevent multiple transfer. # We want the subtensor to be on the GPU to prevent multiple transfer.
t = tensor.fmatrix() t = tensor.fmatrix()
f = theano.function([t], [t[3:4], t+1], mode=mode_with_gpu) f = theano.function([t], [t[3:4], t + 1], mode=mode_with_gpu)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert not any([type(node.op) is tensor.Subtensor for node in topo]) assert not any([type(node.op) is tensor.Subtensor for node in topo])
assert any([isinstance(node.op, cuda.GpuSubtensor) for node in topo]) assert any([isinstance(node.op, cuda.GpuSubtensor) for node in topo])
...@@ -438,7 +434,7 @@ def test_local_gpu_subtensor(): ...@@ -438,7 +434,7 @@ def test_local_gpu_subtensor():
# Test multiple use of the input + input as output # Test multiple use of the input + input as output
# We want the subtensor to be on the GPU to prevent multiple transfer. # We want the subtensor to be on the GPU to prevent multiple transfer.
t = tensor.fmatrix() t = tensor.fmatrix()
f = theano.function([t], [t[3:4], t+1, t], mode=mode_with_gpu) f = theano.function([t], [t[3:4], t + 1, t], mode=mode_with_gpu)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert not any([type(node.op) is tensor.Subtensor for node in topo]) assert not any([type(node.op) is tensor.Subtensor for node in topo])
assert any([isinstance(node.op, cuda.GpuSubtensor) for node in topo]) assert any([isinstance(node.op, cuda.GpuSubtensor) for node in topo])
...@@ -446,7 +442,7 @@ def test_local_gpu_subtensor(): ...@@ -446,7 +442,7 @@ def test_local_gpu_subtensor():
# Test shared forced on CPU end we do computation on the output of # Test shared forced on CPU end we do computation on the output of
# the subtensor. # the subtensor.
t = tensor._shared(numpy.zeros(20, "float32")) t = tensor._shared(numpy.zeros(20, "float32"))
f = theano.function([], t[3:4]+1, mode=mode_with_gpu) f = theano.function([], t[3:4] + 1, mode=mode_with_gpu)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert any([type(node.op) is tensor.Subtensor for node in topo]) assert any([type(node.op) is tensor.Subtensor for node in topo])
assert not any([isinstance(node.op, cuda.GpuSubtensor) for node in topo]) assert not any([isinstance(node.op, cuda.GpuSubtensor) for node in topo])
...@@ -507,10 +503,11 @@ def test_local_gpu_split(): ...@@ -507,10 +503,11 @@ def test_local_gpu_split():
def test_print_op(): def test_print_op():
""" Test that print ops don't block gpu optimization""" """ Test that print ops don't block gpu optimization"""
b = tensor.fmatrix() b = tensor.fmatrix()
f = theano.function([b], theano.printing.Print()(b)*2, mode=mode_with_gpu) f = theano.function(
[b], theano.printing.Print()(b) * 2, mode=mode_with_gpu)
# theano.printing.debugprint(f) # theano.printing.debugprint(f)
# print f.maker.fgraph.toposort() # print f.maker.fgraph.toposort()
#[GpuFromHost(<TensorType(float32, matrix)>), <theano.printing.Print object at 0x3581210>(GpuFromHost.0), GpuElemwise{mul}(CudaNdarray{[[ 2.]]}, <theano.printing.Print object at 0x3581210>.0), HostFromGpu(GpuElemwise{mul}.0)] # [GpuFromHost(<TensorType(float32, matrix)>), <theano.printing.Print object at 0x3581210>(GpuFromHost.0), GpuElemwise{mul}(CudaNdarray{[[ 2.]]}, <theano.printing.Print object at 0x3581210>.0), HostFromGpu(GpuElemwise{mul}.0)]
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert topo[0].op == cuda.gpu_from_host assert topo[0].op == cuda.gpu_from_host
assert isinstance(topo[1].op, theano.printing.Print) assert isinstance(topo[1].op, theano.printing.Print)
...@@ -563,8 +560,10 @@ def test_huge_elemwise_fusion(): ...@@ -563,8 +560,10 @@ def test_huge_elemwise_fusion():
bytes limits. bytes limits.
""" """
shape = (2, 3, 4, 5, 6) shape = (2, 3, 4, 5, 6)
ttype = tensor.tensor(dtype='float32', broadcastable=(False,) * len(shape)) ttype = tensor.tensor(dtype='float32',
gpu_ptr_size = theano.sandbox.cuda.opt.get_device_type_sizes()['gpu_ptr_size'] broadcastable=(False,) * len(shape))
gpu_ptr_size = theano.sandbox.cuda.opt.get_device_type_sizes()[
'gpu_ptr_size']
if gpu_ptr_size == 8: if gpu_ptr_size == 8:
nb_in = 7 nb_in = 7
len_topo = 10 len_topo = 10
...@@ -582,14 +581,19 @@ def test_huge_elemwise_fusion(): ...@@ -582,14 +581,19 @@ def test_huge_elemwise_fusion():
assert isinstance(topo[-3].op.scalar_op, theano.scalar.basic.Sub) assert isinstance(topo[-3].op.scalar_op, theano.scalar.basic.Sub)
assert isinstance(topo[-2].op.scalar_op, theano.scalar.basic.Composite) assert isinstance(topo[-2].op.scalar_op, theano.scalar.basic.Composite)
# let debugmode catch errors # let debugmode catch errors
gen = lambda: theano._asarray(numpy.random.rand(*shape), dtype='float32') # gen = lambda: theano._asarray(numpy.random.rand(*shape), dtype='float32')
def gen():
return(
theano._asarray(numpy.random.rand(*shape), dtype='float32'))
f(*[gen() for i in range(nb_in)]) f(*[gen() for i in range(nb_in)])
# Test the case where we can't put the computation on the gpu! their is too # Test the case where we can't put the computation on the gpu! their is too
# many dimensions to the input to have 2 inputs to the op! # many dimensions to the input to have 2 inputs to the op!
shape = (1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 2, 1, 2, 2, 2,) shape = (1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 2, 1, 2, 2, 2,)
ttype = tensor.tensor(dtype='float32', broadcastable=(False,) * len(shape)) ttype = tensor.tensor(
dtype='float32', broadcastable=(False,) * len(shape))
vars = [tensor.tanh(ttype) for x in range(7)] vars = [tensor.tanh(ttype) for x in range(7)]
f = pfunc(vars, [vars[0] - vars[1] - vars[2] - vars[3] - vars[4] - f = pfunc(vars, [vars[0] - vars[1] - vars[2] - vars[3] - vars[4] -
vars[5] - vars[6]], mode=mode_with_gpu) vars[5] - vars[6]], mode=mode_with_gpu)
...@@ -598,7 +602,9 @@ def test_huge_elemwise_fusion(): ...@@ -598,7 +602,9 @@ def test_huge_elemwise_fusion():
assert sum([isinstance(node.op, cuda.GpuElemwise) for node in topo]) == 0 assert sum([isinstance(node.op, cuda.GpuElemwise) for node in topo]) == 0
assert sum([isinstance(node.op, tensor.Elemwise) for node in topo]) == 1 assert sum([isinstance(node.op, tensor.Elemwise) for node in topo]) == 1
# let debugmode catch errors # let debugmode catch errors
gen = lambda: theano._asarray(numpy.random.rand(*shape), dtype='float32')
def gen():
return(theano._asarray(numpy.random.rand(*shape), dtype='float32'))
f(gen(), gen(), gen(), gen(), gen(), gen(), gen()) f(gen(), gen(), gen(), gen(), gen(), gen(), gen())
def gen(shape): def gen(shape):
...@@ -611,9 +617,9 @@ def test_huge_elemwise_fusion(): ...@@ -611,9 +617,9 @@ def test_huge_elemwise_fusion():
(2, 2, 2, 2), (2, 2, 2, 2),
(2, 2, 2, 2, 2), # 5d (2, 2, 2, 2, 2), # 5d
(2, 2, 2, 2, 2, 2), (2, 2, 2, 2, 2, 2),
# (2, 2, 2, 2, 2, 2, 2), # (2, 2, 2, 2, 2, 2, 2),
# (2, 2, 2, 2, 2, 2, 2, 2), # (2, 2, 2, 2, 2, 2, 2, 2),
# (2, 2, 2, 1, 1, 1, 1, 2, 2), # 9d # (2, 2, 2, 1, 1, 1, 1, 2, 2), # 9d
]: ]:
vals = [cuda.shared_constructor(gen(shape)) for x in range(max_var)] vals = [cuda.shared_constructor(gen(shape)) for x in range(max_var)]
for use_tan in [True, False]: for use_tan in [True, False]:
...@@ -676,7 +682,9 @@ def test_local_gpu_elemwise_0(): ...@@ -676,7 +682,9 @@ def test_local_gpu_elemwise_0():
a = tensor.fmatrix() a = tensor.fmatrix()
from theano.scalar.basic import identity from theano.scalar.basic import identity
out_s = theano.scalar.Composite([a_s, b_s, c_s], out_s = theano.scalar.Composite([a_s, b_s, c_s],
[identity(a_s), identity(c_s), identity(b_s)]) [identity(a_s),
identity(c_s),
identity(b_s)])
outs_op = tensor.Elemwise(out_s) outs_op = tensor.Elemwise(out_s)
f = theano.function([a, b, c], outs_op(a, b, c), mode=mode_with_gpu) f = theano.function([a, b, c], outs_op(a, b, c), mode=mode_with_gpu)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
...@@ -725,9 +733,6 @@ def test_elemwise_fusion(): ...@@ -725,9 +733,6 @@ def test_elemwise_fusion():
theano._asarray(numpy.random.rand(*shape), dtype='float32')) theano._asarray(numpy.random.rand(*shape), dtype='float32'))
import theano.tests.test_ifelse
class TestIfElse(theano.tests.test_ifelse.test_ifelse): class TestIfElse(theano.tests.test_ifelse.test_ifelse):
dtype = "float32" dtype = "float32"
mode = mode_with_gpu mode = mode_with_gpu
...@@ -765,15 +770,17 @@ def test_incsubtensor_mixed(): ...@@ -765,15 +770,17 @@ def test_incsubtensor_mixed():
def test_erfinvgpu(): def test_erfinvgpu():
""" Test that local_gpu_elemwise_0 replaces Erfinv with ErfinvGPU """ """ Test that local_gpu_elemwise_0 replaces Erfinv with ErfinvGPU """
x = tensor.fmatrix() x = tensor.fmatrix()
f = theano.function([x], tensor.Elemwise(erfinv)(x), mode=mode_with_gpu) f = theano.function([x],
f2 = theano.function([x], tensor.Elemwise(erfinv)(x), tensor.Elemwise(erfinv)(x),
mode=mode_with_gpu)
theano.function([x],
tensor.Elemwise(erfinv)(x),
mode=mode_without_gpu) mode=mode_without_gpu)
assert isinstance(f.maker.fgraph.toposort()[1].op, cuda.GpuElemwise) assert isinstance(f.maker.fgraph.toposort()[1].op,
cuda.GpuElemwise)
assert isinstance(f.maker.fgraph.toposort()[1].op.scalar_op, assert isinstance(f.maker.fgraph.toposort()[1].op.scalar_op,
cuda.elemwise.ErfinvGPU) cuda.elemwise.ErfinvGPU)
xv = numpy.random.rand(7, 8).astype('float32') numpy.random.rand(7, 8).astype('float32')
if imported_scipy_special:
assert numpy.allclose(f(xv), f2(xv))
def test_local_gpu_solve(): def test_local_gpu_solve():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论