Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
024706f9
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
024706f9
编写于
4月 01, 2020
作者:
Y
YuJianfeng
提交者:
高东海
4月 08, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize depend edge with make tuple input
上级
948ff63a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
116 addition
and
19 deletion
+116
-19
mindspore/ccsrc/pre_activate/pass/optimize_dependence.cc
mindspore/ccsrc/pre_activate/pass/optimize_dependence.cc
+74
-19
tests/ut/cpp/pre_activate/pass/optimize_dependence_test.cc
tests/ut/cpp/pre_activate/pass/optimize_dependence_test.cc
+20
-0
tests/ut/cpp/python_input/gtest_input/pre_activate/optimize_dependence_test.py
...nput/gtest_input/pre_activate/optimize_dependence_test.py
+22
-0
未找到文件。
mindspore/ccsrc/pre_activate/pass/optimize_dependence.cc
浏览文件 @
024706f9
...
...
@@ -27,6 +27,69 @@
namespace
mindspore
{
namespace
opt
{
constexpr
auto
kSingleInputIndex
=
1
;
namespace
{
AnfNodePtr
GetReplaceNode
(
const
FuncGraphPtr
&
func_graph
,
const
AnfNodePtr
&
node
)
{
MS_EXCEPTION_IF_NULL
(
func_graph
);
MS_EXCEPTION_IF_NULL
(
node
);
if
(
!
node
->
isa
<
CNode
>
())
{
return
nullptr
;
}
auto
cnode
=
node
->
cast
<
CNodePtr
>
();
MS_EXCEPTION_IF_NULL
(
cnode
);
string
op_name
=
AnfAlgo
::
GetCNodeName
(
cnode
);
// Currently we only eliminate transdata or cast nodes.
if
(
op_name
!=
kTransDataOpName
&&
op_name
!=
prim
::
kPrimCast
->
name
())
{
return
nullptr
;
}
auto
manager
=
func_graph
->
manager
();
MS_EXCEPTION_IF_NULL
(
manager
);
// Check whether the node has only one output node.
if
(
manager
->
node_users
().
find
(
cnode
)
==
manager
->
node_users
().
end
())
{
MS_LOG
(
EXCEPTION
)
<<
"The node should be used by at least another node's input"
;
}
if
(
manager
->
node_users
()[
cnode
].
size
()
>
1
)
{
return
nullptr
;
}
CheckCNodeInputSize
(
cnode
,
kSingleInputIndex
+
1
);
return
cnode
->
input
(
kSingleInputIndex
);
}
bool
ReplaceMakeTuple
(
const
FuncGraphPtr
&
func_graph
,
const
CNodePtr
&
cnode
)
{
MS_EXCEPTION_IF_NULL
(
func_graph
);
MS_EXCEPTION_IF_NULL
(
cnode
);
if
(
AnfAlgo
::
GetCNodeName
(
cnode
)
!=
prim
::
kPrimMakeTuple
->
name
())
{
return
false
;
}
std
::
vector
<
AnfNodePtr
>
new_make_tuple_inputs
;
bool
need_update
=
false
;
for
(
const
auto
&
input
:
cnode
->
inputs
())
{
AnfNodePtr
replace_input
=
GetReplaceNode
(
func_graph
,
input
);
// If replace input is not null, it will be the input of the TransData or Cast.
if
(
replace_input
==
nullptr
)
{
new_make_tuple_inputs
.
push_back
(
input
);
continue
;
}
new_make_tuple_inputs
.
push_back
(
replace_input
);
need_update
=
true
;
}
if
(
need_update
)
{
auto
kernel_graph
=
func_graph
->
cast
<
std
::
shared_ptr
<
session
::
KernelGraph
>>
();
CNodePtr
new_make_tuple
=
nullptr
;
if
(
kernel_graph
==
nullptr
)
{
new_make_tuple
=
func_graph
->
NewCNode
(
new_make_tuple_inputs
);
}
else
{
new_make_tuple
=
kernel_graph
->
NewCNode
(
cnode
);
}
MS_EXCEPTION_IF_NULL
(
new_make_tuple
);
new_make_tuple
->
set_inputs
(
new_make_tuple_inputs
);
auto
manager
=
func_graph
->
manager
();
MS_EXCEPTION_IF_NULL
(
manager
);
manager
->
Replace
(
cnode
,
new_make_tuple
);
}
return
true
;
}
}
// namespace
const
BaseRef
OptimizeDependence
::
DefinePattern
()
const
{
VarPtr
X
=
std
::
make_shared
<
Var
>
(
"X"
);
MS_EXCEPTION_IF_NULL
(
X
);
...
...
@@ -43,9 +106,8 @@ const AnfNodePtr OptimizeDependence::Process(const FuncGraphPtr &func_graph, con
return
nullptr
;
}
auto
depend_cnode
=
node
->
cast
<
CNodePtr
>
();
if
(
depend_cnode
->
inputs
().
size
()
<
kDependInputNum
)
{
return
nullptr
;
}
MS_EXCEPTION_IF_NULL
(
depend_cnode
);
CheckCNodeInputSize
(
depend_cnode
,
kDependInputNum
);
auto
replacing_node
=
depend_cnode
->
input
(
kDependInputNum
-
1
);
MS_EXCEPTION_IF_NULL
(
replacing_node
);
if
(
!
replacing_node
->
isa
<
CNode
>
())
{
...
...
@@ -53,36 +115,29 @@ const AnfNodePtr OptimizeDependence::Process(const FuncGraphPtr &func_graph, con
}
auto
replacing_cnode
=
replacing_node
->
cast
<
CNodePtr
>
();
MS_EXCEPTION_IF_NULL
(
replacing_cnode
);
// Currently we only optimize transdata or cast nodes.
string
replacing_cnode_op_name
=
AnfAlgo
::
GetCNodeName
(
replacing_cnode
);
if
(
replacing_cnode_op_name
!=
kTransDataOpName
&&
replacing_cnode_op_name
!=
prim
::
kPrimCast
->
name
())
{
// Deal with the make_tuple with TransData or Cast inputs.
if
(
ReplaceMakeTuple
(
func_graph
,
replacing_cnode
))
{
return
nullptr
;
}
auto
manager
=
func_graph
->
manager
();
MS_EXCEPTION_IF_NULL
(
manager
);
// Check whether the replacing node has only one input and one output.
if
(
replacing_cnode
->
inputs
().
size
()
!=
kSingleInputIndex
+
1
)
{
return
nullptr
;
}
if
(
manager
->
node_users
().
find
(
replacing_node
)
==
manager
->
node_users
().
end
())
{
MS_LOG
(
EXCEPTION
)
<<
"The node should be used by at least another node input"
;
}
if
(
manager
->
node_users
()[
replacing_node
].
size
()
>
1
)
{
AnfNodePtr
replace_node
=
GetReplaceNode
(
func_graph
,
replacing_cnode
);
if
(
replace_node
==
nullptr
)
{
MS_LOG
(
DEBUG
)
<<
"Can not find the TransData or Cast with single output node. Depend node: "
<<
node
->
DebugString
();
return
nullptr
;
}
std
::
vector
<
AnfNodePtr
>
new_depend_inputs
=
{
depend_cnode
->
input
(
kAnfPrimitiveIndex
),
depend_cnode
->
input
(
kRealInputIndexInDepend
),
replacing_cnode
->
input
(
kSingleInputIndex
)};
depend_cnode
->
input
(
kRealInputIndexInDepend
),
replace_node
};
auto
kernel_graph
=
func_graph
->
cast
<
std
::
shared_ptr
<
session
::
KernelGraph
>>
();
CNodePtr
new_depend
;
if
(
kernel_graph
==
nullptr
)
{
new_depend
=
func_graph
->
NewCNode
(
new_depend_inputs
);
MS_EXCEPTION_IF_NULL
(
new_depend
);
new_depend
->
set_abstract
(
node
->
abstract
());
new_depend
->
set_scope
(
node
->
scope
());
}
else
{
new_depend
=
kernel_graph
->
NewCNode
(
depend_cnode
);
MS_EXCEPTION_IF_NULL
(
new_depend
);
new_depend
->
set_inputs
(
new_depend_inputs
);
}
new_depend
->
set_abstract
(
node
->
abstract
());
return
new_depend
;
}
}
// namespace opt
...
...
tests/ut/cpp/pre_activate/pass/optimize_dependence_test.cc
浏览文件 @
024706f9
...
...
@@ -48,5 +48,25 @@ TEST_F(TestHWOptimizeDependence, test_optimize_dependence) {
FuncGraphPtr
g_after
=
get_py_fun_
.
CallAndParseRet
(
"test_optimize_dependence"
,
"after"
);
EXPECT_TRUE
(
CheckEqualGraph
(
g_after
,
new_graph
));
}
TEST_F
(
TestHWOptimizeDependence
,
test_optimize_dependence_with_make_tuple
)
{
/*
* def before(x, y, a, b):
* z = make_tuple(TransData(a), TransData(b))
* depend_intput = depend(y, z)
* sum = add(x, depend_intput)
* return sum
*/
FuncGraphPtr
g
=
get_py_fun_
.
CallAndParseRet
(
"test_optimize_dependence_with_make_tuple"
,
"before"
);
auto
optimizer
=
std
::
make_shared
<
opt
::
GraphOptimizer
>
();
auto
pm
=
std
::
make_shared
<
opt
::
PassManager
>
();
pm
->
AddPass
(
std
::
make_shared
<
opt
::
OptimizeDependence
>
());
optimizer
->
AddPassManager
(
pm
);
FuncGraphPtr
new_graph
=
optimizer
->
Optimize
(
g
);
FuncGraphPtr
g_after
=
get_py_fun_
.
CallAndParseRet
(
"test_optimize_dependence_with_make_tuple"
,
"after"
);
EXPECT_TRUE
(
CheckEqualGraph
(
g_after
,
new_graph
));
}
}
// namespace opt
}
// namespace mindspore
tests/ut/cpp/python_input/gtest_input/pre_activate/optimize_dependence_test.py
浏览文件 @
024706f9
...
...
@@ -18,6 +18,8 @@ from mindspore.ops import Primitive
depend
=
Primitive
(
'depend'
)
TransData
=
Primitive
(
'TransData'
)
add
=
P
.
TensorAdd
()
make_tuple
=
Primitive
(
'make_tuple'
)
class
FnDict
:
def
__init__
(
self
):
...
...
@@ -47,3 +49,23 @@ def test_optimize_dependence(tag):
return
sum
return
fns
[
tag
]
def
test_optimize_dependence_with_make_tuple
(
tag
):
fns
=
FnDict
()
@
fns
def
before
(
x
,
y
,
a
,
b
):
z
=
make_tuple
(
TransData
(
a
),
TransData
(
b
))
depend_intput
=
depend
(
y
,
z
)
sum
=
add
(
x
,
depend_intput
)
return
sum
@
fns
def
after
(
x
,
y
,
a
,
b
):
z
=
make_tuple
(
a
,
b
)
depend_intput
=
depend
(
y
,
z
)
sum
=
add
(
x
,
depend_intput
)
return
sum
return
fns
[
tag
]
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录