提交 91f08497 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Merge pull request #3731 from nouiz/gpualloc_empty

Lift GpuAllocEmpty in the new GPU back-end.
...@@ -779,6 +779,17 @@ class Op(utils.object2, PureOp, CLinkerOp): ...@@ -779,6 +779,17 @@ class Op(utils.object2, PureOp, CLinkerOp):
def _props(self): def _props(self):
return tuple(getattr(self, a) for a in self.__props__) return tuple(getattr(self, a) for a in self.__props__)
def _props_dict(self):
"""This return a dict of all ``__props__`` key-> value.
This is useful in optimization to swap op that should have the
same props. This help detect error that the new op have at
least all the original props.
"""
return dict([(a, getattr(self, a))
for a in self.__props__])
def __hash__(self): def __hash__(self):
if hasattr(self, '__props__'): if hasattr(self, '__props__'):
return hash((type(self), self._props())) return hash((type(self), self._props()))
......
from __future__ import print_function from __future__ import print_function
from itertools import count
import pickle import pickle
import unittest import unittest
import numpy
from itertools import count
from nose.plugins.skip import SkipTest
import numpy
from theano import ( from theano import (
sparse, sparse,
...@@ -361,6 +362,8 @@ class TestAutoName: ...@@ -361,6 +362,8 @@ class TestAutoName:
def test_sparsevariable(self): def test_sparsevariable(self):
# Get counter value # Get counter value
if not sparse.enable_sparse:
raise SkipTest('Optional package SciPy not installed')
autoname_id = next(Variable.__count__) autoname_id = next(Variable.__count__)
Variable.__count__ = count(autoname_id) Variable.__count__ = count(autoname_id)
r1 = sparse.csc_matrix(name='x', dtype='float32') r1 = sparse.csc_matrix(name='x', dtype='float32')
......
...@@ -323,7 +323,7 @@ def test_opt_gpujoin_joinvectors_elemwise_then_minusone(): ...@@ -323,7 +323,7 @@ def test_opt_gpujoin_joinvectors_elemwise_then_minusone():
assert isinstance(graph_nodes[-2].op, cuda.GpuSubtensor) assert isinstance(graph_nodes[-2].op, cuda.GpuSubtensor)
assert isinstance(graph_nodes[-3].op, cuda.GpuJoin) assert isinstance(graph_nodes[-3].op, cuda.GpuJoin)
concat = numpy.concatenate([numpy.cos(_a), numpy.sin(_b)], axis=1) concat = numpy.concatenate([numpy.cos(_a), numpy.sin(_b)], axis=0)
concat = concat[:-1] concat = concat[:-1]
assert numpy.allclose(numpy.asarray(f()), concat) assert numpy.allclose(numpy.asarray(f()), concat)
......
...@@ -273,6 +273,15 @@ def local_gpuaalloc(node, context_name): ...@@ -273,6 +273,15 @@ def local_gpuaalloc(node, context_name):
return GpuAlloc(context_name)(*node.inputs) return GpuAlloc(context_name)(*node.inputs)
@register_opt('fast_compile')
@op_lifter([tensor.AllocEmpty])
def local_gpuaallocempty(node, context_name):
# We use _props_dict() to make sure that the GPU op know all the
# CPU op props.
return GpuAllocEmpty(context_name=context_name,
**node.op._props_dict())(*node.inputs)
@register_opt() @register_opt()
@local_optimizer([GpuAlloc]) @local_optimizer([GpuAlloc])
def local_gpualloc_memset_0(node): def local_gpualloc_memset_0(node):
......
...@@ -9,7 +9,8 @@ from theano.tensor.tests import test_basic ...@@ -9,7 +9,8 @@ from theano.tensor.tests import test_basic
import theano.sandbox.gpuarray import theano.sandbox.gpuarray
from .. import basic_ops from .. import basic_ops
from ..type import GpuArrayType, gpuarray_shared_constructor, get_context from ..type import GpuArrayType, gpuarray_shared_constructor, get_context
from ..basic_ops import GpuAlloc, GpuReshape, GpuFromHost, host_from_gpu from ..basic_ops import (
GpuAlloc, GpuAllocEmpty, GpuReshape, GpuFromHost, host_from_gpu)
from ..elemwise import GpuCAReduceCuda, GpuCAReduceCPY, GpuElemwise from ..elemwise import GpuCAReduceCuda, GpuCAReduceCPY, GpuElemwise
from ..subtensor import GpuSubtensor from ..subtensor import GpuSubtensor
...@@ -151,6 +152,29 @@ def test_local_gpualloc_memset_0(): ...@@ -151,6 +152,29 @@ def test_local_gpualloc_memset_0():
assert (numpy.asarray(f(2)) == 1).all() assert (numpy.asarray(f(2)) == 1).all()
def test_local_gpualloc_empty():
i = theano.tensor.iscalar()
ii = theano.tensor.iscalar()
# Test with vector
a = tensor.AllocEmpty('float32')(i)
f = theano.function([i], a, mode=mode_with_gpu)
topo = f.maker.fgraph.toposort()
assert len(topo) == 2
assert isinstance(topo[0].op, GpuAllocEmpty)
# This return not initilized data, so we can only check the shape
assert f(3).shape == (3,)
# Test with matrix
a = tensor.AllocEmpty('float32')(i, ii)
f = theano.function([i, ii], a, mode=mode_with_gpu)
topo = f.maker.fgraph.toposort()
assert len(topo) == 2
assert isinstance(topo[0].op, GpuAllocEmpty)
# This return not initilized data, so we can only check the shape
assert f(3, 4).shape == (3, 4)
def test_rebroadcast(): def test_rebroadcast():
d = numpy.random.rand(10, 10).astype('float32') d = numpy.random.rand(10, 10).astype('float32')
v = theano.tensor.fmatrix() v = theano.tensor.fmatrix()
......
import unittest
import numpy import numpy
import numpy.linalg import numpy.linalg
from numpy.testing import assert_array_almost_equal
from numpy.testing import dec, assert_array_equal, assert_allclose
from numpy import inf
import theano import theano
from theano import tensor, function from theano import tensor, function
...@@ -16,35 +11,17 @@ from theano.tensor.nlinalg import MatrixInverse ...@@ -16,35 +11,17 @@ from theano.tensor.nlinalg import MatrixInverse
from theano.tensor import DimShuffle from theano.tensor import DimShuffle
# The one in comment are not tested... # The one in comment are not tested...
from theano.sandbox.linalg.ops import (cholesky, from theano.sandbox.linalg.ops import (Cholesky, # op class
Cholesky, # op class
CholeskyGrad,
matrix_inverse, matrix_inverse,
pinv,
Solve, Solve,
solve, solve,
diag,
ExtractDiag,
extract_diag,
AllocDiag,
alloc_diag,
det,
svd,
qr,
# PSD_hint, # PSD_hint,
trace,
matrix_dot,
spectral_radius_bound, spectral_radius_bound,
imported_scipy, imported_scipy,
Eig,
inv_as_solve, inv_as_solve,
norm
) )
from theano.sandbox.linalg import eig, eigh, eigvalsh
from theano.tests.unittest_tools import attr
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose.tools import assert_raises
def test_rop_lop(): def test_rop_lop():
...@@ -156,6 +133,8 @@ def test_transinv_to_invtrans(): ...@@ -156,6 +133,8 @@ def test_transinv_to_invtrans():
def test_tag_solve_triangular(): def test_tag_solve_triangular():
if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky op.")
cholesky_lower = Cholesky(lower=True) cholesky_lower = Cholesky(lower=True)
cholesky_upper = Cholesky(lower=False) cholesky_upper = Cholesky(lower=False)
A = tensor.matrix('A') A = tensor.matrix('A')
......
...@@ -24,6 +24,8 @@ class TestCorr2D(utt.InferShapeTester): ...@@ -24,6 +24,8 @@ class TestCorr2D(utt.InferShapeTester):
self.filters.name = 'default_filters' self.filters.name = 'default_filters'
if not conv.imported_scipy_signal and theano.config.cxx == "": if not conv.imported_scipy_signal and theano.config.cxx == "":
raise SkipTest("CorrMM tests need SciPy or a c++ compiler") raise SkipTest("CorrMM tests need SciPy or a c++ compiler")
if not theano.config.blas.ldflags:
raise SkipTest("CorrMM tests need a BLAS")
def validate(self, image_shape, filter_shape, def validate(self, image_shape, filter_shape,
border_mode='valid', subsample=(1, 1), border_mode='valid', subsample=(1, 1),
......
...@@ -67,7 +67,10 @@ class test_ifelse(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -67,7 +67,10 @@ class test_ifelse(unittest.TestCase, utt.TestOptimizationMixin):
y2 = reduce(lambda x, y: x + y, [y] + list(range(200))) y2 = reduce(lambda x, y: x + y, [y] + list(range(200)))
f = theano.function([c, x, y], ifelse(c, x, y2), mode=mode) f = theano.function([c, x, y], ifelse(c, x, y2), mode=mode)
# For not inplace ifelse # For not inplace ifelse
self.assertFunctionContains1(f, IfElse(1)) ifnode = [n for n in f.maker.fgraph.toposort()
if isinstance(n.op, IfElse)]
assert len(ifnode) == 1
assert not ifnode[0].op.as_view
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
xlen = rng.randint(200) xlen = rng.randint(200)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论