提交 6ba34a31 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Make some other changes to allow the loading to transfer from CPU to GPU if appropriate.

上级 26429e59
...@@ -2,6 +2,7 @@ import atexit, logging, os, stat, sys ...@@ -2,6 +2,7 @@ import atexit, logging, os, stat, sys
from theano.compile import optdb from theano.compile import optdb
from theano.gof.cmodule import get_lib_extension from theano.gof.cmodule import get_lib_extension
from theano.configparser import config, AddConfigVar, StrParam from theano.configparser import config, AddConfigVar, StrParam
from theano.tensor.sharedvar import load_shared_variable
import nvcc_compiler import nvcc_compiler
_logger_name = 'theano.sandbox.cuda' _logger_name = 'theano.sandbox.cuda'
...@@ -279,22 +280,20 @@ def handle_shared_float32(tf): ...@@ -279,22 +280,20 @@ def handle_shared_float32(tf):
theano.compile.shared_constructor(float32_shared_constructor) theano.compile.shared_constructor(float32_shared_constructor)
# this is a bit of hackery to make the shared variables load # this is a bit of hackery to make the shared variables load
# with the proper type. # with the proper type.
copy_reg.pickle(theano.gof.graph.Apply, reduce_apply, copy_reg.pickle(theano.tensor.basic.TensorVariable,
load_shared_pickle) reduce_tensor_variable,
load_shared_variable)
else: else:
raise NotImplementedError('removing our handler') raise NotImplementedError('removing our handler')
def reduce_apply(apply): def reduce_tensor_variable(var):
if isinstance(apply.op, HostFromGpu) and len(apply.inputs) == 1 and \ if isinstance(var.owner.op, HostFromGpu) and len(var.owner.inputs) == 1 \
isinstance(apply.inputs[0], CudaNdarraySharedVariable): and isinstance(var.owner.inputs[0], CudaNdarraySharedVariable):
return load_shared_pickle, apply.inputs[0].get_value() return load_shared_variable, (var.owner.inputs[0].get_value(),)
else: else:
# this will make protocol 2 a little bit less efficient # this will make protocol 2 a little bit less efficient
# but there is no way around it. # but there is no way around it.
return apply.__reduce__() return var.__reduce__()
def load_shared_pickle(val):
return theano.tensor.as_tensor_variable(theano.shared(val))
if config.device.startswith('gpu'): if config.device.startswith('gpu'):
use(device=config.device, force=config.force_device) use(device=config.device, force=config.force_device)
......
...@@ -5,9 +5,17 @@ from basic import TensorType, _tensor_py_operators, autocast_int, autocast_float ...@@ -5,9 +5,17 @@ from basic import TensorType, _tensor_py_operators, autocast_int, autocast_float
from theano.compile import shared_constructor, SharedVariable from theano.compile import shared_constructor, SharedVariable
from theano import config from theano import config
def load_shared_variable(val):
return theano.tensor.as_tensor_variable(theano.shared(val))
# _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__ # _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__
class TensorSharedVariable(_tensor_py_operators, SharedVariable): class TensorSharedVariable(_tensor_py_operators, SharedVariable):
pass def __reduce_ex__(self, proto):
# This is for loading on the GPU if present.
if self.dtype == 'float32':
return load_shared_variable, (self.get_value(),)
else:
return super(TensorSharedVariable, self).__reduce_ex__(proto)
@shared_constructor @shared_constructor
def tensor_constructor(value, name=None, strict=False, allow_downcast=None, borrow=False, broadcastable=None): def tensor_constructor(value, name=None, strict=False, allow_downcast=None, borrow=False, broadcastable=None):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论