class test_mean(): def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_mean(self): """Test mean function.""" a = self.rng.rand(129, 1025).astype(np.float32) out_np = a.mean() out_gp = self.gp.mean(a) assert(np.allclose(out_np, out_gp)) a = self.rng.rand(1025).astype(np.float32) out_np = a.mean() out_gp = self.gp.mean(a) assert(np.allclose(out_np, out_gp)) def test_mean(self): """Test mean function along axes.""" a = self.rng.rand(129, 1025).astype(np.float32) out_np = a.mean(axis=1) out_gp = self.gp.mean(a, axis=1).copy_to_host() assert(np.allclose(out_np, out_gp)) out_np = a.mean(axis=0) out_gp = self.gp.mean(a, axis=0).copy_to_host() assert(np.allclose(out_np, out_gp))
class test_T(): def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_madd(self): a = self.rng.rand(129, 1025).astype(np.float32) b = self.rng.rand(129, 1025).astype(np.float32) out_np = a-b out_gp = self.gp.sub(a,b).copy_to_host() assert(np.allclose(out_np, out_gp)) def test_madd_scale(self): a = self.rng.rand(129, 1025).astype(np.float32) b = self.rng.rand(129, 1025).astype(np.float32) alpha = .4 beta = -1.6 out_np = (alpha*a-beta*b).astype(np.float32) out_gp = self.gp.sub(a,b,alpha = alpha, beta = beta).copy_to_host() print out_np print out_gp assert(np.allclose(out_np, out_gp, atol = 1.e-5)) def test_vadd(self): a = self.rng.rand(1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) out_np = a-b out_gp = self.gp.sub(a,b).copy_to_host() assert(np.allclose(out_np, out_gp)) def test_vadd_scale(self): a = self.rng.rand(1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) alpha = .4 beta = -1.6 out_np = (alpha*a-beta*b).astype(np.float32) out_gp = self.gp.sub(a,b,alpha = alpha, beta = beta).copy_to_host() print out_np print out_gp assert(np.allclose(out_np, out_gp, atol = 1.e-5))
class test_ones(): def setup(self): self.gp = Gpupy() def test_ones(self): """Create arrays of ones.""" out_np = np.ones(shape=(129,1025), dtype=np.float32) out_gp = self.gp.ones(shape=(129,1025)).copy_to_host() assert(np.allclose(out_np, out_gp)) out_np = np.ones(shape=129, dtype=np.float32) out_gp = self.gp.ones(shape=129).copy_to_host() assert(np.allclose(out_np, out_gp)) out_np = np.ones(129, dtype=np.float32) out_gp = self.gp.ones(129).copy_to_host() assert(np.allclose(out_np, out_gp))
class test_reshape(): def setup(self): self.gp = Gpupy() def test_reshape(self): """ Reshapes array. """ a = np.array(np.arange(10), dtype=np.float32, order='F') out_np = np.reshape(a, newshape=(2,5), order='F') out_gp = self.gp.reshape(a, newshape=(2,5)).copy_to_host() assert(np.allclose(out_np, out_gp)) a = np.array(np.arange(10), dtype=np.float32, order='c') out_np = np.reshape(a, newshape=(2,5), order='C') out_gp = self.gp.reshape(a, newshape=(2,5), order='C').copy_to_host() assert(np.allclose(out_np, out_gp))
class test_diag(): def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_v2m(self): a = self.rng.rand(129).astype(np.float32) out_np = np.diag(a) out_gp = self.gp.diag(a).copy_to_host() assert(np.allclose(out_np, out_gp)) def test_m2v(self): a = self.rng.rand(129, 129).astype(np.float32) out_np = np.diag(a) out_gp = self.gp.diag(a).copy_to_host() assert(np.allclose(out_np, out_gp))
class test_zero_diag: def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_zero_diag(self): a = self.rng.rand(129, 129).astype(np.float32) out_np = a - np.diag(np.diag(a)) out_gp = self.gp.zero_diag(a).copy_to_host() assert np.allclose(out_np, out_gp)
class test_relu(): def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_clip_m(self): """clip on matrices.""" a = self.rng.rand(129, 1025).astype(np.float32) out_np = np.clip(a,-.1,.1) out_gp = self.gp.clip(a,-.1,.1).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(129, 1025).astype(np.float32) out_np = np.clip(a,-.1,.1) out_gp = cuda.to_device(a) self.gp.clip(a, -.1, .1, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_clip_v(self): """clip on vectors.""" a = self.rng.rand(1025).astype(np.float32) out_np = np.clip(a,-.1,.1) out_gp = self.gp.clip(a,-.1,.1).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(1025).astype(np.float32) out_np = np.clip(a,-.1,.1) out_gp = cuda.to_device(a) self.gp.clip(a, -.1, .1, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host()))
class test_mult(): def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_matrix_multiply(self): a = self.rng.rand(129, 1025).astype(np.float32) b = self.rng.rand(129, 1025).astype(np.float32) out_np = a*b out_gp = self.gp.mult(a,b).copy_to_host() assert(np.allclose(out_np, out_gp)) def test_vector_multiply(self): a = self.rng.rand(1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) out_np = a*b out_gp = self.gp.mult(a,b).copy_to_host() assert(np.allclose(out_np, out_gp))
class test_scal(): def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_mscal(self): a = self.rng.rand(129, 1025).astype(np.float32) alpha = 2. out_np = (alpha*a).astype(np.float32) out_gp = self.gp.scal(a, alpha).copy_to_host() assert(np.allclose(out_np, out_gp)) def test_vscal(self): a = self.rng.rand(1025).astype(np.float32) alpha = 2. out_np = (alpha*a).astype(np.float32) out_gp = self.gp.scal(a, alpha).copy_to_host() assert(np.allclose(out_np, out_gp))
class test_zeros(): def setup(self): self.gp = Gpupy() def test_zeros(self): """Create array of zeros.""" out_np = np.zeros(shape=(129,1025), dtype=np.float32) out_gp = self.gp.zeros(shape=(129,1025)).copy_to_host() assert(np.allclose(out_np, out_gp)) out_np = np.zeros(shape=(129,1025), dtype=np.float32) out_gp = cuda.to_device(np.ones(shape=(129,1025), dtype=np.float32)) self.gp.zeros(shape=(129,1025), out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) out_np = np.zeros(shape=129, dtype=np.float32) out_gp = self.gp.zeros(shape=129).copy_to_host() assert(np.allclose(out_np, out_gp)) out_np = np.zeros(129, dtype=np.float32) out_gp = cuda.to_device(np.ones(129, dtype=np.float32)) self.gp.zeros(shape=129, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host()))
def setup(self): self.gp = Gpupy()
def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0)
class test_add(): def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_madd(self): """Add two matrices.""" a = self.rng.rand(129, 1025).astype(np.float32) b = self.rng.rand(129, 1025).astype(np.float32) out_np = a+b out_gp = self.gp.add(a,b).copy_to_host() assert(np.allclose(out_np, out_gp)) out_gp = self.gp.zeros(shape=(129, 1025)) self.gp.add(a,b, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_madd_scale(self): a = self.rng.rand(129, 1025).astype(np.float32) b = self.rng.rand(129, 1025).astype(np.float32) alpha = .4 beta = -1.6 out_np = (alpha*a+beta*b).astype(np.float32) out_gp = self.gp.add(a, b, alpha = alpha, beta = beta).copy_to_host() assert(np.allclose(out_np, out_gp, atol = 1.e-5)) out_gp = self.gp.zeros(shape=(129, 1025)) self.gp.add(a,b, alpha = alpha, beta = beta, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host(), atol=1.e-5)) def test_vadd(self): a = self.rng.rand(1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) out_np = a+b out_gp = self.gp.add(a,b).copy_to_host() assert(np.allclose(out_np, out_gp)) out_gp = self.gp.zeros(shape=1025) self.gp.add(a,b, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_vadd_scale(self): a = self.rng.rand(1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) alpha = .4 beta = -1.6 out_np = (alpha*a+beta*b).astype(np.float32) out_gp = self.gp.add(a, b, alpha = alpha, beta = beta).copy_to_host() assert(np.allclose(out_np, out_gp, atol=1.e-5)) out_gp = self.gp.zeros(shape=1025) self.gp.add(a, b, alpha = alpha, beta = beta, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_ms_scale(self): a = self.rng.rand(129,1025).astype(np.float32) b = 1. alpha = .4 beta = -1.6 out_np = (alpha*a+beta*b).astype(np.float32) out_gp = self.gp.add(a,b,alpha = alpha, beta = beta).copy_to_host() assert(np.allclose(out_np, out_gp, atol = 1.e-5)) out_gp = self.gp.zeros(shape=(129, 1025)) self.gp.add(a, b, alpha = alpha, beta = beta, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host(), atol = 1.e-5)) def test_ms(self): a = self.rng.rand(129,1025).astype(np.float32) b = 1. out_np = (a+b).astype(np.float32) out_gp = self.gp.add(a,b).copy_to_host() assert(np.allclose(out_np, out_gp, atol = 1.e-5)) out_gp = self.gp.zeros(shape=(129, 1025)) self.gp.add(a,b, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host(), atol = 1.e-5)) def test_vs_scale(self): a = self.rng.rand(1025).astype(np.float32) b = 1. alpha = .4 beta = -1.6 out_np = (alpha*a+beta*b).astype(np.float32) out_gp = self.gp.add(a,b,alpha = alpha, beta = beta).copy_to_host() assert(np.allclose(out_np, out_gp, atol = 1.e-5)) out_gp = self.gp.zeros(shape=1025) self.gp.add(a, b, alpha = alpha, beta = beta, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host(), atol = 1.e-5)) def test_vs(self): """ Add a vector and a scalar.""" a = self.rng.rand(1025).astype(np.float32) b = 1. out_np = (a+b).astype(np.float32) out_gp = self.gp.add(a,b).copy_to_host() assert(np.allclose(out_np, out_gp, atol = 1.e-5)) out_gp = self.gp.zeros(shape=1025) self.gp.add(a,b, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_mv_scale(self): """ Add a matrix to a vector and scale the result.""" a = self.rng.rand(129,1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) alpha = .4 beta = -1.6 out_np = (alpha*a+beta*b).astype(np.float32) out_gp = self.gp.add(a, b, alpha = alpha, beta = beta).copy_to_host() assert(np.allclose(out_np, out_gp, atol = 1.e-5)) out_gp = self.gp.zeros(shape=(129, 1025)) self.gp.add(a, b, alpha = alpha, beta = beta, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host(), atol = 1.e-5)) def test_mv(self): """ Add a matrix to a vector.""" a = self.rng.rand(129,1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) out_np = (a+b).astype(np.float32) out_gp = self.gp.add(a,b).copy_to_host() assert(np.allclose(out_np, out_gp, atol = 1.e-5)) out_gp = self.gp.zeros(shape=(129, 1025)) self.gp.add(a,b, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_ma_scale(self): """ Add a matrix to a vector with np.newaxis and scale the result.""" a = self.rng.rand(1025, 1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) alpha = .4 beta = -1.6 out_np = (alpha*a+beta*b[np.newaxis,:]).astype(np.float32) out_gp = self.gp.add(a,b[np.newaxis,:],alpha = alpha, beta = beta).copy_to_host() assert(np.allclose(out_np, out_gp, atol=1.e-5)) out_gp = self.gp.zeros(shape=(1025, 1025)) self.gp.add(a, b[np.newaxis,:], alpha = alpha, beta = beta, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host(), atol=1.e-5)) out_np = (alpha*a+beta*b[:,np.newaxis]).astype(np.float32) out_gp = self.gp.add(a, b[:,np.newaxis], alpha = alpha, beta = beta).copy_to_host() assert(np.allclose(out_np, out_gp, atol=1.e-5)) out_gp = self.gp.zeros(shape=(1025, 1025)) self.gp.add(a, b[:,np.newaxis], alpha = alpha, beta = beta, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host(), atol=1.e-5)) def test_ma(self): """Add a matrix to a vector with a np.newaxis.""" a = self.rng.rand(1025, 1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) out_np = (a+b[np.newaxis,:]).astype(np.float32) out_gp = self.gp.add(a,b[np.newaxis,:]).copy_to_host() assert(np.allclose(out_np, out_gp, atol = 1.e-5)) out_gp = self.gp.zeros(shape=(1025, 1025)) self.gp.add(a, b[np.newaxis,:], out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) out_np = (a+b[:,np.newaxis]).astype(np.float32) out_gp = self.gp.add(a,b[:,np.newaxis]).copy_to_host() assert(np.allclose(out_np, out_gp, atol = 1.e-5)) out_gp = self.gp.zeros(shape=(1025, 1025)) self.gp.add(a,b[:,np.newaxis], out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host()))
class test_relu(): def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_relu_m(self): """relu on matrices.""" a = self.rng.rand(129, 1025).astype(np.float32) t = .5 out_np = copy.deepcopy(a) out_np[out_np<t] = 0. out_gp = self.gp.relu(a, thresh=t).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(129, 1025).astype(np.float32) val = .5 out_np = copy.deepcopy(a) out_np[out_np<t] = val out_gp = self.gp.relu(a, thresh=t, set_val=val).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(129, 1025).astype(np.float32) val = .5 out_np = copy.deepcopy(a) out_np[out_np<t] = val out_gp = cuda.to_device(a) self.gp.relu(a, thresh=t, set_val=val, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_relu_m_v(self): """relu on matrices with vectors for thresholds.""" a = self.rng.rand(129, 1025).astype(np.float32) t = self.rng.rand(1025).astype(np.float32) out_np = copy.deepcopy(a) out_np[out_np<t] = 0. out_gp = self.gp.relu(a, thresh=t).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(129, 1025).astype(np.float32) t = self.rng.rand(129).astype(np.float32) val = .5 out_np = copy.deepcopy(a) out_np[out_np<t] = val out_gp = self.gp.relu(a, thresh=t, set_val=val).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(129, 1025).astype(np.float32) t = self.rng.rand(129).astype(np.float32) val = .5 out_np = copy.deepcopy(a) out_np[out_np<t] = val out_gp = cuda.to_device(a) self.gp.relu(a, thresh=t, set_val=val, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_relu_m_v(self): """relu on matrices with newaxis vectors for thresholds.""" a = self.rng.rand(129, 1025).astype(np.float32) t = self.rng.rand(1025).astype(np.float32) out_np = copy.deepcopy(a) out_np[out_np<t[np.newaxis,:]] = 0. out_gp = self.gp.relu(a, thresh=t[np.newaxis,:]).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(129, 1025).astype(np.float32) t = self.rng.rand(129).astype(np.float32) val = .5 out_np = copy.deepcopy(a) out_np[out_np<t[:,np.newaxis]] = val out_gp = self.gp.relu(a, thresh=t[:,np.newaxis], set_val=val).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(129, 1025).astype(np.float32) t = self.rng.rand(129).astype(np.float32) val = .5 out_np = copy.deepcopy(a) out_np[out_np<t[:,np.newaxis]] = val out_gp = cuda.to_device(a) self.gp.relu(a, thresh=t[:,np.newaxis], set_val=val, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_relu_v(self): """relu on vectors""" a = self.rng.rand(129).astype(np.float32) t = .5 out_np = copy.deepcopy(a) out_np[out_np<t] = 0. out_gp = self.gp.relu(a, thresh=t).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(129).astype(np.float32) val = .5 out_np = copy.deepcopy(a) out_np[out_np<t] = val out_gp = self.gp.relu(a, thresh=t, set_val=val).copy_to_host() assert(np.allclose(out_np, out_gp)) a = self.rng.rand(129).astype(np.float32) val = .5 out_np = copy.deepcopy(a) out_np[out_np<t] = val out_gp = cuda.to_device(a) self.gp.relu(a, thresh=t, set_val=val, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host()))
class test_dot(): def setup(self): self.gp = Gpupy() self.rng = np.random.RandomState(0) def test_gemm(self): """ Test matrix-matrix dot product.""" a = self.rng.rand(129, 1025).astype(np.float32) b = self.rng.rand(1025, 257).astype(np.float32) out_np = np.dot(a, b) out_gp = self.gp.dot(a, b).copy_to_host() assert(np.allclose(out_np, out_gp)) out_gp = self.gp.zeros(shape=(129, 257)) self.gp.dot(a, b, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_gemv(self): a = self.rng.rand(129, 1025).astype(np.float32) b = self.rng.rand(1025).astype(np.float32) out_np = np.dot(a, b) out_gp = self.gp.dot(a, b).copy_to_host() assert(np.allclose(out_np, out_gp)) out_gp = self.gp.zeros(shape=129) self.gp.dot(a, b, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_gevm(self): a = self.rng.rand(129, 1025).astype(np.float32) b = self.rng.rand(129).astype(np.float32) out_np = np.dot(b,a) out_gp = self.gp.dot(b,a).copy_to_host() assert(np.allclose(out_np, out_gp)) out_gp = self.gp.zeros(shape=1025) self.gp.dot(b, a, out=out_gp) assert(np.allclose(out_np, out_gp.copy_to_host())) def test_vvdot(self): a = self.rng.rand(129).astype(np.float32) b = self.rng.rand(129).astype(np.float32) out_np = np.dot(a,b) out_gp = self.gp.dot(a,b) assert(np.allclose(out_np, out_gp))