Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d74db35
fix code style
Manfredss Nov 13, 2025
80d3ad9
add doc and signature for bitwise_xor_
Manfredss Nov 14, 2025
c7ed42b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Nov 14, 2025
4ed899f
Sink bitwise_xor to cpp
Manfredss Nov 15, 2025
f6841d0
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Nov 15, 2025
9f92fbd
fix numpy func name error (no attribute '_bitwise_xor') and add type …
Manfredss Nov 16, 2025
4f01bbc
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Nov 16, 2025
6d385e6
fix static check and xpu test
Manfredss Nov 17, 2025
c9e9706
fix
Manfredss Nov 21, 2025
0f7b938
fix
Manfredss Nov 21, 2025
2b1b309
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Nov 25, 2025
e25e4da
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Nov 25, 2025
db3b1fb
fix: use cpp sink only
Manfredss Nov 26, 2025
d5240bf
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Nov 26, 2025
d3bab02
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Dec 3, 2025
38b750c
sunk bitwise_xor to cpp and add decorator for bitwise_xor_
Manfredss Dec 4, 2025
b7bf087
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Dec 4, 2025
a434bce
fix
Manfredss Dec 5, 2025
ea3d45e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Dec 5, 2025
68d4666
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Manfredss Dec 12, 2025
a34a8de
add signatures, add test for broadcast logic, test compat cases for b…
Manfredss Dec 16, 2025
24bc5c1
test compat bitwise_xor_
Manfredss Dec 20, 2025
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
53 changes: 53 additions & 0 deletions python/paddle/_paddle_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2709,3 +2709,56 @@ def abs(
# zhanrongrun

# other
add_doc_and_signature(
"bitwise_xor_",
Copy link
Contributor

Choose a reason for hiding this comment

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

这个是bitwise_xor还是bitwise_xor_?

前者是非inplace的,后者是inplace的

Copy link
Contributor Author

@Manfredss Manfredss Nov 17, 2025

Choose a reason for hiding this comment

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

修复列表里写的是 bitwise_xor_,所以这里写的 bitwise_xor_。这个 API 只能通过装饰器来修改

但 bitwise_xor 是能够 cpp 下沉的,所以有些疑惑 @zhwesky2010

r"""
Performs an in-place bitwise XOR on the input tensor.

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
""",
)
8 changes: 8 additions & 0 deletions python/paddle/tensor/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,7 @@ def bitwise_or_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor:
return _C_ops.bitwise_or_(x, y)


@param_two_alias(["x", "input"], ["y", "other"])
Copy link
Contributor

@zhwesky2010 zhwesky2010 Nov 24, 2025

Choose a reason for hiding this comment

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

写了下沉就不要写装饰器了。

两者选其一就可,另外下沉inplace API(bitwise_xor_)之前需要先把非inplace原版(bitwise_xor)也下沉了,因为两者是复用的一个kernel。

def bitwise_xor(
x: Tensor, y: Tensor, out: Tensor | None = None, name: str | None = None
) -> Tensor:
Expand All @@ -1224,9 +1225,15 @@ def bitwise_xor(

.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

Copy link
Contributor

Choose a reason for hiding this comment

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

下沉后原来的bitwise_xor需要删除掉,从_C_ops中导入

.. note::
Alias Support: The parameter name ``input`` can be used as an alias for ``x``, and ``other`` can be used as an alias for ``y``.
For example, ``bitwise_xor(input=tensor_x, other=tensor_y, ...)`` is equivalent to ``bitwise_xor(x=tensor_x, y=tensor_y, ...)``.

Args:
x (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
alias: ``input``.
y (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
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`.
Expand Down Expand Up @@ -1268,6 +1275,7 @@ def __rxor__(


@inplace_apis_in_dygraph_only
@ParamAliasDecorator({"x": ["input"], "y": ["other"]})
Copy link
Contributor

@zhwesky2010 zhwesky2010 Dec 2, 2025

Choose a reason for hiding this comment

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

这个也下沉吧?

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``.
Expand Down
16 changes: 16 additions & 0 deletions test/legacy_test/test_inplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,22 @@ def non_inplace_api_processing(self, var):
return paddle.bitwise_xor(var, self.y)


class TestDygraphInplacBitwiseXorAlias1(TestDygraphInplacBitwiseAnd):
Copy link
Contributor

Choose a reason for hiding this comment

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

paddle.bitwise_xor测一下compat情况下的用法

Copy link
Contributor Author

@Manfredss Manfredss Dec 12, 2025

Choose a reason for hiding this comment

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

bitwise_xor_ 需要吗?好像 bitwise_xor_ 用了 compat 会有 segfault 的情况,我认为可能是 bitwise_xor_ 下沉了但是 ops.yaml 里没有 bitwise_xor_ 的定义。需要添加吗?

Copy link
Contributor

Choose a reason for hiding this comment

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

bitwise_xor_ 需要吗?好像 bitwise_xor_ 用了 compat 会有 segfault 的情况,我认为可能是 bitwise_xor_ 下沉了但是 ops.yaml 里没有 bitwise_xor_ 的定义。需要添加吗?

ops.yaml里不需要bitwise_xor_定义,会根据bitwise_xor的inplace字段,来生成bitwise_xor_

两者本质是共享一个kernel的,只在输出out处理这里有些区别。

Copy link
Contributor

@zhwesky2010 zhwesky2010 Dec 17, 2025

Choose a reason for hiding this comment

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

bitwise_xor_ 需要吗?好像 bitwise_xor_ 用了 compat 会有 segfault 的情况,我认为可能是 bitwise_xor_ 下沉了但是 ops.yaml 里没有 bitwise_xor_ 的定义。需要添加吗?

先按标准来测试,不要绕过,先提前暴露问题。

如果inplace的API下沉不了就先用装饰器吧,底层问题先暴露出来。因为这也是第1个inplace的API下沉。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好像是 Paddle 的 eager execution engine 执行 inplace 函数的问题,第一个参数(即被修改的参数)必须作为位置参数传递,而不是 kwarg。因为 C++ 返回逻辑会专门在位置元组 args 中查找输入张量并将其作为结果返回。如果将其作为 kwarg 传递,则 args 为空,导致 segfault。

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)
Expand Down
Loading