提交 017a831c authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Add kind and context to the GpuArrayType class and use it when needed. Also the…

Add kind and context to the GpuArrayType class and use it when needed. Also the test should pass now.
上级 481cf68a
import logging
import theano import theano
from theano.configparser import config, AddConfigVar, StrParam, \ from theano.configparser import config, AddConfigVar, StrParam, \
BoolParam, IntParam BoolParam, IntParam
...@@ -6,6 +8,8 @@ _logger_name = 'theano.sandbox.gpuarray' ...@@ -6,6 +8,8 @@ _logger_name = 'theano.sandbox.gpuarray'
_logger = logging.getLogger(_logger_name) _logger = logging.getLogger(_logger_name)
_logger.setLevel(logging.WARNING) _logger.setLevel(logging.WARNING)
error = _logger.error
try: try:
import pygpu.gpuarray import pygpu.gpuarray
except ImportError: except ImportError:
...@@ -33,7 +37,7 @@ def init_dev(dev): ...@@ -33,7 +37,7 @@ def init_dev(dev):
devnum = int(dev[4:]) devnum = int(dev[4:])
elif dev.startswith('opencl'): elif dev.startswith('opencl'):
globals.kind = 'opencl' globals.kind = 'opencl'
devspec = dev[6:] devspec = dev[7:]
plat, dev = devspec.split(':') plat, dev = devspec.split(':')
devnum = int(dev)|(int(plat)>>16) devnum = int(dev)|(int(plat)>>16)
else: else:
...@@ -41,7 +45,6 @@ def init_dev(dev): ...@@ -41,7 +45,6 @@ def init_dev(dev):
if globals.kind: if globals.kind:
globals.context = pygpu.gpuarray.init(globals.kind, devnum) globals.context = pygpu.gpuarray.init(globals.kind, devnum)
if pygpu: if pygpu:
try: try:
if (config.device.startswith('cuda') or if (config.device.startswith('cuda') or
...@@ -53,5 +56,5 @@ if pygpu: ...@@ -53,5 +56,5 @@ if pygpu:
elif config.gpuarray.init_device != '': elif config.gpuarray.init_device != '':
init_dev(config.gpuarray.init_device) init_dev(config.gpuarray.init_device)
except Exception: except Exception:
print >> sys.stderr, "Could not initialize pygpu, support disabled" error("Could not initialize pygpu, support disabled", exc_info=True)
pygpu = None pygpu = None
...@@ -7,8 +7,7 @@ from theano.scalar import Scalar ...@@ -7,8 +7,7 @@ from theano.scalar import Scalar
from theano.gof.python25 import all, any from theano.gof.python25 import all, any
from theano.sandbox.cuda.type import CudaNdArrayType import pygpu
from pygpu import gpuarray, elemwise from pygpu import gpuarray, elemwise
from type import GpuArrayType from type import GpuArrayType
...@@ -21,8 +20,8 @@ def as_gpuarray_variable(x): ...@@ -21,8 +20,8 @@ def as_gpuarray_variable(x):
return gpu_from_host(tensor_x) return gpu_from_host(tensor_x)
def as_gpuarray(x): def as_gpuarray(x, kind, context):
return gpuarray.array(x, copy=False) return gpuarray.array(x, kind=kind, context=context, copy=False)
class HostFromGpu(Op): class HostFromGpu(Op):
...@@ -73,12 +72,13 @@ class GpuFromHost(Op): ...@@ -73,12 +72,13 @@ class GpuFromHost(Op):
if not isinstance(x.type, tensor.TensorType): if not isinstance(x.type, tensor.TensorType):
raise TypeError(x) raise TypeError(x)
return Apply(self, [x], [GpuArrayType(broadcastable=x.broadcastable, return Apply(self, [x], [GpuArrayType(broadcastable=x.broadcastable,
dtype=x.dtype)]()) dtype=x.dtype)()])
def perform(self, node, inp, out): def perform(self, node, inp, out):
x, = inp x, = inp
z, = out z, = out
z[0] = gpuarray.array(x) type = node.outputs[0].type
z[0] = gpuarray.array(x, kind=type.kind, context=type.context)
def grad(self, inputs, grads): def grad(self, inputs, grads):
gz, = grads gz, = grads
...@@ -119,8 +119,8 @@ class GpuFromCuda(Op): ...@@ -119,8 +119,8 @@ class GpuFromCuda(Op):
while hasattr(base, 'base') and base.base is not None: while hasattr(base, 'base') and base.base is not None:
base = base.base base = base.base
raise NotImplementedError("How are we going to get a gpudata pointer from here") raise NotImplementedError("How are we going to get a gpudata pointer from here")
x[0] = gpuarray.from_gpudata(b, 0, base=base, x.dtype, x[0] = gpuarray.from_gpudata(b, 0, x.dtype, x.shape,
x.shape, kind=globals.kind, base=base, kind=globals.kind,
context=globals.context, context=globals.context,
strides=x.strides) strides=x.strides)
else: else:
......
...@@ -5,11 +5,10 @@ import numpy ...@@ -5,11 +5,10 @@ import numpy
import theano import theano
from theano import Type, Variable, tensor, config, scalar from theano import Type, Variable, tensor, config, scalar
import globals
# Make sure this is importable even if pygpu is absent # Make sure this is importable even if pygpu is absent
# (it will not work though) # (it will not work though)
try: try:
import pygpu
from pygpu import gpuarray from pygpu import gpuarray
from pygpu.elemwise import compare from pygpu.elemwise import compare
except ImportError: except ImportError:
...@@ -22,12 +21,21 @@ class GpuArrayType(Type): ...@@ -22,12 +21,21 @@ class GpuArrayType(Type):
@staticmethod @staticmethod
def value_zeros(*args, **kwargs): def value_zeros(*args, **kwargs):
pygpu.gpuarray.zeros(*args, **kwargs, kind=globals.kind, pygpu.gpuarray.zeros(*args, kind=globals.kind,
context=globals.context) context=globals.context, **kwargs)
def __init__(self, dtype, broadcastable, name=None): def __init__(self, dtype, broadcastable, kind=None, context=None,
name=None):
import globals
if kind is None:
kind = globals.kind
if context is None:
context = globals.context
self.dtype = str(dtype) self.dtype = str(dtype)
self.broadcastable = tuple(bool(b), for b in broadcastable) self.broadcastable = tuple(bool(b) for b in broadcastable)
self.ndim = len(self.broadcastable)
self.kind = kind
self.context = context
self.name = name self.name = name
try: try:
self.typecode = gpuarray.dtype_to_typecode(self.dtype) self.typecode = gpuarray.dtype_to_typecode(self.dtype)
...@@ -40,6 +48,10 @@ class GpuArrayType(Type): ...@@ -40,6 +48,10 @@ class GpuArrayType(Type):
if not isinstance(data, gpuarray.GpuArray): if not isinstance(data, gpuarray.GpuArray):
raise TypeError("%s expected a GpuArray object." % self, raise TypeError("%s expected a GpuArray object." % self,
data, type(data)) data, type(data))
if self.kind != data.kind:
raise TypeError("kind of GpuArray does not match")
if self.context != data.context:
raise TypeError("context of GpuArray differs")
if self.typecode != data.typecode: if self.typecode != data.typecode:
raise TypeError("%s expected typecode %d (dtype %s), " raise TypeError("%s expected typecode %d (dtype %s), "
"got %d (dtype %s)." % "got %d (dtype %s)." %
...@@ -48,7 +60,7 @@ class GpuArrayType(Type): ...@@ -48,7 +60,7 @@ class GpuArrayType(Type):
# fallthrough to ndim check # fallthrough to ndim check
elif allow_downcast: elif allow_downcast:
data = gpuarray.array(data, dtype=self.typecode, copy=False, data = gpuarray.array(data, dtype=self.typecode, copy=False,
kind=globals.kind, context=globals.context, kind=self.kind, context=self.context,
ndmin=len(self.broadcastable)) ndmin=len(self.broadcastable))
else: else:
if isinstance(data, gpuarray.GpuArray): if isinstance(data, gpuarray.GpuArray):
...@@ -65,12 +77,10 @@ class GpuArrayType(Type): ...@@ -65,12 +77,10 @@ class GpuArrayType(Type):
"got %s with shape %s." % (self.ndim, data.ndim, "got %s with shape %s." % (self.ndim, data.ndim,
data.shape), data) data.shape), data)
shp = data.shape shp = data.shape
for b in self.broadcastable: for i, b in enumerate(self.broadcastable):
if b and shp[i] != 1: if b and shp[i] != 1:
raise TypeError("Non-unit value on shape on a broadcastable" raise TypeError("Non-unit value on shape on a broadcastable"
" dimension.", shp, self.broadcastable) " dimension.", shp, self.broadcastable)
i += 1
return data return data
def values_eq(self, a, b): def values_eq(self, a, b):
...@@ -83,10 +93,13 @@ class GpuArrayType(Type): ...@@ -83,10 +93,13 @@ class GpuArrayType(Type):
def __eq__(self, other): def __eq__(self, other):
return (type(self) == type(other) and return (type(self) == type(other) and
self.typecode == other.typecode and self.typecode == other.typecode and
self.broadcastable == other.broadcastable) self.broadcastable == other.broadcastable and
self.kind == other.kind and
self.context == other.context)
def __hash__(self): def __hash__(self):
return hash(self.typecode) ^ hash(self.broadcastable) return (hash(self.typecode) ^ hash(self.broadcastable) ^
hash(self.kind) ^ hash(self.context))
def __str__(self): def __str__(self):
return "GpuArray<%s>" % self.dtype return "GpuArray<%s>" % self.dtype
......
...@@ -10,7 +10,7 @@ try: ...@@ -10,7 +10,7 @@ try:
except ImportError: except ImportError:
pass pass
from var import GpuArrayType from type import GpuArrayType
from basic_ops import host_from_gpu, gpu_from_host from basic_ops import host_from_gpu, gpu_from_host
class _operators(tensor.basic._tensor_py_operators): class _operators(tensor.basic._tensor_py_operators):
...@@ -61,7 +61,7 @@ class GpuArraySharedVariable(_operators, SharedVariable): ...@@ -61,7 +61,7 @@ class GpuArraySharedVariable(_operators, SharedVariable):
self.container.value = pygpu.gpuarray.array(value, copy=(not borrow)) self.container.value = pygpu.gpuarray.array(value, copy=(not borrow))
def __getitem__(self, *args): def __getitem__(self, *args):
retrurn _operators.__getitem__(self, *args) return _operators.__getitem__(self, *args)
GpuArrayType.SharedVariable = GpuArraySharedVariable GpuArrayType.SharedVariable = GpuArraySharedVariable
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论