未验证 提交 2bf0d1c8 编写于 作者: H HongyuJia 提交者: GitHub

[Fix Bug] Fix customOP + customDevice scenario selects wrong place (#51996)

上级 6a10e604
...@@ -58,6 +58,7 @@ bool HasAllocation(const phi::TensorBase& t) { ...@@ -58,6 +58,7 @@ bool HasAllocation(const phi::TensorBase& t) {
BackendSet GetTensorBackendSet(const phi::TensorBase& t) { BackendSet GetTensorBackendSet(const phi::TensorBase& t) {
if (HasAllocation(t) && t.place().GetType() != AllocationType::UNDEFINED) { if (HasAllocation(t) && t.place().GetType() != AllocationType::UNDEFINED) {
#ifdef PADDLE_WITH_CUSTOM_DEVICE #ifdef PADDLE_WITH_CUSTOM_DEVICE
// See Note [ Why `SetDevice` when parsing custom place? ]
if (t.place().GetType() == AllocationType::CUSTOM) { if (t.place().GetType() == AllocationType::CUSTOM) {
phi::DeviceManager::SetDevice(t.place()); phi::DeviceManager::SetDevice(t.place());
} }
...@@ -128,6 +129,20 @@ DataType ParseDataTypeWithInputOrder(DataType dtype, const Tensor& tensor) { ...@@ -128,6 +129,20 @@ DataType ParseDataTypeWithInputOrder(DataType dtype, const Tensor& tensor) {
} }
Backend ParseBackend(const Place& place) { Backend ParseBackend(const Place& place) {
#ifdef PADDLE_WITH_CUSTOM_DEVICE
/**
* [ Why `SetDevice` when parsing custom place? ]
* Users are able to call C++ APIs under customOP + customDevice scenario. To
* make sure `GetDevice` function outputs the accurate place when executing
* `GetDeviceContextByBackend` function in C++ API, we need to call
* `SetDevice` first. However, in dygraph mode, `SetDevice` is called at
* CPython level and calling C++ API directly in customOP cannot reach
* CPython. Hence, we need to manually set the device here.
*/
if (place.GetType() == AllocationType::CUSTOM) {
phi::DeviceManager::SetDevice(place);
}
#endif
return phi::TransToPhiBackend(place); return phi::TransToPhiBackend(place);
} }
Backend ParseBackend(const Tensor& tensor) { Backend ParseBackend(const Tensor& tensor) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册