def test_transfer_cuda_gpu(): import theano.sandbox.cuda as cuda_ndarray if cuda_ndarray.cuda_available == False: raise SkipTest("Can't test interaction with cuda if cuda not present") g = GpuArrayType(dtype='float32', broadcastable=(False, False))('g') c = cuda_ndarray.CudaNdarrayType((False, False))('c') av = theano._asarray(rng.rand(5, 4), dtype='float32') gv = gpuarray.array(av) cv = cuda_ndarray.CudaNdarray(av) gvs = gv[:, ::-2] cvs = cv[:, ::-2] f = theano.function([c], gpu_from_cuda(c)) fv = f(cv) assert GpuArrayType.values_eq_approx(fv, gv) fvs = f(cvs) assert GpuArrayType.values_eq_approx(fvs, gvs) f = theano.function([g], cuda_from_gpu(g)) fv = f(gv) assert cuda_ndarray.CudaNdarrayType.values_eq_approx(fv, cv) fvs = f(gvs) assert cuda_ndarray.CudaNdarrayType.values_eq_approx(fvs, cvs)
def make_node(self, *inputs): res = Elemwise.make_node(self, *inputs) outputs = [GpuArrayType(broadcastable=o.type.broadcastable, dtype=o.type.dtype)() for o in res.outputs] inputs = [as_gpuarray_variable(i) for i in inputs] node = Apply(self, inputs, outputs) # Try to generate the kernel to catch SupportCodeErrors try: inps = [make_argument(i, 'i%d' % (n,)) for n, i in enumerate(node.inputs)] scal_ins = [scalar.Scalar(i.dtype) for i in node.inputs] outs = [make_argument(o, 'o%d' % (n,)) for n, o in enumerate(node.outputs) if not n in self.inplace_pattern] scal_out = [scalar.Scalar(o.dtype) for o in node.outputs] fake_node = Apply(self.scalar_op, [i() for i in scal_ins], [o() for o in scal_out]) code = self.scalar_op.c_support_code_apply(fake_node, "test") if code: raise SupportCodeError(code) except MethodNotDefined: pass try: support_code = self.scalar_op.c_support_code() if (support_code.strip() != "#define THEANO_MACRO_MOD(x,y) (x % y)" and support_code.strip() != ""): # The macro is fine, the C++ struct is not. raise SupportCodeError(support_code) except MethodNotDefined: pass return node
def make_node(self, x, b, y_idx): #N.B. won't work when we don't cast y_idx to float anymore x = as_gpuarray_variable(x) b = as_gpuarray_variable(b) y_idx = as_gpuarray_variable(y_idx) nll = GpuArrayType(x.type.dtype, y_idx.type.broadcastable)() sm = x.type() am = y_idx.type() return Apply(self, [x, b, y_idx], [nll, sm, am])
def tensor_to_gpu(x): if isinstance(x.type, tensor.TensorType): y = GpuArrayType(broadcastable=x.type.broadcastable, dtype=x.type.dtype)() if x.name: y.name = x.name + '[Gpua]' return y else: return x
def test_values_eq_approx(): a = rand_gpuarray(20, dtype='float32') g = GpuArrayType(dtype='float32', broadcastable=(False, ))('g') assert GpuArrayType.values_eq_approx(a, a) b = a.copy() b[0] = numpy.asarray(b[0]) + 1. assert not GpuArrayType.values_eq_approx(a, b) b = a.copy() b[0] = -numpy.asarray(b[0]) assert not GpuArrayType.values_eq_approx(a, b)
def make_node(self, *inputs): res = Elemwise.make_node(self, *inputs) outputs = [ GpuArrayType(broadcastable=o.type.broadcastable, dtype=o.type.dtype)() for o in res.outputs ] inputs = [as_gpuarray_variable(i) for i in inputs] res = Apply(self, inputs, outputs) # Try to generate the kernel to catch SupportCodeErrors k = self.generate_kernel(res, 'test') return res
def test_deep_copy(): a = rand_gpuarray(20, dtype='float32') g = GpuArrayType(dtype='float32', broadcastable=(False, ))('g') f = theano.function([g], g) assert isinstance(f.maker.fgraph.toposort()[0].op, DeepCopyOp) res = f(a) assert GpuArrayType.values_eq(res, a)
def test_transfer_cpu_gpu(): a = T.fmatrix('a') g = GpuArrayType(dtype='float32', broadcastable=(False, False))('g') av = numpy.asarray(rng.rand(5, 4), dtype='float32') gv = gpuarray.array(av) f = theano.function([a], gpu_from_host(a)) fv = f(av) assert GpuArrayType.values_eq(fv, gv) f = theano.function([g], host_from_gpu(g)) fv = f(gv) assert numpy.all(fv == av)
def make_node(self, ten4, neib_shape, neib_step): ten4 = as_gpuarray_variable(ten4) neib_shape = T.as_tensor_variable(neib_shape) neib_step = T.as_tensor_variable(neib_step) assert ten4.ndim == 4 assert neib_shape.ndim == 1 assert neib_step.ndim == 1 assert "int" in neib_shape.dtype assert "int" in neib_step.dtype return Apply(self, [ten4, neib_shape, neib_step], [ GpuArrayType(broadcastable=(False, False), dtype=ten4.type.dtype)() ])
def make_node(self, input): res = CAReduceDtype.make_node(self, input) input = as_gpuarray_variable(input) otype = GpuArrayType(dtype=res.outputs[0].dtype, broadcastable=res.outputs[0].broadcastable) if res.op.axis is not None: redux = [] for i in range(len(input.type.broadcastable)): redux.append(i in res.op.axis) # since redux is just another way to describe what is in axis # it doesn't need to be compared in __eq__ or __hash__ res.op.redux = redux return Apply(res.op, [input], [otype()])
def make_node(self, img, kern): if img.dtype != "float32" or kern.dtype != "float32": raise NotImplementedError("GpuConv currently only work" " with float32 dtype") if img.type.ndim != 4: raise TypeError('img must be 4D tensor') if kern.type.ndim != 4: raise TypeError('kern must be 4D tensor') img = as_gpuarray_variable(img) kern = as_gpuarray_variable(kern) broadcastable = [ img.type.broadcastable[0], kern.type.broadcastable[0], False, False ] out = GpuArrayType(img.dtype, broadcastable)() return gof.Apply(self, [img, kern], [out])
def test_shape(): x = GpuArrayType(dtype='float32', broadcastable=[False, False, False])() v = gpuarray.zeros((3, 4, 5), dtype='float32') f = theano.function([x], x.shape) topo = f.maker.fgraph.toposort() assert numpy.all(f(v) == (3, 4, 5)) if theano.config.mode != 'FAST_COMPILE': assert len(topo) == 4 assert isinstance(topo[0].op, T.opt.Shape_i) assert isinstance(topo[1].op, T.opt.Shape_i) assert isinstance(topo[2].op, T.opt.Shape_i) assert isinstance(topo[3].op, T.opt.MakeVector) mode = mode_with_gpu.excluding("local_shape_to_shape_i") f = theano.function([x], x.shape, mode=mode) topo = f.maker.fgraph.toposort() assert numpy.all(f(v) == (3, 4, 5)) assert len(topo) == 1 assert isinstance(topo[0].op, T.Shape)
def test_transfer_strided(): # This is just to ensure that it works in theano # compyte has a much more comprehensive suit of tests to ensure correctness a = T.fmatrix('a') g = GpuArrayType(dtype='float32', broadcastable=(False, False))('g') av = numpy.asarray(rng.rand(5, 8), dtype='float32') gv = gpuarray.array(av) av = av[:, ::2] gv = gv[:, ::2] f = theano.function([a], gpu_from_host(a)) fv = f(av) assert GpuArrayType.values_eq(fv, gv) f = theano.function([g], host_from_gpu(g)) fv = f(gv) assert numpy.all(fv == av)
def make_node(self, input): res = DimShuffle.make_node(self, input) otype = GpuArrayType(dtype=res.outputs[0].type.dtype, broadcastable=res.outputs[0].type.broadcastable) input = as_gpuarray_variable(input) return Apply(self, [input], [otype()])
def make_node(self, x, *inputs): rval = tensor.Subtensor.make_node(self, x, *inputs) otype = GpuArrayType(dtype=rval.outputs[0].type.dtype, broadcastable=rval.outputs[0].type.broadcastable) x = as_gpuarray_variable(x) return gof.Apply(self, [x] + rval.inputs[1:], [otype()])
def new(cls, rstate, ndim, dtype, size): v_size = as_tensor_variable(size) if ndim is None: ndim = get_vector_length(v_size) op = cls(GpuArrayType(dtype, (False,)*ndim)) return op(rstate, cast(v_size, 'int32'))
imported_scipy_convolve2d = True except ImportError: pass import theano from theano import tensor from theano.compat.python2x import any from theano.tests.unittest_tools import seed_rng # We let that import do the init of the back-end if needed. from theano.sandbox.gpuarray.tests.test_basic_ops import (mode_with_gpu, mode_without_gpu) from theano.sandbox.gpuarray.type import GpuArrayType from theano.sandbox.gpuarray.conv import GpuConv import pygpu gftensor4 = GpuArrayType('float32', [False] * 4) device_id = theano.sandbox.cuda.use.device_number # TODO do with with the new back-end. from theano.sandbox.cuda import cuda_ndarray cuda_ndarray = theano.sandbox.cuda.cuda_ndarray.cuda_ndarray device_prop = cuda_ndarray.device_properties(device_id) def py_conv_valid_numpy(img, kern): assert img.shape[1] == kern.shape[1] outshp = (img.shape[0], kern.shape[0], img.shape[2] - kern.shape[2] + 1, img.shape[3] - kern.shape[3] + 1) out = numpy.zeros(outshp, dtype='float32') for b in xrange(out.shape[0]):
def test_specify_shape(): a = rand_gpuarray(20, dtype='float32') g = GpuArrayType(dtype='float32', broadcastable=(False, ))('g') f = theano.function([g], theano.tensor.specify_shape(g, [20])) f(a)