Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
904fa05f
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
904fa05f
编写于
3月 25, 2018
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve layer_norm speed
transfomer on a single device step time reduces from 0.157 to 0.125
上级
898e0ffa
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
116 addition
and
21 deletion
+116
-21
paddle/fluid/operators/layer_norm_op.h
paddle/fluid/operators/layer_norm_op.h
+116
-21
未找到文件。
paddle/fluid/operators/layer_norm_op.h
浏览文件 @
904fa05f
...
...
@@ -22,6 +22,99 @@ limitations under the License. */
namespace
paddle
{
namespace
operators
{
// Wrap RowwiseMean and ColwiseMean.
// Reuse the cpu codes and replace the gpu codes with cublas_gemv, which is
// significantly faster. Unlike the RowwiseMean and ColwiseMean, the
// implementation only considers 2D.
template
<
typename
DeviceContext
,
typename
T
>
struct
RowwiseMean2D
{
RowwiseMean2D
(
int
left
,
int
right
,
const
platform
::
DeviceContext
&
dev_ctx
);
void
operator
()(
const
platform
::
DeviceContext
&
context
,
const
framework
::
Tensor
&
input
,
framework
::
Tensor
*
vec
);
};
template
<
typename
T
>
class
RowwiseMean2D
<
platform
::
CUDADeviceContext
,
T
>
{
public:
RowwiseMean2D
(
int
left
,
int
right
,
const
platform
::
DeviceContext
&
dev_ctx
)
:
left_
(
left
),
right_
(
right
)
{
framework
::
DDim
ones_dim
({
right_
});
divisor_
.
mutable_data
<
T
>
(
ones_dim
,
dev_ctx
.
GetPlace
());
math
::
set_constant
(
dev_ctx
,
&
divisor_
,
1.0
/
right
);
}
void
operator
()(
const
platform
::
CUDADeviceContext
&
context
,
const
framework
::
Tensor
&
input
,
framework
::
Tensor
*
out
)
{
math
::
gemv
<
platform
::
CUDADeviceContext
,
T
>
(
context
,
false
,
left_
,
right_
,
1.
,
input
.
data
<
T
>
(),
divisor_
.
data
<
T
>
(),
0.
,
out
->
data
<
T
>
());
}
private:
int
left_
;
int
right_
;
framework
::
Tensor
divisor_
;
};
template
<
typename
T
>
class
RowwiseMean2D
<
platform
::
CPUDeviceContext
,
T
>
{
public:
RowwiseMean2D
(
int
left
,
int
right
,
const
platform
::
DeviceContext
&
dev_ctx
)
{}
void
operator
()(
const
platform
::
CPUDeviceContext
&
context
,
const
framework
::
Tensor
&
input
,
framework
::
Tensor
*
out
)
{
row_mean_
(
context
,
input
,
out
);
}
private:
math
::
RowwiseMean
<
platform
::
CPUDeviceContext
,
T
>
row_mean_
;
};
template
<
typename
DeviceContext
,
typename
T
>
struct
ColwiseSum2D
{
ColwiseSum2D
(
int
left
,
int
right
,
const
platform
::
DeviceContext
&
dev_ctx
);
void
operator
()(
const
platform
::
DeviceContext
&
context
,
const
framework
::
Tensor
&
input
,
framework
::
Tensor
*
vec
);
};
template
<
typename
T
>
class
ColwiseSum2D
<
platform
::
CUDADeviceContext
,
T
>
{
public:
ColwiseSum2D
(
int
left
,
int
right
,
const
platform
::
DeviceContext
&
dev_ctx
)
:
left_
(
left
),
right_
(
right
)
{
framework
::
DDim
ones_dim
({
left_
});
divisor_
.
mutable_data
<
T
>
(
ones_dim
,
dev_ctx
.
GetPlace
());
math
::
set_constant
(
dev_ctx
,
&
divisor_
,
1.0
);
}
void
operator
()(
const
platform
::
CUDADeviceContext
&
context
,
const
framework
::
Tensor
&
input
,
framework
::
Tensor
*
out
)
{
math
::
gemv
<
platform
::
CUDADeviceContext
,
T
>
(
context
,
true
,
left_
,
right_
,
1.
,
input
.
data
<
T
>
(),
divisor_
.
data
<
T
>
(),
0.
,
out
->
data
<
T
>
());
}
private:
int
left_
;
int
right_
;
framework
::
Tensor
divisor_
;
};
template
<
typename
T
>
class
ColwiseSum2D
<
platform
::
CPUDeviceContext
,
T
>
{
public:
ColwiseSum2D
(
int
left
,
int
right
,
const
platform
::
DeviceContext
&
dev_ctx
)
{}
void
operator
()(
const
platform
::
CPUDeviceContext
&
context
,
const
framework
::
Tensor
&
input
,
framework
::
Tensor
*
out
)
{
col_wise_
(
context
,
input
,
out
);
}
private:
math
::
ColwiseSum
<
platform
::
CPUDeviceContext
,
T
>
col_wise_
;
};
template
<
typename
T
>
struct
SubAndSquareFunctor
{
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
return
(
a
-
b
)
*
(
a
-
b
);
}
...
...
@@ -67,15 +160,15 @@ using DataLayout = framework::DataLayout;
template
<
typename
DeviceContext
,
typename
T
>
class
LayerNormKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
auto
*
scale
=
ctx
.
Input
<
Tensor
>
(
"Scale"
);
auto
*
bias
=
ctx
.
Input
<
Tensor
>
(
"Bias"
);
auto
*
scale
=
ctx
.
Input
<
Tensor
>
(
"Scale"
);
auto
*
bias
=
ctx
.
Input
<
Tensor
>
(
"Bias"
);
auto
x
=
*
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
y
=
ctx
.
Output
<
Tensor
>
(
"Y"
);
auto
*
mean
=
ctx
.
Output
<
Tensor
>
(
"Mean"
);
auto
*
var
=
ctx
.
Output
<
Tensor
>
(
"Variance"
);
auto
*
y
=
ctx
.
Output
<
Tensor
>
(
"Y"
);
auto
*
mean
=
ctx
.
Output
<
Tensor
>
(
"Mean"
);
auto
*
var
=
ctx
.
Output
<
Tensor
>
(
"Variance"
);
const
auto
begin_norm_axis
=
ctx
.
Attr
<
int
>
(
"begin_norm_axis"
);
const
auto
x_dims
=
x
.
dims
();
...
...
@@ -94,8 +187,8 @@ class LayerNormKernel : public framework::OpKernel<T> {
out
.
ShareDataWith
(
*
y
);
out
.
Resize
(
matrix_shape
);
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
math
::
RowwiseMean
<
DeviceContext
,
T
>
row_mean
;
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
RowwiseMean2D
<
DeviceContext
,
T
>
row_mean
(
left
,
right
,
ctx
.
device_context
())
;
// get mean
row_mean
(
dev_ctx
,
x
,
mean
);
...
...
@@ -126,31 +219,32 @@ class LayerNormKernel : public framework::OpKernel<T> {
template
<
typename
DeviceContext
,
typename
T
>
class
LayerNormGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
auto
x
=
*
ctx
.
Input
<
Tensor
>
(
"X"
);
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
*
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"
);
// init output
auto
*
d_x
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
d_scale
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Scale"
));
auto
*
d_bias
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Bias"
));
auto
*
d_x
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
d_scale
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Scale"
));
auto
*
d_bias
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Bias"
));
const
auto
&
x_dims
=
x
.
dims
();
const
auto
&
x_dims
=
x
.
dims
();
auto
matrix_dim
=
framework
::
flatten_to_2d
(
x_dims
,
begin_norm_axis
);
int
left
=
static_cast
<
int
>
(
matrix_dim
[
0
]);
int
right
=
static_cast
<
int
>
(
matrix_dim
[
1
]);
framework
::
DDim
matrix_shape
({
left
,
right
});
d_y
.
Resize
(
matrix_shape
);
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
math
::
ColwiseSum
<
DeviceContext
,
T
>
colwise_sum
;
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
ColwiseSum2D
<
DeviceContext
,
T
>
colwise_sum
(
left
,
right
,
ctx
.
device_context
());
Tensor
temp
;
Tensor
temp_norm
;
...
...
@@ -190,7 +284,8 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
Tensor
temp_vec
;
temp_vec
.
mutable_data
<
T
>
(
vec_shape
,
ctx
.
GetPlace
());
math
::
RowwiseMean
<
DeviceContext
,
T
>
row_mean
;
RowwiseMean2D
<
DeviceContext
,
T
>
row_mean
(
left
,
right
,
ctx
.
device_context
());
if
(
d_scale
)
{
// dy_dx
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录