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

[CustomDevice] add share_external_data support #57253

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions paddle/fluid/distributed/fleet_executor/carrier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ static std::shared_ptr<framework::GarbageCollector> GetGC(
max_memory_size));
}
}
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
if (platform::is_custom_place(place)) {
if (framework::IsFastEagerDeletionModeEnabled()) {
gc.reset(new framework::CustomDeviceUnsafeFastGarbageCollector(
place, max_memory_size));
}
}
#endif
} // max_memory_size >= 0

Expand Down
10 changes: 4 additions & 6 deletions paddle/fluid/framework/custom_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -947,12 +947,10 @@ static void RegisterOperatorKernel(
#ifdef PADDLE_WITH_CUSTOM_DEVICE
auto device_types = phi::DeviceManager::GetAllCustomDeviceTypes();
for (const auto& dev_type : device_types) {
for (auto& dev_id : phi::DeviceManager::GetSelectedDeviceList(dev_type)) {
RegisterOperatorKernelWithPlace(name,
op_kernel_func,
proto::VarType::RAW,
platform::CustomPlace(dev_type, dev_id));
}
RegisterOperatorKernelWithPlace(name,
op_kernel_func,
proto::VarType::RAW,
platform::CustomPlace(dev_type));
}
#endif
}
Expand Down
18 changes: 6 additions & 12 deletions paddle/fluid/inference/api/analysis_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2010,12 +2010,9 @@ std::unique_ptr<ZeroCopyTensor> AnalysisPredictor::GetInputTensor(
}
} else if (platform::is_custom_place(place_)) {
auto custom_place = place_;
auto paddleplace = static_cast<PaddlePlace>(
static_cast<size_t>(PaddlePlace::kCUSTOM) +
phi::CustomRegisteredDeviceMap::Instance()
.GetOrRegisterGlobalDeviceTypeId(place_.GetDeviceType()));
res->SetPlace(
paddleplace, custom_place.GetDeviceId(), place_.GetDeviceType());
res->SetPlace(PaddlePlace::kCUSTOM,
custom_place.GetDeviceId(),
custom_place.GetDeviceType());
} else {
auto gpu_place = place_;
res->SetPlace(PaddlePlace::kGPU, gpu_place.GetDeviceId());
Expand Down Expand Up @@ -2064,12 +2061,9 @@ std::unique_ptr<ZeroCopyTensor> AnalysisPredictor::GetOutputTensor(
}
} else if (platform::is_custom_place(place_)) {
auto custom_place = place_;
auto paddleplace = static_cast<PaddlePlace>(
static_cast<size_t>(PaddlePlace::kCUSTOM) +
phi::CustomRegisteredDeviceMap::Instance()
.GetOrRegisterGlobalDeviceTypeId(place_.GetDeviceType()));
res->SetPlace(
paddleplace, custom_place.GetDeviceId(), place_.GetDeviceType());
res->SetPlace(PaddlePlace::kCUSTOM,
custom_place.GetDeviceId(),
custom_place.GetDeviceType());
} else {
auto gpu_place = place_;
res->SetPlace(PaddlePlace::kGPU, gpu_place.GetDeviceId());
Expand Down
27 changes: 18 additions & 9 deletions paddle/fluid/inference/api/details/zero_copy_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,11 @@ void Tensor::CopyFromCpu(const T *data) {
"Can not create tensor with XPU place because paddle is not compiled "
"with XPU."));
#endif
} else {
} else if (place_ == PlaceType::kCUSTOM) {
#ifdef PADDLE_WITH_CUSTOM_DEVICE
auto device_type_id =
static_cast<size_t>(place_) - static_cast<size_t>(PlaceType::kCUSTOM);
paddle::platform::DeviceContextPool &pool =
paddle::platform::DeviceContextPool::Instance();
paddle::platform::CustomPlace custom_place(
phi::CustomRegisteredDeviceMap::Instance().GetGlobalDeviceType(
device_type_id),
device_);
paddle::platform::CustomPlace custom_place(device_type_, device_);
auto *t_data = tensor->mutable_data<T>(custom_place);
auto *dev_ctx = static_cast<const paddle::platform::CustomDeviceContext *>(
pool.Get(custom_place));
Expand All @@ -264,9 +259,15 @@ void Tensor::CopyFromCpu(const T *data) {
ele_size,
dev_ctx->stream());
#else
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
"The analysis predictor supports CPU, GPU and XPU now."));
PADDLE_THROW(paddle::platform::errors::Unavailable(
"Can not create tensor with Custom place because paddle is not "
"compiled "
"with XPU."));
#endif
} else {
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
"The analysis predictor supports CPU, GPU, XPU and CUSTOM_DEVICE "
"now."));
}
}

Expand Down Expand Up @@ -355,6 +356,14 @@ void Tensor::ShareExternalData(const T *data,
const_cast<T *>(data), size, paddle::platform::XPUPlace(device_)),
meta);
*tensor = std::move(dtensor);
} else if (place == PlaceType::kCUSTOM) {
phi::DenseTensor dtensor(
std::make_shared<phi::Allocation>(
const_cast<T *>(data),
size,
paddle::platform::CustomPlace(device_type_, device_)),
meta);
*tensor = std::move(dtensor);
} else {
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
"PlaceType must be one of [PlaceType::kCPU, PlaceType::kGPU, "
Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/pybind/inference_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ paddle_infer::PlaceType ToPaddleInferPlace(
return paddle_infer::PlaceType::kGPU;
} else if (allocation_type == phi::AllocationType::XPU) {
return paddle_infer::PlaceType::kXPU;
} else if (allocation_type == phi::AllocationType::CUSTOM) {
return paddle_infer::PlaceType::kCUSTOM;
} else {
return paddle_infer::PlaceType::kCPU;
}
Expand Down