提交 d65d0915 authored 作者: Pascal Lamblin's avatar Pascal Lamblin 提交者: GitHub

Merge pull request #4841 from Erotemic/lint_cuda_init_indentation

Linting: Pep8 indentation, consistent quotes
...@@ -6,6 +6,7 @@ import os ...@@ -6,6 +6,7 @@ import os
import shutil import shutil
import stat import stat
import sys import sys
import textwrap
import warnings import warnings
import theano import theano
...@@ -82,10 +83,10 @@ def set_cuda_disabled(): ...@@ -82,10 +83,10 @@ def set_cuda_disabled():
cuda_path = os.path.abspath(os.path.split(__file__)[0]) cuda_path = os.path.abspath(os.path.split(__file__)[0])
cuda_ndarray_loc = os.path.join(config.compiledir, 'cuda_ndarray') cuda_ndarray_loc = os.path.join(config.compiledir, 'cuda_ndarray')
cuda_ndarray_so = os.path.join(cuda_ndarray_loc, cuda_ndarray_so = os.path.join(
'cuda_ndarray.' + get_lib_extension()) cuda_ndarray_loc, 'cuda_ndarray.' + get_lib_extension())
libcuda_ndarray_so = os.path.join(cuda_ndarray_loc, libcuda_ndarray_so = os.path.join(
'libcuda_ndarray.' + get_lib_extension()) cuda_ndarray_loc, 'libcuda_ndarray.' + get_lib_extension())
def try_import(): def try_import():
...@@ -280,22 +281,24 @@ def dnn_available(): ...@@ -280,22 +281,24 @@ def dnn_available():
dnn_available.msg = "Device not supported" dnn_available.msg = "Device not supported"
dnn_available.avail = False dnn_available.avail = False
else: else:
preambule = """ preambule = textwrap.dedent(
#include <stdio.h> """
#include <cuda.h> #include <stdio.h>
#include <cudnn.h> #include <cuda.h>
#include <cudnn_helper.h> #include <cudnn.h>
""" #include <cudnn_helper.h>
""")
body = """
cudnnHandle_t _handle = NULL; body = textwrap.dedent(
cudnnStatus_t err; """
if ((err = cudnnCreate(&_handle)) != CUDNN_STATUS_SUCCESS) { cudnnHandle_t _handle = NULL;
fprintf(stderr, "could not create cuDNN handle: %s", cudnnStatus_t err;
cudnnGetErrorString(err)); if ((err = cudnnCreate(&_handle)) != CUDNN_STATUS_SUCCESS) {
return 1; fprintf(stderr, "could not create cuDNN handle: %s",
} cudnnGetErrorString(err));
""" return 1;
}
""")
params = ["-l", "cudnn", "-I" + os.path.dirname(__file__)] params = ["-l", "cudnn", "-I" + os.path.dirname(__file__)]
if config.dnn.include_path: if config.dnn.include_path:
params.append("-I" + config.dnn.include_path) params.append("-I" + config.dnn.include_path)
...@@ -304,8 +307,8 @@ if ((err = cudnnCreate(&_handle)) != CUDNN_STATUS_SUCCESS) { ...@@ -304,8 +307,8 @@ if ((err = cudnnCreate(&_handle)) != CUDNN_STATUS_SUCCESS) {
if config.nvcc.compiler_bindir: if config.nvcc.compiler_bindir:
params.extend(['--compiler-bindir', params.extend(['--compiler-bindir',
config.nvcc.compiler_bindir]) config.nvcc.compiler_bindir])
params.extend([flag for flag in config.nvcc.flags.split(' ') if flag]) params.extend([flag for flag in config.nvcc.flags.split(' ') if flag])
# Do not run here the test program. It would run on the # Do not run here the test program. It would run on the
# default gpu, not the one selected by the user. If mixed # default gpu, not the one selected by the user. If mixed
# GPU are installed or if the GPUs are configured in # GPU are installed or if the GPUs are configured in
...@@ -367,24 +370,26 @@ class DnnVersion(GpuOp): ...@@ -367,24 +370,26 @@ class DnnVersion(GpuOp):
return ['-Wl,-rpath,' + config.dnn.library_path] return ['-Wl,-rpath,' + config.dnn.library_path]
def c_support_code(self): def c_support_code(self):
return """ return textwrap.dedent(
#if PY_MAJOR_VERSION >= 3 """
#define PyInt_FromLong PyLong_FromLong #if PY_MAJOR_VERSION >= 3
#endif #define PyInt_FromLong PyLong_FromLong
""" #endif
""")
def make_node(self): def make_node(self):
return theano.gof.Apply(self, [], [theano.gof.Generic()()]) return theano.gof.Apply(self, [], [theano.gof.Generic()()])
def c_code(self, node, name, inputs, outputs, sub): def c_code(self, node, name, inputs, outputs, sub):
o = outputs[0] o = outputs[0]
return """ return textwrap.dedent(
#if defined(CUDNN_VERSION) """
%(o)s = PyTuple_Pack(2, PyInt_FromLong(CUDNN_VERSION), PyInt_FromLong(cudnnGetVersion())); #if defined(CUDNN_VERSION)
#else %(o)s = PyTuple_Pack(2, PyInt_FromLong(CUDNN_VERSION), PyInt_FromLong(cudnnGetVersion()));
%(o)s = PyInt_FromLong(-1); #else
#endif %(o)s = PyInt_FromLong(-1);
""" % locals() #endif
""") % locals()
def do_constant_folding(self, node): def do_constant_folding(self, node):
# Needed as we do not want to cache this information. # Needed as we do not want to cache this information.
...@@ -423,12 +428,13 @@ if cuda_available: ...@@ -423,12 +428,13 @@ if cuda_available:
import cuda_ndarray.cuda_ndarray import cuda_ndarray.cuda_ndarray
if cuda_ndarray_so != cuda_ndarray.cuda_ndarray.__file__: if cuda_ndarray_so != cuda_ndarray.cuda_ndarray.__file__:
_logger.warning("cuda_ndarray was loaded from %s, but Theano expected " _logger.warning("cuda_ndarray was loaded from %s, but Theano expected "
"to load it from %s. This is not expected as theano should " "to load it from %s. This is not expected as theano "
"compile it automatically for you. Do you have a directory " "should compile it automatically for you. Do you have "
"called cuda_ndarray in your LD_LIBRARY_PATH environment " "a directory called cuda_ndarray in your "
"variable? If so, please remove it as it is outdated.", "LD_LIBRARY_PATH environment variable? If so, please "
cuda_ndarray.cuda_ndarray.__file__, "remove it as it is outdated.",
cuda_ndarray_so) cuda_ndarray.cuda_ndarray.__file__,
cuda_ndarray_so)
shared_constructor = float32_shared_constructor shared_constructor = float32_shared_constructor
...@@ -443,8 +449,8 @@ if cuda_available: ...@@ -443,8 +449,8 @@ if cuda_available:
ftensor3, ftensor4, ftensor3, ftensor4,
scalar, vector, matrix, row, col, scalar, vector, matrix, row, col,
tensor3, tensor4) tensor3, tensor4)
from .basic_ops import (host_from_gpu, gpu_from_host, from .basic_ops import (host_from_gpu, gpu_from_host, as_cuda_array,
as_cuda_array, as_cuda_ndarray_variable) as_cuda_ndarray_variable)
import cuda_ndarray import cuda_ndarray
from . import opt, dnn from . import opt, dnn
from .rng_curand import CURAND_RandomStreams from .rng_curand import CURAND_RandomStreams
...@@ -494,10 +500,11 @@ def use(device, ...@@ -494,10 +500,11 @@ def use(device,
raise EnvironmentError("You forced the use of gpu device %s, " raise EnvironmentError("You forced the use of gpu device %s, "
"but CUDA initialization failed " "but CUDA initialization failed "
"with error:\n%s" % ( "with error:\n%s" % (
device, cuda_initialization_error_message)) device,
cuda_initialization_error_message))
elif not nvcc_compiler.is_nvcc_available(): elif not nvcc_compiler.is_nvcc_available():
_logger.error('nvcc compiler not found on $PATH.' _logger.error("nvcc compiler not found on $PATH. "
' Check your nvcc installation and try again.') "Check your nvcc installation and try again.")
return return
elif not cuda_available: elif not cuda_available:
error_addendum = "" error_addendum = ""
...@@ -506,10 +513,10 @@ def use(device, ...@@ -506,10 +513,10 @@ def use(device,
error_addendum = (" (error: %s)" % error_addendum = (" (error: %s)" %
cuda_initialization_error_message) cuda_initialization_error_message)
except NameError: except NameError:
# cuda_initialization_error_message is not available b/c compilation failed # cuda_initialization_error_message is not available b/c compilation failed
pass pass
_logger.warning('CUDA is installed, but device %s is not available %s', _logger.warning("CUDA is installed, but device %s is not available %s",
device, error_addendum) device, error_addendum)
return return
if device == 'gpu': if device == 'gpu':
...@@ -622,8 +629,8 @@ def use(device, ...@@ -622,8 +629,8 @@ def use(device,
elif use.device_number != device and device != 'gpu': elif use.device_number != device and device != 'gpu':
_logger.warning(("Ignoring call to use(%s), GPU number %i " _logger.warning(("Ignoring call to use(%s), GPU number %i "
"is already in use."), "is already in use."),
str(device), use.device_number) str(device), use.device_number)
if move_shared_float32_to_gpu: if move_shared_float32_to_gpu:
handle_shared_float32(True) handle_shared_float32(True)
...@@ -701,11 +708,10 @@ elif config.init_gpu_device.startswith('gpu'): ...@@ -701,11 +708,10 @@ elif config.init_gpu_device.startswith('gpu'):
"We can use the Theano flag init_gpu_device" "We can use the Theano flag init_gpu_device"
" only when the Theano flag device=='cpu'") " only when the Theano flag device=='cpu'")
_logger.warning(("GPU device %s will be initialized, and used if a GPU is " _logger.warning(("GPU device %s will be initialized, and used if a GPU is "
"needed. " "needed. However, no computation, nor shared variables, "
"However, no computation, nor shared variables, will be implicitly " "will be implicitly moved to that device. If you want "
"moved to that device. If you want that behavior, use the 'device' " "that behavior, use the 'device' flag instead."),
"flag instead."), config.init_gpu_device)
config.init_gpu_device)
use(device=config.init_gpu_device, use(device=config.init_gpu_device,
force=config.force_device, force=config.force_device,
default_to_move_computation_to_gpu=False, default_to_move_computation_to_gpu=False,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论