def test_zero_MN_gemm(self): # check other border cases... e = gulinalg.matrix_multiply(np.zeros((0,2)), np.zeros((2,2))) assert_allclose(e, np.zeros((0,2))) f = gulinalg.matrix_multiply(np.zeros((2,2)), np.zeros((2,0))) assert_allclose(f, np.zeros((2,0)))
def test_zero_MN_gemm(self): # check other border cases... e = gulinalg.matrix_multiply(np.zeros((0, 2)), np.zeros((2, 2))) assert_allclose(e, np.zeros((0, 2))) f = gulinalg.matrix_multiply(np.zeros((2, 2)), np.zeros((2, 0))) assert_allclose(f, np.zeros((2, 0)))
def test_matrix_multiply_ff_f(self): """matrix multiply two FORTRAN layout matrices, explicit FORTRAN array output""" a = np.asfortranarray(np.random.randn(M, N)) b = np.asfortranarray(np.random.randn(N, K)) res = np.zeros((M, K), order='F') gulinalg.matrix_multiply(a, b, out=res) ref = np.dot(a, b) assert_allclose(res, ref)
def test_uninit_gemm(self): a = np.zeros((298, 64)) b = np.zeros((64, 298)) c = np.zeros((298, 298), order='F') c.fill(np.nan) gulinalg.matrix_multiply(a, b, out=c) assert not np.isnan(c).any()
def test_matrix_multiply_fc_f(self): """matrix multiply FORTRAN layout by C layout matrices, explicit FORTRAN array output""" a = np.asfortranarray(np.random.randn(M,N)) b = np.ascontiguousarray(np.random.randn(N,K)) res = np.zeros((M,K), order='F') gulinalg.matrix_multiply(a,b, out=res) ref = np.dot(a,b) assert_allclose(res, ref)
def test_matrix_multiply_ff_c(self): """matrix multiply two FORTRAN layout matrices, explicit C array output""" a = np.asfortranarray(np.random.randn(M,N)) b = np.asfortranarray(np.random.randn(N,K)) res = np.zeros((M,K), order='C') gulinalg.matrix_multiply(a,b, out=res) ref = np.dot(a,b) assert_allclose(res, ref)
def test_matrix_multiply_fc_c(self): """matrix multiply FORTRAN layout by C layout matrices, explicit C array output""" a = np.asfortranarray(np.random.randn(M, N)) b = np.ascontiguousarray(np.random.randn(N, K)) res = np.zeros((M, K), order='C') gulinalg.matrix_multiply(a, b, out=res) ref = np.dot(a, b) assert_allclose(res, ref)
def test_input_non_contiguous_1(self): """first input not contiguous""" a = np.ascontiguousarray(np.random.randn(M, N, 2))[:, :, 0] b = np.ascontiguousarray(np.random.randn(N, K)) res = np.zeros((M, K), order='C') assert not a.flags.c_contiguous and not a.flags.f_contiguous gulinalg.matrix_multiply(a, b, out=res) ref = np.dot(a, b) assert_allclose(res, ref)
def test_output_non_contiguous(self): """output not contiguous""" a = np.ascontiguousarray(np.random.randn(M,N)) b = np.ascontiguousarray(np.random.randn(N,K)) res = np.zeros((M,K,2), order='C')[:,:,0] assert not res.flags.c_contiguous and not res.flags.f_contiguous gulinalg.matrix_multiply(a, b, out=res) ref = np.dot(a,b) assert_allclose(res, ref)
def test_input_non_contiguous_1(self): """first input not contiguous""" a = np.ascontiguousarray(np.random.randn(M,N,2))[:,:,0] b = np.ascontiguousarray(np.random.randn(N,K)) res = np.zeros((M,K), order='C') assert not a.flags.c_contiguous and not a.flags.f_contiguous gulinalg.matrix_multiply(a, b, out=res) ref = np.dot(a,b) assert_allclose(res, ref)
def test_output_non_contiguous(self): """output not contiguous""" a = np.ascontiguousarray(np.random.randn(M, N)) b = np.ascontiguousarray(np.random.randn(N, K)) res = np.zeros((M, K, 2), order='C')[:, :, 0] assert not res.flags.c_contiguous and not res.flags.f_contiguous gulinalg.matrix_multiply(a, b, out=res) ref = np.dot(a, b) assert_allclose(res, ref)
def test_matrix_multiply_ff_f_batch(self): """matrix multiply two C layout matrices""" n_outer = 4 for workers in [1, -1]: a = np.random.randn(M, N) b = np.random.randn(N, K) ref = np.dot(a, b) a = np.asfortranarray(np.stack((a, ) * n_outer, axis=0)) b = np.asfortranarray(np.stack((b, ) * n_outer, axis=0)) ref = np.stack((ref, ) * n_outer, axis=0) res = np.zeros((n_outer, M, K), order='F') gulinalg.matrix_multiply(a, b, workers=workers, out=res) assert_allclose(res, ref)
def test_output_non_contiguous_batch(self): """output not contiguous""" n_outer = 4 for workers in [1, -1]: a = np.random.randn(M, N) b = np.random.randn(N, K) ref = np.dot(a, b) a = np.ascontiguousarray(np.stack((a, ) * n_outer, axis=0)) b = np.ascontiguousarray(np.stack((b, ) * n_outer, axis=0)) ref = np.stack((ref, ) * n_outer, axis=0) res = np.zeros((n_outer, M, K, 2), order='C')[..., 0] assert not res.flags.c_contiguous and not res.flags.f_contiguous gulinalg.matrix_multiply(a, b, workers=workers, out=res) assert_allclose(res, ref)
def test_input_non_contiguous_3_batch(self): """neither input contiguous""" n_outer = 4 for workers in [1, -1]: a = np.random.randn(M, N, 2) b = np.random.randn(N, K, 2) ref = np.dot(a[..., 0], b[..., 0]) a = np.ascontiguousarray(np.stack((a, ) * n_outer, axis=0))[..., 0] b = np.ascontiguousarray(np.stack((b, ) * n_outer, axis=0))[..., 0] assert not a.flags.c_contiguous and not a.flags.f_contiguous assert not b.flags.c_contiguous and not b.flags.f_contiguous ref = np.stack((ref, ) * n_outer, axis=0) res = np.zeros((n_outer, M, K), order='C') gulinalg.matrix_multiply(a, b, workers=workers, out=res) assert_allclose(res, ref)
def test_matrix_multiply_cc(self): """matrix multiply two C layout matrices""" a = np.ascontiguousarray(np.random.randn(M,N)) b = np.ascontiguousarray(np.random.randn(N,K)) res = gulinalg.matrix_multiply(a,b) ref = np.dot(a,b) assert_allclose(res, ref)
def test_real_vector_f(self): m = 10 rstate = np.random.RandomState(5) a = rstate.randn(n_batch, m, m) a = gulinalg.matrix_multiply(a, a.swapaxes(-2, -1)) # make symmetric a = np.asfortranarray(a) self._run_vector(a)
def test_matrix_multiply_ff(self): """matrix multiply two FORTRAN layout matrices""" a = np.asfortranarray(np.random.randn(M,N)) b = np.asfortranarray(np.random.randn(N,K)) res = gulinalg.matrix_multiply(a,b) ref = np.dot(a,b) assert_allclose(res, ref)
def test_matrix_multiply_ff(self): """matrix multiply two FORTRAN layout matrices""" a = np.asfortranarray(np.random.randn(M, N)) b = np.asfortranarray(np.random.randn(N, K)) res = gulinalg.matrix_multiply(a, b) ref = np.dot(a, b) assert_allclose(res, ref)
def test_matrix_multiply_cc(self): """matrix multiply two C layout matrices""" a = np.ascontiguousarray(np.random.randn(M, N)) b = np.ascontiguousarray(np.random.randn(N, K)) res = gulinalg.matrix_multiply(a, b) ref = np.dot(a, b) assert_allclose(res, ref)
def test_complex_vector(self): m = 10 rstate = np.random.RandomState(5) a = rstate.randn(n_batch, m, m) a = a + 1j * rstate.randn(n_batch, m, m) a = gulinalg.matrix_multiply( a, np.conj(a).swapaxes(-2, -1) # make symmetric ) self._run_vector(a)
def test_zero_K_gemm(self): a = np.zeros((2, 0)) b = np.zeros((0, 2)) c = np.empty((2, 2), order="C") c.fill(np.nan) # make sure that the result is set to zero gulinalg.matrix_multiply(a, b, out=c) assert_allclose(c, np.zeros((2, 2))) # make sure that this works also when order is FORTRAN c = np.empty((2, 2), order="F") c.fill(np.nan) gulinalg.matrix_multiply(a, b, out=c) assert_allclose(c, np.zeros((2, 2))) # check that the correct shape is created as well... d = gulinalg.matrix_multiply(a, b) assert_allclose(d, np.zeros((2, 2)))
def test_broadcast(self): """test broadcast matrix multiply""" a = np.ascontiguousarray(np.random.randn(M, N)) b = np.ascontiguousarray(np.random.randn(10, N, K)) res = gulinalg.matrix_multiply(a,b) assert res.shape == (10, M, K) ref = np.stack([np.dot(a, b[i]) for i in range(len(b))]) assert_allclose(res, ref)
def test_broadcast(self): """test broadcast matrix multiply""" a = np.ascontiguousarray(np.random.randn(M, N)) b = np.ascontiguousarray(np.random.randn(10, N, K)) res = gulinalg.matrix_multiply(a, b) assert res.shape == (10, M, K) ref = np.stack([np.dot(a, b[i]) for i in range(len(b))]) assert_allclose(res, ref)
def test_zero_K_gemm(self): a = np.zeros((2,0)) b = np.zeros((0,2)) c = np.empty((2,2), order="C") c.fill(np.nan) # make sure that the result is set to zero gulinalg.matrix_multiply(a, b, out=c) assert_allclose(c, np.zeros((2,2))) # make sure that this works also when order is FORTRAN c = np.empty((2,2), order="F") c.fill(np.nan) gulinalg.matrix_multiply(a, b, out=c) assert_allclose(c, np.zeros((2,2))) # check that the correct shape is created as well... d = gulinalg.matrix_multiply(a,b) assert_allclose(d, np.zeros((2,2)))
def test_stride_tricks(self): """test that matrices that are contiguous but have their dimension overlapped *copy*, as BLAS does not support them""" a = np.ascontiguousarray(np.random.randn(M + N)) a = np.lib.stride_tricks.as_strided(a, shape=(M,N), strides=(a.itemsize, a.itemsize)) b = np.ascontiguousarray(np.random.randn(N,K)) res = gulinalg.matrix_multiply(a,b) ref = np.dot(a,b) assert_allclose(res, ref)
def test_real_broadcast_L(self): m = 5 nbatch = 16 a = np.asfortranarray(np.random.randn(m, m)) a = np.dot(a, a.T) # make Hermetian symmetric a = np.stack((a, ) * nbatch, axis=0) for workers in [1, -1]: L = gulinalg.poinv(np.tril(a), UPLO='L') assert_allclose(gulinalg.matrix_multiply(a, L), np.stack((np.eye(m), ) * nbatch, axis=0), atol=1e-10)
def test_matrix_multiply_fc_batch(self): """matrix multiply two C layout matrices""" n_outer = 4 for workers in [1, -1]: a = np.random.randn(M, N) b = np.random.randn(N, K) ref = np.dot(a, b) a = np.asfortranarray(np.stack((a, ) * n_outer, axis=0)) b = np.ascontiguousarray(np.stack((b, ) * n_outer, axis=0)) ref = np.stack((ref, ) * n_outer, axis=0) res = gulinalg.matrix_multiply(a, b, workers=workers) assert_allclose(res, ref)
def test_real_vector_noncontig(self): m = 10 rstate = np.random.RandomState(5) a = rstate.randn(n_batch, m, m, 2)[..., 0] a = gulinalg.matrix_multiply(a, a.swapaxes(-2, -1)) # make symmetric self._run_vector(a)
def _check_eigen(self, A, w, v): '''vectorial check of Mv==wv''' lhs = gulinalg.matrix_multiply(A, v) rhs = w*v assert_allclose(lhs, rhs, rtol=1e-6)