Skip to content

Conversation

@HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Jun 22, 2025

PR Category

User Experience

PR Types

Bug fixes

Description

Pcard-75624

tensor.place(pybind自phi::Place)和paddle.XXXPlace(pybind自phi::XXXPlace)之间进行比较时,经常出现不符合预期的结果,举例如下

# 两个CPU设备判等是False,不符合正常使用逻辑 print(paddle.CPUPlace() == paddle.CPUPlace()) # False # 两个同样设备类型、同设备ID的Place判等也是False,不符合正常使用逻辑 p1 = paddle.CUDAPlace(0) print(paddle.randn([2]).to(device=p1).place == p1) # False

其根本原因在于 paddle.XXXPlace和tensor.place两个类型来自C++ pybind上来的class XXXPlace(phi::Place) {...}class Place {...}

py::class_<phi::CPUPlace> cpuplace(m, "CPUPlace", R"DOC(

而在为XXXPlace pybind时并没有为指定其父类,导致了从python传到C++的 place obj无法被py::isinstance识别,一直执行的是return false这个分支

.def("__eq__",
[](const py::object &self, const py::object &other) -> bool {
if (py::isinstance<phi::Place>(other)) {
return self.attr("_equals")(other).cast<bool>();
}
return false;
})

通过在py::class_中指定父类,从而让python传到C++的obj也可以被py::isinstance识别到,这样就可以让place之间的判等行为恢复正常

@paddle-bot
Copy link

paddle-bot bot commented Jun 22, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTMeow 🐾

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@HydrogenSulfate HydrogenSulfate merged commit ad9c251 into PaddlePaddle:develop Jun 25, 2025
105 of 112 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment