def impl_test_binaryop_matvec(self, dtype): x = np.random.normal(scale=5.0, size=(3, 5)).astype(dtype) a = np.random.normal(scale=5.0, size=(1, 5)).astype(dtype) b = np.random.normal(scale=5.0, size=(3, 1)).astype(dtype) # the following two test correct broadcasting on 0D vectors c = np.random.normal(scale=5.0, size=(5, )).astype(dtype) d = np.random.normal(scale=5.0, size=(3, )).astype(dtype) x_gpu = gpuarray.to_gpu(x) a_gpu = gpuarray.to_gpu(a) b_gpu = gpuarray.to_gpu(b) c_gpu = gpuarray.to_gpu(c) d_gpu = gpuarray.to_gpu(d) out = gpuarray.empty(x.shape, dtype=dtype) # addition res = misc.add_matvec(x_gpu, a_gpu, out=out).get() assert np.allclose(res, x+a) assert np.allclose(misc.add_matvec(x_gpu, b_gpu).get(), x+b) assert np.allclose(misc.add_matvec(x_gpu, c_gpu).get(), x+c) assert_raises(ValueError, misc.add_matvec, x_gpu, d_gpu) # multiplication res = misc.mult_matvec(x_gpu, a_gpu, out=out).get() assert np.allclose(res, x*a) assert np.allclose(misc.mult_matvec(x_gpu, b_gpu).get(), x*b) assert np.allclose(misc.mult_matvec(x_gpu, c_gpu).get(), x*c) assert_raises(ValueError, misc.mult_matvec, x_gpu, d_gpu) # division res = misc.div_matvec(x_gpu, a_gpu, out=out).get() assert np.allclose(res, x/a) assert np.allclose(misc.div_matvec(x_gpu, b_gpu).get(), x/b) assert np.allclose(misc.div_matvec(x_gpu, c_gpu).get(), x/c) assert_raises(ValueError, misc.div_matvec, x_gpu, d_gpu)
def impl_test_binaryop_matvec(self, dtype): x = np.random.normal(scale=5.0, size=(3, 5)).astype(dtype) a = np.random.normal(scale=5.0, size=(1, 5)).astype(dtype) b = np.random.normal(scale=5.0, size=(3, 1)).astype(dtype) # the following two test correct broadcasting on 0D vectors c = np.random.normal(scale=5.0, size=(5, )).astype(dtype) d = np.random.normal(scale=5.0, size=(3, )).astype(dtype) x_gpu = gpuarray.to_gpu(x) a_gpu = gpuarray.to_gpu(a) b_gpu = gpuarray.to_gpu(b) c_gpu = gpuarray.to_gpu(c) d_gpu = gpuarray.to_gpu(d) out = gpuarray.empty(x.shape, dtype=dtype) # addition res = misc.add_matvec(x_gpu, a_gpu, out=out).get() assert np.allclose(res, x + a) assert np.allclose(misc.add_matvec(x_gpu, b_gpu).get(), x + b) assert np.allclose(misc.add_matvec(x_gpu, c_gpu).get(), x + c) assert_raises(ValueError, misc.add_matvec, x_gpu, d_gpu) # multiplication res = misc.mult_matvec(x_gpu, a_gpu, out=out).get() assert np.allclose(res, x * a) assert np.allclose(misc.mult_matvec(x_gpu, b_gpu).get(), x * b) assert np.allclose(misc.mult_matvec(x_gpu, c_gpu).get(), x * c) assert_raises(ValueError, misc.mult_matvec, x_gpu, d_gpu) # division res = misc.div_matvec(x_gpu, a_gpu, out=out).get() assert np.allclose(res, x / a) assert np.allclose(misc.div_matvec(x_gpu, b_gpu).get(), x / b) assert np.allclose(misc.div_matvec(x_gpu, c_gpu).get(), x / c) assert_raises(ValueError, misc.div_matvec, x_gpu, d_gpu)
def softmax_gpu2d(x: gpuarray.GPUArray, dim): assert len(x.shape) == 2, 'expected 2-dimension array' assert 0 <= dim <= 1, "expected 0 <= dim <=1" exp_ker = exp_float_ker if x.dtype == np.float32 else exp_double_ker x_exp = gpuarray.empty_like(x) exp_ker(x, x_exp) x_exp_sum = misc.sum(x_gpu=x_exp, axis=dim) x_exp = misc.div_matvec(x_gpu=x_exp, a_gpu=x_exp_sum, axis=1 - dim) return x_exp
def divide_mv(self, m, v, out): cumisc.div_matvec(m, v, out=out)
def _impl_test_binaryop_matvec(self, dtype): if issubclass(dtype, numbers.Integral): x = np.random.randint(1, 10, 15).reshape((3, 5)).astype(dtype) a = np.random.randint(1, 10, 5).reshape((1, 5)).astype(dtype) b = np.random.randint(1, 10, 3).reshape((3, 1)).astype(dtype) # the following two test correct broadcasting on 0D vectors c = np.random.randint(1, 10, 5).reshape((5, )).astype(dtype) d = np.random.randint(1, 10, 3).reshape((3, )).astype(dtype) else: x = np.random.normal(scale=5.0, size=(3, 5)).astype(dtype) a = np.random.normal(scale=5.0, size=(1, 5)).astype(dtype) b = np.random.normal(scale=5.0, size=(3, 1)).astype(dtype) # the following two test correct broadcasting on 0D vectors c = np.random.normal(scale=5.0, size=(5, )).astype(dtype) d = np.random.normal(scale=5.0, size=(3, )).astype(dtype) x_gpu = gpuarray.to_gpu(x) a_gpu = gpuarray.to_gpu(a) b_gpu = gpuarray.to_gpu(b) c_gpu = gpuarray.to_gpu(c) d_gpu = gpuarray.to_gpu(d) out = gpuarray.empty(x.shape, dtype=dtype) # addition res = misc.add_matvec(x_gpu, a_gpu, out=out).get() assert_allclose(res, x + a, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.add_matvec(x_gpu, b_gpu).get(), x + b, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.add_matvec(x_gpu, c_gpu).get(), x + c, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_raises(ValueError, misc.add_matvec, x_gpu, d_gpu) # multiplication res = misc.mult_matvec(x_gpu, a_gpu, out=out).get() assert_allclose(res, x * a, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.mult_matvec(x_gpu, b_gpu).get(), x * b, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.mult_matvec(x_gpu, c_gpu).get(), x * c, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_raises(ValueError, misc.mult_matvec, x_gpu, d_gpu) # division res = misc.div_matvec(x_gpu, a_gpu, out=out).get() if issubclass(dtype, numbers.Integral): assert_allclose(res, x // a, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.div_matvec(x_gpu, b_gpu).get(), x // b, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.div_matvec(x_gpu, c_gpu).get(), x // c, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) else: assert_allclose(res, x / a, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.div_matvec(x_gpu, b_gpu).get(), x / b, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.div_matvec(x_gpu, c_gpu).get(), x / c, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_raises(ValueError, misc.div_matvec, x_gpu, d_gpu)
def _impl_test_binaryop_matvec(self, dtype): if issubclass(dtype, numbers.Integral): x = np.random.randint(1, 10, 15).reshape((3, 5)).astype(dtype) a = np.random.randint(1, 10, 5).reshape((1, 5)).astype(dtype) b = np.random.randint(1, 10, 3).reshape((3, 1)).astype(dtype) # the following two test correct broadcasting on 0D vectors c = np.random.randint(1, 10, 5).reshape((5, )).astype(dtype) d = np.random.randint(1, 10, 3).reshape((3, )).astype(dtype) else: x = np.random.normal(scale=5.0, size=(3, 5)).astype(dtype) a = np.random.normal(scale=5.0, size=(1, 5)).astype(dtype) b = np.random.normal(scale=5.0, size=(3, 1)).astype(dtype) # the following two test correct broadcasting on 0D vectors c = np.random.normal(scale=5.0, size=(5, )).astype(dtype) d = np.random.normal(scale=5.0, size=(3, )).astype(dtype) x_gpu = gpuarray.to_gpu(x) a_gpu = gpuarray.to_gpu(a) b_gpu = gpuarray.to_gpu(b) c_gpu = gpuarray.to_gpu(c) d_gpu = gpuarray.to_gpu(d) out = gpuarray.empty(x.shape, dtype=dtype) # addition res = misc.add_matvec(x_gpu, a_gpu, out=out).get() assert_allclose(res, x+a, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.add_matvec(x_gpu, b_gpu).get(), x+b, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.add_matvec(x_gpu, c_gpu).get(), x+c, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_raises(ValueError, misc.add_matvec, x_gpu, d_gpu) # multiplication res = misc.mult_matvec(x_gpu, a_gpu, out=out).get() assert_allclose(res, x*a, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.mult_matvec(x_gpu, b_gpu).get(), x*b, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.mult_matvec(x_gpu, c_gpu).get(), x*c, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_raises(ValueError, misc.mult_matvec, x_gpu, d_gpu) # division res = misc.div_matvec(x_gpu, a_gpu, out=out).get() if issubclass(dtype, numbers.Integral): assert_allclose(res, x//a, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.div_matvec(x_gpu, b_gpu).get(), x//b, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.div_matvec(x_gpu, c_gpu).get(), x//c, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) else: assert_allclose(res, x/a, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.div_matvec(x_gpu, b_gpu).get(), x/b, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_allclose(misc.div_matvec(x_gpu, c_gpu).get(), x/c, rtol=dtype_to_rtol[dtype], atol=dtype_to_atol[dtype]) assert_raises(ValueError, misc.div_matvec, x_gpu, d_gpu)