From 8546749ca32680ab53b3578f2ebd63f89806efeb Mon Sep 17 00:00:00 2001 From: ronny1996 Date: Tue, 12 Sep 2023 06:39:28 +0000 Subject: [PATCH 1/2] [CustomDevice] add share_external_data support --- .../fluid/inference/api/analysis_predictor.cc | 18 +++++-------- .../inference/api/details/zero_copy_tensor.cc | 27 ++++++++++++------- paddle/fluid/pybind/inference_api.cc | 2 ++ 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index eccc0b773ed1e..1e3be4d0cfcd3 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -2010,12 +2010,9 @@ std::unique_ptr AnalysisPredictor::GetInputTensor( } } else if (platform::is_custom_place(place_)) { auto custom_place = place_; - auto paddleplace = static_cast( - static_cast(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()); @@ -2064,12 +2061,9 @@ std::unique_ptr AnalysisPredictor::GetOutputTensor( } } else if (platform::is_custom_place(place_)) { auto custom_place = place_; - auto paddleplace = static_cast( - static_cast(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()); diff --git a/paddle/fluid/inference/api/details/zero_copy_tensor.cc b/paddle/fluid/inference/api/details/zero_copy_tensor.cc index 193e244f86e38..7a399bb55fe7b 100644 --- a/paddle/fluid/inference/api/details/zero_copy_tensor.cc +++ b/paddle/fluid/inference/api/details/zero_copy_tensor.cc @@ -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(place_) - static_cast(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(custom_place); auto *dev_ctx = static_cast( pool.Get(custom_place)); @@ -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.")); } } @@ -355,6 +356,14 @@ void Tensor::ShareExternalData(const T *data, const_cast(data), size, paddle::platform::XPUPlace(device_)), meta); *tensor = std::move(dtensor); + } else if (place == PlaceType::kCUSTOM) { + phi::DenseTensor dtensor( + std::make_shared( + const_cast(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, " diff --git a/paddle/fluid/pybind/inference_api.cc b/paddle/fluid/pybind/inference_api.cc index 1690d738a2c60..868801201727a 100644 --- a/paddle/fluid/pybind/inference_api.cc +++ b/paddle/fluid/pybind/inference_api.cc @@ -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; } From 55d09295e70099d89e7ad91d73094594204d0b32 Mon Sep 17 00:00:00 2001 From: ronny1996 Date: Tue, 12 Sep 2023 06:54:28 +0000 Subject: [PATCH 2/2] update --- paddle/fluid/distributed/fleet_executor/carrier.cc | 8 ++++++++ paddle/fluid/framework/custom_operator.cc | 10 ++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/distributed/fleet_executor/carrier.cc b/paddle/fluid/distributed/fleet_executor/carrier.cc index 70ab3b94de3c5..6dc25faa80b4b 100644 --- a/paddle/fluid/distributed/fleet_executor/carrier.cc +++ b/paddle/fluid/distributed/fleet_executor/carrier.cc @@ -284,6 +284,14 @@ static std::shared_ptr 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 diff --git a/paddle/fluid/framework/custom_operator.cc b/paddle/fluid/framework/custom_operator.cc index c2dd1bf37dd19..8814935e3fceb 100644 --- a/paddle/fluid/framework/custom_operator.cc +++ b/paddle/fluid/framework/custom_operator.cc @@ -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 }