Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cherry-pick] [PHI] migrate softmax_grad kernel (#46257) #46725

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<T,
dnnl::softmax_forward,
dnnl::softmax_backward>(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<int>("axis"), dims.size());

this->AcquireForwardPrimitiveDescriptor(
prop_kind::forward_scoring, out->mem_desc(), axis);
this->AcquireBackwardPrimitiveDescriptor(
out_grad->mem_desc(), out->mem_desc(), axis);
}
};

template <typename T>
Expand Down Expand Up @@ -133,44 +100,6 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> {
}
};

template <typename T>
class SoftmaxMKLDNNGradKernel : public paddle::framework::OpKernel<T> {
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<MKLDNNDeviceContext>();
const auto& mkldnn_engine = dev_ctx.GetEngine();
const Tensor* output = ctx.Input<Tensor>("Out");
auto* out_grad = ctx.template Input<Tensor>(framework::GradVarName("Out"));
auto* in_x_grad = ctx.template Output<Tensor>(framework::GradVarName("X"));

SoftmaxMKLDNNHandler<T> 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

Expand All @@ -181,7 +110,3 @@ REGISTER_OP_KERNEL(softmax,
::paddle::platform::CPUPlace,
ops::SoftmaxMKLDNNKernel<float>,
ops::SoftmaxMKLDNNKernel<paddle::platform::bfloat16>);
REGISTER_OP_KERNEL(softmax_grad,
MKLDNN,
::paddle::platform::CPUPlace,
ops::SoftmaxMKLDNNGradKernel<float>);
38 changes: 38 additions & 0 deletions paddle/phi/backends/onednn/onednn_reuse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -684,6 +685,43 @@ class ActivationOneDNNHandler
}
};

template <typename T>
class SoftmaxOneDNNHandler
: public OneDNNHandlerNoCachingT<T,
dnnl::softmax_forward,
dnnl::softmax_backward> {
public:
SoftmaxOneDNNHandler(const dnnl::engine onednn_engine,
Place cpu_place,
const DenseTensor* x,
int axis)
: OneDNNHandlerNoCachingT<T,
dnnl::softmax_forward,
dnnl::softmax_backward>(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<T,
dnnl::softmax_forward,
dnnl::softmax_backward>(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<int64_t>& dims, // NOLINT
Expand Down
53 changes: 53 additions & 0 deletions paddle/phi/kernels/onednn/softmax_grad_kernel.cc
Original file line number Diff line number Diff line change
@@ -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 <typename T, typename Context>
void SoftmaxGradKernel(const Context& dev_ctx,
const DenseTensor& out,
const DenseTensor& out_grad,
int axis,
DenseTensor* x_grad) {
funcs::SoftmaxOneDNNHandler<T> 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) {}