From 4329528deb39df76ec51ed12a19a8c43932ecd49 Mon Sep 17 00:00:00 2001 From: ZzSean <18818272991@163.com> Date: Thu, 10 Mar 2022 03:58:24 +0000 Subject: [PATCH] fix --- paddle/fluid/operators/log_softmax_op.cc | 45 ++++------------- paddle/fluid/operators/log_softmax_op.cu | 61 ++++++++++------------- python/paddle/nn/functional/activation.py | 6 +-- 3 files changed, 38 insertions(+), 74 deletions(-) diff --git a/paddle/fluid/operators/log_softmax_op.cc b/paddle/fluid/operators/log_softmax_op.cc index d4e2434ab765a..0e69b397e04c7 100644 --- a/paddle/fluid/operators/log_softmax_op.cc +++ b/paddle/fluid/operators/log_softmax_op.cc @@ -16,7 +16,6 @@ limitations under the License. */ #include #include #include "paddle/fluid/operators/common_infer_shape_functions.h" -#include "paddle/fluid/platform/device/gpu/gpu_dnn.h" namespace paddle { namespace operators { @@ -32,25 +31,17 @@ class LogSoftmaxOp : public framework::OperatorWithKernel { protected: framework::OpKernelType GetExpectedKernelType( const framework::ExecutionContext& ctx) const override { - // choose cudnn kernel if the runtime supported. - framework::LibraryType library{framework::LibraryType::kPlain}; - framework::DataLayout layout = framework::DataLayout::kAnyLayout; - auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X"); - -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (platform::CanCUDNNBeUsed(ctx)) { - library = framework::LibraryType::kCUDNN; - } -#endif + auto input_data_type = + framework::OperatorWithKernel::IndicateVarDataType(ctx, "X"); + #ifdef PADDLE_WITH_MKLDNN - if (library == framework::LibraryType::kPlain && - this->CanMKLDNNBeUsed(ctx, input_data_type)) { - library = framework::LibraryType::kMKLDNN; - layout = framework::DataLayout::kMKLDNN; + if (this->CanMKLDNNBeUsed(ctx, input_data_type)) { + return framework::OpKernelType(input_data_type, ctx.GetPlace(), + framework::DataLayout::kMKLDNN, + framework::LibraryType::kMKLDNN); } #endif - return framework::OpKernelType(input_data_type, ctx.GetPlace(), layout, - library); + return framework::OpKernelType(input_data_type, ctx.GetPlace()); } }; @@ -65,11 +56,6 @@ class LogSoftmaxOpMaker : public framework::OpProtoAndCheckerMaker { "The dimension index of Input(x) to perform log_softmax," "default -1 for last dimension") .SetDefault(-1); - AddAttr( - "use_cudnn", - "(bool, default false) Only used in cudnn kernel, need install cudnn") - .SetDefault(false) - .AsExtra(); AddAttr("use_mkldnn", "(bool, default false) Only used in mkldnn kernel") .SetDefault(false) @@ -112,18 +98,9 @@ class LogSoftmaxGradOp : public framework::OperatorWithKernel { protected: framework::OpKernelType GetExpectedKernelType( const framework::ExecutionContext& ctx) const override { - // choose cudnn kernel if the runtime supported. - framework::LibraryType library{framework::LibraryType::kPlain}; - framework::DataLayout layout = framework::DataLayout::kAnyLayout; - auto input_data_type = OperatorWithKernel::IndicateVarDataType( - ctx, framework::GradVarName("Out")); -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (platform::CanCUDNNBeUsed(ctx)) { - library = framework::LibraryType::kCUDNN; - } -#endif - return framework::OpKernelType(input_data_type, ctx.device_context(), - layout, library); + return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType( + ctx, framework::GradVarName("Out")), + ctx.device_context()); } }; diff --git a/paddle/fluid/operators/log_softmax_op.cu b/paddle/fluid/operators/log_softmax_op.cu index c14897da87e04..1fc56cb928768 100644 --- a/paddle/fluid/operators/log_softmax_op.cu +++ b/paddle/fluid/operators/log_softmax_op.cu @@ -506,45 +506,34 @@ class LogSoftmaxGradCUDNNKernel : public framework::OpKernel { namespace ops = paddle::operators; namespace plat = paddle::platform; -REGISTER_OP_CUDA_KERNEL( - log_softmax, ops::LogSoftmaxKernel, - ops::LogSoftmaxKernel, - ops::LogSoftmaxKernel, - ops::LogSoftmaxKernel); -REGISTER_OP_CUDA_KERNEL( - log_softmax_grad, ops::LogSoftmaxGradKernel, - ops::LogSoftmaxGradKernel, - ops::LogSoftmaxGradKernel, - ops::LogSoftmaxGradKernel); #ifdef PADDLE_WITH_HIP -REGISTER_OP_KERNEL(log_softmax, CUDNN, plat::CUDAPlace, - ops::LogSoftmaxCUDNNKernel, - ops::LogSoftmaxCUDNNKernel, - ops::LogSoftmaxCUDNNKernel); -REGISTER_OP_KERNEL(log_softmax_grad, CUDNN, plat::CUDAPlace, - ops::LogSoftmaxGradCUDNNKernel, - ops::LogSoftmaxGradCUDNNKernel, - ops::LogSoftmaxGradCUDNNKernel); +REGISTER_OP_CUDA_KERNEL(log_softmax, ops::LogSoftmaxCUDNNKernel, + ops::LogSoftmaxCUDNNKernel, + ops::LogSoftmaxCUDNNKernel); +REGISTER_OP_CUDA_KERNEL(log_softmax_grad, CUDNN, plat::CUDAPlace, + ops::LogSoftmaxGradCUDNNKernel, + ops::LogSoftmaxGradCUDNNKernel, + ops::LogSoftmaxGradCUDNNKernel); #else #if CUDNN_VERSION_MIN(8, 1, 0) -REGISTER_OP_KERNEL(log_softmax, CUDNN, plat::CUDAPlace, - ops::LogSoftmaxCUDNNKernel, - ops::LogSoftmaxCUDNNKernel, - ops::LogSoftmaxCUDNNKernel, - ops::LogSoftmaxCUDNNKernel); -REGISTER_OP_KERNEL(log_softmax_grad, CUDNN, plat::CUDAPlace, - ops::LogSoftmaxGradCUDNNKernel, - ops::LogSoftmaxGradCUDNNKernel, - ops::LogSoftmaxGradCUDNNKernel, - ops::LogSoftmaxGradCUDNNKernel); +REGISTER_OP_CUDA_KERNEL(log_softmax, ops::LogSoftmaxCUDNNKernel, + ops::LogSoftmaxCUDNNKernel, + ops::LogSoftmaxCUDNNKernel, + ops::LogSoftmaxCUDNNKernel); +REGISTER_OP_CUDA_KERNEL(log_softmax_grad, ops::LogSoftmaxGradCUDNNKernel, + ops::LogSoftmaxGradCUDNNKernel, + ops::LogSoftmaxGradCUDNNKernel, + ops::LogSoftmaxGradCUDNNKernel); #else -REGISTER_OP_KERNEL(log_softmax, CUDNN, plat::CUDAPlace, - ops::LogSoftmaxCUDNNKernel, - ops::LogSoftmaxCUDNNKernel, - ops::LogSoftmaxCUDNNKernel); -REGISTER_OP_KERNEL(log_softmax_grad, CUDNN, plat::CUDAPlace, - ops::LogSoftmaxGradCUDNNKernel, - ops::LogSoftmaxGradCUDNNKernel, - ops::LogSoftmaxGradCUDNNKernel); +REGISTER_OP_CUDA_KERNEL( + log_softmax, ops::LogSoftmaxCUDNNKernel, + ops::LogSoftmaxCUDNNKernel, + ops::LogSoftmaxCUDNNKernel, + ops::LogSoftmaxKernel); +REGISTER_OP_CUDA_KERNEL( + log_softmax_grad, ops::LogSoftmaxGradCUDNNKernel, + ops::LogSoftmaxGradCUDNNKernel, + ops::LogSoftmaxGradCUDNNKernel, + ops::LogSoftmaxGradKernel); #endif #endif diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index dc7dddf602cd5..91449ef538ff3 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -1359,12 +1359,11 @@ def log_softmax(x, axis=-1, dtype=None, name=None): if (dtype is not None) and (not isinstance(dtype, core.VarDesc.VarType)): dtype = convert_np_dtype_to_dtype_(dtype) - use_cudnn = True if in_dynamic_mode(): if dtype is not None: x = _C_ops.cast(x, 'in_dtype', x.dtype, 'out_dtype', dtype) - return _C_ops.log_softmax(x, 'axis', axis, 'use_cudnn', use_cudnn) + return _C_ops.log_softmax(x, 'axis', axis) if dtype is None: check_variable_and_dtype(x, 'x', ['float16', 'float32', 'float64'], @@ -1389,8 +1388,7 @@ def log_softmax(x, axis=-1, dtype=None, name=None): type='log_softmax', inputs={'X': out_cast}, outputs={'Out': out}, - attrs={'axis': axis, - 'use_cudnn': use_cudnn}) + attrs={'axis': axis}) return out