Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rm unittest eager guard tests part20 sparse_mv2split #48879

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 62 additions & 65 deletions python/paddle/fluid/tests/unittests/test_sparse_mv_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import numpy as np

import paddle
from paddle.fluid.framework import _test_eager_guard

paddle.seed(100)

Expand All @@ -43,38 +42,37 @@ def get_cuda_version():
class TestCsrMv(unittest.TestCase):
# x: csr-matrix, y: dense-vec, out: dense-vec
def test_mv(self):
with _test_eager_guard():
paddle.set_default_dtype('float64')
origin_x = paddle.rand([64, 32])
mask = paddle.randint(0, 2, [64, 32])
origin_x = origin_x * mask
origin_vec = paddle.rand([32])

dense_x = origin_x.detach()
dense_x.stop_gradient = False
dense_vec = origin_vec.detach()
dense_vec.stop_gradient = False
dense_out = paddle.mv(dense_x, dense_vec)
dense_out.backward()

sp_x = origin_x.detach().to_sparse_csr()
sp_x.stop_gradient = False
sp_vec = origin_vec.detach()
sp_vec.stop_gradient = False
sp_out = paddle.sparse.mv(sp_x, sp_vec)
sp_out.backward()

np.testing.assert_allclose(
sp_out.numpy(), dense_out.numpy(), rtol=1e-05
)
np.testing.assert_allclose(
sp_x.grad.to_dense().numpy(),
(dense_x.grad * mask).numpy(),
rtol=1e-05,
)
np.testing.assert_allclose(
sp_vec.grad.numpy(), dense_vec.grad.numpy(), rtol=1e-05
)
paddle.set_default_dtype('float64')
origin_x = paddle.rand([64, 32])
mask = paddle.randint(0, 2, [64, 32])
origin_x = origin_x * mask
origin_vec = paddle.rand([32])

dense_x = origin_x.detach()
dense_x.stop_gradient = False
dense_vec = origin_vec.detach()
dense_vec.stop_gradient = False
dense_out = paddle.mv(dense_x, dense_vec)
dense_out.backward()

sp_x = origin_x.detach().to_sparse_csr()
sp_x.stop_gradient = False
sp_vec = origin_vec.detach()
sp_vec.stop_gradient = False
sp_out = paddle.sparse.mv(sp_x, sp_vec)
sp_out.backward()

np.testing.assert_allclose(
sp_out.numpy(), dense_out.numpy(), rtol=1e-05
)
np.testing.assert_allclose(
sp_x.grad.to_dense().numpy(),
(dense_x.grad * mask).numpy(),
rtol=1e-05,
)
np.testing.assert_allclose(
sp_vec.grad.numpy(), dense_vec.grad.numpy(), rtol=1e-05
)


@unittest.skipIf(
Expand All @@ -84,38 +82,37 @@ def test_mv(self):
class TestCooMv(unittest.TestCase):
# x: csr-matrix, y: dense-vec, out: dense-vec
def test_mv(self):
with _test_eager_guard():
paddle.set_default_dtype('float64')
origin_x = paddle.rand([64, 32])
mask = paddle.randint(0, 2, [64, 32])
origin_x = origin_x * mask
origin_vec = paddle.rand([32])

dense_x = origin_x.detach()
dense_x.stop_gradient = False
dense_vec = origin_vec.detach()
dense_vec.stop_gradient = False
dense_out = paddle.mv(dense_x, dense_vec)
dense_out.backward()

sp_x = origin_x.detach().to_sparse_coo(sparse_dim=2)
sp_x.stop_gradient = False
sp_vec = origin_vec.detach()
sp_vec.stop_gradient = False
sp_out = paddle.sparse.mv(sp_x, sp_vec)
sp_out.backward()

np.testing.assert_allclose(
sp_out.numpy(), dense_out.numpy(), rtol=1e-05
)
np.testing.assert_allclose(
sp_x.grad.to_dense().numpy(),
(dense_x.grad * mask).numpy(),
rtol=1e-05,
)
np.testing.assert_allclose(
sp_vec.grad.numpy(), dense_vec.grad.numpy(), rtol=1e-05
)
paddle.set_default_dtype('float64')
origin_x = paddle.rand([64, 32])
mask = paddle.randint(0, 2, [64, 32])
origin_x = origin_x * mask
origin_vec = paddle.rand([32])

dense_x = origin_x.detach()
dense_x.stop_gradient = False
dense_vec = origin_vec.detach()
dense_vec.stop_gradient = False
dense_out = paddle.mv(dense_x, dense_vec)
dense_out.backward()

sp_x = origin_x.detach().to_sparse_coo(sparse_dim=2)
sp_x.stop_gradient = False
sp_vec = origin_vec.detach()
sp_vec.stop_gradient = False
sp_out = paddle.sparse.mv(sp_x, sp_vec)
sp_out.backward()

np.testing.assert_allclose(
sp_out.numpy(), dense_out.numpy(), rtol=1e-05
)
np.testing.assert_allclose(
sp_x.grad.to_dense().numpy(),
(dense_x.grad * mask).numpy(),
rtol=1e-05,
)
np.testing.assert_allclose(
sp_vec.grad.numpy(), dense_vec.grad.numpy(), rtol=1e-05
)


if __name__ == "__main__":
Expand Down
77 changes: 37 additions & 40 deletions python/paddle/fluid/tests/unittests/test_sparse_pooling_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import numpy as np

import paddle
from paddle.fluid.framework import _test_eager_guard


class TestMaxPool3DFunc(unittest.TestCase):
Expand All @@ -42,32 +41,31 @@ def setUp(self):
self.setPadding()

def test(self):
with _test_eager_guard():
self.setUp()
self.dense_x.stop_gradient = False
sparse_x = self.dense_x.to_sparse_coo(4)
sparse_out = paddle.sparse.nn.functional.max_pool3d(
sparse_x,
self.kernel_sizes,
stride=self.strides,
padding=self.paddings,
)
out = sparse_out.to_dense()
out.backward(out)

dense_x = copy.deepcopy(self.dense_x)
dense_out = paddle.nn.functional.max_pool3d(
dense_x,
self.kernel_sizes,
stride=self.strides,
padding=self.paddings,
data_format='NDHWC',
)
dense_out.backward(dense_out)

# compare with dense
assert np.allclose(dense_out.numpy(), out.numpy())
assert np.allclose(dense_x.grad.numpy(), self.dense_x.grad.numpy())
self.setUp()
self.dense_x.stop_gradient = False
sparse_x = self.dense_x.to_sparse_coo(4)
sparse_out = paddle.sparse.nn.functional.max_pool3d(
sparse_x,
self.kernel_sizes,
stride=self.strides,
padding=self.paddings,
)
out = sparse_out.to_dense()
out.backward(out)

dense_x = copy.deepcopy(self.dense_x)
dense_out = paddle.nn.functional.max_pool3d(
dense_x,
self.kernel_sizes,
stride=self.strides,
padding=self.paddings,
data_format='NDHWC',
)
dense_out.backward(dense_out)

# compare with dense
assert np.allclose(dense_out.numpy(), out.numpy())
assert np.allclose(dense_x.grad.numpy(), self.dense_x.grad.numpy())


class TestStride(TestMaxPool3DFunc):
Expand Down Expand Up @@ -102,19 +100,18 @@ def setInput(self):

class TestMaxPool3DAPI(unittest.TestCase):
def test(self):
with _test_eager_guard():
dense_x = paddle.randn((2, 3, 6, 6, 3))
sparse_x = dense_x.to_sparse_coo(4)
max_pool3d = paddle.sparse.nn.MaxPool3D(
kernel_size=3, data_format='NDHWC'
)
out = max_pool3d(sparse_x)
out = out.to_dense()

dense_out = paddle.nn.functional.max_pool3d(
dense_x, 3, data_format='NDHWC'
)
assert np.allclose(dense_out.numpy(), out.numpy())
dense_x = paddle.randn((2, 3, 6, 6, 3))
sparse_x = dense_x.to_sparse_coo(4)
max_pool3d = paddle.sparse.nn.MaxPool3D(
kernel_size=3, data_format='NDHWC'
)
out = max_pool3d(sparse_x)
out = out.to_dense()

dense_out = paddle.nn.functional.max_pool3d(
dense_x, 3, data_format='NDHWC'
)
assert np.allclose(dense_out.numpy(), out.numpy())


if __name__ == "__main__":
Expand Down
Loading