提交 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',
"Another random number generator that work on the gpu",
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',
("Sets numpy's behaviour for floating-point errors, ",
"see numpy.seterr. "
......
import cPickle
from nose.tools import assert_raises
import numpy
import os.path
from theano import config
from theano.sandbox.cuda import cuda_available
if cuda_available:
from theano.sandbox.cuda import CudaNdarray
def test_unpickle_cudandarray_as_numpy_ndarray():
# testfile created on cuda enabled machine using
# >>> with open('CudaNdarray.pkl', 'wb') as fp:
# >>> cPickle.dump(theano.sandbox.cuda.CudaNdarray(np.array([-42.0], dtype=np.float32)), fp)
# testfile created on cuda enabled machine using
# >>> with open('CudaNdarray.pkl', 'wb') as 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__))
with open(os.path.join(testfile_dir, 'CudaNdarray.pkl')) as fp:
mat = cPickle.load(fp)
if cuda_available:
assert isinstance(mat, CudaNdarray)
mat = cPickle.load(fp)
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
\ No newline at end of file
config.experimental.unpickle_gpu_on_cpu = oldflag
......@@ -7,6 +7,7 @@ import warnings
import numpy
import theano
from theano import config
from theano import Type, Variable
from theano import tensor, config
from theano import scalar as scal
......@@ -488,12 +489,15 @@ theano.compile.register_deep_copy_op_c_code(
# equal the pickled version, and the cmodule cache is not happy with
# the situation.
def CudaNdarray_unpickler(npa):
if cuda:
return cuda.CudaNdarray(npa)
else:
if config.experimental.unpickle_gpu_on_cpu:
# 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
elif cuda:
return cuda.CudaNdarray(npa)
else:
raise ImportError("Cuda not found. Cannot unpickle CudaNdarray")
copy_reg.constructor(CudaNdarray_unpickler)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论