Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
File: opt.py Progetto: jlowin/Theano
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
Esempio n. 4
0
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
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
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)
Esempio n. 9
0
    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
Esempio n. 10
0
 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])
Esempio n. 11
0
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, *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
Esempio n. 13
0
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)
Esempio n. 14
0
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)
Esempio n. 15
0
    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)()
        ])
Esempio n. 16
0
File: conv.py Progetto: yubow/Theano
 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])
Esempio n. 17
0
    def values_eq_approx(a, b):
        """This fct is needed to don't have DebugMode raise useless
        error due to ronding error.

        This happen as We reduce on the two last dimensions, so this
        can raise the absolute error if the number of element we
        reduce on is significant.

        """
        assert a.ndim == 4
        atol = None
        if a.shape[-1] * a.shape[-2] > 100:
            # For float32 the default atol is 1e-5
            atol = 3e-5
        return GpuArrayType.values_eq_approx(a, b, atol=atol)
Esempio n. 18
0
    def values_eq_approx(a, b):
        """This fct is needed to don't have DebugMode raise useless
        error due to ronding error.

        This happen as We reduce on the two last dimensions, so this
        can raise the absolute error if the number of element we
        reduce on is significant.

        """
        assert a.ndim == 4
        atol = None
        if a.shape[-1] * a.shape[-2] > 100:
            # For float32 the default atol is 1e-5
            atol = 3e-5
        return GpuArrayType.values_eq_approx(a, b, atol=atol)
    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()])
Esempio n. 20
0
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)
Esempio n. 21
0
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)
Esempio n. 22
0
def makeTester(name, op, expected, good=None, bad_build=None, checks=None,
               bad_runtime=None, mode=None, skip=False, eps=1e-10):
    if good is None:
        good = {}
    if bad_build is None:
        bad_build = {}
    if bad_runtime is None:
        bad_runtime = {}
    if checks is None:
        checks = {}

    _op = op
    _expected = expected
    _good = good
    _bad_build = bad_build
    _bad_runtime = bad_runtime
    _skip = skip
    _checks = checks

    class Checker(unittest.TestCase):
        op = staticmethod(_op)
        expected = staticmethod(_expected)
        good = _good
        bad_build = _bad_build
        bad_runtime = _bad_runtime
        skip = _skip
        checks = _checks

        def setUp(self):
            eval(self.__class__.__module__ + '.' + self.__class__.__name__)

        def test_good(self):
            if skip:
                raise SkipTest(skip)

            for testname, inputs in good.items():
                inputs = [copy(input) for input in inputs]
                inputrs = [fake_shared(input) for input in inputs]

                try:
                    node = safe_make_node(self.op, *inputrs)
                except Exception, exc:
                    err_msg = ("Test %s::%s: Error occured while making "
                               "a node with inputs %s") % (self.op, testname,
                                                           inputs)
                    exc.args += (err_msg,)
                    raise

                try:
                    f = inplace_func([], node.outputs, mode=mode,
                                     name='test_good')
                except Exception, exc:
                    err_msg = ("Test %s::%s: Error occured while trying to "
                               "make a Function") % (self.op, testname)
                    exc.args += (err_msg,)
                    raise

                if isinstance(self.expected, dict) and \
                        testname in self.expected:
                    expecteds = self.expected[testname]
                else:
                    expecteds = self.expected(*inputs)

                if not isinstance(expecteds, (list, tuple)):
                    expecteds = (expecteds,)

                try:
                    variables = f()
                except Exception, exc:
                    err_msg = ("Test %s::%s: Error occured while calling "
                               "the Function on the inputs %s") % (self.op,
                                                                   testname,
                                                                   inputs)
                    exc.args += (err_msg,)
                    raise

                for i, (variable, expected) in \
                        enumerate(izip(variables, expecteds)):
                    if variable.dtype != expected.dtype or \
                            variable.shape != expected.shape or \
                            not GpuArrayType.values_eq_approx(variable,
                                                             expected):
                        self.fail(("Test %s::%s: Output %s gave the wrong "
                                   "value. With inputs %s, expected %s "
                                   "(dtype %s), got %s (dtype %s).") % (
                                self.op, testname, i, inputs, expected,
                                expected.dtype, variable, variable.dtype))

                for description, check in self.checks.items():
                    if not check(inputs, variables):
                        self.fail(("Test %s::%s: Failed check: %s "
                                   "(inputs were %s, ouputs were %s)") %
                                  (self.op, testname, description,
                                   inputs, variables))
Esempio n. 23
0
 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'))
 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()])
Esempio n. 25
0
 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()])
Esempio n. 26
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)
Esempio n. 27
0
    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]):