Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
67731297
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
67731297
编写于
2月 05, 2018
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code refine
上级
df0e74db
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
24 addition
and
18 deletion
+24
-18
paddle/operators/layer_norm_op.h
paddle/operators/layer_norm_op.h
+24
-18
未找到文件。
paddle/operators/layer_norm_op.h
浏览文件 @
67731297
...
@@ -97,15 +97,15 @@ class LayerNormKernel : public framework::OpKernel<T> {
...
@@ -97,15 +97,15 @@ class LayerNormKernel : public framework::OpKernel<T> {
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
math
::
RowwiseMean
<
DeviceContext
,
T
>
row_mean
;
math
::
RowwiseMean
<
DeviceContext
,
T
>
row_mean
;
//
functor->
get mean
// get mean
row_mean
(
dev_ctx
,
x
,
mean
);
row_mean
(
dev_ctx
,
x
,
mean
);
//
functor->
get variance
// get variance
ElementwiseComputeEx
<
SubAndSquareFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
SubAndSquareFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
x
,
mean
,
/*axis*/
0
,
SubAndSquareFunctor
<
T
>
(),
&
out
);
ctx
,
&
x
,
mean
,
/*axis*/
0
,
SubAndSquareFunctor
<
T
>
(),
&
out
);
row_mean
(
dev_ctx
,
out
,
var
);
row_mean
(
dev_ctx
,
out
,
var
);
//
functor-> get norm_out
//
get x_norm
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
x
,
mean
,
/*axis*/
0
,
SubFunctor
<
T
>
(),
&
out
);
ctx
,
&
x
,
mean
,
/*axis*/
0
,
SubFunctor
<
T
>
(),
&
out
);
ElementwiseComputeEx
<
DivAndSqrtFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
DivAndSqrtFunctor
<
T
>
,
DeviceContext
,
T
>
(
...
@@ -129,9 +129,11 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
...
@@ -129,9 +129,11 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
const
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
auto
x
=
*
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
x
=
*
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
mean
=
*
ctx
.
Input
<
Tensor
>
(
"Mean"
);
auto
*
y
=
ctx
.
Input
<
Tensor
>
(
"Y"
);
auto
var
=
*
ctx
.
Input
<
Tensor
>
(
"Variance"
);
auto
*
mean
=
ctx
.
Input
<
Tensor
>
(
"Mean"
);
auto
scale
=
*
ctx
.
Input
<
Tensor
>
(
"Scale"
);
auto
*
var
=
ctx
.
Input
<
Tensor
>
(
"Variance"
);
auto
*
scale
=
ctx
.
Input
<
Tensor
>
(
"Scale"
);
auto
*
bias
=
ctx
.
Input
<
Tensor
>
(
"Bias"
);
auto
d_y
=
*
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
auto
d_y
=
*
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
const
auto
begin_norm_axis
=
ctx
.
Attr
<
int
>
(
"begin_norm_axis"
);
const
auto
begin_norm_axis
=
ctx
.
Attr
<
int
>
(
"begin_norm_axis"
);
...
@@ -155,15 +157,20 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
...
@@ -155,15 +157,20 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
if
(
d_scale
||
d_x
)
{
if
(
d_scale
||
d_x
)
{
x
.
Resize
(
matrix_shape
);
x
.
Resize
(
matrix_shape
);
temp
.
mutable_data
<
T
>
(
matrix_shape
,
ctx
.
GetPlace
());
temp
.
mutable_data
<
T
>
(
matrix_shape
,
ctx
.
GetPlace
());
temp_norm
.
mutable_data
<
T
>
(
matrix_shape
,
ctx
.
GetPlace
());
if
(
!
(
bias
&&
scale
))
{
temp_norm
.
ShareDataWith
(
*
y
);
temp_norm
.
Resize
(
matrix_shape
);
}
else
{
temp_norm
.
mutable_data
<
T
>
(
matrix_shape
,
ctx
.
GetPlace
());
// get x_norm
// get x_norm
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
x
,
&
mean
,
/*axis*/
0
,
SubFunctor
<
T
>
(),
&
temp_norm
);
ctx
,
&
x
,
mean
,
/*axis*/
0
,
SubFunctor
<
T
>
(),
&
temp_norm
);
ElementwiseComputeEx
<
DivAndSqrtFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
DivAndSqrtFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
temp_norm
,
&
var
,
/*axis*/
0
,
ctx
,
&
temp_norm
,
var
,
/*axis*/
0
,
DivAndSqrtFunctor
<
T
>
(
static_cast
<
T
>
(
epsilon
)),
&
temp_norm
);
DivAndSqrtFunctor
<
T
>
(
static_cast
<
T
>
(
epsilon
)),
&
temp_norm
);
}
}
}
if
(
d_bias
)
{
if
(
d_bias
)
{
d_bias
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
d_bias
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
...
@@ -188,7 +195,7 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
...
@@ -188,7 +195,7 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
if
(
d_scale
)
{
if
(
d_scale
)
{
// dy_dx
// dy_dx
ElementwiseComputeEx
<
MulFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
MulFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
d_y
,
&
scale
,
/*axis*/
1
,
MulFunctor
<
T
>
(),
&
temp
);
ctx
,
&
d_y
,
scale
,
/*axis*/
1
,
MulFunctor
<
T
>
(),
&
temp
);
framework
::
Copy
(
temp
,
ctx
.
GetPlace
(),
ctx
.
device_context
(),
d_x
);
framework
::
Copy
(
temp
,
ctx
.
GetPlace
(),
ctx
.
device_context
(),
d_x
);
// dy_dmean_dx
// dy_dmean_dx
...
@@ -199,7 +206,6 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
...
@@ -199,7 +206,6 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
// dy_var_dx
// dy_var_dx
ElementwiseComputeEx
<
MulFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
MulFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
temp
,
&
temp_norm
,
/*axis*/
0
,
MulFunctor
<
T
>
(),
&
temp
);
ctx
,
&
temp
,
&
temp_norm
,
/*axis*/
0
,
MulFunctor
<
T
>
(),
&
temp
);
}
else
{
}
else
{
// dy_dx
// dy_dx
framework
::
Copy
(
d_y
,
ctx
.
GetPlace
(),
ctx
.
device_context
(),
d_x
);
framework
::
Copy
(
d_y
,
ctx
.
GetPlace
(),
ctx
.
device_context
(),
d_x
);
...
@@ -216,12 +222,12 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
...
@@ -216,12 +222,12 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
// dy_var_dx
// dy_var_dx
row_mean
(
dev_ctx
,
temp
,
&
temp_vec
);
row_mean
(
dev_ctx
,
temp
,
&
temp_vec
);
ElementwiseComputeEx
<
MulFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
MulFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
temp_norm
,
&
temp_vec
,
/*axis*/
0
,
MulFunctor
<
T
>
(),
&
temp
_norm
);
ctx
,
&
temp_norm
,
&
temp_vec
,
/*axis*/
0
,
MulFunctor
<
T
>
(),
&
temp
);
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
d_x
,
&
temp
_norm
,
/*axis*/
0
,
SubFunctor
<
T
>
(),
d_x
);
ctx
,
d_x
,
&
temp
,
/*axis*/
0
,
SubFunctor
<
T
>
(),
d_x
);
ElementwiseComputeEx
<
DivAndSqrtFunctor
<
T
>
,
DeviceContext
,
T
>
(
ElementwiseComputeEx
<
DivAndSqrtFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
d_x
,
&
var
,
/*axis*/
0
,
ctx
,
d_x
,
var
,
/*axis*/
0
,
DivAndSqrtFunctor
<
T
>
(
static_cast
<
T
>
(
epsilon
)),
d_x
);
DivAndSqrtFunctor
<
T
>
(
static_cast
<
T
>
(
epsilon
)),
d_x
);
d_x
->
Resize
(
dx_dim
);
d_x
->
Resize
(
dx_dim
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录