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 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 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 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 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 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))