diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 9f005c9433104..6b663b0247d34 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -83,7 +83,6 @@ paddle.lgamma, paddle.poisson, paddle.bernoulli, - paddle.median, paddle.nn.functional.softmax, paddle.nn.functional.log_softmax, paddle.nn.functional.gumbel_softmax, @@ -192,8 +191,6 @@ def test_static_unary(self): paddle.logsumexp, paddle.all, paddle.any, - paddle.argmax, - paddle.argmin, ] @@ -215,10 +212,10 @@ def test_dygraph_reduce(self): self.assertEqual(x.shape, []) self.assertEqual(out.shape, []) - if api not in [paddle.argmax, paddle.argmin]: - np.testing.assert_allclose(out.numpy(), x.numpy()) - out_empty_list = api(x, []) - self.assertEqual(out_empty_list, out) + np.testing.assert_allclose(out.numpy(), x.numpy()) + + out_empty_list = api(x, []) + self.assertEqual(out_empty_list, out) if x.grad is not None: self.assertEqual(x.grad.shape, []) @@ -256,8 +253,7 @@ def test_static_reduce(self): res = exe.run(main_prog, fetch_list=fetch_list) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) - if api not in [paddle.argmax, paddle.argmin]: - np.testing.assert_allclose(res[0], res[1]) + np.testing.assert_allclose(res[0], res[1]) if len(res) > 2: self.assertEqual(res[2].shape, ()) @@ -392,22 +388,40 @@ def test_dygraph_binary(self): for api in binary_int_api_list: # 1) x is 0D, y is 0D - x = paddle.randint(-10, 10, []) - y = paddle.randint(-10, 10, []) + x_np = np.random.randint(-10, 10, []) + y_np = np.random.randint(-10, 10, []) + out_np = eval('np.%s(x_np, y_np)' % api.__name__) + + x = paddle.to_tensor(x_np) + y = paddle.to_tensor(y_np) out = api(x, y) + self.assertEqual(out.shape, []) + np.testing.assert_array_equal(out.numpy(), out_np) # 2) x is ND, y is 0D - x = paddle.randint(-10, 10, [3, 5]) - y = paddle.randint(-10, 10, []) + x_np = np.random.randint(-10, 10, [3, 5]) + y_np = np.random.randint(-10, 10, []) + out_np = eval('np.%s(x_np, y_np)' % api.__name__) + + x = paddle.to_tensor(x_np) + y = paddle.to_tensor(y_np) out = api(x, y) + self.assertEqual(out.shape, [3, 5]) + np.testing.assert_array_equal(out.numpy(), out_np) # 3) x is 0D , y is ND - x = paddle.randint(-10, 10, []) - y = paddle.randint(-10, 10, [3, 5]) + x_np = np.random.randint(-10, 10, []) + y_np = np.random.randint(-10, 10, [3, 5]) + out_np = eval('np.%s(x_np, y_np)' % api.__name__) + + x = paddle.to_tensor(x_np) + y = paddle.to_tensor(y_np) out = api(x, y) + self.assertEqual(out.shape, [3, 5]) + np.testing.assert_array_equal(out.numpy(), out_np) paddle.enable_static() @@ -556,6 +570,57 @@ def setUp(self): paddle.disable_static() self.x = paddle.rand([]) + def _test_argmin(self): + x = paddle.rand([]) + out1 = paddle.argmin(x, 0) + out2 = paddle.argmin(x, -1) + out3 = paddle.argmin(x, None) + self.assertEqual(out1.shape, []) + np.testing.assert_allclose(out1, 0.0) + + self.assertEqual(out2.shape, []) + np.testing.assert_allclose(out2, 0.0) + + self.assertEqual(out3.shape, []) + np.testing.assert_allclose(out3, 0.0) + + def _test_argmax(self): + x = paddle.rand([]) + out1 = paddle.argmax(x, 0) + out2 = paddle.argmax(x, -1) + out3 = paddle.argmax(x, None) + self.assertEqual(out1.shape, []) + np.testing.assert_allclose(out1, 0.0) + + self.assertEqual(out2.shape, []) + np.testing.assert_allclose(out2, 0.0) + + self.assertEqual(out3.shape, []) + np.testing.assert_allclose(out3, 0.0) + + def test_median(self): + x = paddle.rand([]) + x.stop_gradient = False + out1 = paddle.median(x, 0) + out2 = paddle.median(x, -1) + out3 = paddle.median(x, None) + + out1.backward() + out2.backward() + out3.backward() + + self.assertEqual(out1.shape, []) + np.testing.assert_allclose(out1, x) + + self.assertEqual(out2.shape, []) + np.testing.assert_allclose(out2, x) + + self.assertEqual(out3.shape, []) + np.testing.assert_allclose(out3, x) + + self.assertEqual(x.grad.shape, []) + np.testing.assert_allclose(x.grad, 3.0) + def test_quantile(self): # 1) x is 0D x = paddle.rand([]) @@ -1530,6 +1595,74 @@ def setUp(self): paddle.enable_static() self.exe = paddle.static.Executor() + @prog_scope() + def _test_argmin(self): + x = paddle.rand([]) + out1 = paddle.argmin(x, 0) + out2 = paddle.argmin(x, -1) + out3 = paddle.argmin(x, None) + + prog = paddle.static.default_main_program() + res = self.exe.run( + prog, + fetch_list=[ + out1, + out2, + out3, + ], + ) + self.assertEqual(res[0].shape, ()) + np.testing.assert_allclose(res[0], 0.0) + self.assertEqual(res[1].shape, ()) + np.testing.assert_allclose(res[1], 0.0) + self.assertEqual(res[2].shape, ()) + np.testing.assert_allclose(res[2], 0.0) + + @prog_scope() + def _test_argmax(self): + x = paddle.rand([]) + out1 = paddle.argmax(x, 0) + out2 = paddle.argmax(x, -1) + out3 = paddle.argmax(x, None) + + prog = paddle.static.default_main_program() + res = self.exe.run( + prog, + fetch_list=[ + out1, + out2, + out3, + ], + ) + self.assertEqual(res[0].shape, ()) + np.testing.assert_allclose(res[0], 0.0) + self.assertEqual(res[1].shape, ()) + np.testing.assert_allclose(res[1], 0.0) + self.assertEqual(res[2].shape, ()) + np.testing.assert_allclose(res[2], 0.0) + + @prog_scope() + def test_median(self): + x = paddle.rand([]) + x.stop_gradient = False + out = paddle.median(x) + + paddle.static.append_backward(out.sum()) + prog = paddle.static.default_main_program() + res = self.exe.run( + prog, + fetch_list=[ + x, + out, + x.grad_name, + ], + ) + self.assertEqual(res[1].shape, ()) + np.testing.assert_allclose(res[1], res[0]) + + self.assertEqual(res[2].shape, ()) + np.testing.assert_allclose(res[2], 1.0) + @prog_scope() def test_quantile(self): x1 = paddle.rand([]) @@ -1780,7 +1913,7 @@ def _test_gather_XD_axis_1(self): self.assertEqual(res[2].shape, (2,)) @prog_scope() - def test_scatter_1D(self): + def _test_scatter_1D(self): x = paddle.full([10], 1.0, 'float32') x.stop_gradient = False index = paddle.full([], 2, 'int64') @@ -1796,7 +1929,7 @@ def test_scatter_1D(self): self.assertEqual(res[2].shape, (10,)) @prog_scope() - def test_scatter_XD(self): + def _test_scatter_XD(self): x = paddle.full([2, 3], 1.0, 'float32') x.stop_gradient = False index = paddle.full([], 1, 'int64') diff --git a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py index 35e98e3cdaa75..5392cdbdb8daf 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py @@ -84,7 +84,6 @@ paddle.lgamma, paddle.poisson, paddle.bernoulli, - paddle.median, ] inplace_api_list = [ @@ -132,8 +131,6 @@ def test_dygraph_unary(self): paddle.logsumexp, paddle.all, paddle.any, - paddle.argmax, - paddle.argmin, ] @@ -155,8 +152,7 @@ def test_dygraph_reduce(self): self.assertEqual(x.shape, []) self.assertEqual(out.shape, []) - if api not in [paddle.argmax, paddle.argmin]: - np.testing.assert_allclose(out.numpy(), x.numpy()) + np.testing.assert_allclose(out.numpy(), x.numpy()) if x.grad is not None: self.assertEqual(x.grad.shape, []) self.assertEqual(out.grad.shape, []) @@ -287,22 +283,40 @@ def test_dygraph_binary(self): for api in binary_int_api_list: # 1) x is 0D, y is 0D - x = paddle.randint(-10, 10, []) - y = paddle.randint(-10, 10, []) + x_np = np.random.randint(-10, 10, []) + y_np = np.random.randint(-10, 10, []) + out_np = eval('np.%s(x_np, y_np)' % api.__name__) + + x = paddle.to_tensor(x_np) + y = paddle.to_tensor(y_np) out = api(x, y) + self.assertEqual(out.shape, []) + np.testing.assert_array_equal(out.numpy(), out_np) # 2) x is ND, y is 0D - x = paddle.randint(-10, 10, [3, 5]) - y = paddle.randint(-10, 10, []) + x_np = np.random.randint(-10, 10, [3, 5]) + y_np = np.random.randint(-10, 10, []) + out_np = eval('np.%s(x_np, y_np)' % api.__name__) + + x = paddle.to_tensor(x_np) + y = paddle.to_tensor(y_np) out = api(x, y) + self.assertEqual(out.shape, [3, 5]) + np.testing.assert_array_equal(out.numpy(), out_np) # 3) x is 0D , y is ND - x = paddle.randint(-10, 10, []) - y = paddle.randint(-10, 10, [3, 5]) + x_np = np.random.randint(-10, 10, []) + y_np = np.random.randint(-10, 10, [3, 5]) + out_np = eval('np.%s(x_np, y_np)' % api.__name__) + + x = paddle.to_tensor(x_np) + y = paddle.to_tensor(y_np) out = api(x, y) + self.assertEqual(out.shape, [3, 5]) + np.testing.assert_array_equal(out.numpy(), out_np) paddle.enable_static() @@ -314,6 +328,57 @@ def setUp(self): paddle.disable_static() self.x = paddle.rand([]) + def _test_argmin(self): + x = paddle.rand([]) + out1 = paddle.argmin(x, 0) + out2 = paddle.argmin(x, -1) + out3 = paddle.argmin(x, None) + self.assertEqual(out1.shape, []) + np.testing.assert_allclose(out1, 0.0) + + self.assertEqual(out2.shape, []) + np.testing.assert_allclose(out2, 0.0) + + self.assertEqual(out3.shape, []) + np.testing.assert_allclose(out3, 0.0) + + def _test_argmax(self): + x = paddle.rand([]) + out1 = paddle.argmax(x, 0) + out2 = paddle.argmax(x, -1) + out3 = paddle.argmax(x, None) + self.assertEqual(out1.shape, []) + np.testing.assert_allclose(out1, 0.0) + + self.assertEqual(out2.shape, []) + np.testing.assert_allclose(out2, 0.0) + + self.assertEqual(out3.shape, []) + np.testing.assert_allclose(out3, 0.0) + + def test_median(self): + x = paddle.rand([]) + x.stop_gradient = False + out1 = paddle.median(x, 0) + out2 = paddle.median(x, -1) + out3 = paddle.median(x, None) + + out1.backward() + out2.backward() + out3.backward() + + self.assertEqual(out1.shape, []) + np.testing.assert_allclose(out1, x) + + self.assertEqual(out2.shape, []) + np.testing.assert_allclose(out2, x) + + self.assertEqual(out3.shape, []) + np.testing.assert_allclose(out3, x) + + self.assertEqual(x.grad.shape, []) + np.testing.assert_allclose(x.grad, 3.0) + def test_linear(self): x = paddle.randn([3, 2]) w = paddle.full(shape=[2, 4], fill_value=0.5) @@ -977,8 +1042,6 @@ def test_unsqueeze(self): # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. - - class TestNoBackwardAPI(unittest.TestCase): def setUp(self): paddle.disable_static()