提交 c23321fb authored 作者: abergeron's avatar abergeron

Merge pull request #1734 from herr-biber/nocuda-unpickle-cudandarray-as-nparray

Unpickle CudaNdarray as numpy.ndarray, controlled by an experimental flag.
......@@ -268,6 +268,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. "
......
......@@ -185,7 +185,7 @@ def AddConfigVar(name, doc, configparam, root=config, in_c_key=True):
parameter
:type root: object
:param root: used for recusive calls -- do not provide an argument for
:param root: used for recursive calls -- do not provide an argument for
this parameter.
:type in_c_key: boolean
......
ctheano.sandbox.cuda.type
CudaNdarray_unpickler
p1
(cnumpy.core.multiarray
_reconstruct
p2
(cnumpy
ndarray
p3
(I0
tS'b'
tRp4
(I1
(I1
tcnumpy
dtype
p5
(S'f4'
I0
I1
tRp6
(I3
S'<'
NNNI-1
I-1
I0
tbI00
S'\x00\x00(\xc2'
tbtR.
\ No newline at end of file
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
# 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, "Config flag experimental.unpickle_gpu_on_cpu is " \
+ "set to true. Make sure the default value stays false " \
+ "and that you have not set the flag manually."
def test_unpickle_cudandarray_as_numpy_ndarray_flag0():
oldflag = config.experimental.unpickle_gpu_on_cpu
config.experimental.unpickle_gpu_on_cpu = False
try:
testfile_dir = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(testfile_dir, 'CudaNdarray.pkl')) as fp:
if cuda_available:
mat = cPickle.load(fp)
else:
assert_raises(ImportError, cPickle.load, fp)
if cuda_available:
assert isinstance(mat, CudaNdarray)
assert numpy.asarray(mat)[0] == -42.0
finally:
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
try:
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
finally:
config.experimental.unpickle_gpu_on_cpu = oldflag
......@@ -2,10 +2,12 @@
"""
import os
import copy_reg
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
......@@ -487,7 +489,16 @@ 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 config.experimental.unpickle_gpu_on_cpu:
# directly return numpy array
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论