From 998403ffe27a7120770eca135e366e4517934e60 Mon Sep 17 00:00:00 2001 From: AyaseNana <13659110308@163.com> Date: Sat, 14 Jun 2025 11:32:32 +0800 Subject: [PATCH] fix outer accuracy --- python/paddle/tensor/math.py | 10 ++++++++++ test/legacy_test/test_outer.py | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index a6a47a6def1e36..641a0fd9840146 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -2953,6 +2953,16 @@ def outer(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: else: ny = y.reshape((1, -1)) + if x.dtype in [ + paddle.int32, + paddle.int64, + DataType.INT32, + DataType.INT64, + VarDesc.VarType.INT32, + VarDesc.VarType.INT64, + ]: + return nx * ny + if in_dynamic_mode(): return _C_ops.matmul(nx, ny, False, False) diff --git a/test/legacy_test/test_outer.py b/test/legacy_test/test_outer.py index 520405d8378ab7..e5dbee9dfaf9d8 100644 --- a/test/legacy_test/test_outer.py +++ b/test/legacy_test/test_outer.py @@ -87,6 +87,18 @@ def test_multiply_static(self): res = self._run_static_graph_case(x_data, y_data) np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05) + # test static computation graph: 3-d int32 big array + x_data = np.random.randint(-80000, 80000, [5, 10, 10]).astype(np.int32) + y_data = np.random.randint(-80000, 80000, [2, 10]).astype(np.int32) + res = self._run_static_graph_case(x_data, y_data) + np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05) + + # test static computation graph: 3-d int64 big array + x_data = np.random.randint(-80000, 80000, [5, 10, 10]).astype(np.int64) + y_data = np.random.randint(-80000, 80000, [2, 10]).astype(np.int64) + res = self._run_static_graph_case(x_data, y_data) + np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05) + def test_multiply_dynamic(self): # test dynamic computation graph: 3-d array x_data = np.random.rand(5, 10, 10).astype(np.float64) @@ -138,6 +150,18 @@ def test_multiply_dynamic(self): res = self._run_dynamic_graph_case(x_data, y_data) np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05) + # test dynamic computation graph: 3-d int32 big array + x_data = np.random.randint(-80000, 80000, [5, 10, 10]).astype(np.int32) + y_data = np.random.randint(-80000, 80000, [2, 10]).astype(np.int32) + res = self._run_dynamic_graph_case(x_data, y_data) + np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05) + + # test dynamic computation graph: 3-d int64 big array + x_data = np.random.randint(-80000, 80000, [5, 10, 10]).astype(np.int64) + y_data = np.random.randint(-80000, 80000, [2, 10]).astype(np.int64) + res = self._run_dynamic_graph_case(x_data, y_data) + np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05) + class TestMultiplyError(unittest.TestCase):