Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleRec
提交
4f3eba94
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看板
提交
4f3eba94
编写于
8月 01, 2019
作者:
R
rensilin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add_executor_ut
Change-Id: Ibc3221bdb90032663e7078e63a839dbde4c119d1
上级
8699c196
变更
2
展开全部
显示空白变更内容
内联
并排
Showing
2 changed file
with
86 addition
and
10 deletion
+86
-10
BCLOUD
BCLOUD
+1
-1
paddle/fluid/train/custom_trainer/feed/unit_test/test_executor.cc
...luid/train/custom_trainer/feed/unit_test/test_executor.cc
+85
-9
未找到文件。
BCLOUD
浏览文件 @
4f3eba94
此差异已折叠。
点击以展开。
paddle/fluid/train/custom_trainer/feed/unit_test/test_executor.cc
浏览文件 @
4f3eba94
...
...
@@ -13,26 +13,90 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include <iostream>
#include <fstream>
#include <gtest/gtest.h>
#include "paddle/fluid/train/custom_trainer/feed/executor/executor.h"
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/io/fs.h"
namespace
paddle
{
namespace
custom_trainer
{
namespace
feed
{
<<<<<<<
HEAD
TEST
(
testSimpleExecutor
,
initialize
)
{
SimpleExecutor
execute
;
auto
context_ptr
=
std
::
make_shared
<
TrainerContext
>
();
=======
const
char
test_data_dir
[]
=
"test_data"
;
const
char
main_program_path
[]
=
"test_data/main_program"
;
const
char
startup_program_path
[]
=
"test_data/startup_program"
;
class
SimpleExecuteTest
:
public
testing
::
Test
{
public:
static
void
SetUpTestCase
()
{
::
paddle
::
framework
::
localfs_mkdir
(
test_data_dir
);
{
std
::
unique_ptr
<
paddle
::
framework
::
ProgramDesc
>
startup_program
(
new
paddle
::
framework
::
ProgramDesc
());
std
::
ofstream
fout
(
startup_program_path
,
std
::
ios
::
out
|
std
::
ios
::
binary
);
ASSERT_TRUE
(
fout
);
fout
<<
startup_program
->
Proto
()
->
SerializeAsString
();
fout
.
close
();
}
{
std
::
unique_ptr
<
paddle
::
framework
::
ProgramDesc
>
main_program
(
new
paddle
::
framework
::
ProgramDesc
());
auto
load_block
=
main_program
->
MutableBlock
(
0
);
framework
::
OpDesc
*
op
=
load_block
->
AppendOp
();
op
->
SetType
(
"mean"
);
op
->
SetInput
(
"X"
,
{
"x"
});
op
->
SetOutput
(
"Out"
,
{
"mean"
});
op
->
CheckAttrs
();
std
::
ofstream
fout
(
main_program_path
,
std
::
ios
::
out
|
std
::
ios
::
binary
);
ASSERT_TRUE
(
fout
);
fout
<<
main_program
->
Proto
()
->
SerializeAsString
();
fout
.
close
();
}
}
static
void
TearDownTestCase
()
{
::
paddle
::
framework
::
localfs_remove
(
test_data_dir
);
}
virtual
void
SetUp
()
{
context_ptr
.
reset
(
new
TrainerContext
());
}
virtual
void
TearDown
()
{
context_ptr
=
nullptr
;
}
std
::
shared_ptr
<
TrainerContext
>
context_ptr
;
};
TEST_F
(
SimpleExecuteTest
,
initialize
)
{
SimpleExecute
execute
;
>>>>>>>
add_executor_ut
YAML
::
Node
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
}"
);
config
=
YAML
::
Load
(
std
::
string
()
+
"{startup_program: "
+
startup_program_path
+
", main_program: "
+
main_program_path
+
"
}"
);
ASSERT_EQ
(
0
,
execute
.
initialize
(
config
,
context_ptr
));
config
=
YAML
::
Load
(
"{thread_num: 2, startup_program: ./data/startup_program, main_program: ./data/main_program
}"
);
config
=
YAML
::
Load
(
std
::
string
()
+
"{thread_num: 2, startup_program: "
+
startup_program_path
+
", main_program: "
+
main_program_path
+
"
}"
);
ASSERT_EQ
(
0
,
execute
.
initialize
(
config
,
context_ptr
));
}
<<<<<<<
HEAD
float
uniform
(
float
min
,
float
max
)
{
float
result
=
(
float
)
rand
()
/
RAND_MAX
;
return
min
+
result
*
(
max
-
min
);
...
...
@@ -58,21 +122,33 @@ TEST(testSimpleExecutor, run) {
SimpleExecutor
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}"
);
=======
TEST_F
(
SimpleExecuteTest
,
run
)
{
SimpleExecute
execute
;
auto
config
=
YAML
::
Load
(
std
::
string
()
+
"{thread_num: 2, startup_program: "
+
startup_program_path
+
", main_program: "
+
main_program_path
+
"}"
);
>>>>>>>
add_executor_ut
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
"
);
execute
.
mutable_var
<::
paddle
::
framework
::
LoDTensor
>
(
"mean
"
);
ASSERT_NE
(
nullptr
,
x_var
);
ASSERT_NE
(
nullptr
,
y_var
);
next_batch
(
1024
,
context_ptr
->
cpu_place
,
x_var
,
y_var
);
int
x_len
=
10
;
x_var
->
Resize
({
1
,
x_len
});
auto
x_data
=
x_var
->
mutable_data
<
float
>
(
context_ptr
->
cpu_place
);
std
::
cout
<<
"x: "
;
for
(
int
i
=
0
;
i
<
x_len
;
++
i
)
{
x_data
[
i
]
=
i
;
std
::
cout
<<
i
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
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
;
auto
mean_var
=
execute
.
var
<::
paddle
::
framework
::
LoDTensor
>
(
"mean"
);
auto
mean
=
mean_var
.
data
<
float
>
()[
0
];
std
::
cout
<<
"mean: "
<<
mean
<<
std
::
endl
;
ASSERT_NEAR
(
4.5
,
mean
,
1e-9
);
}
}
// namespace feed
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录