Skip to content

fixes for support of new backend #836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions lasagne/layers/corrmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,34 @@
from .conv import conv_output_length, BaseConvLayer
from ..utils import as_tuple

from theano.sandbox.cuda.basic_ops import gpu_contiguous
from theano.sandbox.cuda.blas import GpuCorrMM

# check if Theano's new GPU backend is available and in use
try:
from theano import gpuarray as gpu
except ImportError:
from theano.sandbox import gpuarray as gpu
gpu_enabled = gpu.pygpu_activated
# if not, try to fall back to Theano's old GPU backend
if not gpu_enabled:
try:
from theano.sandbox import cuda as gpu
except ImportError:
gpu_enabled = False
else:
gpu_enabled = gpu.cuda_enabled
# if either of the backends is available, use it, otherwise bail out
if gpu_enabled:
gpu_contiguous = gpu.basic_ops.gpu_contiguous
GpuCorrMM = gpu.blas.GpuCorrMM
else:
raise ImportError(
"requires GPU support -- see http://lasagne.readthedocs.org/en/"
"latest/user/installation.html#gpu-support") # pragma: no cover
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also include the comments I had in my suggestion? (Except for the one in square brackets.)


__all__ = [
"Conv2DMMLayer",
]


if not theano.sandbox.cuda.cuda_enabled:
raise ImportError(
"requires GPU support -- see http://lasagne.readthedocs.org/en/"
"latest/user/installation.html#gpu-support") # pragma: no cover


class Conv2DMMLayer(BaseConvLayer):
"""
lasagne.layers.Conv2DMMLayer(incoming, num_filters, filter_size,
Expand Down
39 changes: 29 additions & 10 deletions lasagne/layers/dnn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import theano
from theano.sandbox.cuda import dnn

from .. import init
from .. import nonlinearities
Expand All @@ -10,16 +9,36 @@
from .normalization import BatchNormLayer
from ..utils import as_tuple

if not theano.sandbox.cuda.cuda_enabled:
# check if Theano's new GPU backend is available and in use
try:
from theano import gpuarray as gpu
except ImportError:
from theano.sandbox import gpuarray as gpu
gpu_enabled = gpu.pygpu_activated
dnn_enabled = gpu.dnn.dnn_present
# if not, try to fall back to Theano's old GPU backend
if not gpu_enabled:
try:
from theano.sandbox import cuda as gpu
import theano.sandbox.cuda.dnn
except ImportError:
gpu_enabled = False
else:
gpu_enabled = gpu.cuda_enabled
dnn_enabled = gpu.dnn.dnn_available
# if either of the backends is available, use it, otherwise bail out
if gpu_enabled:
if dnn_enabled():
dnn = gpu.dnn
else:
raise ImportError(
"cuDNN not available: %s\nSee http://lasagne.readthedocs.org\
/en/latest/user/installation.html#cudnn\
" % dnn_enabled.msg) # pragma: no cover
else:
raise ImportError(
"requires GPU support -- see http://lasagne.readthedocs.org/en/"
"latest/user/installation.html#gpu-support") # pragma: no cover
elif not dnn.dnn_available():
raise ImportError(
"cuDNN not available: %s\nSee http://lasagne.readthedocs.org/en/"
"latest/user/installation.html#cudnn" %
dnn.dnn_available.msg) # pragma: no cover

"requires GPU support -- see http://lasagne.readthedocs.org/en/"
"latest/user/installation.html#gpu-support") # pragma: no cover
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, please include the comments I had in my suggestion.


__all__ = [
"Pool2DDNNLayer",
Expand Down
38 changes: 31 additions & 7 deletions lasagne/tests/layers/test_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
import lasagne
from lasagne.utils import floatX, as_tuple

try:
from theano import gpuarray
theano_backend = "pygpu"
except ImportError:
from theano.sandbox import gpuarray
theano_backend = "pygpu_sandbox"
gpu = gpuarray.pygpu_activated
if not gpu:
try:
from theano.sandbox import cuda
theano_backend = "cuda_sandbox"
gpu = cuda.cuda_enabled
except ImportError:
gpu = False
if not gpu:
theano_backend = "cpu"


def convNd(input, kernel, pad, stride=1, n=None):
"""Execute a batch of a stack of N-dimensional convolutions.
Expand Down Expand Up @@ -660,18 +677,26 @@ def test_unsupported_settings(self, DummyInputLayer):

class TestConv2DDNNLayer:
def test_import_without_gpu_or_cudnn_raises(self):
from theano.sandbox import cuda
if cuda.cuda_enabled and cuda.dnn.dnn_available():
pytest.skip()
if theano_backend == 'pygpu':
from theano.gpuarray import dnn
if dnn.dnn_present():
pytest.skip()
elif theano_backend == 'pygpu_sandbox':
from theano.sandbox.gpuarray import dnn
if dnn.dnn_present():
pytest.skip()
elif theano_backend == 'cuda_sandbox':
from theano.sandbox.cuda import dnn
if dnn.dnn_available():
pytest.skip()
else:
with pytest.raises(ImportError):
import lasagne.layers.dnn


class TestConv2DMMLayer:
def test_import_without_gpu_raises(self):
from theano.sandbox import cuda
if cuda.cuda_enabled:
if theano_backend in ['pygpu', 'pygpu_sandbox', 'cuda_sandbox']:
pytest.skip()
else:
with pytest.raises(ImportError):
Expand All @@ -680,8 +705,7 @@ def test_import_without_gpu_raises(self):

class TestConv2DCCLayer:
def test_import_without_gpu_raises(self):
from theano.sandbox import cuda
if cuda.cuda_enabled:
if theano_backend in ['pygpu', 'pygpu_sandbox', 'cuda_sandbox']:
pytest.skip()
else:
with pytest.raises(ImportError):
Expand Down
3 changes: 1 addition & 2 deletions lasagne/tests/layers/test_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ def test_get_output_for_non_deterministic(self, layer):
def test_get_output_for_deterministic(self, layer):
input = theano.shared(numpy.ones((3, 3)))
result = layer.get_output_for(input, deterministic=True)
result_eval = result.eval()
assert (result_eval == input.eval()).all()
assert np.allclose(result.eval(), input.eval())

def test_specified_rng(self, input_layer):
from lasagne.layers.noise import GaussianNoiseLayer
Expand Down
10 changes: 6 additions & 4 deletions lasagne/tests/layers/test_special.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
import theano
from lasagne.layers import InputLayer, standardize, get_output, get_all_params
from lasagne.utils import floatX


class TestExpressionLayer:
Expand Down Expand Up @@ -633,8 +634,8 @@ def ParametricRectifierLayer(self):
@pytest.fixture
def init_alpha(self):
# initializer for a tensor of unique values
return lambda shape: (np.arange(np.prod(shape)).reshape(shape)) \
/ np.prod(shape)
return lambda shape: floatX((np.arange(
np.prod(shape)).reshape(shape)) / floatX(np.prod(shape)))

def test_alpha_init(self, ParametricRectifierLayer, init_alpha):
input_shape = (None, 3, 28, 28)
Expand Down Expand Up @@ -698,7 +699,8 @@ def test_get_output_for(self, ParametricRectifierLayer, init_alpha):
alpha_v = layer.alpha.get_value()
expected = np.maximum(input, 0) + np.minimum(input, 0) * \
alpha_v[None, :, :, :]
assert np.allclose(layer.get_output_for(input).eval(), expected)
assert np.allclose(layer.get_output_for(input).eval(), expected,
atol=1e-07)

# alphas shared over the 1st and 4th axes
layer = ParametricRectifierLayer(input_shape, shared_axes=(0, 3),
Expand Down Expand Up @@ -726,7 +728,7 @@ def test_prelu(self, init_alpha):
expected = np.dot(input, W) + b
expected = np.maximum(expected, 0) + \
np.minimum(expected, 0) * alpha_v
assert np.allclose(output.eval(), expected)
assert np.allclose(output.eval(), expected, atol=1e-07)


class TestRandomizedRectifierLayer:
Expand Down