diff --git a/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc index 2bb82186483da807f30c6479f4435971780843dd..659539e5e39b8ffe2053682ec2467ef04fc0f7b0 100644 --- a/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc @@ -1,11 +1,8 @@ /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. - Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -55,36 +52,6 @@ class SoftmaxMKLDNNHandler this->AcquireForwardPrimitiveDescriptor( prop_kind::forward_scoring, input->mem_desc(), axis); } - - SoftmaxMKLDNNHandler(const framework::ExecutionContext& ctx, - const dnnl::engine mkldnn_engine, - platform::Place cpu_place, - const Tensor* out, - const Tensor* out_grad, - Tensor* in_x_grad, - const std::string& unique_name) - : platform::MKLDNNHandlerNoCachingT(mkldnn_engine, - cpu_place) { - PADDLE_ENFORCE_EQ(out_grad->dims(), - in_x_grad->dims(), - platform::errors::InvalidArgument( - "The shape of softmax_grad's input " - "and output must be identical, but shapes differ, " - "out_grad: %s in_grad: %s", - out_grad->dims(), - in_x_grad->dims())); - - auto dims = out_grad->dims(); // input and output share the same shape - const int axis = - phi::funcs::CanonicalAxis(ctx.Attr("axis"), dims.size()); - - this->AcquireForwardPrimitiveDescriptor( - prop_kind::forward_scoring, out->mem_desc(), axis); - this->AcquireBackwardPrimitiveDescriptor( - out_grad->mem_desc(), out->mem_desc(), axis); - } }; template @@ -133,44 +100,6 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel { } }; -template -class SoftmaxMKLDNNGradKernel : public paddle::framework::OpKernel { - public: - void Compute(const paddle::framework::ExecutionContext& ctx) const override { - PADDLE_ENFORCE_EQ(platform::is_cpu_place(ctx.GetPlace()), - true, - paddle::platform::errors::PreconditionNotMet( - "Operator DNNL SoftmaxGrad must use CPUPlace")); - auto& dev_ctx = ctx.template device_context(); - const auto& mkldnn_engine = dev_ctx.GetEngine(); - const Tensor* output = ctx.Input("Out"); - auto* out_grad = ctx.template Input(framework::GradVarName("Out")); - auto* in_x_grad = ctx.template Output(framework::GradVarName("X")); - - SoftmaxMKLDNNHandler handler(ctx, - mkldnn_engine, - ctx.GetPlace(), - output, - out_grad, - in_x_grad, - ctx.InputName("Out")); - - auto dst_memory_p = handler.AcquireDstMemory(output); - auto diff_dst_memory_p = handler.AcquireDiffDstMemory(out_grad); - auto diff_src_memory_p = handler.AcquireDiffSrcMemory(in_x_grad); - - auto softmax_bwd_p = handler.AcquireBackwardPrimitive(); - - auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); - softmax_bwd_p->execute(astream, - {{DNNL_ARG_DST, *dst_memory_p}, - {DNNL_ARG_DIFF_DST, *diff_dst_memory_p}, - {DNNL_ARG_DIFF_SRC, *diff_src_memory_p}}); - astream.wait(); - - in_x_grad->set_mem_desc(diff_src_memory_p->get_desc()); - } -}; } // namespace operators } // namespace paddle @@ -181,7 +110,3 @@ REGISTER_OP_KERNEL(softmax, ::paddle::platform::CPUPlace, ops::SoftmaxMKLDNNKernel, ops::SoftmaxMKLDNNKernel); -REGISTER_OP_KERNEL(softmax_grad, - MKLDNN, - ::paddle::platform::CPUPlace, - ops::SoftmaxMKLDNNGradKernel); diff --git a/paddle/phi/backends/onednn/onednn_reuse.h b/paddle/phi/backends/onednn/onednn_reuse.h index 6b806748d0ef806d94f5a64b4b8857c170165702..bd5b348a05429cf8e01acb8d3e4d4c48c5d153d8 100644 --- a/paddle/phi/backends/onednn/onednn_reuse.h +++ b/paddle/phi/backends/onednn/onednn_reuse.h @@ -28,6 +28,7 @@ limitations under the License. */ #include "paddle/phi/common/place.h" #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/kernels/funcs/axis_utils.h" #include "paddle/phi/kernels/funcs/data_layout_transform.h" #include "paddle/phi/kernels/funcs/pooling.h" @@ -684,6 +685,43 @@ class ActivationOneDNNHandler } }; +template +class SoftmaxOneDNNHandler + : public OneDNNHandlerNoCachingT { + public: + SoftmaxOneDNNHandler(const dnnl::engine onednn_engine, + Place cpu_place, + const DenseTensor* x, + int axis) + : OneDNNHandlerNoCachingT(onednn_engine, + cpu_place) { + const int canonical_axis = funcs::CanonicalAxis(axis, x->dims().size()); + this->AcquireForwardPrimitiveDescriptor( + dnnl::prop_kind::forward_scoring, x->mem_desc(), canonical_axis); + } + + SoftmaxOneDNNHandler(const dnnl::engine onednn_engine, + Place cpu_place, + int axis, + const DenseTensor* out, + const DenseTensor* out_grad) + : OneDNNHandlerNoCachingT(onednn_engine, + cpu_place) { + const int canonical_axis = + funcs::CanonicalAxis(axis, out_grad->dims().size()); + this->AcquireForwardPrimitiveDescriptor( + dnnl::prop_kind::forward_scoring, out->mem_desc(), canonical_axis); + this->AcquireBackwardPrimitiveDescriptor( + out_grad->mem_desc(), out->mem_desc(), canonical_axis); + } +}; + class ReorderOneDNNHandler { public: ReorderOneDNNHandler(std::vector& dims, // NOLINT diff --git a/paddle/phi/kernels/onednn/softmax_grad_kernel.cc b/paddle/phi/kernels/onednn/softmax_grad_kernel.cc new file mode 100644 index 0000000000000000000000000000000000000000..0209992a679cdc0c125b63c8e56f6747ad7bab3f --- /dev/null +++ b/paddle/phi/kernels/onednn/softmax_grad_kernel.cc @@ -0,0 +1,53 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "paddle/phi/kernels/softmax_grad_kernel.h" + +#include "paddle/phi/backends/onednn/onednn_context.h" +#include "paddle/phi/backends/onednn/onednn_reuse.h" +#include "paddle/phi/common/bfloat16.h" +#include "paddle/phi/common/place.h" +#include "paddle/phi/core/kernel_registry.h" + +namespace phi { + +template +void SoftmaxGradKernel(const Context& dev_ctx, + const DenseTensor& out, + const DenseTensor& out_grad, + int axis, + DenseTensor* x_grad) { + funcs::SoftmaxOneDNNHandler handler( + dev_ctx.GetEngine(), dev_ctx.GetPlace(), axis, &out, &out_grad); + + auto dst_memory_p = handler.AcquireDstMemory(&out); + auto diff_dst_memory_p = handler.AcquireDiffDstMemory(&out_grad); + auto diff_src_memory_p = handler.AcquireDiffSrcMemory(x_grad); + + auto softmax_bwd_p = handler.AcquireBackwardPrimitive(); + + auto& astream = OneDNNContext::tls().get_stream(); + softmax_bwd_p->execute(astream, + {{DNNL_ARG_DST, *dst_memory_p}, + {DNNL_ARG_DIFF_DST, *diff_dst_memory_p}, + {DNNL_ARG_DIFF_SRC, *diff_src_memory_p}}); + astream.wait(); + + x_grad->set_mem_desc(diff_src_memory_p->get_desc()); +} + +} // namespace phi + +PD_REGISTER_KERNEL( + softmax_grad, OneDNN, ALL_LAYOUT, phi::SoftmaxGradKernel, float) {}