Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
beb32dfe
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
332
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
beb32dfe
编写于
2月 14, 2020
作者:
Y
Yiqun Liu
提交者:
GitHub
2月 14, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[X86] Optimize gru and softmax (#2877) (#2884)
test=develop test=release/2.3
上级
2ef97e68
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
51 addition
and
44 deletion
+51
-44
lite/backends/x86/jit/more/mkl/mkl.h
lite/backends/x86/jit/more/mkl/mkl.h
+3
-4
lite/backends/x86/math/math_function.cc
lite/backends/x86/math/math_function.cc
+1
-5
lite/kernels/x86/gru_compute.h
lite/kernels/x86/gru_compute.h
+24
-21
lite/kernels/x86/softmax_compute.h
lite/kernels/x86/softmax_compute.h
+23
-14
未找到文件。
lite/backends/x86/jit/more/mkl/mkl.h
浏览文件 @
beb32dfe
...
...
@@ -142,14 +142,13 @@ void StrideScal(const T* a, const T* x, T* y, int n, int stride);
// remain is the product of dimension shapes after the axis dimension
template
<
typename
T
>
void
Softmax
(
const
T
*
x
,
T
*
y
,
int
n
,
int
bs
,
int
remain
=
1
)
{
std
::
vector
<
T
>
entities
(
bs
);
for
(
int
i
=
0
;
i
<
bs
;
++
i
)
{
entities
[
i
]
=
x
[
i
*
n
];
T
entity
=
x
[
i
*
n
];
for
(
int
c
=
1
;
c
<
n
;
++
c
)
{
entit
ies
[
i
]
=
x
[
i
*
n
+
c
]
>
entities
[
i
]
?
x
[
i
*
n
+
c
]
:
entities
[
i
]
;
entit
y
=
x
[
i
*
n
+
c
]
>
entity
?
x
[
i
*
n
+
c
]
:
entity
;
}
for
(
int
c
=
0
;
c
<
n
;
++
c
)
{
y
[
i
*
n
+
c
]
=
x
[
i
*
n
+
c
]
-
entit
ies
[
i
]
;
y
[
i
*
n
+
c
]
=
x
[
i
*
n
+
c
]
-
entit
y
;
}
}
VExp
(
y
,
y
,
n
*
bs
);
...
...
lite/backends/x86/math/math_function.cc
浏览文件 @
beb32dfe
...
...
@@ -110,11 +110,7 @@ void set_constant(const lite::Context<Target>& context,
lite
::
Tensor
*
tensor
,
float
value
)
{
TensorSetConstantWithTarget
<
Target
>
func
(
context
,
tensor
,
value
);
// #ifdef PADDLE_WITH_CUDA
// tensor->target().apply_visitor(func);
// #else
func
();
// #endif
}
template
<
typename
T
>
...
...
@@ -123,7 +119,7 @@ struct RowwiseAdd<lite::TargetType::kX86, T> {
const
lite
::
Tensor
&
input
,
const
lite
::
Tensor
&
vector
,
lite
::
Tensor
*
output
)
{
auto
in_dims
=
input
.
dims
();
const
auto
&
in_dims
=
input
.
dims
();
auto
size
=
input
.
numel
()
/
in_dims
[
0
];
PADDLE_ENFORCE_EQ
(
vector
.
numel
(),
size
);
PADDLE_ENFORCE_EQ
(
output
->
dims
(),
in_dims
);
...
...
lite/kernels/x86/gru_compute.h
浏览文件 @
beb32dfe
...
...
@@ -48,6 +48,10 @@ inline void ReorderInitState(const lite::Context<TARGET(kX86)>& context,
row_shuffle
(
context
,
src
,
index_lod
,
dst
,
indexed_src
);
}
static
inline
int64_t
CalculateSeqWidth
(
const
DDim
&
dims
)
{
return
dims
.
count
(
1
,
dims
.
size
());
}
template
<
typename
T
>
class
GRUCompute
:
public
KernelLite
<
TARGET
(
kX86
),
PRECISION
(
kFloat
)
>
{
public:
...
...
@@ -65,15 +69,16 @@ class GRUCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
auto
*
bias
=
param
.
bias
;
auto
*
batch_gate
=
param
.
batch_gate
;
batch_gate
->
mutable_data
<
T
>
();
auto
*
batch_reset_hidden_prev
=
param
.
batch_reset_hidden_prev
;
batch_reset_hidden_prev
->
mutable_data
<
T
>
();
auto
*
batch_hidden
=
param
.
batch_hidden
;
batch_hidden
->
mutable_data
<
T
>
();
T
*
batch_gate_ptr
=
batch_gate
->
mutable_data
<
T
>
();
T
*
batch_reset_hidden_prev_ptr
=
batch_reset_hidden_prev
->
mutable_data
<
T
>
();
T
*
batch_hidden_ptr
=
batch_hidden
->
mutable_data
<
T
>
();
auto
*
hidden
=
param
.
hidden
;
hidden
->
mutable_data
<
T
>
();
auto
hidden_dims
=
hidden
->
dims
();
const
auto
&
hidden_dims
=
hidden
->
dims
();
lite
::
x86
::
math
::
LoDTensor2BatchFunctor
<
TARGET
(
kX86
),
T
>
to_batch
;
to_batch
(
context
,
*
input
,
batch_gate
,
true
,
is_reverse
);
...
...
@@ -90,19 +95,23 @@ class GRUCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
const_cast
<
T
*>
(
weight_data
+
2
*
frame_size
*
frame_size
);
Tensor
ordered_h0
;
std
::
vector
<
size_t
>
order
(
batch_gate
->
lod
()[
2
]);
if
(
h0
)
{
// Since the batch computing for GRU reorders the input sequences
// according to their length. The initialized cell state also needs
// to reorder.
const
std
::
vector
<
size_t
>&
order
(
batch_gate
->
lod
()[
2
]);
ReorderInitState
<
T
>
(
context
,
*
h0
,
order
,
&
ordered_h0
,
true
);
gru_value
.
prev_out_value
=
ordered_h0
.
mutable_data
<
T
>
();
}
else
{
gru_value
.
prev_out_value
=
nullptr
;
}
auto
batch_starts
=
batch_gate
->
lod
()[
0
];
const
auto
&
batch_starts
=
batch_gate
->
lod
()[
0
];
size_t
seq_len
=
batch_starts
.
size
()
-
1
;
int64_t
batch_gate_width
=
CalculateSeqWidth
(
batch_gate
->
dims
());
int64_t
batch_reset_hidden_prev_width
=
CalculateSeqWidth
(
batch_reset_hidden_prev
->
dims
());
int64_t
batch_hidden_width
=
CalculateSeqWidth
(
batch_hidden
->
dims
());
auto
active_node
=
lite
::
x86
::
math
::
detail
::
GetActivationType
(
param
.
activation
);
auto
active_gate
=
...
...
@@ -145,13 +154,10 @@ class GRUCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
int64_t
bend
=
static_cast
<
int64_t
>
(
batch_starts
[
n
+
1
]);
int64_t
cur_batch_size
=
bend
-
bstart
;
Tensor
gate_t
=
batch_gate
->
Slice
<
T
>
(
bstart
,
bend
);
Tensor
reset_hidden_prev_t
=
batch_reset_hidden_prev
->
Slice
<
T
>
(
bstart
,
bend
);
Tensor
hidden_t
=
batch_hidden
->
Slice
<
T
>
(
bstart
,
bend
);
gru_value
.
output_value
=
hidden_t
.
mutable_data
<
T
>
();
gru_value
.
gate_value
=
gate_t
.
mutable_data
<
T
>
();
gru_value
.
reset_output_value
=
reset_hidden_prev_t
.
mutable_data
<
T
>
();
gru_value
.
output_value
=
batch_hidden_ptr
+
bstart
*
batch_hidden_width
;
gru_value
.
gate_value
=
batch_gate_ptr
+
bstart
*
batch_gate_width
;
gru_value
.
reset_output_value
=
batch_reset_hidden_prev_ptr
+
bstart
*
batch_reset_hidden_prev_width
;
if
(
gru_value
.
prev_out_value
)
{
blas
.
GEMM_COMPUTE
(
CblasNoTrans
,
...
...
@@ -188,13 +194,10 @@ class GRUCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
int64_t
bend
=
static_cast
<
int64_t
>
(
batch_starts
[
n
+
1
]);
int64_t
cur_batch_size
=
bend
-
bstart
;
Tensor
gate_t
=
batch_gate
->
Slice
<
T
>
(
bstart
,
bend
);
Tensor
reset_hidden_prev_t
=
batch_reset_hidden_prev
->
Slice
<
T
>
(
bstart
,
bend
);
Tensor
hidden_t
=
batch_hidden
->
Slice
<
T
>
(
bstart
,
bend
);
gru_value
.
output_value
=
hidden_t
.
mutable_data
<
T
>
();
gru_value
.
gate_value
=
gate_t
.
mutable_data
<
T
>
();
gru_value
.
reset_output_value
=
reset_hidden_prev_t
.
mutable_data
<
T
>
();
gru_value
.
output_value
=
batch_hidden_ptr
+
bstart
*
batch_hidden_width
;
gru_value
.
gate_value
=
batch_gate_ptr
+
bstart
*
batch_gate_width
;
gru_value
.
reset_output_value
=
batch_reset_hidden_prev_ptr
+
bstart
*
batch_reset_hidden_prev_width
;
lite
::
x86
::
math
::
GRUUnitFunctor
<
TARGET
(
kX86
),
T
>::
compute
(
context
,
...
...
lite/kernels/x86/softmax_compute.h
浏览文件 @
beb32dfe
...
...
@@ -55,24 +55,33 @@ class SoftmaxCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
auto
&
context
=
ctx_
->
As
<
X86Context
>
();
CHECK
(
param
.
output
);
CHECK
(
param
.
x
);
param
.
output
->
mutable_data
<
T
>
();
const
int
rank
=
param
.
x
->
dims
().
size
();
auto
*
x
=
param
.
x
;
auto
*
output
=
param
.
output
;
output
->
mutable_data
<
T
>
();
const
int
rank
=
x
->
dims
().
size
();
const
int
axis
=
CanonicalAxis
(
param
.
axis
,
rank
);
int
axis_dim
=
param
.
x
->
dims
()[
axis
];
const
int
n
=
SizeToAxis
(
axis
,
param
.
x
->
dims
());
const
int
d
=
SizeFromAxis
(
axis
,
param
.
x
->
dims
());
int
axis_dim
=
x
->
dims
()[
axis
];
if
(
rank
==
2
&&
axis
==
1
)
{
lite
::
x86
::
math
::
SoftmaxFunctor
<
lite
::
TargetType
::
kX86
,
T
,
true
>
()(
context
,
axis_dim
,
x
,
output
);
}
else
{
const
int
n
=
SizeToAxis
(
axis
,
x
->
dims
());
const
int
d
=
SizeFromAxis
(
axis
,
x
->
dims
());
DDim
shape
(
std
::
vector
<
DDim
::
value_type
>
{
n
,
d
});
DDim
x_dims
=
x
->
dims
();
DDim
out_dims
=
output
->
dims
();
Tensor
input_2d
;
Tensor
out_2d
;
input_2d
.
ShareDataWith
(
*
param
.
x
);
input_2d
.
Resize
(
shape
);
out_2d
.
ShareDataWith
(
*
param
.
output
);
out_2d
.
Resize
(
shape
);
DDim
shape_2d
(
std
::
vector
<
DDim
::
value_type
>
{
n
,
d
});
x
->
Resize
(
shape_2d
);
output
->
Resize
(
shape_2d
);
lite
::
x86
::
math
::
SoftmaxFunctor
<
lite
::
TargetType
::
kX86
,
T
,
true
>
()(
context
,
axis_dim
,
&
input_2d
,
&
out_2d
);
lite
::
x86
::
math
::
SoftmaxFunctor
<
lite
::
TargetType
::
kX86
,
T
,
true
>
()(
context
,
axis_dim
,
x
,
output
);
x
->
Resize
(
x_dims
);
output
->
Resize
(
out_dims
);
}
}
virtual
~
SoftmaxCompute
()
=
default
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录