Skip to content

Commit

Permalink
【Paddle Tensor No.22】: 新增 paddle.less Tensor.less (#69270)
Browse files Browse the repository at this point in the history
* feat: alias less_than to less

* test: add test for less,less_

* fix: test_less

* refactor: 更换别名映射方式

* test: test_less 添加静态图测试

* test: add paddle.less in test_layers
  • Loading branch information
MrXnneHang authored Nov 12, 2024
1 parent c8a1c14 commit 4e5210e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@
less_equal,
less_equal_,
less_than,
less_than as less,
less_than_,
less_than_ as less_,
logical_and,
logical_and_,
logical_not,
Expand Down Expand Up @@ -858,6 +860,8 @@
'full_like',
'less_than',
'less_than_',
'less',
'less_',
'kron',
'clip',
'Tensor',
Expand Down
4 changes: 4 additions & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@
less_equal,
less_equal_,
less_than,
less_than as less,
less_than as less_,
less_than_,
logical_and,
logical_and_,
Expand Down Expand Up @@ -628,6 +630,8 @@
'less_equal_',
'less_than',
'less_than_',
'less',
'less_',
'logical_and',
'logical_and_',
'logical_not',
Expand Down
8 changes: 8 additions & 0 deletions test/legacy_test/test_inplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test/legacy_test/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,19 @@ 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]
with self.dynamic_graph():
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():
Expand Down
7 changes: 5 additions & 2 deletions test/legacy_test/test_math_op_patch_pir.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,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(
Expand All @@ -338,18 +339,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)
Expand Down
11 changes: 11 additions & 0 deletions test/legacy_test/test_math_op_patch_var_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4e5210e

Please sign in to comment.