From 67a91ca28b0f37348dffcb1191ad589ec899016a Mon Sep 17 00:00:00 2001 From: MrXnneHang Date: Sun, 10 Nov 2024 20:13:43 +0800 Subject: [PATCH 1/6] feat: alias less_than to less --- python/paddle/__init__.py | 4 ++++ python/paddle/tensor/__init__.py | 4 ++++ python/paddle/tensor/logic.py | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 24bef2bf0af37c..a88e9477199be3 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -262,6 +262,8 @@ is_empty, is_tensor, isclose, + less, + less_, less_equal, less_equal_, less_than, @@ -858,6 +860,8 @@ 'full_like', 'less_than', 'less_than_', + 'less', + 'less_', 'kron', 'clip', 'Tensor', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 6cb01ba1e3da24..79ef6dabf0a950 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -125,6 +125,8 @@ is_empty, is_tensor, isclose, + less, + less_, less_equal, less_equal_, less_than, @@ -628,6 +630,8 @@ 'less_equal_', 'less_than', 'less_than_', + 'less', + 'less_', 'logical_and', 'logical_and_', 'logical_not', diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index a6078929084e8e..3dd02b23cbf052 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1013,6 +1013,11 @@ def less_than_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: return _C_ops.less_than_(x, y) +less = less_than + +less_ = less_than_ + + def not_equal(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: """ Returns the truth value of :math:`x != y` elementwise, which is equivalent function to the overloaded operator `!=`. From 5d76efd116770b57dc1aaeed73cda01b5a995db4 Mon Sep 17 00:00:00 2001 From: MrXnneHang Date: Sun, 10 Nov 2024 20:17:06 +0800 Subject: [PATCH 2/6] test: add test for less,less_ --- test/legacy_test/test_inplace.py | 8 ++++++++ test/legacy_test/test_math_op_patch_var_base.py | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 22a844690a0953..6c5d8274aceca8 100755 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -1430,6 +1430,14 @@ def non_inplace_api_processing(self, var): return paddle.less_than(var, self.y) +class TestDygraphInplaceLess(TestDygraphInplaceLogicAnd): + def inplace_api_processing(self, var): + return paddle.less_(var, self.y) + + def non_inplace_api_processing(self, var): + return paddle.less(var, self.y) + + class TestDygraphInplaceLessEqual(TestDygraphInplaceLogicAnd): def inplace_api_processing(self, var): return paddle.less_equal_(var, self.y) diff --git a/test/legacy_test/test_math_op_patch_var_base.py b/test/legacy_test/test_math_op_patch_var_base.py index 5d5ece13679736..1aa66e705d9d2f 100644 --- a/test/legacy_test/test_math_op_patch_var_base.py +++ b/test/legacy_test/test_math_op_patch_var_base.py @@ -445,6 +445,17 @@ def test_less_than(self): res = a < b np.testing.assert_array_equal(res.numpy(), a_np < b_np) + def test_less(self): + a_np = np.random.random(self.shape).astype(self.dtype) + b_np = np.random.random(self.shape).astype(self.dtype) + with base.dygraph.guard(): + a = paddle.to_tensor(a_np) + b = paddle.to_tensor(b_np) + res_tensor = a.less(b) + res_paddle = paddle.less(a, b) + np.testing.assert_array_equal(res_tensor.numpy(), a_np < b_np) + np.testing.assert_array_equal(res_paddle.numpy, a_np < b_np) + def test_less_equal(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) From 0f32e8d9c6cfec450753fd682acee0b6aabd63ec Mon Sep 17 00:00:00 2001 From: MrXnneHang Date: Sun, 10 Nov 2024 20:23:41 +0800 Subject: [PATCH 3/6] fix: test_less --- test/legacy_test/test_math_op_patch_var_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_math_op_patch_var_base.py b/test/legacy_test/test_math_op_patch_var_base.py index 1aa66e705d9d2f..63dd6bdeacef9c 100644 --- a/test/legacy_test/test_math_op_patch_var_base.py +++ b/test/legacy_test/test_math_op_patch_var_base.py @@ -454,7 +454,7 @@ def test_less(self): res_tensor = a.less(b) res_paddle = paddle.less(a, b) np.testing.assert_array_equal(res_tensor.numpy(), a_np < b_np) - np.testing.assert_array_equal(res_paddle.numpy, a_np < b_np) + np.testing.assert_array_equal(res_paddle.numpy(), a_np < b_np) def test_less_equal(self): a_np = np.random.random(self.shape).astype(self.dtype) From 710689668f50f92cce3012fda6ce1066ce6e608a Mon Sep 17 00:00:00 2001 From: MrXnneHang Date: Sun, 10 Nov 2024 20:48:04 +0800 Subject: [PATCH 4/6] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=8D=A2=E5=88=AB?= =?UTF-8?q?=E5=90=8D=E6=98=A0=E5=B0=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/__init__.py | 4 ++-- python/paddle/tensor/__init__.py | 4 ++-- python/paddle/tensor/logic.py | 5 ----- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index a88e9477199be3..045e6445ae0783 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -262,12 +262,12 @@ is_empty, is_tensor, isclose, - less, - less_, less_equal, less_equal_, less_than, + less_than as less, less_than_, + less_than_ as less_, logical_and, logical_and_, logical_not, diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 79ef6dabf0a950..823d25356f7726 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -125,11 +125,11 @@ is_empty, is_tensor, isclose, - less, - less_, less_equal, less_equal_, less_than, + less_than as less, + less_than as less_, less_than_, logical_and, logical_and_, diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 3dd02b23cbf052..a6078929084e8e 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1013,11 +1013,6 @@ def less_than_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: return _C_ops.less_than_(x, y) -less = less_than - -less_ = less_than_ - - def not_equal(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: """ Returns the truth value of :math:`x != y` elementwise, which is equivalent function to the overloaded operator `!=`. From 567c434401fb2ad5a6545d9ffdcea7bea6eaa6ac Mon Sep 17 00:00:00 2001 From: MrXnneHang Date: Sun, 10 Nov 2024 20:49:05 +0800 Subject: [PATCH 5/6] =?UTF-8?q?test:=20test=5Fless=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=9D=99=E6=80=81=E5=9B=BE=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/legacy_test/test_math_op_patch_pir.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_math_op_patch_pir.py b/test/legacy_test/test_math_op_patch_pir.py index ccb80af41ee081..52b62ebbb2af6d 100644 --- a/test/legacy_test/test_math_op_patch_pir.py +++ b/test/legacy_test/test_math_op_patch_pir.py @@ -316,6 +316,7 @@ def test_less(self): res_np_c = paddle.less_than( paddle.to_tensor(x_np), paddle.to_tensor(y_np) ) + res_np_c_ = paddle.less(paddle.to_tensor(x_np), paddle.to_tensor(y_np)) res_np_d = x_np.__lt__(y_np) res_np_e = x_np <= y_np res_np_f = paddle.less_equal( @@ -332,18 +333,20 @@ def test_less(self): z = paddle.static.data(name="z", shape=[-1, 1], dtype='float32') b = x < y c = x.less_than(y) + c_ = x.less(y) d = x.__lt__(y) e = x <= y f = x.less_equal(y) g = x.__le__(y) h = x <= z - (b_np, c_np, d_np, e_np, f_np, g_np, h_np) = exe.run( + (b_np, c_np, c_np_, d_np, e_np, f_np, g_np, h_np) = exe.run( main_program, feed={"x": x_np, "y": y_np, "z": z_np}, - fetch_list=[b, c, d, e, f, g, h], + fetch_list=[b, c, c_, d, e, f, g, h], ) np.testing.assert_array_equal(res_np_b, b_np) np.testing.assert_array_equal(res_np_c, c_np) + np.testing.assert_array_equal(res_np_c_, c_np_) np.testing.assert_array_equal(res_np_d, d_np) np.testing.assert_array_equal(res_np_e, e_np) np.testing.assert_array_equal(res_np_f, f_np) From 176922faae1fee0774ae1186c02cd2bb440e51df Mon Sep 17 00:00:00 2001 From: MrXnneHang Date: Mon, 11 Nov 2024 14:49:01 +0800 Subject: [PATCH 6/6] test: add paddle.less in test_layers --- test/legacy_test/test_layers.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/legacy_test/test_layers.py b/test/legacy_test/test_layers.py index 3bbdaa40b1fbab..5a0e6283b59c9c 100644 --- a/test/legacy_test/test_layers.py +++ b/test/legacy_test/test_layers.py @@ -399,6 +399,7 @@ def test_compare(self): a = paddle.static.data(name='a', shape=[-1, 1], dtype='int64') b = paddle.static.data(name='b', shape=[-1, 1], dtype='int64') cond = paddle.less_than(x=a, y=b) + cond_ = paddle.less(x=a, y=b) static_ret = self.get_static_graph_result( feed={"a": value_a, "b": value_b}, fetch_list=[cond] )[0] @@ -406,9 +407,11 @@ def test_compare(self): da = paddle.to_tensor(value_a) db = paddle.to_tensor(value_b) dcond = paddle.less_than(x=da, y=db) + dcond_ = paddle.less(x=da, y=db) for i in range(len(static_ret)): self.assertTrue(dcond.numpy()[i] == static_ret[i]) + self.assertTrue(dcond_.numpy()[i] == static_ret[i]) # less equal with self.static_graph():