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