Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
47ff9c15
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看板
提交
47ff9c15
编写于
12月 25, 2018
作者:
L
liuruilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add load when predict
上级
69ae3e0b
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
89 addition
and
7 deletion
+89
-7
src/common/types.h
src/common/types.h
+4
-0
src/framework/executor.cpp
src/framework/executor.cpp
+55
-0
src/framework/executor.h
src/framework/executor.h
+10
-1
src/framework/loader.h
src/framework/loader.h
+2
-0
src/io/paddle_mobile.cpp
src/io/paddle_mobile.cpp
+3
-4
src/io/paddle_mobile.h
src/io/paddle_mobile.h
+10
-0
test/net/test_super.cpp
test/net/test_super.cpp
+5
-2
未找到文件。
src/common/types.h
浏览文件 @
47ff9c15
...
...
@@ -107,6 +107,10 @@ enum PoolingType {
AVG
=
1
,
};
struct
PaddleMobileConfigInternal
{
bool
load_when_predict
=
false
;
};
extern
const
char
*
G_OP_TYPE_CONV
;
extern
const
char
*
G_OP_TYPE_BATCHNORM
;
extern
const
char
*
G_OP_TYPE_BOX_CODER
;
...
...
src/framework/executor.cpp
浏览文件 @
47ff9c15
...
...
@@ -37,6 +37,12 @@ namespace framework {
#pragma mark - executor
template
<
typename
Device
,
typename
T
>
Executor
<
Device
,
T
>::
Executor
(
const
Program
<
Device
>
&
program
,
paddle_mobile
::
PaddleMobileConfigInternal
config
,
int
batch_size
,
const
bool
use_optimize
,
const
bool
lod_mode
)
:
Executor
(
program
,
batch_size
,
use_optimize
,
lod_mode
)
{
config_
=
config
;
};
template
<
typename
Device
,
typename
T
>
Executor
<
Device
,
T
>::
Executor
(
const
Program
<
Device
>
&
program
,
int
batch_size
,
const
bool
use_optimize
,
const
bool
lod_mode
)
...
...
@@ -212,10 +218,16 @@ void Executor<Device, T>::InitCombineMemory() {
if
(
var_desc
->
Name
()
==
"feed"
||
var_desc
->
Name
()
==
"fetch"
)
{
continue
;
}
DLOG
<<
" init combine memory persistable: "
<<
var_desc
->
Name
();
LoadMemory
(
reinterpret_cast
<
void
**>
(
&
data
),
var_desc
,
tensor
);
}
else
{
if
(
var_desc
->
Type
()
==
VARTYPE_TYPE_LOD_TENSOR
)
{
DLOG
<<
" init combine memory no persistable in lod: "
<<
var_desc
->
Name
();
varInputMemory
(
var_desc
,
var
,
tensor
);
}
else
{
DLOG
<<
" init combine memory no persistable: "
<<
var_desc
->
Name
();
}
}
}
...
...
@@ -226,6 +238,32 @@ void Executor<Device, T>::InitCombineMemory() {
LOG
(
kLOG_INFO
)
<<
"init combine memory finish"
;
}
template
<
typename
Device
,
typename
T
>
void
Executor
<
Device
,
T
>::
InitNoPersistableMemory
(
const
LoDTensor
&
input_tensor
)
{
for
(
const
auto
&
block
:
program_desc_
->
Blocks
())
{
for
(
const
auto
&
var_desc
:
block
->
Vars
())
{
auto
var
=
program_
.
scope
->
Var
(
var_desc
->
Name
());
auto
tensor
=
var
->
template
GetMutable
<
LoDTensor
>();
if
(
var_desc
->
Persistable
())
{
if
(
var_desc
->
Name
()
==
"feed"
||
var_desc
->
Name
()
==
"fetch"
)
{
continue
;
}
}
else
{
if
(
var_desc
->
Type
()
==
VARTYPE_TYPE_LOD_TENSOR
)
{
DDim
tensor_dim
=
tensor
->
dims
();
DDim
new_dim
=
make_ddim
({
tensor_dim
[
0
],
tensor_dim
[
1
],
input_tensor
.
dims
()[
2
],
input_tensor
.
dims
()[
3
]});
tensor
->
template
Resize
(
new_dim
);
tensor
->
template
mutable_data
<
T
>();
}
}
}
}
std
::
shared_ptr
<
LoDTensor
>
output
=
GetOutput
(
"fetch"
);
output
->
Resize
(
input_tensor
.
dims
());
output
->
mutable_data
<
T
>
();
}
template
<
typename
Device
,
typename
T
>
bool
Executor
<
Device
,
T
>::
varInputMemory
(
const
std
::
shared_ptr
<
VarDesc
>
&
var_desc
,
Variable
*
var
,
...
...
@@ -275,6 +313,7 @@ PMStatus Executor<Device, T>::Predict(
template
<
typename
Device
,
typename
T
>
std
::
vector
<
T
>
Executor
<
Device
,
T
>::
Predict
(
const
std
::
vector
<
T
>
&
input
,
const
std
::
vector
<
int64_t
>
&
dims
)
{
Tensor
feed_tensor
(
input
,
make_ddim
(
dims
));
SetInput
(
feed_tensor
,
"feed"
);
std
::
vector
<
T
>
output
;
...
...
@@ -293,7 +332,15 @@ void Executor<Device, T>::SetInput(const Tensor &input,
auto
*
target_var
=
program_
.
scope
->
FindVar
(
var_name
);
PADDLE_MOBILE_ENFORCE
(
target_var
!=
nullptr
,
"Variable %s is not exist"
,
var_name
.
c_str
());
auto
*
target_tensor
=
target_var
->
template
GetMutable
<
LoDTensor
>();
if
(
config_
.
load_when_predict
)
{
if
(
target_tensor
->
IsInitialized
()
&&
target_tensor
->
dims
()
!=
input
.
dims
())
{
InitNoPersistableMemory
(
*
target_tensor
);
}
}
target_tensor
->
Resize
(
input
.
dims
());
target_tensor
->
ShareDataWith
(
input
);
}
...
...
@@ -301,10 +348,18 @@ void Executor<Device, T>::SetInput(const Tensor &input,
template
<
typename
Device
,
typename
T
>
void
Executor
<
Device
,
T
>::
SetInput
(
const
LoDTensor
&
input
,
const
std
::
string
&
var_name
)
{
auto
*
target_var
=
program_
.
scope
->
FindVar
(
var_name
);
PADDLE_MOBILE_ENFORCE
(
target_var
!=
nullptr
,
"Variable %s is not exist"
,
var_name
.
c_str
());
auto
*
target_tensor
=
target_var
->
template
GetMutable
<
LoDTensor
>();
if
(
config_
.
load_when_predict
)
{
if
(
target_tensor
->
IsInitialized
()
&&
target_tensor
->
dims
()
!=
input
.
dims
())
{
InitNoPersistableMemory
(
*
target_tensor
);
}
}
target_tensor
->
Resize
(
input
.
dims
());
target_tensor
->
ShareDataWith
(
input
);
target_tensor
->
set_lod
(
input
.
lod
());
...
...
src/framework/executor.h
浏览文件 @
47ff9c15
...
...
@@ -32,6 +32,8 @@ namespace framework {
template
<
typename
Device
,
typename
T
=
float
>
class
Executor
{
public:
Executor
(
const
Program
<
Device
>
&
program
,
paddle_mobile
::
PaddleMobileConfigInternal
config
,
int
batch_size
=
1
,
const
bool
use_optimize
=
true
,
const
bool
lod_mode
=
false
);
Executor
(
const
Program
<
Device
>
&
program
,
int
batch_size
=
1
,
const
bool
use_optimize
=
true
,
const
bool
lod_mode
=
false
);
...
...
@@ -60,10 +62,13 @@ class Executor {
protected:
Executor
()
=
default
;
bool
varInputMemory
(
const
std
::
shared_ptr
<
VarDesc
>
&
var_desc
,
Variable
*
var
,
LoDTensor
*
tensor
)
const
;
void
InitMemory
();
void
InitCombineMemory
();
void
InitNoPersistableMemory
(
const
LoDTensor
&
input_tensor
);
void
LoadMemory
(
void
**
data
,
const
std
::
shared_ptr
<
VarDesc
>
var_desc
,
LoDTensor
*
tensor
);
#ifdef PADDLE_MOBILE_CL
...
...
@@ -73,14 +78,18 @@ class Executor {
int
batch_size_
;
bool
use_optimize_
;
bool
lod_mode_
;
PaddleMobileConfigInternal
config_
=
PaddleMobileConfigInternal
();
Program
<
Device
>
program_
;
std
::
shared_ptr
<
ProgramDesc
>
program_desc_
;
typedef
std
::
shared_ptr
<
OperatorBase
<
Device
>>
OperatorBasePtr
;
std
::
vector
<
std
::
vector
<
OperatorBasePtr
>>
ops_of_block_
;
// operators list
std
::
vector
<
OperatorBasePtr
>
ops_list_
;
// for super resoltion
DDim
input_dim_
;
#ifdef PADDLE_MOBILE_PROFILE
struct
ProfInfo
{
int
tid
=
0
;
...
...
src/framework/loader.h
浏览文件 @
47ff9c15
...
...
@@ -25,6 +25,7 @@ namespace framework {
template
<
typename
Device
=
CPU
,
typename
T
=
float
>
class
Loader
{
public:
/*
* @b load separate format fluid model
* @b 加载分开存储的fluid模型
...
...
@@ -59,6 +60,7 @@ class Loader {
void
InitMemoryFromProgram
(
const
std
::
shared_ptr
<
ProgramDesc
>
&
originProgramDesc
,
const
std
::
shared_ptr
<
Scope
>
&
scope
);
};
}
// namespace framework
...
...
src/io/paddle_mobile.cpp
浏览文件 @
47ff9c15
...
...
@@ -42,7 +42,7 @@ PMStatus PaddleMobile<Device, T>::Load(const std::string &dirname,
if
(
executor_
.
get
()
==
nullptr
)
{
executor_
=
std
::
make_shared
<
framework
::
Executor
<
Device
,
T
>>
(
loader_
->
Load
(
dirname
,
optimize
,
quantification
),
batch_size
,
optimize
,
loader_
->
Load
(
dirname
,
optimize
,
quantification
),
config_
,
batch_size
,
optimize
,
loddable
);
}
else
{
LOG
(
kLOG_INFO
)
<<
"executor inited"
;
...
...
@@ -64,8 +64,7 @@ PMStatus PaddleMobile<Device, T>::Load(const std::string &model_path,
if
(
executor_
.
get
()
==
nullptr
)
{
executor_
=
std
::
make_shared
<
framework
::
Executor
<
Device
,
T
>>
(
loader_
->
Load
(
model_path
,
para_path
,
optimize
,
quantification
),
batch_size
,
optimize
,
loddable
);
loader_
->
Load
(
model_path
,
para_path
,
optimize
,
quantification
),
config_
,
batch_size
,
optimize
,
loddable
);
}
else
{
LOG
(
kLOG_INFO
)
<<
"executor inited"
;
}
...
...
@@ -87,7 +86,7 @@ bool PaddleMobile<Device, T>::LoadCombinedMemory(
executor_
=
std
::
make_shared
<
framework
::
Executor
<
Device
,
T
>>
(
loader_
->
LoadCombinedMemory
(
model_len
,
model_buf
,
combined_params_len
,
combined_params_buf
,
optimize
,
quantification
),
quantification
),
config_
,
batch_size
,
optimize
,
loddable
);
}
else
{
LOG
(
kLOG_INFO
)
<<
"executor inited"
;
...
...
src/io/paddle_mobile.h
浏览文件 @
47ff9c15
...
...
@@ -33,9 +33,18 @@ limitations under the License. */
namespace
paddle_mobile
{
template
<
typename
Device
,
typename
T
=
float
>
class
PaddleMobile
{
public:
PaddleMobile
(
PaddleMobileConfigInternal
config
)
:
config_
(
config
){
#ifndef PADDLE_MOBILE_CL
bool
is_gpu
=
std
::
is_same
<
DeviceType
<
kGPU_CL
>
,
Device
>::
value
;
PADDLE_MOBILE_ENFORCE
(
!
is_gpu
,
"Please recompile with GPU_CL is on"
);
#endif
}
PaddleMobile
()
{
#ifndef PADDLE_MOBILE_CL
bool
is_gpu
=
std
::
is_same
<
DeviceType
<
kGPU_CL
>
,
Device
>::
value
;
...
...
@@ -100,6 +109,7 @@ class PaddleMobile {
private:
std
::
shared_ptr
<
framework
::
Loader
<
Device
,
T
>>
loader_
;
std
::
shared_ptr
<
framework
::
Executor
<
Device
,
T
>>
executor_
;
PaddleMobileConfigInternal
config_
;
};
}
// namespace paddle_mobile
test/net/test_super.cpp
浏览文件 @
47ff9c15
...
...
@@ -18,7 +18,10 @@ limitations under the License. */
#include "../test_include.h"
int
main
()
{
paddle_mobile
::
PaddleMobile
<
paddle_mobile
::
CPU
>
paddle_mobile
;
paddle_mobile
::
PaddleMobileConfigInternal
config
;
config
.
load_when_predict
=
true
;
paddle_mobile
::
PaddleMobile
<
paddle_mobile
::
CPU
>
paddle_mobile
(
config
);
// paddle_mobile.SetThreadNum(4);
auto
time1
=
paddle_mobile
::
time
();
#ifdef PADDLE_MOBILE_CL
...
...
@@ -27,7 +30,7 @@ int main() {
auto
isok
=
paddle_mobile
.
Load
(
std
::
string
(
g_super
)
+
"/model"
,
std
::
string
(
g_super
)
+
"/params"
,
true
,
false
,
1
,
tru
e
);
1
,
fals
e
);
// auto isok = paddle_mobile.Load(std::string(g_mobilenet_mul), true);
if
(
isok
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录