Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【SCU】【Paddle Tensor No.5】新增 Tensor.__rand__,复用已有接口Tensor.__and__ #69473

Merged
merged 30 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
15399e4
Add English explanation
SCUcookie Nov 14, 2024
8986f04
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 15, 2024
17bbaeb
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 15, 2024
773e954
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
09fe571
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
d20ae97
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
18349c5
modify code
SCUcookie Nov 18, 2024
34d57d5
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
2461584
add __rand__
SCUcookie Nov 18, 2024
72fc12d
cancel update
SCUcookie Nov 18, 2024
6d4addc
add __rand__
SCUcookie Nov 18, 2024
eeb07ec
delete superfluous code
SCUcookie Nov 18, 2024
48e14ac
recover code
SCUcookie Nov 18, 2024
ae0a24b
Merge branch 'PaddlePaddle:develop' into Origin
SCUcookie Nov 18, 2024
82c622c
develop
SCUcookie Nov 18, 2024
73bb823
recover
SCUcookie Nov 18, 2024
cf81a6d
Merge branch 'PaddlePaddle:develop' into develop
SCUcookie Nov 18, 2024
376e452
add __rand__
SCUcookie Nov 18, 2024
777324f
Merge branch 'develop' of github.com:SCUcookie/Paddle into MyTensor
SCUcookie Nov 18, 2024
43a4d5c
Merge branch 'Origin' of github.com:SCUcookie/Paddle into MyTensor
SCUcookie Nov 18, 2024
214b64c
finish
SCUcookie Nov 18, 2024
b1a12a2
modify v1
SCUcookie Nov 19, 2024
3bb3cdd
question
SCUcookie Nov 21, 2024
c99d929
try
SCUcookie Nov 21, 2024
a456dbe
precommit
SCUcookie Nov 21, 2024
fddc3c2
modifying
SCUcookie Nov 21, 2024
0f9469c
add error report
SCUcookie Nov 21, 2024
f689980
precommit
SCUcookie Nov 21, 2024
78f4800
precommit
SCUcookie Nov 21, 2024
f190969
modify test_math_op_patch and log.py
SCUcookie Nov 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
transpose_,
)
from .logic import ( # noqa: F401
__rand__,
allclose,
bitwise_and,
bitwise_and_,
Expand Down Expand Up @@ -862,6 +863,7 @@
# this list used in math_op_patch.py for magic_method bind
magic_method_func = [
('__and__', 'bitwise_and'),
('__rand__', '__rand__'),
('__or__', 'bitwise_or'),
('__xor__', 'bitwise_xor'),
('__invert__', 'bitwise_not'),
Expand Down
10 changes: 10 additions & 0 deletions python/paddle/tensor/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,16 @@ def bitwise_and(
)


def __rand__(x: Tensor, y: int | bool):
if isinstance(y, (int, bool)):
y_tensor = paddle.to_tensor(y, dtype=x.dtype)
return bitwise_and(y_tensor, x, None, None)
else:
raise TypeError(
f"unsupported operand type(s) for |: '{type(y).__name__}' and 'Tensor'"
)


@inplace_apis_in_dygraph_only
def bitwise_and_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
r"""
Expand Down
1 change: 1 addition & 0 deletions python/paddle/tensor/tensor.prototype.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class AbstractTensor:
def __rpow__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore
def __rdiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore
def __rfloordiv__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore
def __rand__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore

# type cast
def __bool__(self) -> bool: ...
Expand Down
12 changes: 7 additions & 5 deletions test/legacy_test/test_math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,21 +299,23 @@ def test_astype(self):
np.testing.assert_allclose(a_np.astype('float32'), b_np, rtol=1e-05)

def test_bitwise_and(self):
temp = 2
x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32")
y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32")
out_np = x_np & y_np

e_np = temp & x_np
x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32")
y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32")
z = x & y

e = temp & x
exe = base.Executor()
out = exe.run(
(out, e_out) = exe.run(
base.default_main_program(),
feed={"x": x_np, "y": y_np},
fetch_list=[z],
fetch_list=[z, e],
)
np.testing.assert_array_equal(out[0], out_np)
np.testing.assert_array_equal(out, out_np)
np.testing.assert_array_equal(e_out, e_np)

@prog_scope()
def test_bitwise_or(self):
Expand Down
8 changes: 6 additions & 2 deletions test/legacy_test/test_math_op_patch_pir.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一个文件需要添加单测:Paddle/test/legacy_test/test_math_op_patch.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

谢谢老师,已修改

Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ def test_bitwise_and(self):
paddle.to_tensor(x_np), paddle.to_tensor(y_np)
)
res_np_d = x_np.__and__(y_np)
temp = 2
res_np_e = temp & y_np
paddle.enable_static()
with paddle.pir_utils.IrGuard():
main_program, exe, program_guard = new_program()
Expand All @@ -269,14 +271,16 @@ def test_bitwise_and(self):
b = x & y
c = x.bitwise_and(y)
d = x.__and__(y)
(b_np, c_np, d_np) = exe.run(
e = temp & y
(b_np, c_np, d_np, e_np) = exe.run(
main_program,
feed={"x": x_np, "y": y_np},
fetch_list=[b, c, d],
fetch_list=[b, c, d, e],
)
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_d, d_np)
np.testing.assert_array_equal(res_np_e, e_np)

def test_positive(self):
paddle.disable_static()
Expand Down