Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleRec
提交
d8260b87
P
PaddleRec
项目概览
PaddlePaddle
/
PaddleRec
通知
68
Star
12
Fork
5
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
27
列表
看板
标记
里程碑
合并请求
10
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleRec
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
27
Issue
27
列表
看板
标记
里程碑
合并请求
10
合并请求
10
Pages
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d8260b87
编写于
7月 31, 2019
作者:
R
rensilin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
execute
Change-Id: I316472bb3c9a2c9334876f3e4e6e9869aa4c3252
上级
29aec8e0
变更
5
展开全部
显示空白变更内容
内联
并排
Showing
5 changed file
with
188 addition
and
10 deletion
+188
-10
BCLOUD
BCLOUD
+18
-1
paddle/fluid/train/custom_trainer/feed/executor/executor.cc
paddle/fluid/train/custom_trainer/feed/executor/executor.cc
+106
-0
paddle/fluid/train/custom_trainer/feed/executor/executor.h
paddle/fluid/train/custom_trainer/feed/executor/executor.h
+10
-9
paddle/fluid/train/custom_trainer/feed/unit_test/main.cc
paddle/fluid/train/custom_trainer/feed/unit_test/main.cc
+10
-0
paddle/fluid/train/custom_trainer/feed/unit_test/test_executor.cc
...luid/train/custom_trainer/feed/unit_test/test_executor.cc
+44
-0
未找到文件。
BCLOUD
浏览文件 @
d8260b87
此差异已折叠。
点击以展开。
paddle/fluid/train/custom_trainer/feed/executor/executor.cc
0 → 100644
浏览文件 @
d8260b87
#include "paddle/fluid/train/custom_trainer/feed/executor/executor.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/platform/init.h"
#include "paddle/fluid/platform/cpu_helper.h"
#include "paddle/fluid/inference/api/details/reset_tensor_array.h"
namespace
paddle
{
namespace
custom_trainer
{
namespace
feed
{
namespace
{
int
ReadBinaryFile
(
const
std
::
string
&
filename
,
std
::
string
*
contents
)
{
std
::
ifstream
fin
(
filename
,
std
::
ios
::
in
|
std
::
ios
::
binary
);
if
(
!
fin
)
{
VLOG
(
4
)
<<
"Cannot open file "
<<
filename
;
return
-
1
;
}
fin
.
seekg
(
0
,
std
::
ios
::
end
);
contents
->
clear
();
contents
->
resize
(
fin
.
tellg
());
fin
.
seekg
(
0
,
std
::
ios
::
beg
);
fin
.
read
(
&
(
contents
->
at
(
0
)),
contents
->
size
());
fin
.
close
();
return
0
;
}
std
::
unique_ptr
<
paddle
::
framework
::
ProgramDesc
>
Load
(
paddle
::
framework
::
Executor
*
/*executor*/
,
const
std
::
string
&
model_filename
)
{
VLOG
(
3
)
<<
"loading model from "
<<
model_filename
;
std
::
string
program_desc_str
;
if
(
ReadBinaryFile
(
model_filename
,
&
program_desc_str
)
!=
0
)
{
return
nullptr
;
}
std
::
unique_ptr
<
paddle
::
framework
::
ProgramDesc
>
main_program
(
new
paddle
::
framework
::
ProgramDesc
(
program_desc_str
));
return
main_program
;
}
}
struct
SimpleExecute
::
Context
{
Context
(
const
::
paddle
::
platform
::
Place
&
place
)
:
place
(
place
),
executor
(
place
)
{
}
const
::
paddle
::
platform
::
Place
&
place
;
::
paddle
::
framework
::
Executor
executor
;
::
std
::
unique_ptr
<::
paddle
::
framework
::
ProgramDesc
>
main_program
;
::
std
::
unique_ptr
<
framework
::
ExecutorPrepareContext
>
prepare_context
;
details
::
TensorArrayBatchCleaner
tensor_array_batch_cleaner
;
};
SimpleExecute
::
SimpleExecute
()
{
}
SimpleExecute
::~
SimpleExecute
()
{
}
int
SimpleExecute
::
initialize
(
YAML
::
Node
&
exe_config
,
std
::
shared_ptr
<
TrainerContext
>
context_ptr
)
{
paddle
::
framework
::
InitDevices
(
false
);
if
(
exe_config
[
"num_threads"
])
{
paddle
::
platform
::
SetNumThreads
(
exe_config
[
"num_threads"
].
as
<
int
>
());
}
else
{
paddle
::
platform
::
SetNumThreads
(
1
);
}
_context
.
reset
(
new
SimpleExecute
::
Context
(
context_ptr
->
cpu_place
));
auto
startup_program
=
Load
(
&
_context
->
executor
,
exe_config
[
"startup_program"
].
as
<
std
::
string
>
());
if
(
startup_program
==
nullptr
)
{
VLOG
(
4
)
<<
"fail to load startup_program: "
<<
exe_config
[
"startup_program"
].
as
<
std
::
string
>
();
return
-
1
;
}
_context
->
executor
.
Run
(
*
startup_program
,
this
->
scope
(),
0
,
false
,
true
);
_context
->
main_program
=
Load
(
&
_context
->
executor
,
exe_config
[
"main_program"
].
as
<
std
::
string
>
());
if
(
_context
->
main_program
==
nullptr
)
{
VLOG
(
4
)
<<
"fail to load main_program: "
<<
exe_config
[
"main_program"
].
as
<
std
::
string
>
();
return
-
1
;
}
_context
->
prepare_context
=
_context
->
executor
.
Prepare
(
*
_context
->
main_program
,
0
);
_context
->
executor
.
CreateVariables
(
*
_context
->
main_program
,
this
->
scope
(),
0
);
return
0
;
}
int
SimpleExecute
::
run
()
{
_context
->
executor
.
RunPreparedContext
(
_context
->
prepare_context
.
get
(),
this
->
scope
(),
false
,
/* don't create local scope each time*/
false
/* don't create variable each time */
);
// For some other vector like containers not cleaned after each batch.
_context
->
tensor_array_batch_cleaner
.
CollectNoTensorVars
(
this
->
scope
());
_context
->
tensor_array_batch_cleaner
.
ResetNoTensorVars
();
return
0
;
}
}
// namespace feed
}
// namespace custom_trainer
}
// namespace paddle
paddle/fluid/train/custom_trainer/feed/executor/executor.h
浏览文件 @
d8260b87
#pragma once
#include <functional>
#include "paddle/fluid/framework/
executor
.h"
#include "paddle/fluid/framework/
scope
.h"
#include "paddle/fluid/train/custom_trainer/feed/common/registerer.h"
#include "paddle/fluid/train/custom_trainer/feed/trainer_context.h"
...
...
@@ -23,16 +23,16 @@ public:
}
//直接取var
template
<
class
T
>
T
*
var
(
const
std
::
string
&
name
)
{
return
_scope
.
Var
(
name
)
.
Get
<
T
>
();
const
T
&
var
(
const
std
::
string
&
name
)
{
return
_scope
.
Var
(
name
)
->
Get
<
T
>
();
}
template
<
class
T
>
T
*
mutable_var
(
const
std
::
string
&
name
)
{
return
_scope
.
Var
(
name
)
->
GetMutable
<
T
>
();
}
//执行
n轮训练,每轮回调(epoch_id, _scope)
virtual
int
run
(
uint32_t
epoch_num
,
std
::
function
<
void
(
uint32_t
,
::
paddle
::
framework
::
Scope
*
)
>
)
=
0
;
//执行
训练
virtual
int
run
()
=
0
;
protected:
::
paddle
::
framework
::
Scope
_scope
;
...
...
@@ -41,13 +41,14 @@ REGISTER_REGISTERER(Execute);
class
SimpleExecute
:
public
Execute
{
public:
SimpleExecute
()
{}
virtual
~
SimpleExecute
()
{}
SimpleExecute
()
;
virtual
~
SimpleExecute
()
;
virtual
int
initialize
(
YAML
::
Node
&
exe_config
,
std
::
shared_ptr
<
TrainerContext
>
context_ptr
);
virtual
int
run
(
uint32_t
epoch_num
,
std
::
function
<
void
(
uint32_t
,
::
paddle
::
framework
::
Scope
*
)
>
)
=
0
;
virtual
int
run
(
)
;
protected:
::
paddle
::
framework
::
Executor
_execute
;
struct
Context
;
std
::
unique_ptr
<
Context
>
_context
;
};
}
// namespace feed
...
...
paddle/fluid/train/custom_trainer/feed/unit_test/main.cc
0 → 100644
浏览文件 @
d8260b87
#include <gtest/gtest.h>
#include <gflags/gflags.h>
#include <glog/logging.h>
int32_t
main
(
int32_t
argc
,
char
**
argv
)
{
::
testing
::
InitGoogleTest
(
&
argc
,
argv
);
::
google
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
google
::
InitGoogleLogging
(
"paddle_trainer"
);
return
RUN_ALL_TESTS
();
}
paddle/fluid/train/custom_trainer/feed/unit_test/test_executor.cc
0 → 100644
浏览文件 @
d8260b87
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <gtest/gtest.h>
#include "paddle/fluid/train/custom_trainer/feed/executor/executor.h"
namespace
paddle
{
namespace
custom_trainer
{
namespace
feed
{
TEST
(
testSimpleExecute
,
initialize
)
{
SimpleExecute
execute
;
auto
context_ptr
=
std
::
make_shared
<
TrainerContext
>
();
auto
config
=
YAML
::
Load
(
"[1, 2, 3]"
);
ASSERT_NE
(
0
,
execute
.
initialize
(
config
,
context_ptr
));
config
=
YAML
::
Load
(
"{startup_program: ./data/startup_program, main_program: ./data/main_program}"
);
ASSERT_EQ
(
0
,
execute
.
initialize
(
config
,
context_ptr
));
config
=
YAML
::
Load
(
"{thread_num: 2, startup_program: ./data/startup_program, main_program: ./data/main_program}"
);
ASSERT_EQ
(
0
,
execute
.
initialize
(
config
,
context_ptr
));
}
TEST
(
testSimpleExecute
,
run
)
{
SimpleExecute
execute
;
auto
context_ptr
=
std
::
make_shared
<
TrainerContext
>
();
auto
config
=
YAML
::
Load
(
"{thread_num: 2, startup_program: ./data/startup_program, main_program: ./data/main_program}"
);
ASSERT_EQ
(
0
,
execute
.
initialize
(
config
,
context_ptr
));
ASSERT_EQ
(
0
,
execute
.
run
());
}
}
// namespace feed
}
// namespace custom_trainer
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录