-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR不正确,__rand__
的作用是为了支持int & Tensor
这种操作,而你的PR只是将__and__
复制了一份,并没有实现预期的功能
res_np_c = paddle.bitwise_and( | ||
paddle.to_tensor(y_np), paddle.to_tensor(x_np) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
此处测试应该直接调用__rand__
或者通过int & Tensor
间接调用__rand__
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的
@@ -269,14 +270,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 = x.__rand__(y) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里应该通过 int/bool & Tensor
的方式触发 Tensor.__rand__
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的
老师我感觉这个逻辑没什么问题呀,为什么ctest不通过呢 |
单测使用 python xxx.py运行,写完代码可以编包安装,再自测一下。 |
好的 |
python/paddle/tensor/logic.py
Outdated
@@ -1231,6 +1231,11 @@ def bitwise_and( | |||
) | |||
|
|||
|
|||
def __rand__(x: Tensor, y: int | bool): | |||
y_tensor = paddle.to_tensor(y, dtype=x.dtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里应该有一个判断,只有int、bool类型的才需要to_tensor,再计算and,其他类型的应该抛出错误
raise TypeError(
f"unsupported operand type(s) for |: '{type(y).__name__}' and 'Tensor'"
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已改
python/paddle/tensor/logic.py
Outdated
@@ -1231,6 +1231,16 @@ def bitwise_and( | |||
) | |||
|
|||
|
|||
def __rand__(x: Tensor, y: int | bool): | |||
if not isinstance(y, (int, bool)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
从代码规范上来说,代码尽量不要用取反逻辑,减少理解成本,建议把两个分支互换一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
谢谢老师。已修改
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
谢谢老师,已修改
检测基本完成,麻烦老师看一下 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR Category
User Experience
PR Types
New Features
Description
Paddle Tensor 规范化:新增 Tensor.rand,复用已有接口 Tensor.and