Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
7cd71c31
MegEngine
项目概览
MegEngine 天元
/
MegEngine
大约 1 年 前同步成功
通知
399
Star
4705
Fork
582
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
7cd71c31
编写于
11月 03, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mgb/gopt): fix cd4 elewise transform
GitOrigin-RevId: 027d5e53e43088fb3bfb9932369a4a0429f736bc
上级
cae8c8a4
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
86 addition
and
4 deletion
+86
-4
src/gopt/impl/inference.cpp
src/gopt/impl/inference.cpp
+14
-3
src/gopt/impl/tensor_reformat.cpp
src/gopt/impl/tensor_reformat.cpp
+2
-0
src/gopt/test/inference.cpp
src/gopt/test/inference.cpp
+70
-1
未找到文件。
src/gopt/impl/inference.cpp
浏览文件 @
7cd71c31
...
@@ -1561,15 +1561,26 @@ std::unique_ptr<ConvertFormatPass> ConvertFormatPass::make_nhwcd4_converter() {
...
@@ -1561,15 +1561,26 @@ std::unique_ptr<ConvertFormatPass> ConvertFormatPass::make_nhwcd4_converter() {
return
new_opr
;
return
new_opr
;
};
};
auto
replace_elemwise_opr
=
[](
OperatorNodeBase
*
opr
,
auto
replace_elemwise_opr
=
[
&
relayout_inp_to_chw
](
OperatorNodeBase
*
opr
,
const
VarNodeArray
&
new_inp
)
{
const
VarNodeArray
&
new_inp
)
{
mgb_assert
(
opr
->
input
().
size
()
==
new_inp
.
size
());
mgb_assert
(
opr
->
input
().
size
()
==
new_inp
.
size
());
bool
has_inp_changed
=
false
;
bool
has_inp_changed
=
false
;
bool
can_exec_cd4
=
true
;
for
(
size_t
i
=
0
;
i
<
opr
->
input
().
size
();
i
++
)
{
for
(
size_t
i
=
0
;
i
<
opr
->
input
().
size
();
i
++
)
{
if
(
!
new_inp
[
i
]
->
format
().
is_default
())
{
if
(
!
new_inp
[
i
]
->
format
().
is_default
())
{
has_inp_changed
=
true
;
has_inp_changed
=
true
;
break
;
}
else
if
(
new_inp
[
i
]
->
shape
().
ndim
==
4
)
{
if
(
new_inp
[
i
]
->
shape
()[
1
]
%
4
!=
0
)
{
can_exec_cd4
=
false
;
}
//! cd4 elemwise with scaler is supported
}
else
if
(
!
new_inp
[
i
]
->
shape
().
is_scalar
())
{
can_exec_cd4
=
false
;
}
}
}
if
(
!
can_exec_cd4
)
{
return
relayout_inp_to_chw
(
opr
,
new_inp
);
}
}
if
(
has_inp_changed
)
{
if
(
has_inp_changed
)
{
// assumption: all inputs are changed from nchw to nhwcd4
// assumption: all inputs are changed from nchw to nhwcd4
...
...
src/gopt/impl/tensor_reformat.cpp
浏览文件 @
7cd71c31
...
@@ -2232,6 +2232,8 @@ void EnableNchwxxPass::fill_opr_convert_fun(size_t pack_c_size) {
...
@@ -2232,6 +2232,8 @@ void EnableNchwxxPass::fill_opr_convert_fun(size_t pack_c_size) {
if
(
new_inp
[
i
]
->
shape
()[
1
]
%
pack_c_size
!=
0
)
{
if
(
new_inp
[
i
]
->
shape
()[
1
]
%
pack_c_size
!=
0
)
{
can_exec_ncwxx
=
false
;
can_exec_ncwxx
=
false
;
}
}
}
else
if
(
!
new_inp
[
i
]
->
shape
().
is_scalar
())
{
can_exec_ncwxx
=
false
;
}
}
}
}
if
(
has_inp_changed
)
{
if
(
has_inp_changed
)
{
...
...
src/gopt/test/inference.cpp
浏览文件 @
7cd71c31
...
@@ -1197,6 +1197,67 @@ TEST(TestGoptInference, ConvertFormatNHWCD4) {
...
@@ -1197,6 +1197,67 @@ TEST(TestGoptInference, ConvertFormatNHWCD4) {
MGB_ASSERT_TENSOR_NEAR
(
host_y
,
host_y_opt
,
1e-3
);
MGB_ASSERT_TENSOR_NEAR
(
host_y
,
host_y_opt
,
1e-3
);
}
}
TEST
(
TestGoptInference
,
ConvertFormatNHWCD4Elemwise
)
{
// hwcd4 is only supported in naive handle
NaiveMegDNNHandleScope
naive_megdnn_handle
;
HostTensorGenerator
<>
gen
;
auto
cn
=
CompNode
::
load
(
"cpu0"
);
auto
graph
=
ComputingGraph
::
make
();
graph
->
options
().
graph_opt_level
=
0
;
auto
mkvar
=
[
&
](
const
char
*
name
,
const
TensorShape
&
shp
)
{
return
opr
::
Host2DeviceCopy
::
make
(
*
graph
,
gen
(
shp
,
cn
)).
rename
(
name
);
};
auto
mkcvar
=
[
&
](
const
char
*
name
,
const
TensorShape
&
shp
)
{
return
opr
::
SharedDeviceTensor
::
make
(
*
graph
,
*
gen
(
shp
,
cn
))
.
rename
(
name
);
};
auto
host_x
=
gen
({
8
,
8
,
8
,
8
},
cn
);
auto
x
=
opr
::
Host2DeviceCopy
::
make
(
*
graph
,
host_x
);
opr
::
Convolution
::
Param
param
;
param
.
pad_h
=
param
.
pad_w
=
0
;
auto
w1
=
mkcvar
(
"w1"
,
{
8
,
8
,
3
,
3
}),
conv
=
opr
::
Convolution
::
make
(
x
,
w1
,
param
);
auto
b
=
mkvar
(
"b"
,
{
1
,
1
,
1
,
1
}),
elem
=
opr
::
Elemwise
::
make
({
conv
+
b
},
opr
::
Elemwise
::
Param
::
Mode
::
RELU
);
param
.
pad_h
=
param
.
pad_w
=
1
;
auto
w2
=
mkcvar
(
"w2"
,
{
8
,
8
,
3
,
3
}),
conv2
=
opr
::
Convolution
::
make
(
elem
,
w2
,
param
);
auto
b_scaler
=
mkvar
(
"b"
,
{
1
}),
elem2
=
conv2
+
b_scaler
;
param
.
pad_h
=
param
.
pad_w
=
1
;
auto
w3
=
mkcvar
(
"w2"
,
{
8
,
8
,
3
,
3
}),
y
=
opr
::
Convolution
::
make
(
elem2
,
w3
,
param
);
SymbolVar
y_opt
;
auto
options
=
gopt
::
OptimizeForInferenceOptions
{};
options
.
enable_nhwcd4
();
unpack_vector
(
gopt
::
optimize_for_inference
({
y
},
options
),
y_opt
);
ASSERT_EQ
(
opr
::
Convolution
::
Param
::
Format
::
NHWCD4
,
find_opr
<
opr
::
Convolution
>
(
y_opt
).
param
().
format
);
graph
->
compile
({{
y_opt
,
{}}})
->
to_json
()
->
writeto_fpath
(
output_file
(
"TestGoptInference.ConvertFormatNHWCD4Elemwise.json"
));
HostTensorND
host_y_opt
,
host_y
;
auto
func
=
graph
->
compile
({
make_callback_copy
(
y
,
host_y
),
make_callback_copy
(
y_opt
,
host_y_opt
)});
func
->
execute
();
MGB_ASSERT_TENSOR_NEAR
(
host_y
,
host_y_opt
,
1e-3
);
*
host_x
=
*
gen
({
8
,
8
,
16
,
16
},
cn
);
func
->
execute
();
MGB_ASSERT_TENSOR_NEAR
(
host_y
,
host_y_opt
,
1e-3
);
}
TEST
(
TestGoptInference
,
ConvertFormatNHWCD4LOCAL
)
{
TEST
(
TestGoptInference
,
ConvertFormatNHWCD4LOCAL
)
{
// hwcd4 is only supported in naive handle
// hwcd4 is only supported in naive handle
NaiveMegDNNHandleScope
naive_megdnn_handle
;
NaiveMegDNNHandleScope
naive_megdnn_handle
;
...
@@ -3228,7 +3289,15 @@ TEST(TestGoptInference, ConvertFormatNCHW44MultiInput) {
...
@@ -3228,7 +3289,15 @@ TEST(TestGoptInference, ConvertFormatNCHW44MultiInput) {
conv1
=
opr
::
Convolution
::
make
(
x
,
w1
,
param_conv
);
conv1
=
opr
::
Convolution
::
make
(
x
,
w1
,
param_conv
);
auto
b
=
mkvar
(
"b"
,
{
1
,
1
,
16
,
16
}),
auto
b
=
mkvar
(
"b"
,
{
1
,
1
,
16
,
16
}),
y
=
opr
::
Elemwise
::
make
({
conv1
+
b
},
opr
::
Elemwise
::
Param
::
Mode
::
RELU
);
elem0
=
opr
::
Elemwise
::
make
({
conv1
+
b
+
b
},
opr
::
Elemwise
::
Param
::
Mode
::
RELU
);
auto
w2
=
mkcvar
(
"w2"
,
{
8
,
8
,
3
,
3
}),
conv2
=
opr
::
Convolution
::
make
(
elem0
,
w2
,
param_conv
);
auto
b1
=
mkvar
(
"b1"
,
{
1
}),
y
=
opr
::
Elemwise
::
make
({
conv2
+
b1
+
b
},
opr
::
Elemwise
::
Param
::
Mode
::
RELU
);
SymbolVar
y_opt
;
SymbolVar
y_opt
;
auto
options
=
gopt
::
OptimizeForInferenceOptions
{};
auto
options
=
gopt
::
OptimizeForInferenceOptions
{};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录