Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
c9e56f49
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看板
提交
c9e56f49
编写于
5月 26, 2022
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(imperative/amp): add dimshuffle before creating nhwc tensor
GitOrigin-RevId: 4461f9a0d3d6428282fcd858c677263d9a08fa9f
上级
d57a0712
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
22 addition
and
13 deletion
+22
-13
imperative/python/megengine/amp/convert_format.py
imperative/python/megengine/amp/convert_format.py
+1
-1
imperative/python/test/unit/core/test_formatted_tensor.py
imperative/python/test/unit/core/test_formatted_tensor.py
+9
-9
imperative/src/impl/transformations/format.cpp
imperative/src/impl/transformations/format.cpp
+12
-3
未找到文件。
imperative/python/megengine/amp/convert_format.py
浏览文件 @
c9e56f49
...
...
@@ -33,7 +33,7 @@ def convert_tensor_format(x: Tensor, inplace: bool = True):
if
x
.
format
!=
"nhwc"
:
if
inplace
:
# hostvalue should still be valid, so no d2h cost.
data
=
x
.
numpy
()
.
transpose
(
*
pattern
)
data
=
x
.
numpy
()
# reset will destroy existed backward grad
x
[...]
=
Tensor
(
data
,
format
=
"nhwc"
)
else
:
...
...
imperative/python/test/unit/core/test_formatted_tensor.py
浏览文件 @
c9e56f49
...
...
@@ -38,7 +38,7 @@ def test_basic():
def
_compare_nchw_nhwc
(
data
,
func
,
is_symbolic
=
None
):
x1
=
tensor
(
data
)
x2
=
tensor
(
data
.
transpose
(
0
,
2
,
3
,
1
)
,
format
=
"nhwc"
)
x2
=
tensor
(
data
,
format
=
"nhwc"
)
if
is_symbolic
is
not
None
:
func
=
trace
(
func
,
symbolic
=
is_symbolic
)
out1
=
func
(
x1
)
...
...
@@ -247,8 +247,8 @@ def test_conv2d(is_symbolic):
if
x
.
format
==
"nhwc"
:
x
=
F
.
conv2d
(
x
,
weight
=
mge
.
tensor
(
np
.
ones
((
3
,
1
,
1
,
2
)),
format
=
"nhwc"
),
bias
=
mge
.
tensor
(
np
.
ones
((
1
,
1
,
1
,
3
)),
format
=
"nhwc"
),
weight
=
mge
.
tensor
(
np
.
ones
((
3
,
2
,
1
,
1
)),
format
=
"nhwc"
),
bias
=
mge
.
tensor
(
np
.
ones
((
1
,
3
,
1
,
1
)),
format
=
"nhwc"
),
)
assert
x
.
format
==
"nhwc"
return
x
.
numpy
()
...
...
@@ -265,8 +265,8 @@ def test_group_conv2d(is_symbolic):
if
x
.
format
==
"nhwc"
:
x
=
F
.
conv2d
(
x
,
weight
=
mge
.
tensor
(
np
.
ones
((
2
,
2
,
1
,
1
,
2
)),
format
=
"nhwc"
),
bias
=
mge
.
tensor
(
np
.
ones
((
1
,
1
,
1
,
4
)),
format
=
"nhwc"
),
weight
=
mge
.
tensor
(
np
.
ones
((
2
,
2
,
2
,
1
,
1
)),
format
=
"nhwc"
),
bias
=
mge
.
tensor
(
np
.
ones
((
1
,
4
,
1
,
1
)),
format
=
"nhwc"
),
groups
=
2
,
)
assert
x
.
format
==
"nhwc"
...
...
@@ -286,10 +286,10 @@ def test_bn(is_symbolic):
if
x
.
format
==
"nhwc"
:
oups
=
F
.
batch_norm
(
x
.
astype
(
"float32"
),
running_mean
=
mge
.
tensor
(
np
.
ones
((
1
,
1
,
1
,
2
)),
format
=
"nhwc"
),
running_var
=
mge
.
tensor
(
np
.
ones
((
1
,
1
,
1
,
2
)),
format
=
"nhwc"
),
weight
=
mge
.
tensor
(
np
.
ones
((
1
,
1
,
1
,
2
)),
format
=
"nhwc"
),
bias
=
mge
.
tensor
(
np
.
ones
((
1
,
1
,
1
,
2
)),
format
=
"nhwc"
),
running_mean
=
mge
.
tensor
(
np
.
ones
((
1
,
2
,
1
,
1
)),
format
=
"nhwc"
),
running_var
=
mge
.
tensor
(
np
.
ones
((
1
,
2
,
1
,
1
)),
format
=
"nhwc"
),
weight
=
mge
.
tensor
(
np
.
ones
((
1
,
2
,
1
,
1
)),
format
=
"nhwc"
),
bias
=
mge
.
tensor
(
np
.
ones
((
1
,
2
,
1
,
1
)),
format
=
"nhwc"
),
training
=
True
,
inplace
=
False
,
)
...
...
imperative/src/impl/transformations/format.cpp
浏览文件 @
c9e56f49
...
...
@@ -27,7 +27,11 @@ TypedValueRef<FormattedTensorValue> FormatTransformation::to(
pattern
=
{
0
,
3
,
1
,
2
};
}
}
else
if
((
format
==
FT
::
NCHW
||
format
==
FT
::
DEFAULT
)
&&
target
==
FT
::
NHWC
)
{
pattern
=
{
0
,
2
,
3
,
1
};
if
(
tensor
.
value
().
shape
().
cast
<
ShapeValue
>
().
ndim
==
5
)
{
pattern
=
{
0
,
1
,
3
,
4
,
2
};
}
else
{
pattern
=
{
0
,
2
,
3
,
1
};
}
}
else
{
mgb_throw
(
MegBrainError
,
"Unsupport format conversion from %s to %s"
,
...
...
@@ -572,8 +576,13 @@ ValueRefList FormatTransformation::apply_transformation(
}
}
else
if
(
auto
*
create_tensor
=
op
.
as
<
CreateTensor
>
())
{
auto
format
=
create_tensor
->
format
();
// TODO: add dimshuffle for nhwc format
return
{
wrap_output
(
imperative
::
apply
(
op
,
inputs
)[
0
],
format
)};
if
(
format
==
FT
::
NHWC
)
{
auto
output
=
wrap_output
(
imperative
::
apply
(
op
,
inputs
)[
0
]);
output
=
to
(
output
.
cast
(
m_value_type
),
FT
::
NHWC
,
""
);
return
{
output
};
}
else
{
return
{
wrap_output
(
imperative
::
apply
(
op
,
inputs
)[
0
],
format
)};
}
}
else
if
(
auto
*
get_attr
=
op
.
as
<
GetAttr
>
())
{
auto
&&
input
=
inputs
.
item
();
if
(
!
input
.
is
(
m_value_type
))
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录