提交 cb566da6 authored 作者: Markus Roth's avatar Markus Roth

Add config flag experimental.unpickle_gpu_on_cpu.

Allows unpickling gpu cudaNdarray as numpy.ndarray. See AddConfigVar comment for more details.
上级 0c9e4255
...@@ -266,6 +266,19 @@ AddConfigVar('experimental.mrg', ...@@ -266,6 +266,19 @@ AddConfigVar('experimental.mrg',
"Another random number generator that work on the gpu", "Another random number generator that work on the gpu",
BoolParam(False)) BoolParam(False))
AddConfigVar('experimental.unpickle_gpu_on_cpu',
"Allow unpickling of pickled CudaNdarrays as numpy.ndarrays."
"This is useful, if you want to open a CudaNdarray without "
"having cuda installed."
"If you have cuda installed, this will force unpickling to"
"be done on the cpu to numpy.ndarray."
"Please be aware that this may get you access to the data,"
"however, trying to unpicke gpu functions will not succeed."
"This flag is experimental and may be removed any time, when"
"gpu<>cpu transparency is solved.",
BoolParam(default=False),
in_c_key=False)
AddConfigVar('numpy.seterr_all', AddConfigVar('numpy.seterr_all',
("Sets numpy's behaviour for floating-point errors, ", ("Sets numpy's behaviour for floating-point errors, ",
"see numpy.seterr. " "see numpy.seterr. "
......
import cPickle import cPickle
from nose.tools import assert_raises
import numpy import numpy
import os.path import os.path
from theano import config
from theano.sandbox.cuda import cuda_available from theano.sandbox.cuda import cuda_available
if cuda_available: if cuda_available:
from theano.sandbox.cuda import CudaNdarray from theano.sandbox.cuda import CudaNdarray
def test_unpickle_cudandarray_as_numpy_ndarray(): # testfile created on cuda enabled machine using
# testfile created on cuda enabled machine using # >>> with open('CudaNdarray.pkl', 'wb') as fp:
# >>> with open('CudaNdarray.pkl', 'wb') as fp: # >>> cPickle.dump(theano.sandbox.cuda.CudaNdarray(np.array([-42.0], dtype=np.float32)), fp)
# >>> cPickle.dump(theano.sandbox.cuda.CudaNdarray(np.array([-42.0], dtype=np.float32)), fp)
def test_unpickle_flag_is_false_by_default():
assert not config.experimental.unpickle_gpu_on_cpu
def test_unpickle_cudandarray_as_numpy_ndarray_flag0():
oldflag = config.experimental.unpickle_gpu_on_cpu
config.experimental.unpickle_gpu_on_cpu = False
testfile_dir = os.path.dirname(os.path.realpath(__file__)) testfile_dir = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(testfile_dir, 'CudaNdarray.pkl')) as fp: with open(os.path.join(testfile_dir, 'CudaNdarray.pkl')) as fp:
mat = cPickle.load(fp)
if cuda_available: if cuda_available:
assert isinstance(mat, CudaNdarray) mat = cPickle.load(fp)
else: else:
assert isinstance(mat, numpy.ndarray) assert_raises(ImportError, cPickle.load, fp)
if cuda_available:
assert isinstance(mat, CudaNdarray)
assert mat[0] == -42.0
config.experimental.unpickle_gpu_on_cpu = oldflag
def test_unpickle_cudandarray_as_numpy_ndarray_flag1():
oldflag = config.experimental.unpickle_gpu_on_cpu
config.experimental.unpickle_gpu_on_cpu = True
testfile_dir = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(testfile_dir, 'CudaNdarray.pkl')) as fp:
mat = cPickle.load(fp)
assert isinstance(mat, numpy.ndarray)
assert mat[0] == -42.0
assert mat[0] == -42.0 config.experimental.unpickle_gpu_on_cpu = oldflag
\ No newline at end of file
...@@ -7,6 +7,7 @@ import warnings ...@@ -7,6 +7,7 @@ import warnings
import numpy import numpy
import theano import theano
from theano import config
from theano import Type, Variable from theano import Type, Variable
from theano import tensor, config from theano import tensor, config
from theano import scalar as scal from theano import scalar as scal
...@@ -488,12 +489,15 @@ theano.compile.register_deep_copy_op_c_code( ...@@ -488,12 +489,15 @@ theano.compile.register_deep_copy_op_c_code(
# equal the pickled version, and the cmodule cache is not happy with # equal the pickled version, and the cmodule cache is not happy with
# the situation. # the situation.
def CudaNdarray_unpickler(npa): def CudaNdarray_unpickler(npa):
if cuda:
return cuda.CudaNdarray(npa) if config.experimental.unpickle_gpu_on_cpu:
else:
# directly return numpy array # directly return numpy array
warnings.warn("CUDA not found. Unpickling CudaNdarray as numpy.ndarray") warnings.warn("config.experimental.unpickle_gpu_on_cpu is set to True. Unpickling CudaNdarray as numpy.ndarray")
return npa return npa
elif cuda:
return cuda.CudaNdarray(npa)
else:
raise ImportError("Cuda not found. Cannot unpickle CudaNdarray")
copy_reg.constructor(CudaNdarray_unpickler) copy_reg.constructor(CudaNdarray_unpickler)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论