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

【Hackathon 5th No.34】 为 Paddle 新增 bitwise_right_shift / bitwise_right_shift_ / bitwise_left_shift / bitwise_left_shift_ API (update RFC) #788

Merged
merged 5 commits into from
Jan 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions rfcs/APIs/20230927_api_design_for_bitwise_shift.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# paddle.pdist设计文档
# paddle.bitwise_left_shift/paddle.bitwise_right_shift设计文档

| API 名称 | paddle.bitwise_right_shift<br />paddle.bitwise_left_shift |
| ------------ | --------------------------------------------------------- |
Expand Down Expand Up @@ -145,7 +145,9 @@ void rshift_kernel_cuda(TensorIteratorBase& iter) {
+ 算术右移时,`max_shift`需要考虑最大的移动距离,有符号数最高位为符号位,故表示数值的位数实际上会少一位。
+ 有符号数时,例如`int8 x=-100`,补码为`1001,1100`,最高位为符号位,仅需要右移7位,所有的`int8`就都会变成`1111,1111`,即`-1`;
+ 无符号数时候,例如`uint8 x=200`,存储为`1100,1000`,八位均表示数值大小,需要右移8位才可以将所有的`uint8`变为`0000,0000`,即`0`;
+ 当`b`位负数这一未定义行为时,同样等效于右移无穷位,与移动`max_shift`等效,有符号数变为`-1`,无符号数变为`0`
+ 当`b`位负数这一未定义行为时,同样等效于右移无穷位,与移动`max_shift`等效(从代码中可以看到,`b<0`和`b>=max_shift`是在同一个`if`判断中),只要满足两个条件中任意一个,则使得有符号数变为`-1`,无符号数变为`0`。

Choose a reason for hiding this comment

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

“当‘b’位负数这一未定义行为时“,有个错别字“位“应该为”为“

Copy link
Contributor Author

Choose a reason for hiding this comment

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

“当‘b’位负数这一未定义行为时“,有个错别字“位“应该为”为“

sry,更正了~


**在paddle API的设计过程中,也按照这样的方式来实现,当`b`为负数或者移动超过最大值,则使得有符号数变为`-1`,无符号数变为`0`**

Choose a reason for hiding this comment

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

错别字,“位“应该为”为“。对于未定义的行为怎么处理,应该列出竞品实际是如何处理的,处理方式是否一致,这些都是要说清楚的。



Expand Down Expand Up @@ -342,7 +344,9 @@ PyTorch是将算子注册到element wise系列中,Numpy也类似地`BINARY_LOO

同时,PyTorch与Numpy中都仅支持算术位移,不支持逻辑位移,而JAX中实现了算术位移和逻辑位移。

PyTorch和numpy的处理基本一致,在前面Numpy的调研部分详细说明了两者略微的差异,这个差异不影响最终结果,两者处理的思路都是一致的。

面对第二个参数`b`为负数的时候,都是将其等效为位移无穷大的距离(这两个判断条件在同一个`if`中,用“或”逻辑连接),处理方式都是使有符号数时变为`-1`,无符号数时变为`0`。

# 五、设计思路与实现方案

Expand Down Expand Up @@ -415,7 +419,7 @@ cpp的kernel实现主要通过elementwise的方法,与`bitwise_and`等bitwise
2. 逻辑左移时,符号位同其他位一样,一起左移,右边补0;
3. 算术右移时,符号位同其他位一样,一起右移,左边补符号位;
4. 逻辑右移时,符号位同其他位一样,一起右移,左边补0;

注意:当有符号数左移发生溢出时,其值不可控,可能会在左移时突然变号,这是因为在左移时,有符号数的符号位同样进行左移,会导致符号位右侧的值不断成为符号位,例如
```
example1:
Expand Down