未验证 提交 44ecae6c 编写于 作者: S Sławomir Siwek 提交者: GitHub

[PHI] migrate softmax_grad kernel (#46257) (#46725)

* init

* remove softmaxop

* merge dev

* correct dir

* style
上级 3cc3f60f
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -55,36 +52,6 @@ class SoftmaxMKLDNNHandler ...@@ -55,36 +52,6 @@ class SoftmaxMKLDNNHandler
this->AcquireForwardPrimitiveDescriptor( this->AcquireForwardPrimitiveDescriptor(
prop_kind::forward_scoring, input->mem_desc(), axis); 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> template <typename T>
...@@ -133,44 +100,6 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> { ...@@ -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 operators
} // namespace paddle } // namespace paddle
...@@ -181,7 +110,3 @@ REGISTER_OP_KERNEL(softmax, ...@@ -181,7 +110,3 @@ REGISTER_OP_KERNEL(softmax,
::paddle::platform::CPUPlace, ::paddle::platform::CPUPlace,
ops::SoftmaxMKLDNNKernel<float>, ops::SoftmaxMKLDNNKernel<float>,
ops::SoftmaxMKLDNNKernel<paddle::platform::bfloat16>); ops::SoftmaxMKLDNNKernel<paddle::platform::bfloat16>);
REGISTER_OP_KERNEL(softmax_grad,
MKLDNN,
::paddle::platform::CPUPlace,
ops::SoftmaxMKLDNNGradKernel<float>);
...@@ -28,6 +28,7 @@ limitations under the License. */ ...@@ -28,6 +28,7 @@ limitations under the License. */
#include "paddle/phi/common/place.h" #include "paddle/phi/common/place.h"
#include "paddle/phi/common/scalar.h" #include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/dense_tensor.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/data_layout_transform.h"
#include "paddle/phi/kernels/funcs/pooling.h" #include "paddle/phi/kernels/funcs/pooling.h"
...@@ -684,6 +685,43 @@ class ActivationOneDNNHandler ...@@ -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 { class ReorderOneDNNHandler {
public: public:
ReorderOneDNNHandler(std::vector<int64_t>& dims, // NOLINT ReorderOneDNNHandler(std::vector<int64_t>& dims, // NOLINT
......
// 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) {}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册