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
9 changes: 9 additions & 0 deletions test/legacy_test/test_diff_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,15 @@ def test_fp16_with_gpu(self):
paddle.disable_static()


class TestDiffOp_ZeroSize(TestDiffOp):
def set_args(self):
self.input = np.array([1, 0, 5, 2]).astype('float32')
self.n = 2
self.axis = 0
self.prepend = None
self.append = None


if __name__ == '__main__':
paddle.enable_static()
unittest.main()
24 changes: 24 additions & 0 deletions test/legacy_test/test_glu.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,29 @@ def test_errors(self):
self.assertRaises(TypeError, act, x_int32)


class TestGLU_ZeroSize(unittest.TestCase):
def setUp(self):
self.x = np.random.randn(5, 0, 20)
self.dim = -1
self.out = glu(self.x, self.dim)

def check_dygraph(self, place):
with dg.guard(place):
x_var = paddle.to_tensor(self.x)
x_var.stop_gradient = False
y_var = F.glu(x_var, self.dim)
y_np = y_var.numpy()
np.testing.assert_allclose(y_np, self.out)

loss = paddle.sum(y_var)
loss.backward()
np.testing.assert_allclose(x_var.grad.shape, x_var.shape)

def test_case(self):
self.check_dygraph(base.CPUPlace())
if base.is_compiled_with_cuda():
self.check_dygraph(base.CUDAPlace(0))


if __name__ == '__main__':
unittest.main()
34 changes: 34 additions & 0 deletions test/legacy_test/test_nan_to_num_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,39 @@ def test_check_grad(self):
# 'neginf': -10
# }


class TestNanToNum_ZeroSize(unittest.TestCase):
def setUp(self):
self.place = (
paddle.CUDAPlace(0)
if core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
self.x_np = np.random.random([2, 0, 3]).astype(np.float32)

def test_dygraph(self):
paddle.disable_static(place=self.place)

with paddle.base.dygraph.guard():
x_tensor = paddle.to_tensor(self.x_np, stop_gradient=False)

out_tensor = paddle.nan_to_num(x_tensor)
out_np = np_nan_to_num(self.x_np)
np.testing.assert_allclose(out_tensor.numpy(), out_np)

paddle.enable_static()

def test_check_grad(self):
paddle.disable_static(place=self.place)
x_tensor = paddle.to_tensor(self.x_np, stop_gradient=False)
x_tensor.stop_gradient = False
y = paddle.nan_to_num(x_tensor)
loss = paddle.sum(y)
loss.backward()
np.testing.assert_allclose(x_tensor.grad.shape, x_tensor.shape)

paddle.enable_static()


if __name__ == "__main__":
unittest.main()
49 changes: 49 additions & 0 deletions test/legacy_test/test_nanmean_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,54 @@ def test_case(x, axis=None, keepdim=False):
paddle.enable_static()


class TestNanmeanAPI_ZeroSize(unittest.TestCase):
# test paddle.tensor.math.nanmean

def setUp(self):
self.x_shape = [2, 0, 4, 5]
self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32)
self.x[0, :, :, :] = np.nan
self.place = (
paddle.CUDAPlace(0)
if core.is_compiled_with_cuda()
else paddle.CPUPlace()
)

def test_api_dygraph(self):
paddle.disable_static(self.place)

def test_case(x, axis=None, keepdim=False):
x_tensor = paddle.to_tensor(x)
out = paddle.nanmean(x_tensor, axis, keepdim)
if isinstance(axis, list):
axis = tuple(axis)
if len(axis) == 0:
axis = None

out_ref = np.nanmean(x, axis, keepdims=keepdim)
np.testing.assert_allclose(out.numpy(), out_ref, rtol=0.0001)

test_case(self.x)
paddle.enable_static()

def test_api_dygraph_grad(self):
paddle.disable_static(self.place)

def test_case(x, axis=None, keepdim=False):
if isinstance(axis, list):
axis = list(axis)
if len(axis) == 0:
axis = None
x_tensor = paddle.to_tensor(x, stop_gradient=False)
x_tensor.stop_gradient = False
y = paddle.nanmean(x_tensor, axis, keepdim)
loss = paddle.sum(y)
loss.backward()
np.testing.assert_allclose(x_tensor.grad.shape, x_tensor.shape)

test_case(self.x)
paddle.enable_static()


if __name__ == "__main__":
unittest.main()
21 changes: 21 additions & 0 deletions test/legacy_test/test_nansum_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,26 @@ def test_dygraph(self):
)


class API_Test_Nansum_ZeroSize(unittest.TestCase):

def test_dygraph(self):
x = np.random.random([2, 0, 3]).astype(np.float32)
with base.dygraph.guard():
inputs = paddle.to_tensor(x)
inputs.stop_gradient = False
out = paddle.nansum(inputs)
out_ref = np.nansum(x).astype(np.float32)

self.assertTrue(
(out.numpy() == out_ref).all(),
msg='nansum output is wrong, out =' + str(out.numpy()),
)

# check grad shape
loss = paddle.sum(out)
loss.backward()
np.testing.assert_allclose(inputs.grad.shape, inputs.shape)


if __name__ == "__main__":
unittest.main()
14 changes: 14 additions & 0 deletions test/legacy_test/test_quantile_and_nanquantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ def check_grad(x, q, axis, target_grad, apis=None):
[(paddle.nanquantile, None)],
)

def test_nanquantile_ZeroSize(self):
input_data = np.full(shape=[2, 0, 3], fill_value=np.nan)
x = paddle.to_tensor(input_data)
x.stop_gradient = False
paddle_res = paddle.nanquantile(x, q=0.35, axis=0)
np_res = np.nanquantile(input_data, q=0.35, axis=0)
np.testing.assert_allclose(
paddle_res.numpy(), np_res, rtol=1e-05, equal_nan=True
)

loss = paddle.sum(paddle_res)
loss.backward()
np.testing.assert_allclose(x.grad.shape, x.shape)


class TestMuitlpleQ(unittest.TestCase):
"""
Expand Down