From afd5a96b3c0bd90d0c34d40c13d1ab6f12e873d8 Mon Sep 17 00:00:00 2001 From: HongyuJia Date: Tue, 25 Oct 2022 11:04:41 +0800 Subject: [PATCH] opt conv_transpose cudnn (#47294) --- paddle/fluid/operators/conv_transpose_op.cc | 69 ++++++------------- .../platform/device/gpu/cuda/cudnn_helper.h | 2 +- .../platform/device/gpu/rocm/miopen_helper.h | 2 +- 3 files changed, 24 insertions(+), 49 deletions(-) diff --git a/paddle/fluid/operators/conv_transpose_op.cc b/paddle/fluid/operators/conv_transpose_op.cc index 7c2d917d57..f5702f2179 100644 --- a/paddle/fluid/operators/conv_transpose_op.cc +++ b/paddle/fluid/operators/conv_transpose_op.cc @@ -28,6 +28,9 @@ limitations under the License. */ #ifdef PADDLE_WITH_MKLDNN #include "paddle/fluid/platform/mkldnn_helper.h" #endif +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +#include "paddle/fluid/platform/device/gpu/gpu_dnn.h" +#endif namespace paddle { namespace operators { @@ -38,15 +41,11 @@ framework::OpKernelType ConvTransposeOp::GetExpectedKernelType( const framework::ExecutionContext& ctx) const { auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "Input"); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (platform::is_gpu_place(ctx.GetPlace())) { - auto& dev_ctx = ctx.template device_context(); - if (ctx.HasAttr("use_cudnn") && ctx.Attr("use_cudnn") && - dev_ctx.cudnn_handle() != nullptr) { - return framework::OpKernelType(data_type, - ctx.GetPlace(), - phi::DataLayout::kAnyLayout, - framework::LibraryType::kCUDNN); - } + if (platform::CanCUDNNBeUsed(ctx)) { + return framework::OpKernelType(data_type, + ctx.GetPlace(), + phi::DataLayout::kAnyLayout, + framework::LibraryType::kCUDNN); } #endif return framework::OpKernelType(data_type, ctx.GetPlace()); @@ -268,28 +267,16 @@ Example: framework::OpKernelType ConvTransposeOpGrad::GetExpectedKernelType( const framework::ExecutionContext& ctx) const { - bool use_cudnn = - ctx.HasAttr("use_cudnn") ? ctx.Attr("use_cudnn") : false; - use_cudnn &= platform::is_gpu_place(ctx.GetPlace()); + auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "Input"); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (platform::is_gpu_place(ctx.GetPlace())) { - auto& dev_ctx = ctx.template device_context(); - use_cudnn &= dev_ctx.cudnn_handle() != nullptr; + if (platform::CanCUDNNBeUsed(ctx)) { + return framework::OpKernelType(data_type, + ctx.GetPlace(), + phi::DataLayout::kAnyLayout, + framework::LibraryType::kCUDNN); } #endif - framework::LibraryType library_; - if (use_cudnn) { - library_ = framework::LibraryType::kCUDNN; - } else { - library_ = framework::LibraryType::kPlain; - } - - phi::DataLayout layout_ = phi::DataLayout::kAnyLayout; - return framework::OpKernelType( - OperatorWithKernel::IndicateVarDataType(ctx, "Input"), - ctx.GetPlace(), - layout_, - library_); + return framework::OpKernelType(data_type, ctx.GetPlace()); } template @@ -355,28 +342,16 @@ class ConvTransposeDoubleGradMaker : public framework::SingleGradOpMaker { framework::OpKernelType ConvTransposeOpDoubleGrad::GetExpectedKernelType( const framework::ExecutionContext& ctx) const { - bool use_cudnn = - ctx.HasAttr("use_cudnn") ? ctx.Attr("use_cudnn") : false; - use_cudnn &= platform::is_gpu_place(ctx.GetPlace()); + auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "Input"); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (platform::is_gpu_place(ctx.GetPlace())) { - auto& dev_ctx = ctx.template device_context(); - use_cudnn &= dev_ctx.cudnn_handle() != nullptr; + if (platform::CanCUDNNBeUsed(ctx)) { + return framework::OpKernelType(data_type, + ctx.GetPlace(), + phi::DataLayout::kAnyLayout, + framework::LibraryType::kCUDNN); } #endif - framework::LibraryType library_; - if (use_cudnn) { - library_ = framework::LibraryType::kCUDNN; - } else { - library_ = framework::LibraryType::kPlain; - } - - phi::DataLayout layout_ = phi::DataLayout::kAnyLayout; - return framework::OpKernelType( - OperatorWithKernel::IndicateVarDataType(ctx, "Input"), - ctx.GetPlace(), - layout_, - library_); + return framework::OpKernelType(data_type, ctx.GetPlace()); } } // namespace operators diff --git a/paddle/fluid/platform/device/gpu/cuda/cudnn_helper.h b/paddle/fluid/platform/device/gpu/cuda/cudnn_helper.h index 2f63ee880b..4fa2547633 100644 --- a/paddle/fluid/platform/device/gpu/cuda/cudnn_helper.h +++ b/paddle/fluid/platform/device/gpu/cuda/cudnn_helper.h @@ -617,7 +617,7 @@ class ScopedActivationDescriptor { }; inline bool CanCUDNNBeUsed(const framework::ExecutionContext& ctx) { - bool use_cudnn = ctx.Attr("use_cudnn"); + bool use_cudnn = ctx.HasAttr("use_cudnn") && ctx.Attr("use_cudnn"); use_cudnn &= paddle::platform::is_gpu_place(ctx.GetPlace()); #ifdef PADDLE_WITH_CUDA if (use_cudnn) { diff --git a/paddle/fluid/platform/device/gpu/rocm/miopen_helper.h b/paddle/fluid/platform/device/gpu/rocm/miopen_helper.h index 7a77a47189..0c9d6d24cd 100644 --- a/paddle/fluid/platform/device/gpu/rocm/miopen_helper.h +++ b/paddle/fluid/platform/device/gpu/rocm/miopen_helper.h @@ -554,7 +554,7 @@ class ScopedActivationDescriptor { }; inline bool CanCUDNNBeUsed(const framework::ExecutionContext& ctx) { - bool use_cudnn = ctx.Attr("use_cudnn"); + bool use_cudnn = ctx.HasAttr("use_cudnn") && ctx.Attr("use_cudnn"); use_cudnn &= paddle::platform::is_gpu_place(ctx.GetPlace()); #ifdef PADDLE_WITH_HIP if (use_cudnn) { -- GitLab