Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleRec
提交
db3cc7a7
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看板
提交
db3cc7a7
编写于
7月 31, 2019
作者:
R
rensilin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
finish execute
Change-Id: I76a7a42deaab3f748d958d03c26d88e808f6c6d8
上级
28c049b8
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
79 addition
and
22 deletion
+79
-22
BCLOUD
BCLOUD
+2
-0
paddle/fluid/train/custom_trainer/feed/executor/executor.cc
paddle/fluid/train/custom_trainer/feed/executor/executor.cc
+39
-22
paddle/fluid/train/custom_trainer/feed/main.cc
paddle/fluid/train/custom_trainer/feed/main.cc
+1
-0
paddle/fluid/train/custom_trainer/feed/unit_test/main.cc
paddle/fluid/train/custom_trainer/feed/unit_test/main.cc
+1
-0
paddle/fluid/train/custom_trainer/feed/unit_test/test_executor.cc
...luid/train/custom_trainer/feed/unit_test/test_executor.cc
+36
-0
未找到文件。
BCLOUD
浏览文件 @
db3cc7a7
...
...
@@ -84,6 +84,8 @@ def UT_FILE(filename):
UT_DIR = 'paddle/fluid/train/custom_trainer/feed/unit_test'
return os.path.join(UT_DIR, filename)
CPPFLAGS('-DPYBIND_AVX_MKLML') # for paddle with avx and mklml
custom_trainer_src = GLOB('paddle/fluid/train/custom_trainer/feed/*/*.cc', Exclude(UT_FILE('*')))
Application('feed_trainer', Sources('paddle/fluid/train/custom_trainer/feed/main.cc', custom_trainer_src), CppFlags('-DHPPL_STUB_FUNC -DLAPACK_FOUND -DPADDLE_DISABLE_PROFILER -DPADDLE_NO_PYTHON -DCUSTOM_TRAINER -DPADDLE_ON_INFERENCE -DPADDLE_USE_DSO -DPADDLE_USE_PTHREAD_BARRIER -DPADDLE_USE_PTHREAD_SPINLOCK -DPADDLE_VERSION=0.0.0 -DPADDLE_WITH_AVX -DPADDLE_WITH_MKLML -DPADDLE_WITH_XBYAK -DXBYAK64 -DXBYAK_NO_OP_NAMES -D_GNU_SOURCE -D__STDC_LIMIT_MACROS -DPYBIND_AVX_MKLML' + r" -DPADDLE_REVISION=\"%s@%s@%s\"" % (REPO_URL(), REPO_BRANCH(), REPO_REVISION())), CFlags('-std=c++11 -m64 -fPIC -fno-omit-frame-pointer -Werror -Wall -Wextra -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wno-unused-parameter -Wno-unused-function -Wno-error=literal-suffix -Wno-error=sign-compare -Wno-error=unused-local-typedefs -Wno-error=maybe-uninitialized -fopenmp -mavx -O3 -DNDEBUG '), CxxFlags('-std=c++11 -m64 -fPIC -fno-omit-frame-pointer -Werror -Wall -Wextra -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wno-unused-parameter -Wno-unused-function -Wno-error=literal-suffix -Wno-error=sign-compare -Wno-error=unused-local-typedefs -Wno-error=maybe-uninitialized -fopenmp -mavx -O3 -DNDEBUG '), Libraries('$OUT/lib/libpaddle_fluid_avx_mklml.a'))
...
...
paddle/fluid/train/custom_trainer/feed/executor/executor.cc
浏览文件 @
db3cc7a7
...
...
@@ -5,6 +5,7 @@
#include "paddle/fluid/platform/init.h"
#include "paddle/fluid/platform/cpu_helper.h"
#include "paddle/fluid/inference/api/details/reset_tensor_array.h"
#include "paddle/fluid/platform/enforce.h"
namespace
paddle
{
...
...
@@ -16,7 +17,7 @@ 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
;
VLOG
(
2
)
<<
"Cannot open file "
<<
filename
;
return
-
1
;
}
fin
.
seekg
(
0
,
std
::
ios
::
end
);
...
...
@@ -73,37 +74,53 @@ int SimpleExecute::initialize(YAML::Node& exe_config,
if
(
!
exe_config
[
"startup_program"
]
||
!
exe_config
[
"main_program"
])
{
VLOG
(
4
)
<<
"fail to load config"
;
VLOG
(
2
)
<<
"fail to load config"
;
return
-
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
>
();
try
{
_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
(
2
)
<<
"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
(
2
)
<<
"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
);
}
catch
(
::
paddle
::
platform
::
EnforceNotMet
&
err
)
{
VLOG
(
2
)
<<
err
.
what
();
_context
.
reset
(
nullptr
);
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
();
if
(
_context
==
nullptr
)
{
VLOG
(
2
)
<<
"need initialize before run"
;
return
-
1
;
}
try
{
_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
();
}
catch
(
::
paddle
::
platform
::
EnforceNotMet
&
err
)
{
VLOG
(
2
)
<<
err
.
what
();
return
-
1
;
}
return
0
;
}
...
...
paddle/fluid/train/custom_trainer/feed/main.cc
浏览文件 @
db3cc7a7
...
...
@@ -5,6 +5,7 @@
#include "paddle/fluid/train/custom_trainer/feed/trainer_context.h"
#include "paddle/fluid/train/custom_trainer/feed/process/process.h"
#include "paddle/fluid/train/custom_trainer/feed/process/init_env_process.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/pybind/pybind.h"
using
namespace
paddle
::
custom_trainer
::
feed
;
...
...
paddle/fluid/train/custom_trainer/feed/unit_test/main.cc
浏览文件 @
db3cc7a7
#include <gtest/gtest.h>
#include <gflags/gflags.h>
#include <glog/logging.h>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/pybind/pybind.h"
int32_t
main
(
int32_t
argc
,
char
**
argv
)
{
...
...
paddle/fluid/train/custom_trainer/feed/unit_test/test_executor.cc
浏览文件 @
db3cc7a7
...
...
@@ -12,9 +12,11 @@ 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 <iostream>
#include <gtest/gtest.h>
#include "paddle/fluid/train/custom_trainer/feed/executor/executor.h"
#include "paddle/fluid/framework/tensor_util.h"
namespace
paddle
{
namespace
custom_trainer
{
...
...
@@ -31,12 +33,46 @@ TEST(testSimpleExecute, initialize) {
ASSERT_EQ
(
0
,
execute
.
initialize
(
config
,
context_ptr
));
}
float
uniform
(
float
min
,
float
max
)
{
float
result
=
(
float
)
rand
()
/
RAND_MAX
;
return
min
+
result
*
(
max
-
min
);
}
void
next_batch
(
int
batch_size
,
const
paddle
::
platform
::
Place
&
place
,
paddle
::
framework
::
LoDTensor
*
x_tensor
,
paddle
::
framework
::
LoDTensor
*
y_tensor
)
{
x_tensor
->
Resize
({
batch_size
,
2
});
auto
x_data
=
x_tensor
->
mutable_data
<
float
>
(
place
);
y_tensor
->
Resize
({
batch_size
,
1
});
auto
y_data
=
y_tensor
->
mutable_data
<
float
>
(
place
);
for
(
int
i
=
0
;
i
<
batch_size
;
++
i
)
{
x_data
[
i
*
2
]
=
uniform
(
-
2
,
2
);
x_data
[
i
*
2
+
1
]
=
uniform
(
-
2
,
2
);
float
dis
=
x_data
[
i
*
2
]
*
x_data
[
i
*
2
]
+
x_data
[
i
*
2
+
1
]
*
x_data
[
i
*
2
+
1
];
y_data
[
i
]
=
dis
<
1.0
?
1.0
:
0.0
;
}
}
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
));
auto
x_var
=
execute
.
mutable_var
<::
paddle
::
framework
::
LoDTensor
>
(
"x"
);
auto
y_var
=
execute
.
mutable_var
<::
paddle
::
framework
::
LoDTensor
>
(
"y"
);
ASSERT_NE
(
nullptr
,
x_var
);
ASSERT_NE
(
nullptr
,
y_var
);
next_batch
(
1024
,
context_ptr
->
cpu_place
,
x_var
,
y_var
);
ASSERT_EQ
(
0
,
execute
.
run
());
auto
loss_var
=
execute
.
var
<::
paddle
::
framework
::
LoDTensor
>
(
"loss"
);
auto
loss
=
loss_var
.
data
<
float
>
()[
0
];
std
::
cout
<<
"loss: "
<<
loss
<<
std
::
endl
;
}
}
// namespace feed
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录