def _testSparseSparse(self, transpose_a, transpose_b, adjoint_a, adjoint_b): if not self._gpu_available: return sparsify = lambda m: m * (m > 0) dense_shape_a = [5, 13, 7] if transpose_a or adjoint_a else [5, 7, 13] dense_shape_b = [5, 15, 13] if transpose_b or adjoint_b else [5, 13, 15] dtypes_to_test = [np.float32, np.complex64] for dtype in dtypes_to_test: a_mats = sparsify((np.random.randn(*dense_shape_a) + 1.j * np.random.randn(*dense_shape_a))).astype(dtype) b_mats = sparsify((np.random.randn(*dense_shape_b) + 1.j * np.random.randn(*dense_shape_b))).astype(dtype) a_sm = sparse_csr_matrix_ops.CSRSparseMatrix(a_mats) b_sm = sparse_csr_matrix_ops.CSRSparseMatrix(b_mats) c_dense = test_util.matmul_without_tf32( a_mats, b_mats, transpose_a=transpose_a, transpose_b=transpose_b, adjoint_a=adjoint_a, adjoint_b=adjoint_b) c_sm = sparse_csr_matrix_ops.matmul( a_sm, b_sm, transpose_a=transpose_a, transpose_b=transpose_b, adjoint_a=adjoint_a, adjoint_b=adjoint_b) self.assertIsInstance(c_sm, sparse_csr_matrix_ops.CSRSparseMatrix) c_sm_dense = c_sm.to_dense() c_dense, c_sm_dense = self.evaluate([c_dense, c_sm_dense]) self.assertAllClose(c_dense, c_sm_dense)
def _testDenseSparse(self, transpose_a, transpose_b, adjoint_a, adjoint_b): if not self._gpu_available: return sparsify = lambda m: m * (m > 0) dense_shape_a = [5, 13, 7] if transpose_a or adjoint_a else [5, 7, 13] dense_shape_b = [5, 15, 13 ] if transpose_b or adjoint_b else [5, 13, 15] for dtype in np.float32, np.complex64: a_mats = (np.random.randn(*dense_shape_a) + 1.j * np.random.randn(*dense_shape_a)).astype(dtype) b_mats = sparsify( (np.random.randn(*dense_shape_b) + 1.j * np.random.randn(*dense_shape_b))).astype(dtype) b_sm = sparse_csr_matrix_ops.CSRSparseMatrix(b_mats) c_dense = math_ops.matmul(a_mats, b_mats, transpose_a=transpose_a, transpose_b=transpose_b, adjoint_a=adjoint_a, adjoint_b=adjoint_b) c_sm_dense = sparse_csr_matrix_ops.matmul(a_mats, b_sm, transpose_a=transpose_a, transpose_b=transpose_b, adjoint_a=adjoint_a, adjoint_b=adjoint_b) c_dense, c_sm_dense = self.evaluate([c_dense, c_sm_dense]) self.assertAllClose(c_dense, c_sm_dense)
def _testDenseSparse(self, transpose_a, transpose_b, adjoint_a, adjoint_b): if not self._gpu_available: return sparsify = lambda m: m * (m > 0) dense_shape_a = [5, 13, 7] if transpose_a or adjoint_a else [5, 7, 13] dense_shape_b = [5, 15, 13 ] if transpose_b or adjoint_b else [5, 13, 15] dtypes_to_test = [np.float32] if not test.is_built_with_rocm(): # complex type is not supported on the ROCm platform dtypes_to_test += [np.complex64] for dtype in dtypes_to_test: a_mats = (np.random.randn(*dense_shape_a) + 1.j * np.random.randn(*dense_shape_a)).astype(dtype) b_mats = sparsify( (np.random.randn(*dense_shape_b) + 1.j * np.random.randn(*dense_shape_b))).astype(dtype) b_sm = sparse_csr_matrix_ops.CSRSparseMatrix(b_mats) c_dense = math_ops.matmul(a_mats, b_mats, transpose_a=transpose_a, transpose_b=transpose_b, adjoint_a=adjoint_a, adjoint_b=adjoint_b) c_sm_dense = sparse_csr_matrix_ops.matmul(a_mats, b_sm, transpose_a=transpose_a, transpose_b=transpose_b, adjoint_a=adjoint_a, adjoint_b=adjoint_b) c_dense, c_sm_dense = self.evaluate([c_dense, c_sm_dense]) self.assertAllClose(c_dense, c_sm_dense)
def testConj(self): if not self._gpu_available: return sparsify = lambda m: m * (m.real > 0) dense_shape = [5, 7, 13] a_mats = sparsify( (np.random.randn(*dense_shape) + 1.j * np.random.randn(*dense_shape)) .astype(np.complex64)) a_sm = sparse_csr_matrix_ops.CSRSparseMatrix(a_mats) a_sm_conj = a_sm.conj() self.assertIsInstance(a_sm_conj, sparse_csr_matrix_ops.CSRSparseMatrix) a_sm_conj_dense = a_sm_conj.to_dense() a_sm_conj_dense = self.evaluate(a_sm_conj_dense) self.assertAllClose(a_mats.conj(), a_sm_conj_dense)
def testConstructorFromDenseTensorNoIndices(self): if not self._gpu_available: return sparsify = lambda m: m * (m > 0) dense_shape = [5, 7, 13] a_mats = sparsify(np.random.randn(*dense_shape)).astype(np.float32) a_sm = sparse_csr_matrix_ops.CSRSparseMatrix(a_mats) self.assertEqual(a_sm.shape, a_mats.shape) a_sm_rt = a_sm.to_dense() a_sm_nnz = a_sm.nnz() a_sm_nnz, a_sm_rt = self.evaluate([a_sm_nnz, a_sm_rt]) # Count number of nonzero entries for each batch using bincount. nz = np.bincount(a_mats.nonzero()[0], minlength=a_mats.shape[0]) self.assertAllEqual(nz, a_sm_nnz) self.assertAllClose(a_mats, a_sm_rt)
def testConstructorFromSparseTensor(self): if not self._gpu_available: return a_indices = np.array([[0, 0], [2, 3], [2, 4], [3, 0]]) a_values = [1.0, 5.0, -1.0, -2.0] a_dense_shape = [5, 6] a_st = sparse_tensor.SparseTensor(a_indices, a_values, a_dense_shape) a_st = math_ops.cast(a_st, dtypes.float32) a_sm = sparse_csr_matrix_ops.CSRSparseMatrix(a_st) self.assertEqual(a_sm.shape, a_dense_shape) a_st_rt = a_sm.to_sparse_tensor() a_st_rt = self.evaluate(a_st_rt) self.assertAllEqual(a_indices, a_st_rt.indices) self.assertAllClose(a_values, a_st_rt.values) self.assertAllEqual(a_dense_shape, a_st_rt.dense_shape)
def testConstructorFromDenseTensorWithIndices(self): if not self._gpu_available: return dense_shape = [5, 7, 13] a_mats = np.random.randn(*dense_shape).astype(np.float32) indices = np.array([[0, 0, 0], [1, 0, 0]], dtype=np.int64) a_sm = sparse_csr_matrix_ops.CSRSparseMatrix(a_mats, indices=indices) self.assertEqual(a_sm.shape, a_mats.shape) a_sm_st = a_sm.to_sparse_tensor() a_sm_st = self.evaluate(a_sm_st) # Count number of nonzero entries for each batch using bincount. self.assertAllEqual(indices, a_sm_st.indices) self.assertAllEqual(dense_shape, a_sm.shape) self.assertAllEqual(dense_shape, a_sm_st.dense_shape) self.assertAllClose([a_mats[tuple(x)] for x in indices], a_sm_st.values)
def testTranspose(self): if not self._gpu_available: return for conjugate in False, True: sparsify = lambda m: m * (m > 0) dense_shape = [5, 7, 13] a_mats = sparsify((np.random.randn(*dense_shape) + 1.j * np.random.randn(*dense_shape))).astype( np.complex64) expected = np.transpose(a_mats, (0, 2, 1)) if conjugate: expected = np.conj(expected) a_sm = sparse_csr_matrix_ops.CSRSparseMatrix(a_mats) if conjugate: a_sm_t = a_sm.hermitian_transpose() else: a_sm_t = a_sm.transpose() self.assertIsInstance(a_sm_t, sparse_csr_matrix_ops.CSRSparseMatrix) a_sm_t_dense = a_sm_t.to_dense() a_sm_t_dense = self.evaluate(a_sm_t_dense) self.assertAllClose(expected, a_sm_t_dense)
from tensorflow.python.ops.linalg.sparse import sparse_csr_matrix_ops vals = [[1.0, 1.0, 1.0]] st = tf.sparse.eye(hidden_size) st = tf.sparse.reshape(st, [block_size, block_size, size[0], size[1]]) st = tf.sparse.transpose(st, [0,2,1,3]) s = tf.sparse.eye(16) s = tf.sparse.reshape(s, [4, 4, 4, 4]) s = tf.sparse.transpose(s, [0,2,1,3]) qq = r(tf.sparse.to_dense(s)) s1 = tf.sparse.reshape(s, [-1, 4, 4]) sm = sparse_csr_matrix_ops.CSRSparseMatrix(s1) mat = sparse_csr_matrix_ops.CSRSparseMatrix(vals, indices=indices) from tensorflow.python.ops.linalg.sparse import sparse_csr_matrix_ops def thunk():