Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
a4a9ce0e
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a4a9ce0e
编写于
11月 08, 2022
作者:
T
TeFeng Chen
提交者:
GitHub
11月 08, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix cinn_instruction_run_op_test when FLAGS_use_system_allocator=True (#47731)
上级
50c3632f
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
70 addition
and
76 deletion
+70
-76
paddle/fluid/operators/cinn/cinn_instruction_run_op_test.cc
paddle/fluid/operators/cinn/cinn_instruction_run_op_test.cc
+66
-73
paddle/fluid/operators/cinn/cinn_launch_op_test.cc
paddle/fluid/operators/cinn/cinn_launch_op_test.cc
+1
-1
paddle/fluid/operators/cinn/test_helper.h
paddle/fluid/operators/cinn/test_helper.h
+3
-2
未找到文件。
paddle/fluid/operators/cinn/cinn_instruction_run_op_test.cc
浏览文件 @
a4a9ce0e
...
@@ -39,36 +39,45 @@ namespace paddle::operators {
...
@@ -39,36 +39,45 @@ namespace paddle::operators {
using
framework
::
paddle2cinn
::
CinnCompiler
;
using
framework
::
paddle2cinn
::
CinnCompiler
;
TEST
(
CinnInstructionOpTest
,
TestWithElementwiseAdd
)
{
class
TestCinnInstructionRunOp
:
public
::
testing
::
Test
{
paddle
::
framework
::
InitDevices
();
public:
platform
::
SetNumThreads
(
1
);
const
char
*
test_op_out_name
=
"test_op_out"
;
// cache test graph into CinnCompiler
const
char
*
add_op_out_name
=
"add_op_out"
;
const
std
::
string
&
test_op_out_name
=
"cinn_instruction_run_op_out"
;
std
::
unique_ptr
<
framework
::
OperatorBase
>
cinn_launch_op
;
const
std
::
string
&
add_op_out_name
=
"add_op_out"
;
std
::
unique_ptr
<
framework
::
OperatorBase
>
cinn_instruction_run_op
;
std
::
unique_ptr
<
framework
::
OperatorBase
>
elementwise_add_op
;
void
SetUp
()
override
{
auto
compilation_key
=
CinnCompiler
::
GetInstance
()
->
AddGraph
(
auto
compilation_key
=
CinnCompiler
::
GetInstance
()
->
AddGraph
(
CreateOnlyElementwiseAddGraph
(
"x"
,
"y"
,
test_op_out_name
));
CreateOnlyElementwiseAddGraph
(
"x"
,
"y"
,
test_op_out_name
));
// create necessary ops
// create necessary ops
auto
cinn_instruction_run_op
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
cinn_launch_op
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
"cinn_instruction_run"
,
{{
"X"
,
{
"x"
,
"y"
}}},
{{
"Out"
,
{
test_op_out_name
}}},
{{
"cached_index"
,
0
},
{
"instruction_index"
,
0
}});
auto
cinn_launch_op
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
"cinn_launch"
,
"cinn_launch"
,
{{
"X"
,
{
"x"
,
"y"
}}},
{{
"X"
,
{
"x"
,
"y"
}}},
{{
"Out"
,
{
test_op_out_name
}}},
{{
"Out"
,
{
test_op_out_name
}}},
{{
"compilation_key"
,
compilation_key
}});
{{
"compilation_key"
,
compilation_key
}});
cinn_instruction_run_op
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
"cinn_instruction_run"
,
{{
"X"
,
{
"x"
,
"y"
}}},
{{
"Out"
,
{
test_op_out_name
}}},
{{
"cached_index"
,
0
},
{
"instruction_index"
,
0
}});
elementwise_add_op
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
"elementwise_add"
,
{{
"X"
,
{
"x"
}},
{
"Y"
,
{
"y"
}}},
{{
"Out"
,
{
add_op_out_name
}}},
{{}});
}
void
Compile
(
const
platform
::
Place
&
place
)
{
// check case: a compiled object not cached before cinn_launch_op run,
// check case: a compiled object not cached before cinn_launch_op run,
// so a cinn_instruction_run_op will throw an error
// so a cinn_instruction_run_op will throw an error
framework
::
Scope
scope
;
framework
::
Scope
scope
;
platform
::
CPUPlace
place
;
InitVariablesWithRandomValue
<
float
>
({
"x"
,
"y"
},
{
10
,
20
},
place
,
&
scope
);
InitVariablesWithRandomValue
<
float
>
({
"x"
,
"y"
},
{
10
,
20
},
place
,
&
scope
);
scope
.
Var
(
test_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
scope
.
Var
(
test_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
ASSERT_THROW
(
cinn_instruction_run_op
->
Run
(
scope
,
place
),
ASSERT_THROW
(
cinn_instruction_run_op
->
Run
(
scope
,
place
),
paddle
::
platform
::
EnforceNotMet
);
paddle
::
platform
::
EnforceNotMet
);
// run cinn_launch_op firstly to launch the compilation
// run cinn_launch_op firstly to launch the compilation
// of the above graph and cache two compiled results
// of the above graph and cache two compiled results
// of both type float and int
// of both type float and int
...
@@ -77,54 +86,38 @@ TEST(CinnInstructionOpTest, TestWithElementwiseAdd) {
...
@@ -77,54 +86,38 @@ TEST(CinnInstructionOpTest, TestWithElementwiseAdd) {
scope
.
Var
(
test_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
scope
.
Var
(
test_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
InitVariablesWithRandomValue
<
int
>
({
"x"
,
"y"
},
{
30
,
40
},
place
,
&
scope
);
InitVariablesWithRandomValue
<
int
>
({
"x"
,
"y"
},
{
30
,
40
},
place
,
&
scope
);
cinn_launch_op
->
Run
(
scope
,
place
);
cinn_launch_op
->
Run
(
scope
,
place
);
}
void
RunAndCheck
(
const
platform
::
Place
&
place
)
{
// Run ops and check the computation results
// Run ops and check the computation results
auto
run_and_check_fn
=
[
&
](
const
platform
::
Place
&
place
)
{
framework
::
Scope
scope
;
framework
::
Scope
scope
;
InitVariablesWithRandomValue
<
float
>
({
"x"
,
"y"
},
{
10
,
20
},
place
,
&
scope
);
scope
.
Var
(
test_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
scope
.
Var
(
test_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
scope
.
Var
(
add_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
scope
.
Var
(
add_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
auto
elementwise_add_op
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
"elementwise_add"
,
{{
"X"
,
{
"x"
}},
{
"Y"
,
{
"y"
}}},
{{
"Out"
,
{
add_op_out_name
}}},
{{}});
// 1. check on type float
InitVariablesWithRandomValue
<
float
>
({
"x"
,
"y"
},
{
10
,
20
},
place
,
&
scope
);
cinn_instruction_run_op
->
SetAttr
(
"cached_index"
,
0
);
cinn_instruction_run_op
->
Run
(
scope
,
place
);
elementwise_add_op
->
Run
(
scope
,
place
);
elementwise_add_op
->
Run
(
scope
,
place
);
cinn_launch_op
->
Run
(
scope
,
place
);
CompareOpResult
<
float
>
(
scope
.
GetVar
(
test_op_out_name
),
CompareOpResult
<
float
>
(
scope
.
GetVar
(
test_op_out_name
),
scope
.
GetVar
(
add_op_out_name
));
scope
.
GetVar
(
add_op_out_name
));
}
// 2. check on type int to indicate cinn_instruction_run op
void
TearDown
()
override
{
CinnCompiler
::
GetInstance
()
->
Clear
();
}
// can mutable data according compiled result
};
scope
.
EraseVars
({
"x"
,
"y"
,
test_op_out_name
,
add_op_out_name
});
scope
.
Var
(
test_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
scope
.
Var
(
add_op_out_name
)
->
GetMutable
<
LoDTensor
>
();
InitVariablesWithRandomValue
<
int
>
({
"x"
,
"y"
},
{
30
,
40
},
place
,
&
scope
);
TEST_F
(
TestCinnInstructionRunOp
,
CPU
)
{
cinn_instruction_run_op
->
SetAttr
(
"cached_index"
,
1
);
platform
::
CPUPlace
place
;
cinn_instruction_run_op
->
Run
(
scope
,
place
);
Compile
(
place
);
// need reconstruct elementwise_add_op to choose a new kernel with type int
RunAndCheck
(
place
);
elementwise_add_op
=
// the second run on the same place is to check the cache logic
paddle
::
framework
::
OpRegistry
::
CreateOp
(
"elementwise_add"
,
RunAndCheck
(
place
);
{{
"X"
,
{
"x"
}},
{
"Y"
,
{
"y"
}}},
}
{{
"Out"
,
{
add_op_out_name
}}},
{{}});
elementwise_add_op
->
Run
(
scope
,
place
);
CompareOpResult
<
int
>
(
scope
.
GetVar
(
test_op_out_name
),
scope
.
GetVar
(
add_op_out_name
));
};
// CPU
run_and_check_fn
(
platform
::
CPUPlace
());
run_and_check_fn
(
platform
::
CPUPlace
());
#ifdef PADDLE_WITH_CUDA
#ifdef PADDLE_WITH_CUDA
// GPU
TEST_F
(
TestCinnInstructionRunOp
,
GPU
)
{
run_and_check_fn
(
platform
::
CUDAPlace
());
platform
::
CUDAPlace
place
;
run_and_check_fn
(
platform
::
CUDAPlace
());
Compile
(
place
);
#endif
RunAndCheck
(
place
);
RunAndCheck
(
place
);
}
}
#endif
}
// namespace paddle::operators
}
// namespace paddle::operators
paddle/fluid/operators/cinn/cinn_launch_op_test.cc
浏览文件 @
a4a9ce0e
...
@@ -49,7 +49,7 @@ using framework::paddle2cinn::CinnCompiler;
...
@@ -49,7 +49,7 @@ using framework::paddle2cinn::CinnCompiler;
class
TestCinnLaunchOp
:
public
::
testing
::
Test
{
class
TestCinnLaunchOp
:
public
::
testing
::
Test
{
public:
public:
const
char
*
test_op_out_name
=
"
add
_op_out"
;
const
char
*
test_op_out_name
=
"
test
_op_out"
;
const
char
*
add_op_out_name
=
"add_op_out"
;
const
char
*
add_op_out_name
=
"add_op_out"
;
std
::
unique_ptr
<
framework
::
OperatorBase
>
cinn_launch_op
;
std
::
unique_ptr
<
framework
::
OperatorBase
>
cinn_launch_op
;
std
::
unique_ptr
<
framework
::
OperatorBase
>
elementwise_add_op
;
std
::
unique_ptr
<
framework
::
OperatorBase
>
elementwise_add_op
;
...
...
paddle/fluid/operators/cinn/test_helper.h
浏览文件 @
a4a9ce0e
...
@@ -102,6 +102,7 @@ void InitVariablesWithRandomValue(const std::vector<std::string>& var_names,
...
@@ -102,6 +102,7 @@ void InitVariablesWithRandomValue(const std::vector<std::string>& var_names,
tmp_tensor
.
mutable_data
<
DataType
>
(
common_ddim
,
platform
::
CPUPlace
());
tmp_tensor
.
mutable_data
<
DataType
>
(
common_ddim
,
platform
::
CPUPlace
());
for
(
const
auto
&
var_name
:
var_names
)
{
for
(
const
auto
&
var_name
:
var_names
)
{
auto
*
tensor
=
scope
->
Var
(
var_name
)
->
GetMutable
<
LoDTensor
>
();
auto
*
tensor
=
scope
->
Var
(
var_name
)
->
GetMutable
<
LoDTensor
>
();
tensor
->
mutable_data
<
DataType
>
(
common_ddim
,
place
);
for
(
auto
i
=
0
;
i
<
tensor
->
numel
();
++
i
)
{
for
(
auto
i
=
0
;
i
<
tensor
->
numel
();
++
i
)
{
tmp_data
[
i
]
=
static_cast
<
DataType
>
(
dist
(
engine
));
tmp_data
[
i
]
=
static_cast
<
DataType
>
(
dist
(
engine
));
}
}
...
@@ -121,9 +122,9 @@ void CompareOpResult(Variable* test_out, Variable* expected_out) {
...
@@ -121,9 +122,9 @@ void CompareOpResult(Variable* test_out, Variable* expected_out) {
ASSERT_TRUE
(
expected_tensor
.
IsInitialized
());
ASSERT_TRUE
(
expected_tensor
.
IsInitialized
());
ASSERT_EQ
(
test_tensor
.
dims
(),
expected_tensor
.
dims
());
ASSERT_EQ
(
test_tensor
.
dims
(),
expected_tensor
.
dims
());
const
auto
*
test_data
=
test_tensor
.
data
<
DataType
>
();
const
auto
*
test_data
=
test_tensor
.
data
<
DataType
>
();
const
auto
*
ex
cep
ted_data
=
expected_tensor
.
data
<
DataType
>
();
const
auto
*
ex
pec
ted_data
=
expected_tensor
.
data
<
DataType
>
();
for
(
auto
i
=
0
;
i
<
expected_tensor
.
numel
();
++
i
)
{
for
(
auto
i
=
0
;
i
<
expected_tensor
.
numel
();
++
i
)
{
EXPECT_EQ
(
test_data
[
i
],
ex
cep
ted_data
[
i
]);
EXPECT_EQ
(
test_data
[
i
],
ex
pec
ted_data
[
i
]);
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录