From e6e0ffbce6ad360d9d444bf02f9bc20378c2ee30 Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Thu, 22 May 2025 16:48:15 +0800 Subject: [PATCH] Fix accuracy diff for var&&std API --- python/paddle/tensor/stat.py | 6 ++++-- test/legacy_test/test_zero_dim_sundry_dygraph_api.py | 10 +++++----- test/xpu/test_zero_dim_tensor_xpu.py | 11 ++++++----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index 97e62310c89d0..a69321f7ef874 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -14,6 +14,7 @@ from __future__ import annotations +import warnings from typing import TYPE_CHECKING, Literal from typing_extensions import TypeAlias, overload @@ -29,7 +30,6 @@ from ..common_ops_import import Variable from ..framework import LayerHelper, core from .math import _get_reduce_axis_with_tensor -from .search import where if TYPE_CHECKING: from collections.abc import Sequence @@ -204,7 +204,9 @@ def var( n = n.astype(dtype) if unbiased: one_const = paddle.ones([], x.dtype) - n = where(n > one_const, n - 1.0, one_const) + if paddle.in_dynamic_mode() and n <= one_const: + warnings.warn("Degrees of freedom is <= 0.", stacklevel=2) + n = n - 1.0 n.stop_gradient = True out /= n if out.dtype != x.dtype: diff --git a/test/legacy_test/test_zero_dim_sundry_dygraph_api.py b/test/legacy_test/test_zero_dim_sundry_dygraph_api.py index 7c9df6ea78d41..2df72dd951d87 100644 --- a/test/legacy_test/test_zero_dim_sundry_dygraph_api.py +++ b/test/legacy_test/test_zero_dim_sundry_dygraph_api.py @@ -748,8 +748,8 @@ def test_std(self): self.assertEqual(out1.shape, []) self.assertEqual(out2.shape, []) - self.assertEqual(out1, 0) - self.assertEqual(out2, 0) + self.assertTrue(np.isnan(out1.numpy())) + self.assertTrue(np.isnan(out2.numpy())) self.assertEqual(x.grad.shape, []) @@ -773,11 +773,11 @@ def test_var(self): self.assertEqual(out1.shape, []) self.assertEqual(out2.shape, []) - self.assertEqual(out1, 0) - self.assertEqual(out2, 0) + self.assertTrue(np.isnan(out1.numpy())) + self.assertTrue(np.isnan(out2.numpy())) self.assertEqual(x.grad.shape, []) - np.testing.assert_allclose(x.grad, 0) + self.assertTrue(np.isnan(x.grad.numpy())) # 2) x is ND x = paddle.rand([3, 5]) diff --git a/test/xpu/test_zero_dim_tensor_xpu.py b/test/xpu/test_zero_dim_tensor_xpu.py index ac5e2df75b46f..20134b789d384 100644 --- a/test/xpu/test_zero_dim_tensor_xpu.py +++ b/test/xpu/test_zero_dim_tensor_xpu.py @@ -1074,10 +1074,11 @@ def test_std(self): self.assertEqual(out1.shape, []) self.assertEqual(out2.shape, []) - self.assertEqual(out1, 0) - self.assertEqual(out2, 0) + self.assertTrue(np.isnan(out1.numpy())) + self.assertTrue(np.isnan(out2.numpy())) self.assertEqual(x.grad.shape, []) + self.assertTrue(np.isnan(x.grad.numpy())) # 2) x is ND x = paddle.rand([3, 5]) @@ -1099,11 +1100,11 @@ def test_var(self): self.assertEqual(out1.shape, []) self.assertEqual(out2.shape, []) - self.assertEqual(out1, 0) - self.assertEqual(out2, 0) + self.assertTrue(np.isnan(out1.numpy())) + self.assertTrue(np.isnan(out2.numpy())) self.assertEqual(x.grad.shape, []) - np.testing.assert_allclose(x.grad, 0) + self.assertTrue(np.isnan(x.grad.numpy())) # 2) x is ND x = paddle.rand([3, 5])