Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3941c2dd
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
3941c2dd
编写于
3月 26, 2018
作者:
X
Xin Pan
提交者:
GitHub
3月 26, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9355 from panyx0718/layer_norm
Improve layer_norm speed
上级
4f522fa8
1a4be55a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
120 addition
and
21 deletion
+120
-21
paddle/fluid/operators/layer_norm_op.h
paddle/fluid/operators/layer_norm_op.h
+120
-21
未找到文件。
paddle/fluid/operators/layer_norm_op.h
浏览文件 @
3941c2dd
...
@@ -22,6 +22,103 @@ limitations under the License. */
...
@@ -22,6 +22,103 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
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
);
};
#ifdef PADDLE_WITH_CUDA
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_
;
};
#endif
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
);
};
#ifdef PADDLE_WITH_CUDA
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_
;
};
#endif
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
>
template
<
typename
T
>
struct
SubAndSquareFunctor
{
struct
SubAndSquareFunctor
{
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
return
(
a
-
b
)
*
(
a
-
b
);
}
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
return
(
a
-
b
)
*
(
a
-
b
);
}
...
@@ -67,15 +164,15 @@ using DataLayout = framework::DataLayout;
...
@@ -67,15 +164,15 @@ using DataLayout = framework::DataLayout;
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
LayerNormKernel
:
public
framework
::
OpKernel
<
T
>
{
class
LayerNormKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
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
*
scale
=
ctx
.
Input
<
Tensor
>
(
"Scale"
);
auto
*
scale
=
ctx
.
Input
<
Tensor
>
(
"Scale"
);
auto
*
bias
=
ctx
.
Input
<
Tensor
>
(
"Bias"
);
auto
*
bias
=
ctx
.
Input
<
Tensor
>
(
"Bias"
);
auto
x
=
*
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
x
=
*
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
y
=
ctx
.
Output
<
Tensor
>
(
"Y"
);
auto
*
y
=
ctx
.
Output
<
Tensor
>
(
"Y"
);
auto
*
mean
=
ctx
.
Output
<
Tensor
>
(
"Mean"
);
auto
*
mean
=
ctx
.
Output
<
Tensor
>
(
"Mean"
);
auto
*
var
=
ctx
.
Output
<
Tensor
>
(
"Variance"
);
auto
*
var
=
ctx
.
Output
<
Tensor
>
(
"Variance"
);
const
auto
begin_norm_axis
=
ctx
.
Attr
<
int
>
(
"begin_norm_axis"
);
const
auto
begin_norm_axis
=
ctx
.
Attr
<
int
>
(
"begin_norm_axis"
);
const
auto
x_dims
=
x
.
dims
();
const
auto
x_dims
=
x
.
dims
();
...
@@ -94,8 +191,8 @@ class LayerNormKernel : public framework::OpKernel<T> {
...
@@ -94,8 +191,8 @@ class LayerNormKernel : public framework::OpKernel<T> {
out
.
ShareDataWith
(
*
y
);
out
.
ShareDataWith
(
*
y
);
out
.
Resize
(
matrix_shape
);
out
.
Resize
(
matrix_shape
);
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
math
::
RowwiseMean
<
DeviceContext
,
T
>
row_mean
;
RowwiseMean2D
<
DeviceContext
,
T
>
row_mean
(
left
,
right
,
ctx
.
device_context
())
;
// get mean
// get mean
row_mean
(
dev_ctx
,
x
,
mean
);
row_mean
(
dev_ctx
,
x
,
mean
);
...
@@ -126,31 +223,32 @@ class LayerNormKernel : public framework::OpKernel<T> {
...
@@ -126,31 +223,32 @@ class LayerNormKernel : public framework::OpKernel<T> {
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
LayerNormGradKernel
:
public
framework
::
OpKernel
<
T
>
{
class
LayerNormGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
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
*
y
=
ctx
.
Input
<
Tensor
>
(
"Y"
);
auto
*
y
=
ctx
.
Input
<
Tensor
>
(
"Y"
);
auto
*
mean
=
ctx
.
Input
<
Tensor
>
(
"Mean"
);
auto
*
mean
=
ctx
.
Input
<
Tensor
>
(
"Mean"
);
auto
*
var
=
ctx
.
Input
<
Tensor
>
(
"Variance"
);
auto
*
var
=
ctx
.
Input
<
Tensor
>
(
"Variance"
);
auto
*
scale
=
ctx
.
Input
<
Tensor
>
(
"Scale"
);
auto
*
scale
=
ctx
.
Input
<
Tensor
>
(
"Scale"
);
auto
*
bias
=
ctx
.
Input
<
Tensor
>
(
"Bias"
);
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"
);
// init output
// init output
auto
*
d_x
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
d_x
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
d_scale
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Scale"
));
auto
*
d_scale
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Scale"
));
auto
*
d_bias
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Bias"
));
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
);
auto
matrix_dim
=
framework
::
flatten_to_2d
(
x_dims
,
begin_norm_axis
);
int
left
=
static_cast
<
int
>
(
matrix_dim
[
0
]);
int
left
=
static_cast
<
int
>
(
matrix_dim
[
0
]);
int
right
=
static_cast
<
int
>
(
matrix_dim
[
1
]);
int
right
=
static_cast
<
int
>
(
matrix_dim
[
1
]);
framework
::
DDim
matrix_shape
({
left
,
right
});
framework
::
DDim
matrix_shape
({
left
,
right
});
d_y
.
Resize
(
matrix_shape
);
d_y
.
Resize
(
matrix_shape
);
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
math
::
ColwiseSum
<
DeviceContext
,
T
>
colwise_sum
;
ColwiseSum2D
<
DeviceContext
,
T
>
colwise_sum
(
left
,
right
,
ctx
.
device_context
());
Tensor
temp
;
Tensor
temp
;
Tensor
temp_norm
;
Tensor
temp_norm
;
...
@@ -190,7 +288,8 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
...
@@ -190,7 +288,8 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
Tensor
temp_vec
;
Tensor
temp_vec
;
temp_vec
.
mutable_data
<
T
>
(
vec_shape
,
ctx
.
GetPlace
());
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
)
{
if
(
d_scale
)
{
// dy_dx
// dy_dx
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录