Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
67731297
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看板
提交
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> {
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
math
::
RowwiseMean
<
DeviceContext
,
T
>
row_mean
;
//
functor->
get mean
// get mean
row_mean
(
dev_ctx
,
x
,
mean
);
//
functor->
get variance
// get variance
ElementwiseComputeEx
<
SubAndSquareFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
x
,
mean
,
/*axis*/
0
,
SubAndSquareFunctor
<
T
>
(),
&
out
);
row_mean
(
dev_ctx
,
out
,
var
);
//
functor-> get norm_out
//
get x_norm
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
x
,
mean
,
/*axis*/
0
,
SubFunctor
<
T
>
(),
&
out
);
ElementwiseComputeEx
<
DivAndSqrtFunctor
<
T
>
,
DeviceContext
,
T
>
(
...
...
@@ -129,9 +129,11 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
auto
x
=
*
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
mean
=
*
ctx
.
Input
<
Tensor
>
(
"Mean"
);
auto
var
=
*
ctx
.
Input
<
Tensor
>
(
"Variance"
);
auto
scale
=
*
ctx
.
Input
<
Tensor
>
(
"Scale"
);
auto
*
y
=
ctx
.
Input
<
Tensor
>
(
"Y"
);
auto
*
mean
=
ctx
.
Input
<
Tensor
>
(
"Mean"
);
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"
));
const
auto
begin_norm_axis
=
ctx
.
Attr
<
int
>
(
"begin_norm_axis"
);
...
...
@@ -155,14 +157,19 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
if
(
d_scale
||
d_x
)
{
x
.
Resize
(
matrix_shape
);
temp
.
mutable_data
<
T
>
(
matrix_shape
,
ctx
.
GetPlace
());
temp_norm
.
mutable_data
<
T
>
(
matrix_shape
,
ctx
.
GetPlace
());
// get x_norm
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
x
,
&
mean
,
/*axis*/
0
,
SubFunctor
<
T
>
(),
&
temp_norm
);
ElementwiseComputeEx
<
DivAndSqrtFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
temp_norm
,
&
var
,
/*axis*/
0
,
DivAndSqrtFunctor
<
T
>
(
static_cast
<
T
>
(
epsilon
)),
&
temp_norm
);
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
ElementwiseComputeEx
<
SubFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
x
,
mean
,
/*axis*/
0
,
SubFunctor
<
T
>
(),
&
temp_norm
);
ElementwiseComputeEx
<
DivAndSqrtFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
temp_norm
,
var
,
/*axis*/
0
,
DivAndSqrtFunctor
<
T
>
(
static_cast
<
T
>
(
epsilon
)),
&
temp_norm
);
}
}
if
(
d_bias
)
{
...
...
@@ -188,7 +195,7 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
if
(
d_scale
)
{
// dy_dx
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
);
// dy_dmean_dx
...
...
@@ -199,7 +206,6 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
// dy_var_dx
ElementwiseComputeEx
<
MulFunctor
<
T
>
,
DeviceContext
,
T
>
(
ctx
,
&
temp
,
&
temp_norm
,
/*axis*/
0
,
MulFunctor
<
T
>
(),
&
temp
);
}
else
{
// dy_dx
framework
::
Copy
(
d_y
,
ctx
.
GetPlace
(),
ctx
.
device_context
(),
d_x
);
...
...
@@ -216,12 +222,12 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
// dy_var_dx
row_mean
(
dev_ctx
,
temp
,
&
temp_vec
);
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
>
(
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
>
(
ctx
,
d_x
,
&
var
,
/*axis*/
0
,
ctx
,
d_x
,
var
,
/*axis*/
0
,
DivAndSqrtFunctor
<
T
>
(
static_cast
<
T
>
(
epsilon
)),
d_x
);
d_x
->
Resize
(
dx_dim
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录