Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
7f2b4391
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
331
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看板
提交
7f2b4391
编写于
7月 23, 2020
作者:
C
chenjiaoAngel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fiix build error, test=develop
上级
1391e255
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
83 addition
and
71 deletion
+83
-71
lite/backends/arm/math/sequence_pool.cc
lite/backends/arm/math/sequence_pool.cc
+14
-0
lite/backends/arm/math/sequence_pool_grad.cc
lite/backends/arm/math/sequence_pool_grad.cc
+14
-12
lite/kernels/arm/sequence_pool_grad_compute.cc
lite/kernels/arm/sequence_pool_grad_compute.cc
+2
-4
lite/tests/kernels/sequence_pool_grad_compute_test.cc
lite/tests/kernels/sequence_pool_grad_compute_test.cc
+53
-55
未找到文件。
lite/backends/arm/math/sequence_pool.cc
浏览文件 @
7f2b4391
...
...
@@ -32,10 +32,12 @@ void seq_pool_sum<float>(const float* din,
float
*
dout
,
const
std
::
vector
<
uint64_t
>
lod
,
int64_t
width
)
{
LOG
(
INFO
)
<<
"size: "
<<
lod
.
size
()
-
1
;
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod
.
size
())
-
1
;
++
i
)
{
const
float
*
din_ptr
=
din
+
lod
[
i
]
*
width
;
float
*
dout_ptr
=
dout
+
i
*
width
;
int64_t
height
=
static_cast
<
int64_t
>
(
lod
[
i
+
1
]
-
lod
[
i
]);
if
(
height
>
0
)
{
if
(
width
==
1
)
{
float
sum
=
0.
f
;
for
(
int
h
=
0
;
h
<
height
;
++
h
)
{
...
...
@@ -46,6 +48,16 @@ void seq_pool_sum<float>(const float* din,
memcpy
(
dout_ptr
,
din_ptr
,
width
*
sizeof
(
float
));
din_ptr
+=
width
;
height
=
height
-
1
;
/* for (int h = 0; h < height; h++) {
for (int w = 0; w < width; ++w) {
dout_ptr[w] += din_ptr[w];
}
din_ptr += width;
}
*/
// continue;
if
(
height
==
0
)
return
;
int
cnt_w
=
width
>>
2
;
int
remain_w
=
width
&
3
;
int
cnt_h
=
height
>>
2
;
...
...
@@ -101,8 +113,10 @@ void seq_pool_sum<float>(const float* din,
}
dout_ptr
++
;
}
}
}
}
printf
(
"end--
\n
"
);
}
template
<
>
...
...
lite/backends/arm/math/sequence_pool_grad.cc
浏览文件 @
7f2b4391
...
...
@@ -33,22 +33,24 @@ void seq_pool_sum_grad<float>(const float* din,
float
*
dout
,
const
std
::
vector
<
uint64_t
>
lod
,
int64_t
width
)
{
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod
.
size
())
-
1
;
++
i
)
{
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod
.
size
())
-
1
;
i
++
)
{
int64_t
height
=
static_cast
<
int64_t
>
(
lod
[
i
+
1
]
-
lod
[
i
]);
const
float
*
din_ptr
=
din
+
lod
[
i
]
*
width
;
const
float
*
din_grad_ptr
=
din
+
i
*
width
;
const
float
*
din_grad_ptr
=
din
_grad
+
i
*
width
;
float
*
dout_ptr
=
dout
+
lod
[
i
]
*
width
;
if
(
width
==
1
)
{
for
(
int
h
=
0
;
h
<
height
;
++
h
)
{
if
(
height
>
0
)
{
if
(
width
==
1
)
{
for
(
int
h
=
0
;
h
<
height
;
++
h
)
{
dout_ptr
[
h
]
=
din_grad_ptr
[
h
];
}
}
else
{
for
(
int
w
=
0
;
w
<
width
;
w
++
)
{
for
(
int
h
=
0
;
h
<
height
;
h
++
)
{
}
}
else
{
for
(
int
w
=
0
;
w
<
width
;
w
++
)
{
for
(
int
h
=
0
;
h
<
height
;
h
++
)
{
dout_ptr
[
h
]
=
*
din_grad_ptr
;
dout_ptr
+=
width
;
}
din_grad_ptr
++
;
}
din_grad_ptr
++
;
}
}
}
}
...
...
@@ -63,7 +65,7 @@ void seq_pool_average_grad<float>(const float* din,
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod
.
size
())
-
1
;
++
i
)
{
int64_t
height
=
static_cast
<
int64_t
>
(
lod
[
i
+
1
]
-
lod
[
i
]);
const
float
*
din_ptr
=
din
+
lod
[
i
]
*
width
;
const
float
*
din_grad_ptr
=
din
+
i
*
width
;
const
float
*
din_grad_ptr
=
din
_grad
+
i
*
width
;
float
*
dout_ptr
=
dout
+
lod
[
i
]
*
width
;
float
alpha
=
1.0
/
height
;
if
(
height
>
0
)
{
...
...
@@ -93,7 +95,7 @@ void seq_pool_sqrt_grad<float>(const float* din,
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod
.
size
())
-
1
;
++
i
)
{
int64_t
height
=
static_cast
<
int64_t
>
(
lod
[
i
+
1
]
-
lod
[
i
]);
const
float
*
din_ptr
=
din
+
lod
[
i
]
*
width
;
const
float
*
din_grad_ptr
=
din
+
i
*
width
;
const
float
*
din_grad_ptr
=
din
_grad
+
i
*
width
;
float
*
dout_ptr
=
dout
+
lod
[
i
]
*
width
;
float
alpha
=
1.0
/
sqrtf
(
height
);
if
(
height
>
0
)
{
...
...
lite/kernels/arm/sequence_pool_grad_compute.cc
浏览文件 @
7f2b4391
...
...
@@ -33,14 +33,12 @@ void SequencePoolGradCompute::Run() {
auto
&
x_grad
=
param
.
X_Grad
;
const
auto
*
din_ptr
=
param
.
X
->
data
<
float
>
();
const
auto
*
dout_grad_ptr
=
output_grad
->
data
<
float
>
();
float
*
x_grad_ptr
=
x_grad
->
mutable_data
a
<
float
>
();
float
*
x_grad_ptr
=
x_grad
->
mutable_data
<
float
>
();
const
auto
pool_type
=
param
.
pool_type
;
const
auto
lod
=
param
.
X
->
lod
()[
0
];
int64_t
width
=
param
.
X
->
numel
()
/
param
.
X
->
dims
()[
0
];
if
(
pool_type
==
"SUM"
||
pool_type
==
"MAX"
||
pool_type
==
"MIN"
)
{
lite
::
arm
::
math
::
seq_pool_grad
(
din_ptr
,
dout_grad_ptr
,
x_grad_ptr
,
lod
,
width
);
lite
::
arm
::
math
::
seq_pool_
sum_
grad
(
din_ptr
,
dout_grad_ptr
,
x_grad_ptr
,
lod
,
width
);
}
else
if
(
pool_type
==
"AVERAGE"
)
{
lite
::
arm
::
math
::
seq_pool_average_grad
(
din_ptr
,
dout_grad_ptr
,
x_grad_ptr
,
lod
,
width
);
}
else
if
(
pool_type
==
"SQRT"
)
{
...
...
lite/tests/kernels/sequence_pool_grad_compute_test.cc
浏览文件 @
7f2b4391
...
...
@@ -20,14 +20,21 @@
namespace
paddle
{
namespace
lite
{
namespace
kernels
{
namespace
arm
{
using
param_t
=
operators
::
SequencePoolParam
;
using
grad_param_t
=
operators
::
SequencePoolGradParam
;
using
kernel_t
=
SequencePoolCompute
;
using
grad_kernel_t
=
SequencePoolGradCompute
;
template
<
class
kernel_t
,
class
grad_kernel_t
>
class
SequencePoolGradTester
{
public:
explicit
SequencePoolGradTester
(
DDim
dims
)
:
dims_
(
dims
)
{}
explicit
SequencePoolGradTester
(
DDim
dims
,
std
::
vector
<
std
::
vector
<
uint64_t
>>
lod
,
std
::
string
pool_type
)
:
dims_
(
dims
),
lod_
(
lod
),
pool_type_
(
pool_type
)
{}
void
prepare_kernel
()
{
std
::
unique_ptr
<
KernelContext
>
ctx1
(
new
KernelContext
);
...
...
@@ -43,18 +50,6 @@ class SequencePoolGradTester {
grad_kernel_
.
SetContext
(
std
::
move
(
ctx3
));
}
void
generate_lod
(
int
seq_num
,
int
max_len
,
std
::
vector
<
uint64_t
>&
seq_offset
)
{
// NOLINT
seq_offset
.
clear
();
int
sum
=
0
;
seq_offset
.
push_back
(
sum
);
for
(
int
i
=
0
;
i
<
seq_num
;
i
++
)
{
sum
+=
std
::
rand
()
%
max_len
+
1
;
seq_offset
.
push_back
(
uint64_t
(
sum
));
}
}
void
run_forward
(
param_t
*
param
,
kernel_t
*
kernel
,
const
std
::
vector
<
float
>&
in_vec
,
...
...
@@ -67,13 +62,12 @@ class SequencePoolGradTester {
for
(
int
i
=
0
;
i
<
dims_
.
production
();
i
++
)
{
x_data
[
i
]
=
in_vec
[
i
];
}
x
->
set_lod
(
lod_
);
x
.
set_lod
(
lod_
);
param
->
X
=
&
x
;
param
->
pool_type
=
pool_type_
;
param
->
Out
=
&
output
;
kernel
->
SetParam
(
*
param
);
kernel
->
Launch
();
auto
*
output_data
=
output
.
mutable_data
<
float
>
();
for
(
int
i
=
0
;
i
<
output
.
numel
();
i
++
)
{
out_vec
[
i
]
=
output_data
[
i
];
...
...
@@ -83,38 +77,32 @@ class SequencePoolGradTester {
void
run_backward
(
grad_param_t
*
param
,
grad_kernel_t
*
kernel
,
const
std
::
vector
<
float
>&
in_vec
,
const
std
::
vector
<
float
>&
out_vec
,
const
std
::
vector
<
float
>&
out_grad_vec
,
float
*
in_grad_vec
)
{
Tensor
x
;
Tensor
out
;
Tensor
x_grad
;
Tensor
out_grad
;
x
.
Resize
(
dims_
);
x_grad
.
Resize
(
dims_
);
x
.
set_lod
(
lod_
);
// backword
out_grad
.
Resize
(
out_dims_
);
out
.
Resize
(
out_dims_
);
auto
*
x_data
=
x
.
mutable_data
<
float
>
();
auto
*
out_data
=
out
.
mutable_data
<
float
>
();
auto
*
out_grad_data
=
out_grad
.
mutable_data
<
float
>
();
for
(
int
i
=
0
;
i
<
dims_
.
production
();
i
++
)
{
x_data
[
i
]
=
in_vec
[
i
];
}
for
(
int
i
=
0
;
i
<
out_dims_
.
production
();
i
++
)
{
out_data
[
i
]
=
out_vec
[
i
];
out_grad_data
[
i
]
=
out_grad_vec
[
i
];
}
param
->
X
=
&
x
;
param
->
Out
=
&
out
;
param
->
X_grad
=
&
x_grad
;
param
->
Out_grad
=
&
out_grad
;
param
->
X_Grad
=
&
x_grad
;
param
->
Out_Grad
=
&
out_grad
;
param
->
pool_type
=
pool_type_
;
kernel
->
SetParam
(
*
param
);
kernel
->
Launch
();
auto
*
x_grad_data
=
x_grad
.
mutable_data
<
float
>
();
auto
*
x_grad_data
=
x_grad
.
data
<
float
>
();
for
(
int
i
=
0
;
i
<
dims_
.
production
();
i
++
)
{
in_grad_vec
[
i
]
=
x_grad_data
[
i
];
}
...
...
@@ -131,38 +119,21 @@ class SequencePoolGradTester {
static_cast
<
float
>
(
i
%
19
-
10.0
)
/
10.0
*
0.333
+
static_cast
<
float
>
(
i
%
39
-
20.0
)
/
20.0
*
0.333
+
0.001213
;
}
LOG
(
INFO
)
<<
"run_forward:"
;
this
->
run_forward
(
&
param_
,
&
kernel_
,
x
,
out
.
data
());
std
::
vector
<
float
>
out_grad
(
out_dims_
.
production
());
std
::
vector
<
float
>
x_grad
(
dims_
.
production
());
std
::
vector
<
float
>
x_delta
(
dims_
.
production
());
std
::
vector
<
float
>
out_delta
(
out_dims_
.
production
());
for
(
int
i
=
0
;
i
<
out_dims_
.
production
();
i
++
)
{
out_grad
[
i
]
=
1.0
;
x_grad
[
i
]
=
1.0
;
}
LOG
(
INFO
)
<<
"run_backward:"
;
this
->
run_backward
(
&
grad_param_
,
&
grad_kernel_
,
x
,
out
,
out_grad
,
x_grad
.
data
());
for
(
int
i
=
0
;
i
<
dims_
.
production
();
i
++
)
{
for
(
int
j
=
0
;
j
<
dims_
.
production
();
j
++
)
{
if
(
i
==
j
)
{
x_delta
[
j
]
=
x
[
j
]
+
delta
;
}
else
{
x_delta
[
j
]
=
x
[
j
];
}
}
this
->
run_forward
(
&
delta_param_
,
&
delta_kernel_
,
x_delta
,
out_delta
.
data
());
float
sum
=
0
;
for
(
int
j
=
0
;
j
<
out_dims_
.
production
();
j
++
)
{
sum
+=
(
out_delta
[
j
]
-
out
[
j
]);
}
EXPECT_NEAR
(
x_grad
[
i
],
sum
/
delta
,
max_grad_delta
);
}
}
&
grad_param_
,
&
grad_kernel_
,
x
,
out_grad
,
x_grad
.
data
());
}
private:
DDim
dims_
;
...
...
@@ -177,6 +148,18 @@ class SequencePoolGradTester {
grad_param_t
grad_param_
;
};
void
generate_lod
(
int
seq_num
,
int
max_len
,
std
::
vector
<
uint64_t
>&
seq_offset
)
{
// NOLINT
seq_offset
.
clear
();
int
sum
=
0
;
seq_offset
.
push_back
(
sum
);
for
(
int
i
=
0
;
i
<
seq_num
;
i
++
)
{
sum
+=
std
::
rand
()
%
max_len
+
1
;
seq_offset
.
push_back
(
uint64_t
(
sum
));
}
}
void
TestSequencePoolGrad
(
DDim
dims
,
std
::
vector
<
std
::
vector
<
uint64_t
>>
lod
,
std
::
string
pool_type
)
{
LOG
(
INFO
)
<<
"Test SequencePool grad"
;
std
::
unique_ptr
<
SequencePoolGradTester
>
tester
(
new
SequencePoolGradTester
(
...
...
@@ -190,24 +173,39 @@ void TestSequencePoolGrad(DDim dims, std::vector<std::vector<uint64_t>> lod, std
TEST
(
sequence_pool_grad_host
,
compute
)
{
int
max_len
=
2
;
DeviceInfo
::
Init
();
for
(
auto
seq_num
:
{
1
,
3
,
5
})
{
for
(
auto
c
:
{
2
,
9
})
{
for
(
auto
h
:
{
2
,
1
})
{
for
(
auto
w
:
{
2
,
10
})
{
for
(
auto
pool_type
:
{
"SUM"
,
"AVERAGE"
,
"SQRT"
,
"MAX"
,
"MIN"
,
"FIRST"
,
"LAST"
})
{
for
(
auto
c
:
{
2
,
4
})
{
for
(
auto
h
:
{
1
,
3
,
4
})
{
for
(
auto
w
:
{
1
,
3
,
4
})
{
for
(
auto
pool_type
:
{
"SUM"
,
"AVERAGE"
,
"SQRT"
,
"MAX"
,
"MIN"
,
"FIRST"
,
"LAST"
})
{
for
(
auto
seq_num
:
{
1
,
3
,
5
})
{
std
::
vector
<
std
::
vector
<
uint64_t
>>
lod
;
lod
.
resize
(
1
);
generate_lod
(
seq_num
,
max_len
,
lod
[
0
]);
x
.
set_lod
(
lod
);
int64_t
n
=
int64_t
(
lod
[
0
].
back
());
LOG
(
INFO
)
<<
"sequence_pool_grad parameter: "
<<
", n = "
<<
n
<<
", c = "
<<
c
<<
", h = "
<<
h
<<
", w = "
<<
w
<<
", seq_num = "
<<
seq_num
<<
", pool_type = "
<<
pool_type
;
TestSequencePoolGrad
(
DDim
(
std
::
vector
<
int64_t
>
({
n
,
c
,
h
,
w
})),
lod
,
pool_type
);
}
}
}
}
}
}
}
// namespace arm
}
// namespace kernels
}
// namespace lite
}
// namespace paddle
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录