Skip to content
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
38 changes: 38 additions & 0 deletions test/legacy_test/test_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,43 @@ def setUp(self):
self.aw_s = -1.0


class Cov_Test_ZeroSize(unittest.TestCase):
def setUp(self):
self.shape = [0, 4]

def test_tensor_cov_default(self):
typelist = ['float64']
places = []
if (
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
in ['1', 'true', 'on']
or not base.core.is_compiled_with_cuda()
):
places.append(base.CPUPlace())
if base.core.is_compiled_with_cuda():
places.append(base.CUDAPlace(0))

for idx, p in enumerate(places):
if idx == 0:
paddle.set_device('cpu')
else:
paddle.set_device('gpu')

for dtype in typelist:
np_arr = np.random.rand(*self.shape).astype(dtype)
tensor = paddle.to_tensor(np_arr, place=p)
tensor.stop_gradient = False
cov = paddle.linalg.cov(
tensor, rowvar=True, ddof=True, fweights=None, aweights=None
)
np_cov = numpy_cov(
np_arr, rowvar=True, ddof=1, fweights=None, aweights=None
)
np.testing.assert_allclose(np_cov, cov.numpy(), rtol=1e-05)
loss = paddle.sum(cov)
loss.backward()
np.testing.assert_equal(tensor.grad.shape, tensor.shape)


if __name__ == '__main__':
unittest.main()
27 changes: 27 additions & 0 deletions test/legacy_test/test_inverse_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,33 @@ def test_dygraph(self):
print("The mat is singular")


class TestInverseAPI_ZeroSize(unittest.TestCase):
def setUp(self):
np.random.seed(123)
self.places = []
if (
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
in ['1', 'true', 'on']
or not core.is_compiled_with_cuda()
):
self.places.append(base.CPUPlace())
if core.is_compiled_with_cuda():
self.places.append(base.CUDAPlace(0))

def test_dygraph(self):
for place in self.places:
with base.dygraph.guard(place):
input_np = np.random.random([4, 0]).astype("float64")
input = paddle.to_tensor(input_np)
input.stop_gradient = False
result = paddle.linalg.inv(input)
np_out = np.random.random([4, 0]).astype("float64")
np.testing.assert_allclose(result.numpy(), np_out, rtol=1e-05)
loss = paddle.sum(result)
loss.backward()
np.testing.assert_allclose(input.grad.shape, input.shape)


if __name__ == "__main__":
paddle.enable_static()
unittest.main()
30 changes: 30 additions & 0 deletions test/legacy_test/test_norm_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,28 @@ def check_linalg_vector_dygraph_and_grad(
loss.backward()
np.testing.assert_equal(x_paddle.grad.shape, x_paddle.shape)

def check_linalg_matrix_dygraph_and_grad(
self, p, axis, shape_x, dtype, keep_dim, check_dim=False
):
x_numpy = (np.random.random(shape_x) + 1.0).astype(dtype)
expected_result = np_linalg_matrix_norm(
x_numpy, porder=p, axis=axis, keepdims=keep_dim
)
x_paddle = paddle.to_tensor(x_numpy)
x_paddle.stop_gradient = False
result1 = paddle.linalg.matrix_norm(
x=x_paddle, p=p, axis=axis, keepdim=keep_dim
)
result = result1.numpy()
np.testing.assert_allclose(
result, expected_result, rtol=1e-6, atol=1e-8
)
if keep_dim and check_dim:
np.testing.assert_equal(result.shape, expected_result.shape)
loss = paddle.sum(result1)
loss.backward()
np.testing.assert_equal(x_paddle.grad.shape, x_paddle.shape)

def test_dygraph(self):
paddle.disable_static()
keep_dims = {False, True}
Expand All @@ -1680,6 +1702,14 @@ def test_dygraph(self):
keep_dim=keep,
)

self.check_linalg_matrix_dygraph_and_grad(
p=np.inf,
axis=[1, 2],
shape_x=[0, 3, 4],
dtype="float32",
keep_dim=keep,
)


if __name__ == '__main__':
paddle.enable_static()
Expand Down
Loading