-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[API Compatibility No.6] Sink bitwise_xor and bitwise_xor_ to cpp -part #76341
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
base: develop
Are you sure you want to change the base?
Changes from 8 commits
d74db35
80d3ad9
c7ed42b
4ed899f
f6841d0
9f92fbd
4f01bbc
6d385e6
c9e9706
0f7b938
2b1b309
e25e4da
db3b1fb
d5240bf
d3bab02
38b750c
b7bf087
a434bce
ea3d45e
68d4666
a34a8de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,12 @@ | |
| args_alias: | ||
| use_default_mapping : True | ||
|
|
||
| - op : bitwise_xor | ||
| name : [paddle.bitwise_xor, paddle.Tensor.bitwise_xor] | ||
| args_alias : | ||
| x : [input] | ||
|
||
| y : [other] | ||
|
|
||
| - op : argmax | ||
| name : [paddle.argmax, paddle.Tensor.argmax] | ||
| args_mapper : | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2709,3 +2709,56 @@ def abs( | |
| # zhanrongrun | ||
|
|
||
| # other | ||
| add_doc_and_signature( | ||
| "bitwise_xor_", | ||
|
||
| r""" | ||
| An inplace version of ``bitwise_xor``. | ||
|
|
||
| This API applies bitwise XOR between the elements of `x` and the corresponding elements of `y`, | ||
| modifies `x` in-place, and returns the modified `x`. | ||
|
|
||
| Note: | ||
| - Broadcasting is supported; see `Introduction to Tensor`_ for broadcasting rules. | ||
| .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor | ||
| - This is an in-place operation. It mutates `x`. Use with care in dynamic graph / autograd | ||
| scenarios as it may affect gradients and the computation graph. | ||
|
|
||
| Args: | ||
| x (Tensor): The input tensor to be modified in-place. Supported dtypes: bool, uint8, int8, | ||
| int16, int32, int64. | ||
| y (Tensor): The tensor whose values are XORed with `x`. Must have a dtype compatible with `x`. | ||
| Broadcasting with `x` is supported. | ||
| name (str|None, optional): Name for the operation (optional, default is None). Typically not required. | ||
|
|
||
| Returns: | ||
| Tensor: The modified tensor `x`. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| >>> import paddle | ||
|
|
||
| >>> # Integer example | ||
| >>> x = paddle.to_tensor([-5, -1, 1], dtype='int64') | ||
| >>> y = paddle.to_tensor([ 4, 2, -3], dtype='int64') | ||
| >>> paddle.bitwise_xor_(x, y) # in-place; equivalent to x.bitwise_xor_(y) | ||
| >>> print(x) | ||
| Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, | ||
| [-1, -3, -4]) | ||
|
|
||
| >>> # Boolean example | ||
| >>> x = paddle.to_tensor([True, False, True], dtype='bool') | ||
| >>> y = paddle.to_tensor([True, True, False], dtype='bool') | ||
| >>> x.bitwise_xor_(y) # in-place | ||
| >>> print(x) | ||
| Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, | ||
| [False, True, True]) | ||
| """, | ||
| """ | ||
| def bitwise_xor_( | ||
| x: Tensor, | ||
| y: Tensor, | ||
| name: str | None = None, | ||
| ) -> Tensor | ||
| """, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
| from typing import TYPE_CHECKING, Any # , overload | ||
|
|
||
| from typing_extensions import TypeGuard | ||
|
|
||
|
|
@@ -49,6 +49,25 @@ | |
| if TYPE_CHECKING: | ||
| from paddle import Tensor | ||
|
|
||
| # @overload | ||
|
||
| # def bitwise_xor( | ||
| # x: Tensor, y: Tensor, out: Tensor | None = None, name: str | None = None | ||
| # ) -> Tensor: ... | ||
|
|
||
| # @overload | ||
| # def bitwise_xor( | ||
| # *, x: Tensor, y: Tensor, out: Tensor | None = None, name: str | None = None | ||
| # ) -> Tensor: ... | ||
|
|
||
| # def bitwise_xor( | ||
| # *, | ||
| # input: Tensor, | ||
| # other: Tensor, | ||
| # out: Tensor | None = None, | ||
| # name: str | None = None, | ||
| # ) -> Tensor: ... | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 下沉后,需要移除之前的python api,就没有这一层逻辑了。直接走c++ |
||
| __all__ = [] | ||
|
|
||
|
|
||
|
|
@@ -1226,7 +1245,9 @@ def bitwise_xor( | |
|
|
||
|
||
| Args: | ||
| x (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64. | ||
| Parameter alias: ``input``. | ||
| y (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64. | ||
| Parameter alias: ``other``. | ||
| out (Tensor|None, optional): Result of ``bitwise_xor`` . It is a N-D Tensor with the same data type of input Tensor. Default: None. | ||
| name (str|None, optional): The default value is None. Normally there is no need for | ||
| user to set this property. For more information, please refer to :ref:`api_guide_Name`. | ||
|
|
@@ -1240,10 +1261,17 @@ def bitwise_xor( | |
| >>> import paddle | ||
| >>> x = paddle.to_tensor([-5, -1, 1]) | ||
| >>> y = paddle.to_tensor([4, 2, -3]) | ||
| >>> # Using original parameter name | ||
| >>> res = paddle.bitwise_xor(x, y) | ||
| >>> print(res) | ||
| Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, | ||
| [-1, -3, -4]) | ||
|
|
||
| >>> # Using Pytorch-compatible parameter name | ||
| >>> res = paddle.bitwise_xor(input=x, other=y) | ||
| >>> print(res) | ||
| Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, | ||
| [-1, -3, -4]) | ||
| """ | ||
| if in_dynamic_or_pir_mode() and out is None: | ||
| return _C_ops.bitwise_xor(x, y) | ||
|
|
@@ -1268,6 +1296,7 @@ def __rxor__( | |
|
|
||
|
|
||
| @inplace_apis_in_dygraph_only | ||
| @ParamAliasDecorator({"x": ["input"], "y": ["other"]}) | ||
|
||
| def bitwise_xor_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: | ||
| r""" | ||
| Inplace version of ``bitwise_xor`` API, the output Tensor will be inplaced with input ``x``. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1553,6 +1553,22 @@ def non_inplace_api_processing(self, var): | |
| return paddle.bitwise_xor(var, self.y) | ||
|
|
||
|
|
||
| class TestDygraphInplacBitwiseXorAlias1(TestDygraphInplacBitwiseAnd): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. paddle.bitwise_xor测一下compat情况下的用法
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bitwise_xor_ 需要吗?好像 bitwise_xor_ 用了 compat 会有 segfault 的情况,我认为可能是 bitwise_xor_ 下沉了但是 ops.yaml 里没有 bitwise_xor_ 的定义。需要添加吗?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ops.yaml里不需要bitwise_xor_定义,会根据bitwise_xor的inplace字段,来生成bitwise_xor_ 两者本质是共享一个kernel的,只在输出out处理这里有些区别。
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
先按标准来测试,不要绕过,先提前暴露问题。 如果inplace的API下沉不了就先用装饰器吧,底层问题先暴露出来。因为这也是第1个inplace的API下沉。 |
||
| def inplace_api_processing(self, var): | ||
| return paddle.bitwise_xor_(var, other=self.y) | ||
|
|
||
| def non_inplace_api_processing(self, var): | ||
| return paddle.bitwise_xor(var, other=self.y) | ||
|
|
||
|
|
||
| class TestDygraphInplacBitwiseXorAlias2(TestDygraphInplacBitwiseAnd): | ||
| def inplace_api_processing(self, var): | ||
| return paddle.bitwise_xor_(input=var, other=self.y) | ||
|
|
||
| def non_inplace_api_processing(self, var): | ||
| return paddle.bitwise_xor(input=var, other=self.y) | ||
|
|
||
|
|
||
| class TestDygraphInplacBitwiseNot(TestDygraphInplacBitwiseAnd): | ||
| def inplace_api_processing(self, var): | ||
| return paddle.bitwise_not_(var) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,7 +167,7 @@ def test_dygraph_binary(self): | |
| # 1) x is 0D, y is 0D | ||
| x_np = np.random.randint(-10, 10, []) | ||
| y_np = np.random.randint(-10, 10, []) | ||
| out_np = eval(f'np.{api.__name__}(x_np, y_np)') | ||
| out_np = eval(f'np.{api.__name__.lstrip("_")}(x_np, y_np)') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. zero_dim这里为啥需要改
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的改动是因为测试会有报错 ======================================================================
ERROR: test_dygraph_binary (__main__.TestBinaryAPI.test_dygraph_binary)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\Xue\ML\PaddleDebug\test\legacy_test\test_zero_dim_binary_api.py", line 170, in test_dygraph_binary
out_np = eval(f'np.{api.__name__}(x_np, y_np)')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
File "C:\Users\tzy12\anaconda3\envs\paddle\Lib\site-packages\numpy\__init__.py", line 808, in __getattr__
raise AttributeError(f"module {__name__!r} has no attribute {attr!r}")
AttributeError: module 'numpy' has no attribute '_bitwise_xor'. Did you mean: 'bitwise_xor'?
----------------------------------------------------------------------
Ran 6 tests in 0.508s |
||
|
|
||
| x = paddle.to_tensor(x_np) | ||
| y = paddle.to_tensor(y_np) | ||
|
|
@@ -179,7 +179,7 @@ def test_dygraph_binary(self): | |
| # 2) x is ND, y is 0D | ||
| x_np = np.random.randint(-10, 10, [3, 5]) | ||
| y_np = np.random.randint(-10, 10, []) | ||
| out_np = eval(f'np.{api.__name__}(x_np, y_np)') | ||
| out_np = eval(f'np.{api.__name__.lstrip("_")}(x_np, y_np)') | ||
|
|
||
| x = paddle.to_tensor(x_np) | ||
| y = paddle.to_tensor(y_np) | ||
|
|
@@ -191,7 +191,7 @@ def test_dygraph_binary(self): | |
| # 3) x is 0D , y is ND | ||
| x_np = np.random.randint(-10, 10, []) | ||
| y_np = np.random.randint(-10, 10, [3, 5]) | ||
| out_np = eval(f'np.{api.__name__}(x_np, y_np)') | ||
| out_np = eval(f'np.{api.__name__.lstrip("_")}(x_np, y_np)') | ||
|
|
||
| x = paddle.to_tensor(x_np) | ||
| y = paddle.to_tensor(y_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.
下沉后原来的python 层API需要删掉,只有c++接口了
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.
#76341 (comment) 主要是这个问题,所以我还留着
Uh oh!
There was an error while loading. Please reload this page.
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.
好的