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