Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
4dad95cc
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
4dad95cc
编写于
9月 20, 2022
作者:
S
Sławomir Siwek
提交者:
GitHub
9月 20, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[PHI] migrate softmax_grad kernel (#46257)
* init * remove softmaxop * merge dev * correct dir * style
上级
b232b5e9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
91 addition
and
75 deletion
+91
-75
paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc
paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc
+0
-75
paddle/phi/backends/onednn/onednn_reuse.h
paddle/phi/backends/onednn/onednn_reuse.h
+38
-0
paddle/phi/kernels/onednn/softmax_grad_kernel.cc
paddle/phi/kernels/onednn/softmax_grad_kernel.cc
+53
-0
未找到文件。
paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc
浏览文件 @
4dad95cc
/* 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
<
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
>
...
...
@@ -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
...
...
@@ -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
>
);
paddle/phi/backends/onednn/onednn_reuse.h
浏览文件 @
4dad95cc
...
...
@@ -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
<
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
...
...
paddle/phi/kernels/onednn/softmax_grad_kernel.cc
0 → 100644
浏览文件 @
4dad95cc
// 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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录