From 74e46a93a2c704170919fa2fea2db037fd6eb941 Mon Sep 17 00:00:00 2001 From: Wilber Date: Mon, 1 Aug 2022 15:05:00 +0800 Subject: [PATCH] infer context fix place error. (#44726) * infer context fix place error. * update * update --- paddle/fluid/inference/api/analysis_predictor.cc | 2 +- paddle/fluid/inference/api/infer_context.cc | 10 +++++++++- paddle/fluid/inference/api/infer_context.h | 2 ++ paddle/phi/backends/gpu/gpu_context.cc | 4 ---- paddle/phi/backends/gpu/gpu_context.h | 1 - paddle/phi/tests/common/test_scalar.cu | 5 +++-- .../phi/tests/kernels/test_sparse_conv3d_dev_api.cc | 2 +- paddle/phi/tests/kernels/test_sparse_pool_dev_api.cc | 2 +- .../phi/tests/kernels/test_sparse_utils_dev_api.cc | 12 ++++++------ 9 files changed, 23 insertions(+), 17 deletions(-) diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index 39c83d24a1..8971448071 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -393,7 +393,7 @@ void AnalysisPredictor::InitDeviceContexts() { place_, std::async(std::launch::deferred, [=] { auto *gpu_resource = ResourceManager::Instance().GetGPUResource(predictor_stream_); - auto *gpu_context = new InferGPUContext(); + auto *gpu_context = new InferGPUContext(place_); gpu_context->SetAllocator( memory::allocation::AllocatorFacade::Instance() .GetAllocator(place_, gpu_resource->GetStream()) diff --git a/paddle/fluid/inference/api/infer_context.cc b/paddle/fluid/inference/api/infer_context.cc index 7706f2d082..9a76528a48 100644 --- a/paddle/fluid/inference/api/infer_context.cc +++ b/paddle/fluid/inference/api/infer_context.cc @@ -13,5 +13,13 @@ // limitations under the License. #include "paddle/fluid/inference/api/infer_context.h" +#include "paddle/phi/backends/gpu/gpu_context.h" -namespace paddle {} // namespace paddle +namespace paddle { + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +InferGPUContext::InferGPUContext(const phi::Place& place) + : phi::GPUContext(place, false) {} +#endif + +} // namespace paddle diff --git a/paddle/fluid/inference/api/infer_context.h b/paddle/fluid/inference/api/infer_context.h index c2a23a7ca2..5dbcf355a6 100644 --- a/paddle/fluid/inference/api/infer_context.h +++ b/paddle/fluid/inference/api/infer_context.h @@ -14,6 +14,7 @@ #pragma once #include "paddle/phi/backends/all_context.h" +#include "paddle/phi/common/place.h" namespace paddle { @@ -25,6 +26,7 @@ class InferCPUContext : public phi::CPUContext { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) class InferGPUContext : public phi::GPUContext { public: + explicit InferGPUContext(const phi::Place& place); using phi::GPUContext::SetBlasHandle; using phi::GPUContext::SetBlasTensorCoreHandle; using phi::GPUContext::SetBlasTF32Handle; diff --git a/paddle/phi/backends/gpu/gpu_context.cc b/paddle/phi/backends/gpu/gpu_context.cc index a7d7973814..87d779f919 100644 --- a/paddle/phi/backends/gpu/gpu_context.cc +++ b/paddle/phi/backends/gpu/gpu_context.cc @@ -240,8 +240,6 @@ struct GPUContext::Impl { InitDnnWorkspace(); } - Impl() : place_(GPUPlace()) {} - explicit Impl(const GPUPlace& place) : place_(place) {} ~Impl() { @@ -784,8 +782,6 @@ struct GPUContext::Impl { std::unique_ptr eigen_stream_{nullptr}; }; -GPUContext::GPUContext() : DeviceContext(), impl_(std::make_unique()) {} - GPUContext::GPUContext(GPUContext&&) = default; GPUContext& GPUContext::operator=(GPUContext&&) = default; diff --git a/paddle/phi/backends/gpu/gpu_context.h b/paddle/phi/backends/gpu/gpu_context.h index 376d9e47f4..989bbbcbbf 100644 --- a/paddle/phi/backends/gpu/gpu_context.h +++ b/paddle/phi/backends/gpu/gpu_context.h @@ -79,7 +79,6 @@ class DnnWorkspaceHandle { class PADDLE_API GPUContext : public DeviceContext { public: - GPUContext(); explicit GPUContext(const GPUPlace& place, bool init = true); GPUContext(GPUContext&&); diff --git a/paddle/phi/tests/common/test_scalar.cu b/paddle/phi/tests/common/test_scalar.cu index e985f1c417..ad41201b93 100644 --- a/paddle/phi/tests/common/test_scalar.cu +++ b/paddle/phi/tests/common/test_scalar.cu @@ -21,6 +21,7 @@ limitations under the License. */ #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/common/complex.h" #include "paddle/phi/common/float16.h" +#include "paddle/phi/common/place.h" #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/kernel_registry.h" @@ -159,7 +160,7 @@ TEST(Scalar, ConstructFromDenseTensor7) { alloc.get(), phi::DenseTensorMeta( phi::DataType::FLOAT32, phi::make_ddim({1}), phi::DataLayout::NCHW)); - phi::GPUContext dev_ctx; + phi::GPUContext dev_ctx{phi::GPUPlace()}; dev_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance() .GetAllocator(phi::GPUPlace()) .get()); @@ -181,7 +182,7 @@ TEST(Scalar, ConstructFromTensor) { phi::DenseTensorMeta( phi::DataType::FLOAT32, phi::make_ddim({1}), phi::DataLayout::NCHW)); - phi::GPUContext dev_ctx; + phi::GPUContext dev_ctx{phi::GPUPlace()}; dev_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance() .GetAllocator(phi::GPUPlace()) .get()); diff --git a/paddle/phi/tests/kernels/test_sparse_conv3d_dev_api.cc b/paddle/phi/tests/kernels/test_sparse_conv3d_dev_api.cc index f7c7b7e948..f9c41d1826 100644 --- a/paddle/phi/tests/kernels/test_sparse_conv3d_dev_api.cc +++ b/paddle/phi/tests/kernels/test_sparse_conv3d_dev_api.cc @@ -160,7 +160,7 @@ void TestConv3dBase(const std::vector& indices, // test gpu #if defined(PADDLE_WITH_CUDA) - phi::GPUContext dev_ctx_gpu; + phi::GPUContext dev_ctx_gpu{phi::GPUPlace()}; dev_ctx_gpu.PartialInitWithoutAllocator(); dev_ctx_gpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() diff --git a/paddle/phi/tests/kernels/test_sparse_pool_dev_api.cc b/paddle/phi/tests/kernels/test_sparse_pool_dev_api.cc index ffc2604e6b..b4e1def372 100644 --- a/paddle/phi/tests/kernels/test_sparse_pool_dev_api.cc +++ b/paddle/phi/tests/kernels/test_sparse_pool_dev_api.cc @@ -122,7 +122,7 @@ void TestMaxPoolBase(const std::vector& indices, // test gpu #if defined(PADDLE_WITH_CUDA) - phi::GPUContext dev_ctx_gpu; + phi::GPUContext dev_ctx_gpu{phi::GPUPlace()}; dev_ctx_gpu.PartialInitWithoutAllocator(); dev_ctx_gpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() diff --git a/paddle/phi/tests/kernels/test_sparse_utils_dev_api.cc b/paddle/phi/tests/kernels/test_sparse_utils_dev_api.cc index 70c9f4cfc6..29300d8f58 100644 --- a/paddle/phi/tests/kernels/test_sparse_utils_dev_api.cc +++ b/paddle/phi/tests/kernels/test_sparse_utils_dev_api.cc @@ -105,7 +105,7 @@ void TestDenseToSparseCoo(const DenseTensor& dense_x, // 2. test cuda #if defined(PADDLE_WITH_CUDA) - phi::GPUContext dev_ctx_gpu; + phi::GPUContext dev_ctx_gpu{phi::GPUPlace()}; dev_ctx_gpu.PartialInitWithoutAllocator(); dev_ctx_gpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() @@ -319,7 +319,7 @@ void TestSparseCsrToCoo(const DDim& dense_dims, alloc); // 2. test cuda #if defined(PADDLE_WITH_CUDA) - phi::GPUContext dev_ctx_gpu; + phi::GPUContext dev_ctx_gpu{phi::GPUPlace()}; dev_ctx_gpu.PartialInitWithoutAllocator(); dev_ctx_gpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() @@ -505,7 +505,7 @@ void TestCooToCsr(const DDim& dense_dims, const auto cuda_alloc = std::make_shared( paddle::platform::CUDAPlace()); - phi::GPUContext dev_ctx_gpu; + phi::GPUContext dev_ctx_gpu{phi::GPUPlace()}; dev_ctx_gpu.PartialInitWithoutAllocator(); dev_ctx_gpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() @@ -608,7 +608,7 @@ void TestDenseToSparseCsr(const DenseTensor& dense_x, cuda_alloc.get(), DenseTensorMeta(dense_x.dtype(), dense_x.dims(), dense_x.layout())); - phi::GPUContext dev_ctx_gpu; + phi::GPUContext dev_ctx_gpu{phi::GPUPlace()}; dev_ctx_gpu.PartialInitWithoutAllocator(); dev_ctx_gpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() @@ -741,7 +741,7 @@ void TestSparseCooToDense(const DDim& dense_dims, const auto cuda_alloc = std::make_shared( paddle::platform::CUDAPlace()); - phi::GPUContext dev_ctx_gpu; + phi::GPUContext dev_ctx_gpu{phi::GPUPlace()}; dev_ctx_gpu.PartialInitWithoutAllocator(); dev_ctx_gpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() @@ -889,7 +889,7 @@ void TestSparseCsrToDense(const DDim& dense_dims, const auto cuda_alloc = std::make_shared( paddle::platform::CUDAPlace()); - phi::GPUContext dev_ctx_gpu; + phi::GPUContext dev_ctx_gpu{phi::GPUPlace()}; dev_ctx_gpu.PartialInitWithoutAllocator(); dev_ctx_gpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() -- GitLab