def impl_test_binaryop_2d(self, dtype): if issubclass(dtype, numbers.Integral): a_sca = np.array(np.random.randint(1, 10), dtype=dtype) b_sca = np.array(np.random.randint(1, 10), dtype=dtype) a_vec = np.random.randint(1, 10, 3).astype(dtype) b_vec = np.random.randint(1, 10, 3).astype(dtype) a_mat = np.random.randint(1, 10, 6).reshape((3, 2)).astype(dtype) b_mat = np.random.randint(1, 10, 6).reshape((3, 2)).astype(dtype) else: a_sca = np.random.normal(scale=5.0, size=()).astype(dtype) b_sca = np.random.normal(scale=5.0, size=()).astype(dtype) a_vec = np.random.normal(scale=5.0, size=(3, )).astype(dtype) b_vec = np.random.normal(scale=5.0, size=(3, )).astype(dtype) a_mat = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype) b_mat = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype) a_sca_gpu = gpuarray.to_gpu(a_sca) b_sca_gpu = gpuarray.to_gpu(b_sca) a_vec_gpu = gpuarray.to_gpu(a_vec) b_vec_gpu = gpuarray.to_gpu(b_vec) a_mat_gpu = gpuarray.to_gpu(a_mat) b_mat_gpu = gpuarray.to_gpu(b_mat) # addition assert np.allclose(misc.add(a_sca_gpu, b_sca_gpu).get(), a_sca + b_sca) assert np.allclose(misc.add(a_vec_gpu, b_vec_gpu).get(), a_vec + b_vec) assert np.allclose(misc.add(a_mat_gpu, b_mat_gpu).get(), a_mat + b_mat) # subtract assert np.allclose( misc.subtract(a_sca_gpu, b_sca_gpu).get(), a_sca - b_sca) assert np.allclose( misc.subtract(a_vec_gpu, b_vec_gpu).get(), a_vec - b_vec) assert np.allclose( misc.subtract(a_mat_gpu, b_mat_gpu).get(), a_mat - b_mat) # multiplication assert np.allclose( misc.multiply(a_sca_gpu, b_sca_gpu).get(), a_sca * b_sca) assert np.allclose( misc.multiply(a_vec_gpu, b_vec_gpu).get(), a_vec * b_vec) assert np.allclose( misc.multiply(a_mat_gpu, b_mat_gpu).get(), a_mat * b_mat) # division assert np.allclose( misc.divide(a_sca_gpu, b_sca_gpu).get(), a_sca / b_sca) assert np.allclose( misc.divide(a_vec_gpu, b_vec_gpu).get(), a_vec / b_vec) assert np.allclose( misc.divide(a_mat_gpu, b_mat_gpu).get(), a_mat / b_mat)
def thunk(): alpha = gpuarray.to_gpu(np.squeeze(np.asarray(inputs[0]))[:, None]) x_t = gpuarray.to_gpu(np.asarray(inputs[1])[0, :, :]) x_f = gpuarray.to_gpu(np.asarray(inputs[2])[0, :, :]) Xt = cumath.exp(misc.add(linalg.dot(x_t, A), b)) Xf = cumath.exp(misc.add(linalg.dot(x_f, A), b)) Xtn = misc.sum(Xt, axis=1, keepdims=True) Xfn = misc.sum(Xf, axis=1, keepdims=True) Xt = misc.divide(Xt, Xtn) Xf = misc.divide(Xf, Xfn) w = misc.multiply(Xt, alpha) + misc.multiply(Xf, 1 - alpha) dq = Xt - Xf qdw = dq / w t1 = misc.sum(x * qdw, axis=1) f = 2 * depth + self.base.n t2 = f * misc.sum(dq, axis=1) / misc.sum(w, axis=1) t3 = misc.sum(x, axis=1) * misc.sum(qdw, axis=1) dalpha = t1 - t2 + t3 del dq, t1, f, t2, t3 iw = 1 / w S1 = misc.multiply( depth[:, None] * (self.base.n - 1) / self.base.n, iw) S2 = (self.base.n + depth[:, None]) / cumath.log( misc.sum(w, axis=1, keepdims=True)) F = misc.multiply(misc.subtract((x * iw) - S1, S2), alpha) del w, iw, S1, S2 cast = gpuarray.zeros((x_t.shape[1], Xt.shape[1]), dtype=theano.config.floatX) dLq_t = gpuarray.zeros(x_t.shape, dtype=theano.config.floatX) dLq_f = gpuarray.zeros(x_f.shape, dtype=theano.config.floatX) for i in range(Xt.shape[0]): S1 = misc.multiply(Xt[None, i, :], A) S2 = misc.sum(S1, axis=1, keepdims=True) S2 = misc.multiply(S2, misc.add(Xt[None, i, :], cast)) dLq_t[i, :] = misc.sum(misc.multiply(F[None, i, :], S1 - S2), axis=1) S1 = misc.multiply(Xf[None, i, :], A) S2 = misc.sum(S1, axis=1, keepdims=True) S2 = misc.multiply(S2, misc.add(Xf[None, i, :], cast)) dLq_f[i, :] = misc.sum(misc.multiply(F[None, i, :], S1 - S2), axis=1) outputs[0][0] = dalpha.get() outputs[1][0] = dLq_t.get() outputs[2][0] = dLq_f.get() for v in node.outputs: compute_map[v][0] = True
def impl_test_binaryop_2d(self, dtype): if issubclass(dtype, numbers.Integral): a_sca = np.array(np.random.randint(1, 10), dtype=dtype) b_sca = np.array(np.random.randint(1, 10), dtype=dtype) a_vec = np.random.randint(1, 10, 3).astype(dtype) b_vec = np.random.randint(1, 10, 3).astype(dtype) a_mat = np.random.randint(1, 10, 6).reshape((3, 2)).astype(dtype) b_mat = np.random.randint(1, 10, 6).reshape((3, 2)).astype(dtype) else: a_sca = np.random.normal(scale=5.0, size=()).astype(dtype) b_sca = np.random.normal(scale=5.0, size=()).astype(dtype) a_vec = np.random.normal(scale=5.0, size=(3,)).astype(dtype) b_vec = np.random.normal(scale=5.0, size=(3,)).astype(dtype) a_mat = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype) b_mat = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype) a_sca_gpu = gpuarray.to_gpu(a_sca) b_sca_gpu = gpuarray.to_gpu(b_sca) a_vec_gpu = gpuarray.to_gpu(a_vec) b_vec_gpu = gpuarray.to_gpu(b_vec) a_mat_gpu = gpuarray.to_gpu(a_mat) b_mat_gpu = gpuarray.to_gpu(b_mat) # addition assert np.allclose(misc.add(a_sca_gpu, b_sca_gpu).get(), a_sca+b_sca) assert np.allclose(misc.add(a_vec_gpu, b_vec_gpu).get(), a_vec+b_vec) assert np.allclose(misc.add(a_mat_gpu, b_mat_gpu).get(), a_mat+b_mat) # subtract assert np.allclose(misc.subtract(a_sca_gpu, b_sca_gpu).get(), a_sca-b_sca) assert np.allclose(misc.subtract(a_vec_gpu, b_vec_gpu).get(), a_vec-b_vec) assert np.allclose(misc.subtract(a_mat_gpu, b_mat_gpu).get(), a_mat-b_mat) # multiplication assert np.allclose(misc.multiply(a_sca_gpu, b_sca_gpu).get(), a_sca*b_sca) assert np.allclose(misc.multiply(a_vec_gpu, b_vec_gpu).get(), a_vec*b_vec) assert np.allclose(misc.multiply(a_mat_gpu, b_mat_gpu).get(), a_mat*b_mat) # division assert np.allclose(misc.divide(a_sca_gpu, b_sca_gpu).get(), a_sca/b_sca) assert np.allclose(misc.divide(a_vec_gpu, b_vec_gpu).get(), a_vec/b_vec) assert np.allclose(misc.divide(a_mat_gpu, b_mat_gpu).get(), a_mat/b_mat)
def thunk(): alpha = gpuarray.to_gpu(np.squeeze(np.asarray(inputs[0]))[:, None]) x_t = gpuarray.to_gpu(np.asarray(inputs[1])[0, :, :]) x_f = gpuarray.to_gpu(np.asarray(inputs[2])[0, :, :]) Xt = cumath.exp(misc.add(linalg.dot(x_t, A), b)) Xf = cumath.exp(misc.add(linalg.dot(x_f, A), b)) Xtn = misc.sum(Xt, axis=1, keepdims=True) Xfn = misc.sum(Xf, axis=1, keepdims=True) Xt = misc.divide(Xt, Xtn) Xf = misc.divide(Xf, Xfn) w = misc.multiply(Xt, alpha) + misc.multiply(Xf, 1 - alpha) wp = cumath.log(w) wpn = misc.sum(wp, axis=1, keepdims=True) / self.n wp = misc.subtract(wp, wpn) t1 = misc.sum(x * wp, axis=1) t2 = (self.n + depth) * cumath.log(misc.sum(w, axis=1)) t3 = depth * wpn outputs[0][0] = misc.sum(t1 - t2 + t3).get() for v in node.outputs: compute_map[v][0] = True
def skcuda_linalg(a, b): linalg.init() a = np.asarray(a, np.float32) b = np.asarray(b, np.float32) a_gpu = gpuarray.to_gpu(a) b_gpu = gpuarray.to_gpu(b) c_gpu = linalg.dot(a_gpu, b_gpu, 'T') a_nrm = linalg.norm(a_gpu) b_nrm = linalg.norm(b_gpu) type(a_nrm) ans = misc.divide(c_gpu, a_nrm * b_nrm) print ans
def _cuda_norm(self, X): """Caluclate L2-norm on gpu. Parameters ---------- X: array Array to normalize Returns ------- normX: array Normalized array """ return misc.divide(X, misc.sum(X**2, axis=1, keepdims=True)**0.5)
def __rdiv__(self, other): return cumisc.divide(other, self) def __rmatmul__(self, other): return culinalg.dot(other, self)
def __div__(self, other): return cumisc.divide( self, other) def __matmul__(self, other): return culinalg.dot(self, other)
ff = transf(ff) ff[ff<0] = 0 ff[ff>2**15] = 0 # sometimes there is a problem with saving signed/unsigned ff values while ff.max() > 7: # rescale ff ff /= 10 # print(ff.max()) else: ff = np.zeros(outShape) if useGPU: signorms = linalg.norm(signals, axis=1, keepdims=True) signormsRep = np.repeat(signorms, signals.shape[1], axis=1) signormsGPU = pycuda.gpuarray.to_gpu(signormsRep.astype(np.float32)) signalsGPU = pycuda.gpuarray.to_gpu(signals.astype(np.float32)) signalsGPU = sklinalg.transpose(skmisc.divide(signalsGPU, signormsGPU)) del signormsGPU ROWSTEP = 14 if fitType == 0: signorms = linalg.norm(signals, axis=1, keepdims=True) signormsRep = np.repeat(signorms, signals.shape[1], axis=1) signalsCPU = np.transpose( signals / signormsRep) ROWSTEP = 14 for slc in range(*sliceRange): print(slc) if fatT2 <= 0: print("Searching fat...") fatT2 = fitSlc(int((sliceRange[1]-sliceRange[0])/2+sliceRange[0]), True, t2, b1, ff) ffl = FatFractionLookup(t2Lim, b1Lim, fatT2, etl, echoSpacing, refocusingFactor)
def _impl_test_binaryop_2d(self, dtype): if issubclass(dtype, numbers.Integral): a_sca = np.array(np.random.randint(1, 10), dtype=dtype) b_sca = np.array(np.random.randint(1, 10), dtype=dtype) a_vec = np.random.randint(1, 10, 3).astype(dtype) b_vec = np.random.randint(1, 10, 3).astype(dtype) a_mat = np.random.randint(1, 10, 6).reshape((3, 2)).astype(dtype) b_mat = np.random.randint(1, 10, 6).reshape((3, 2)).astype(dtype) b_mat_f = np.random.randint(1, 10, 6).reshape( (3, 2)).astype(dtype, order='F') else: a_sca = np.random.normal(scale=5.0, size=()).astype(dtype) b_sca = np.random.normal(scale=5.0, size=()).astype(dtype) a_vec = np.random.normal(scale=5.0, size=(3, )).astype(dtype) b_vec = np.random.normal(scale=5.0, size=(3, )).astype(dtype) a_mat = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype) b_mat = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype) b_mat_f = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype, order='F') a_sca_gpu = gpuarray.to_gpu(a_sca) b_sca_gpu = gpuarray.to_gpu(b_sca) a_vec_gpu = gpuarray.to_gpu(a_vec) b_vec_gpu = gpuarray.to_gpu(b_vec) a_mat_gpu = gpuarray.to_gpu(a_mat) b_mat_gpu = gpuarray.to_gpu(b_mat) b_mat_f_gpu = gpuarray.to_gpu(b_mat_f) # addition assert_allclose(misc.add(a_sca_gpu, b_sca_gpu).get(), a_sca + b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.add(a_vec_gpu, b_vec_gpu).get(), a_vec + b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.add(a_mat_gpu, b_mat_gpu).get(), a_mat + b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) # subtract assert_allclose(misc.subtract(a_sca_gpu, b_sca_gpu).get(), a_sca - b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.subtract(a_vec_gpu, b_vec_gpu).get(), a_vec - b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.subtract(a_mat_gpu, b_mat_gpu).get(), a_mat - b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) # multiplication assert_allclose(misc.multiply(a_sca_gpu, b_sca_gpu).get(), a_sca * b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.multiply(a_vec_gpu, b_vec_gpu).get(), a_vec * b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.multiply(a_mat_gpu, b_mat_gpu).get(), a_mat * b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) # division if issubclass(dtype, numbers.Integral): assert_allclose(misc.divide(a_sca_gpu, b_sca_gpu).get(), a_sca // b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.divide(a_vec_gpu, b_vec_gpu).get(), a_vec // b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.divide(a_mat_gpu, b_mat_gpu).get(), a_mat // b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) else: assert_allclose(misc.divide(a_sca_gpu, b_sca_gpu).get(), a_sca / b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.divide(a_vec_gpu, b_vec_gpu).get(), a_vec / b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.divide(a_mat_gpu, b_mat_gpu).get(), a_mat / b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) # mismatched order assert_raises(ValueError, misc.add, a_mat_gpu, b_mat_f_gpu)
def _impl_test_binaryop_2d(self, dtype): if issubclass(dtype, numbers.Integral): a_sca = np.array(np.random.randint(1, 10), dtype=dtype) b_sca = np.array(np.random.randint(1, 10), dtype=dtype) a_vec = np.random.randint(1, 10, 3).astype(dtype) b_vec = np.random.randint(1, 10, 3).astype(dtype) a_mat = np.random.randint(1, 10, 6).reshape((3, 2)).astype(dtype) b_mat = np.random.randint(1, 10, 6).reshape((3, 2)).astype(dtype) b_mat_f = np.random.randint(1, 10, 6).reshape((3, 2)).astype(dtype, order='F') else: a_sca = np.random.normal(scale=5.0, size=()).astype(dtype) b_sca = np.random.normal(scale=5.0, size=()).astype(dtype) a_vec = np.random.normal(scale=5.0, size=(3,)).astype(dtype) b_vec = np.random.normal(scale=5.0, size=(3,)).astype(dtype) a_mat = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype) b_mat = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype) b_mat_f = np.random.normal(scale=5.0, size=(3, 2)).astype(dtype, order='F') a_sca_gpu = gpuarray.to_gpu(a_sca) b_sca_gpu = gpuarray.to_gpu(b_sca) a_vec_gpu = gpuarray.to_gpu(a_vec) b_vec_gpu = gpuarray.to_gpu(b_vec) a_mat_gpu = gpuarray.to_gpu(a_mat) b_mat_gpu = gpuarray.to_gpu(b_mat) b_mat_f_gpu = gpuarray.to_gpu(b_mat_f) # addition assert_allclose(misc.add(a_sca_gpu, b_sca_gpu).get(), a_sca+b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.add(a_vec_gpu, b_vec_gpu).get(), a_vec+b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.add(a_mat_gpu, b_mat_gpu).get(), a_mat+b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) # subtract assert_allclose(misc.subtract(a_sca_gpu, b_sca_gpu).get(), a_sca-b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.subtract(a_vec_gpu, b_vec_gpu).get(), a_vec-b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.subtract(a_mat_gpu, b_mat_gpu).get(), a_mat-b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) # multiplication assert_allclose(misc.multiply(a_sca_gpu, b_sca_gpu).get(), a_sca*b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.multiply(a_vec_gpu, b_vec_gpu).get(), a_vec*b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.multiply(a_mat_gpu, b_mat_gpu).get(), a_mat*b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) # division if issubclass(dtype, numbers.Integral): assert_allclose(misc.divide(a_sca_gpu, b_sca_gpu).get(), a_sca//b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.divide(a_vec_gpu, b_vec_gpu).get(), a_vec//b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.divide(a_mat_gpu, b_mat_gpu).get(), a_mat//b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) else: assert_allclose(misc.divide(a_sca_gpu, b_sca_gpu).get(), a_sca/b_sca, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.divide(a_vec_gpu, b_vec_gpu).get(), a_vec/b_vec, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.divide(a_mat_gpu, b_mat_gpu).get(), a_mat/b_mat, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) # mismatched order assert_raises(ValueError, misc.add, a_mat_gpu, b_mat_f_gpu)