Skip to content

Commit 5422a44

Browse files
authored
[CustomDevice] add share_external_data support (#57253)
* [CustomDevice] add share_external_data support * update
1 parent 25d7198 commit 5422a44

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

paddle/fluid/distributed/fleet_executor/carrier.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ static std::shared_ptr<framework::GarbageCollector> GetGC(
284284
max_memory_size));
285285
}
286286
}
287+
#endif
288+
#ifdef PADDLE_WITH_CUSTOM_DEVICE
289+
if (platform::is_custom_place(place)) {
290+
if (framework::IsFastEagerDeletionModeEnabled()) {
291+
gc.reset(new framework::CustomDeviceUnsafeFastGarbageCollector(
292+
place, max_memory_size));
293+
}
294+
}
287295
#endif
288296
} // max_memory_size >= 0
289297

paddle/fluid/framework/custom_operator.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -947,12 +947,10 @@ static void RegisterOperatorKernel(
947947
#ifdef PADDLE_WITH_CUSTOM_DEVICE
948948
auto device_types = phi::DeviceManager::GetAllCustomDeviceTypes();
949949
for (const auto& dev_type : device_types) {
950-
for (auto& dev_id : phi::DeviceManager::GetSelectedDeviceList(dev_type)) {
951-
RegisterOperatorKernelWithPlace(name,
952-
op_kernel_func,
953-
proto::VarType::RAW,
954-
platform::CustomPlace(dev_type, dev_id));
955-
}
950+
RegisterOperatorKernelWithPlace(name,
951+
op_kernel_func,
952+
proto::VarType::RAW,
953+
platform::CustomPlace(dev_type));
956954
}
957955
#endif
958956
}

paddle/fluid/inference/api/analysis_predictor.cc

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,12 +2010,9 @@ std::unique_ptr<ZeroCopyTensor> AnalysisPredictor::GetInputTensor(
20102010
}
20112011
} else if (platform::is_custom_place(place_)) {
20122012
auto custom_place = place_;
2013-
auto paddleplace = static_cast<PaddlePlace>(
2014-
static_cast<size_t>(PaddlePlace::kCUSTOM) +
2015-
phi::CustomRegisteredDeviceMap::Instance()
2016-
.GetOrRegisterGlobalDeviceTypeId(place_.GetDeviceType()));
2017-
res->SetPlace(
2018-
paddleplace, custom_place.GetDeviceId(), place_.GetDeviceType());
2013+
res->SetPlace(PaddlePlace::kCUSTOM,
2014+
custom_place.GetDeviceId(),
2015+
custom_place.GetDeviceType());
20192016
} else {
20202017
auto gpu_place = place_;
20212018
res->SetPlace(PaddlePlace::kGPU, gpu_place.GetDeviceId());
@@ -2064,12 +2061,9 @@ std::unique_ptr<ZeroCopyTensor> AnalysisPredictor::GetOutputTensor(
20642061
}
20652062
} else if (platform::is_custom_place(place_)) {
20662063
auto custom_place = place_;
2067-
auto paddleplace = static_cast<PaddlePlace>(
2068-
static_cast<size_t>(PaddlePlace::kCUSTOM) +
2069-
phi::CustomRegisteredDeviceMap::Instance()
2070-
.GetOrRegisterGlobalDeviceTypeId(place_.GetDeviceType()));
2071-
res->SetPlace(
2072-
paddleplace, custom_place.GetDeviceId(), place_.GetDeviceType());
2064+
res->SetPlace(PaddlePlace::kCUSTOM,
2065+
custom_place.GetDeviceId(),
2066+
custom_place.GetDeviceType());
20732067
} else {
20742068
auto gpu_place = place_;
20752069
res->SetPlace(PaddlePlace::kGPU, gpu_place.GetDeviceId());

paddle/fluid/inference/api/details/zero_copy_tensor.cc

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,11 @@ void Tensor::CopyFromCpu(const T *data) {
244244
"Can not create tensor with XPU place because paddle is not compiled "
245245
"with XPU."));
246246
#endif
247-
} else {
247+
} else if (place_ == PlaceType::kCUSTOM) {
248248
#ifdef PADDLE_WITH_CUSTOM_DEVICE
249-
auto device_type_id =
250-
static_cast<size_t>(place_) - static_cast<size_t>(PlaceType::kCUSTOM);
251249
paddle::platform::DeviceContextPool &pool =
252250
paddle::platform::DeviceContextPool::Instance();
253-
paddle::platform::CustomPlace custom_place(
254-
phi::CustomRegisteredDeviceMap::Instance().GetGlobalDeviceType(
255-
device_type_id),
256-
device_);
251+
paddle::platform::CustomPlace custom_place(device_type_, device_);
257252
auto *t_data = tensor->mutable_data<T>(custom_place);
258253
auto *dev_ctx = static_cast<const paddle::platform::CustomDeviceContext *>(
259254
pool.Get(custom_place));
@@ -264,9 +259,15 @@ void Tensor::CopyFromCpu(const T *data) {
264259
ele_size,
265260
dev_ctx->stream());
266261
#else
267-
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
268-
"The analysis predictor supports CPU, GPU and XPU now."));
262+
PADDLE_THROW(paddle::platform::errors::Unavailable(
263+
"Can not create tensor with Custom place because paddle is not "
264+
"compiled "
265+
"with XPU."));
269266
#endif
267+
} else {
268+
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
269+
"The analysis predictor supports CPU, GPU, XPU and CUSTOM_DEVICE "
270+
"now."));
270271
}
271272
}
272273

@@ -355,6 +356,14 @@ void Tensor::ShareExternalData(const T *data,
355356
const_cast<T *>(data), size, paddle::platform::XPUPlace(device_)),
356357
meta);
357358
*tensor = std::move(dtensor);
359+
} else if (place == PlaceType::kCUSTOM) {
360+
phi::DenseTensor dtensor(
361+
std::make_shared<phi::Allocation>(
362+
const_cast<T *>(data),
363+
size,
364+
paddle::platform::CustomPlace(device_type_, device_)),
365+
meta);
366+
*tensor = std::move(dtensor);
358367
} else {
359368
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
360369
"PlaceType must be one of [PlaceType::kCPU, PlaceType::kGPU, "

paddle/fluid/pybind/inference_api.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ paddle_infer::PlaceType ToPaddleInferPlace(
245245
return paddle_infer::PlaceType::kGPU;
246246
} else if (allocation_type == phi::AllocationType::XPU) {
247247
return paddle_infer::PlaceType::kXPU;
248+
} else if (allocation_type == phi::AllocationType::CUSTOM) {
249+
return paddle_infer::PlaceType::kCUSTOM;
248250
} else {
249251
return paddle_infer::PlaceType::kCPU;
250252
}

0 commit comments

Comments
 (0)