Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
85ff90c2
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看板
提交
85ff90c2
编写于
5月 12, 2020
作者:
Y
yujianfeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add input shape condition for transpose_reshape fusion pass
上级
298a7848
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
79 addition
and
3 deletion
+79
-3
mindspore/ccsrc/pre_activate/ascend/ir_fusion/reshape_transpose_fusion.cc
...pre_activate/ascend/ir_fusion/reshape_transpose_fusion.cc
+17
-0
mindspore/ccsrc/pre_activate/ascend/ir_fusion/transpose_reshape_fusion.cc
...pre_activate/ascend/ir_fusion/transpose_reshape_fusion.cc
+17
-0
tests/ut/cpp/pre_activate/ascend/ir_fusion/reshape_transpose_fusion_test.cc
...ctivate/ascend/ir_fusion/reshape_transpose_fusion_test.cc
+22
-1
tests/ut/cpp/pre_activate/ascend/ir_fusion/transpose_reshape_fusion_test.cc
...ctivate/ascend/ir_fusion/transpose_reshape_fusion_test.cc
+22
-1
tests/ut/cpp/python_input/gtest_input/pre_activate/reshape_transpose_fusion_test.py
...gtest_input/pre_activate/reshape_transpose_fusion_test.py
+1
-1
未找到文件。
mindspore/ccsrc/pre_activate/ascend/ir_fusion/reshape_transpose_fusion.cc
浏览文件 @
85ff90c2
...
...
@@ -23,6 +23,18 @@
namespace
mindspore
{
namespace
opt
{
namespace
{
bool
CheckShapeDimInfo
(
const
std
::
vector
<
size_t
>
&
shape
)
{
if
(
shape
.
empty
())
{
return
false
;
}
if
(
shape
.
size
()
==
1
&&
shape
[
0
]
%
kCubeSize
!=
0
)
{
return
false
;
}
return
!
(
shape
.
size
()
>=
2
&&
(
shape
[
shape
.
size
()
-
1
]
%
kCubeSize
!=
0
||
shape
[
shape
.
size
()
-
2
]
%
kCubeSize
!=
0
));
}
}
// namespace
const
BaseRef
ReshapeTransposeFusion
::
DefinePattern
()
const
{
const
auto
prim_reshape
=
std
::
make_shared
<
Primitive
>
(
prim
::
kPrimReshape
->
name
());
VectorRef
reshape
({
prim_reshape
,
input_varptr_
});
...
...
@@ -38,6 +50,11 @@ const AnfNodePtr ReshapeTransposeFusion::Process(const FuncGraphPtr &func_graph,
MS_EXCEPTION_IF_NULL
(
transpose_cnode
);
auto
reshape_cnode
=
CheckAnfNodeIfCNodeAndInputSize
(
transpose_cnode
->
input
(
1
),
kBackendReshapeInputNum
);
MS_EXCEPTION_IF_NULL
(
reshape_cnode
);
std
::
vector
<
size_t
>
reshape_input0_shape
=
AnfAlgo
::
GetPrevNodeOutputInferShape
(
reshape_cnode
,
0
);
std
::
vector
<
size_t
>
transpose_input0_shape
=
AnfAlgo
::
GetPrevNodeOutputInferShape
(
transpose_cnode
,
0
);
if
(
!
CheckShapeDimInfo
(
reshape_input0_shape
)
||
!
CheckShapeDimInfo
(
transpose_input0_shape
))
{
return
nullptr
;
}
auto
prim
=
std
::
make_shared
<
Primitive
>
(
kConfusionTransposeDOpName
);
std
::
vector
<
AnfNodePtr
>
inputs
=
{
NewValueNode
(
prim
),
utils
::
cast
<
AnfNodePtr
>
((
*
equiv
)[
input_varptr_
])};
auto
new_node
=
func_graph
->
NewCNode
(
inputs
);
...
...
mindspore/ccsrc/pre_activate/ascend/ir_fusion/transpose_reshape_fusion.cc
浏览文件 @
85ff90c2
...
...
@@ -23,6 +23,18 @@
namespace
mindspore
{
namespace
opt
{
namespace
{
bool
CheckShapeDimInfo
(
const
std
::
vector
<
size_t
>
&
shape
)
{
if
(
shape
.
empty
())
{
return
false
;
}
if
(
shape
.
size
()
==
1
&&
shape
[
0
]
%
kCubeSize
!=
0
)
{
return
false
;
}
return
!
(
shape
.
size
()
>=
2
&&
(
shape
[
shape
.
size
()
-
1
]
%
kCubeSize
!=
0
||
shape
[
shape
.
size
()
-
2
]
%
kCubeSize
!=
0
));
}
}
// namespace
const
BaseRef
TransposeReshapeFusion
::
DefinePattern
()
const
{
const
auto
prim_reshape
=
std
::
make_shared
<
Primitive
>
(
prim
::
kPrimReshape
->
name
());
VectorRef
transpose
({
prim
::
kPrimTranspose
,
input_varptr_
});
...
...
@@ -38,6 +50,11 @@ const AnfNodePtr TransposeReshapeFusion::Process(const FuncGraphPtr &func_graph,
MS_EXCEPTION_IF_NULL
(
reshape_cnode
);
auto
transpose_cnode
=
CheckAnfNodeIfCNodeAndInputSize
(
reshape_cnode
->
input
(
1
),
kBackendReshapeInputNum
);
MS_EXCEPTION_IF_NULL
(
transpose_cnode
);
std
::
vector
<
size_t
>
reshape_input0_shape
=
AnfAlgo
::
GetPrevNodeOutputInferShape
(
reshape_cnode
,
0
);
std
::
vector
<
size_t
>
transpose_input0_shape
=
AnfAlgo
::
GetPrevNodeOutputInferShape
(
transpose_cnode
,
0
);
if
(
!
CheckShapeDimInfo
(
reshape_input0_shape
)
||
!
CheckShapeDimInfo
(
transpose_input0_shape
))
{
return
nullptr
;
}
auto
prim
=
std
::
make_shared
<
Primitive
>
(
kConfusionTransposeDOpName
);
std
::
vector
<
AnfNodePtr
>
inputs
=
{
NewValueNode
(
prim
),
utils
::
cast
<
AnfNodePtr
>
((
*
equiv
)[
input_varptr_
])};
auto
new_node
=
func_graph
->
NewCNode
(
inputs
);
...
...
tests/ut/cpp/pre_activate/ascend/ir_fusion/reshape_transpose_fusion_test.cc
浏览文件 @
85ff90c2
...
...
@@ -39,7 +39,7 @@ TEST_F(TestHWReshapeTransposeFusion, test_reshape_transpose_fusion) {
* return transpose
*/
FuncGraphPtr
g
=
get_py_fun_
.
CallAndParseRet
(
"test_reshape_transpose_fusion"
,
"before"
);
std
::
vector
<
int
>
shp
{
2
,
4
,
8
,
16
};
std
::
vector
<
int
>
shp
{
2
,
2
,
16
,
16
};
auto
x_abstract
=
std
::
make_shared
<
abstract
::
AbstractTensor
>
(
kFloat32
,
shp
);
AbstractBasePtrList
args_spec_list
{
x_abstract
};
auto
kg
=
GetKernelGraph
(
g
,
args_spec_list
);
...
...
@@ -59,5 +59,26 @@ TEST_F(TestHWReshapeTransposeFusion, test_reshape_transpose_fusion) {
FuncGraphPtr
g_after
=
get_py_fun_
.
CallAndParseRet
(
"test_reshape_transpose_fusion"
,
"after"
);
EXPECT_TRUE
(
CheckEqualGraph
(
g_after
,
new_graph
));
}
TEST_F
(
TestHWReshapeTransposeFusion
,
test_reshape_transpose_no_fusion
)
{
/*
* def before(input0, input1):
* reshape = Reshape(input0, input1)
* transpose = Transpose(reshape)
* return transpose
*/
FuncGraphPtr
g
=
get_py_fun_
.
CallAndParseRet
(
"test_reshape_transpose_fusion"
,
"before"
);
std
::
vector
<
int
>
shp
{
2
,
4
,
8
,
16
};
auto
x_abstract
=
std
::
make_shared
<
abstract
::
AbstractTensor
>
(
kFloat32
,
shp
);
AbstractBasePtrList
args_spec_list
{
x_abstract
};
auto
kg
=
GetKernelGraph
(
g
,
args_spec_list
);
auto
optimizer
=
std
::
make_shared
<
opt
::
GraphOptimizer
>
();
auto
pm
=
std
::
make_shared
<
opt
::
PassManager
>
();
pm
->
AddPass
(
std
::
make_shared
<
opt
::
ReshapeTransposeFusion
>
());
optimizer
->
AddPassManager
(
pm
);
FuncGraphPtr
new_graph
=
optimizer
->
Optimize
(
kg
);
EXPECT_TRUE
(
CheckEqualGraph
(
kg
,
new_graph
));
}
}
// namespace opt
}
// namespace mindspore
tests/ut/cpp/pre_activate/ascend/ir_fusion/transpose_reshape_fusion_test.cc
浏览文件 @
85ff90c2
...
...
@@ -39,7 +39,7 @@ TEST_F(TestHWTransposeReshapeFusion, test_transpose_reshape_fusion) {
* return transpose
*/
FuncGraphPtr
g
=
get_py_fun_
.
CallAndParseRet
(
"test_transpose_reshape_fusion"
,
"before"
);
std
::
vector
<
int
>
shp
{
2
,
4
,
8
,
16
};
std
::
vector
<
int
>
shp
{
2
,
2
,
16
,
16
};
auto
x_abstract
=
std
::
make_shared
<
abstract
::
AbstractTensor
>
(
kFloat32
,
shp
);
AbstractBasePtrList
args_spec_list
{
x_abstract
};
auto
kg
=
GetKernelGraph
(
g
,
args_spec_list
);
...
...
@@ -61,5 +61,26 @@ TEST_F(TestHWTransposeReshapeFusion, test_transpose_reshape_fusion) {
FuncGraphPtr
g_after
=
get_py_fun_
.
CallAndParseRet
(
"test_transpose_reshape_fusion"
,
"after"
);
EXPECT_TRUE
(
CheckEqualGraph
(
g_after
,
new_graph
));
}
TEST_F
(
TestHWTransposeReshapeFusion
,
test_transpose_reshape_no_fusion
)
{
/*
* def before(input0, input1):
* reshape = Reshape(input0, input1)
* transpose = Transpose(reshape)
* return transpose
*/
FuncGraphPtr
g
=
get_py_fun_
.
CallAndParseRet
(
"test_transpose_reshape_fusion"
,
"before"
);
std
::
vector
<
int
>
shp
{
2
,
4
,
8
,
16
};
auto
x_abstract
=
std
::
make_shared
<
abstract
::
AbstractTensor
>
(
kFloat32
,
shp
);
AbstractBasePtrList
args_spec_list
{
x_abstract
};
auto
kg
=
GetKernelGraph
(
g
,
args_spec_list
);
auto
optimizer
=
std
::
make_shared
<
opt
::
GraphOptimizer
>
();
auto
pm
=
std
::
make_shared
<
opt
::
PassManager
>
();
pm
->
AddPass
(
std
::
make_shared
<
opt
::
TransposeReshapeFusion
>
());
optimizer
->
AddPassManager
(
pm
);
FuncGraphPtr
new_graph
=
optimizer
->
Optimize
(
kg
);
EXPECT_TRUE
(
CheckEqualGraph
(
kg
,
new_graph
));
}
}
// namespace opt
}
// namespace mindspore
tests/ut/cpp/python_input/gtest_input/pre_activate/reshape_transpose_fusion_test.py
浏览文件 @
85ff90c2
...
...
@@ -36,7 +36,7 @@ def test_reshape_transpose_fusion(tag):
@
fns
def
before
(
input0
):
reshape
=
Reshape
(
input0
,
(
2
,
4
,
8
,
16
))
reshape
=
Reshape
(
input0
,
(
2
,
2
,
16
,
16
))
transpose
=
Transpose
(
reshape
,
(
1
,
0
,
2
,
3
))
return
transpose
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录