Enable sparse tensor dense matmul op tests in eager mode.
PiperOrigin-RevId: 335670100 Change-Id: I959563a43a8955810e9a8672f127fc0af6aab5cd
This commit is contained in:
parent
6ae2b68fd3
commit
e665554b90
@ -97,7 +97,6 @@ class SparseTensorDenseMatMulTest(test.TestCase):
|
|||||||
|
|
||||||
self._testMatmul(x, y, indices_dtype=indices_dtype)
|
self._testMatmul(x, y, indices_dtype=indices_dtype)
|
||||||
|
|
||||||
@test_util.run_deprecated_v1
|
|
||||||
def testBasic(self):
|
def testBasic(self):
|
||||||
np.random.seed(127) # Repeatable results
|
np.random.seed(127) # Repeatable results
|
||||||
self._testBasic(np.int32)
|
self._testBasic(np.int32)
|
||||||
@ -108,7 +107,6 @@ class SparseTensorDenseMatMulTest(test.TestCase):
|
|||||||
self._testBasic(np.int32, indices_dtype=np.int32)
|
self._testBasic(np.int32, indices_dtype=np.int32)
|
||||||
self._testBasic(np.float32, indices_dtype=np.int32)
|
self._testBasic(np.float32, indices_dtype=np.int32)
|
||||||
|
|
||||||
@test_util.run_deprecated_v1
|
|
||||||
def testShapeInference(self):
|
def testShapeInference(self):
|
||||||
x = np.random.rand(10, 10)
|
x = np.random.rand(10, 10)
|
||||||
x[np.abs(x) < 0.5] = 0 # Make it sparse
|
x[np.abs(x) < 0.5] = 0 # Make it sparse
|
||||||
@ -116,6 +114,8 @@ class SparseTensorDenseMatMulTest(test.TestCase):
|
|||||||
x_indices = np.vstack(np.where(x)).astype(np.int64).T
|
x_indices = np.vstack(np.where(x)).astype(np.int64).T
|
||||||
x_values = x[np.where(x)]
|
x_values = x[np.where(x)]
|
||||||
x_shape = x.shape
|
x_shape = x.shape
|
||||||
|
|
||||||
|
with ops.Graph().as_default():
|
||||||
x_st = sparse_tensor.SparseTensor(x_indices, x_values, x_shape)
|
x_st = sparse_tensor.SparseTensor(x_indices, x_values, x_shape)
|
||||||
result = sparse_ops.sparse_tensor_dense_matmul(x_st, y)
|
result = sparse_ops.sparse_tensor_dense_matmul(x_st, y)
|
||||||
self.assertEqual(result.get_shape(), (10, 20))
|
self.assertEqual(result.get_shape(), (10, 20))
|
||||||
@ -129,15 +129,14 @@ class SparseTensorDenseMatMulTest(test.TestCase):
|
|||||||
[None, 20])
|
[None, 20])
|
||||||
|
|
||||||
x_shape_inconsistent = [10, 15]
|
x_shape_inconsistent = [10, 15]
|
||||||
x_st_shape_inconsistent = sparse_tensor.SparseTensor(x_indices, x_values,
|
x_st_shape_inconsistent = sparse_tensor.SparseTensor(
|
||||||
x_shape_inconsistent)
|
x_indices, x_values, x_shape_inconsistent)
|
||||||
with self.assertRaisesRegex(ValueError, "Dimensions must be equal"):
|
with self.assertRaisesRegex(ValueError, "Dimensions must be equal"):
|
||||||
sparse_ops.sparse_tensor_dense_matmul(x_st_shape_inconsistent, y)
|
sparse_ops.sparse_tensor_dense_matmul(x_st_shape_inconsistent, y)
|
||||||
|
|
||||||
@test_util.deprecated_graph_mode_only
|
@test_util.run_in_graph_and_eager_modes(use_gpu=False)
|
||||||
def testInvalidIndicesForSparseTensorDenseMatmul(self):
|
def testInvalidIndicesForSparseTensorDenseMatmul(self):
|
||||||
# Note: use_gpu=False because nice errors are only returned from CPU kernel.
|
# TODO(b/169813429): Make GPU kernel return nice errors too.
|
||||||
with self.session(use_gpu=False):
|
|
||||||
indices = np.matrix([[1, 10]]).astype(np.int64)
|
indices = np.matrix([[1, 10]]).astype(np.int64)
|
||||||
values = np.array([10]).astype(np.float32)
|
values = np.array([10]).astype(np.float32)
|
||||||
shape = [3, 2]
|
shape = [3, 2]
|
||||||
@ -146,33 +145,26 @@ class SparseTensorDenseMatMulTest(test.TestCase):
|
|||||||
# Test multiplying by both a small and large dense matrix, to hit
|
# Test multiplying by both a small and large dense matrix, to hit
|
||||||
# both cases in the kernel.
|
# both cases in the kernel.
|
||||||
dense_t = np.matrix([[1] * 5, [2] * 5], dtype=np.float32)
|
dense_t = np.matrix([[1] * 5, [2] * 5], dtype=np.float32)
|
||||||
with self.assertRaisesOpError(
|
with self.assertRaisesOpError("k .10. from index.0,1. out of bounds .>=2."):
|
||||||
"k .10. from index.0,1. out of bounds .>=2."):
|
|
||||||
self.evaluate(sparse_ops.sparse_tensor_dense_matmul(sparse_t, dense_t))
|
self.evaluate(sparse_ops.sparse_tensor_dense_matmul(sparse_t, dense_t))
|
||||||
dense_t = np.matrix([[1] * 500, [2] * 500], dtype=np.float32)
|
dense_t = np.matrix([[1] * 500, [2] * 500], dtype=np.float32)
|
||||||
with self.assertRaisesOpError(
|
with self.assertRaisesOpError("k .10. from index.0,1. out of bounds .>=2."):
|
||||||
"k .10. from index.0,1. out of bounds .>=2."):
|
|
||||||
self.evaluate(sparse_ops.sparse_tensor_dense_matmul(sparse_t, dense_t))
|
self.evaluate(sparse_ops.sparse_tensor_dense_matmul(sparse_t, dense_t))
|
||||||
|
|
||||||
# Repeat with adjoint_a, to get a different error.
|
# Repeat with adjoint_a, to get a different error.
|
||||||
dense_t = np.matrix([[1] * 5, [2] * 5, [3] * 5], dtype=np.float32)
|
dense_t = np.matrix([[1] * 5, [2] * 5, [3] * 5], dtype=np.float32)
|
||||||
with self.assertRaisesOpError(
|
with self.assertRaisesOpError("m .10. from index.0,1. out of bounds .>=2."):
|
||||||
"m .10. from index.0,1. out of bounds .>=2."):
|
|
||||||
self.evaluate(
|
self.evaluate(
|
||||||
sparse_ops.sparse_tensor_dense_matmul(
|
sparse_ops.sparse_tensor_dense_matmul(
|
||||||
sparse_t, dense_t, adjoint_a=True))
|
sparse_t, dense_t, adjoint_a=True))
|
||||||
dense_t = np.matrix([[1] * 500, [2] * 500, [3] * 500], dtype=np.float32)
|
dense_t = np.matrix([[1] * 500, [2] * 500, [3] * 500], dtype=np.float32)
|
||||||
with self.assertRaisesOpError(
|
with self.assertRaisesOpError("m .10. from index.0,1. out of bounds .>=2."):
|
||||||
"m .10. from index.0,1. out of bounds .>=2."):
|
|
||||||
self.evaluate(
|
self.evaluate(
|
||||||
sparse_ops.sparse_tensor_dense_matmul(
|
sparse_ops.sparse_tensor_dense_matmul(
|
||||||
sparse_t, dense_t, adjoint_a=True))
|
sparse_t, dense_t, adjoint_a=True))
|
||||||
|
|
||||||
|
@test_util.run_gpu_only
|
||||||
def testInvalidIndicesForSparseTensorDenseMatmulOnGPU(self):
|
def testInvalidIndicesForSparseTensorDenseMatmulOnGPU(self):
|
||||||
# Note: use_gpu=False because nice errors are only returned from CPU kerne
|
|
||||||
if not test.is_gpu_available():
|
|
||||||
return
|
|
||||||
with self.session(use_gpu=True):
|
|
||||||
indices = np.array([[1, 10]]).astype(np.int64)
|
indices = np.array([[1, 10]]).astype(np.int64)
|
||||||
values = np.array([10]).astype(np.float32)
|
values = np.array([10]).astype(np.float32)
|
||||||
shape = [3, 2]
|
shape = [3, 2]
|
||||||
@ -182,15 +174,13 @@ class SparseTensorDenseMatMulTest(test.TestCase):
|
|||||||
# both cases in the kernel.
|
# both cases in the kernel.
|
||||||
dense_t = np.matrix([[1] * 5, [2] * 5], dtype=np.float32)
|
dense_t = np.matrix([[1] * 5, [2] * 5], dtype=np.float32)
|
||||||
expected_t = np.array([[0] * 5, [np.nan] * 5, [0] * 5], dtype=np.float32)
|
expected_t = np.array([[0] * 5, [np.nan] * 5, [0] * 5], dtype=np.float32)
|
||||||
self.assertAllClose(expected_t,
|
self.assertAllClose(
|
||||||
sparse_ops.sparse_tensor_dense_matmul(
|
expected_t, sparse_ops.sparse_tensor_dense_matmul(sparse_t, dense_t))
|
||||||
sparse_t, dense_t))
|
|
||||||
dense_t = np.matrix([[1] * 500, [2] * 500], dtype=np.float32)
|
dense_t = np.matrix([[1] * 500, [2] * 500], dtype=np.float32)
|
||||||
expected_t = np.array(
|
expected_t = np.array([[0] * 500, [np.nan] * 500, [0] * 500],
|
||||||
[[0] * 500, [np.nan] * 500, [0] * 500], dtype=np.float32)
|
dtype=np.float32)
|
||||||
self.assertAllClose(expected_t,
|
self.assertAllClose(
|
||||||
sparse_ops.sparse_tensor_dense_matmul(
|
expected_t, sparse_ops.sparse_tensor_dense_matmul(sparse_t, dense_t))
|
||||||
sparse_t, dense_t))
|
|
||||||
|
|
||||||
# Repeat with adjoint_a, now the error is that the sparse index
|
# Repeat with adjoint_a, now the error is that the sparse index
|
||||||
# is OOO w.r.t. the output. The GPU kernel can't do much here,
|
# is OOO w.r.t. the output. The GPU kernel can't do much here,
|
||||||
@ -198,13 +188,15 @@ class SparseTensorDenseMatMulTest(test.TestCase):
|
|||||||
|
|
||||||
dense_t = np.matrix([[1] * 5, [2] * 5, [3] * 5], dtype=np.float32)
|
dense_t = np.matrix([[1] * 5, [2] * 5, [3] * 5], dtype=np.float32)
|
||||||
expected_t = np.array([[0] * 5, [0] * 5], dtype=np.float32)
|
expected_t = np.array([[0] * 5, [0] * 5], dtype=np.float32)
|
||||||
self.assertAllClose(expected_t,
|
self.assertAllClose(
|
||||||
|
expected_t,
|
||||||
sparse_ops.sparse_tensor_dense_matmul(
|
sparse_ops.sparse_tensor_dense_matmul(
|
||||||
sparse_t, dense_t, adjoint_a=True))
|
sparse_t, dense_t, adjoint_a=True))
|
||||||
|
|
||||||
dense_t = np.matrix([[1] * 500, [2] * 500, [3] * 500], dtype=np.float32)
|
dense_t = np.matrix([[1] * 500, [2] * 500, [3] * 500], dtype=np.float32)
|
||||||
expected_t = np.array([[0] * 500, [0] * 500], dtype=np.float32)
|
expected_t = np.array([[0] * 500, [0] * 500], dtype=np.float32)
|
||||||
self.assertAllClose(expected_t,
|
self.assertAllClose(
|
||||||
|
expected_t,
|
||||||
sparse_ops.sparse_tensor_dense_matmul(
|
sparse_ops.sparse_tensor_dense_matmul(
|
||||||
sparse_t, dense_t, adjoint_a=True))
|
sparse_t, dense_t, adjoint_a=True))
|
||||||
|
|
||||||
@ -235,7 +227,6 @@ class SparseTensorDenseMatMulTest(test.TestCase):
|
|||||||
self._testLarge(np.complex128)
|
self._testLarge(np.complex128)
|
||||||
|
|
||||||
# Tests random sized matrices.
|
# Tests random sized matrices.
|
||||||
@test_util.run_deprecated_v1
|
|
||||||
def testFloatRandom(self):
|
def testFloatRandom(self):
|
||||||
np.random.seed(127) # Repeatable results
|
np.random.seed(127) # Repeatable results
|
||||||
for _ in range(8):
|
for _ in range(8):
|
||||||
|
Loading…
Reference in New Issue
Block a user