Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
729242f9
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看板
提交
729242f9
编写于
3月 07, 2022
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(imperative): move typecvt code of sereval ops to c++
GitOrigin-RevId: 4ffaa376c1f8edaaff7a9c9fb14cbe3ddd186515
上级
3c3fc6f3
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
54 addition
and
21 deletion
+54
-21
imperative/python/megengine/functional/nn.py
imperative/python/megengine/functional/nn.py
+2
-21
imperative/src/impl/transformations/dtype_promote.cpp
imperative/src/impl/transformations/dtype_promote.cpp
+52
-0
未找到文件。
imperative/python/megengine/functional/nn.py
浏览文件 @
729242f9
...
...
@@ -320,12 +320,6 @@ def conv3d(
stride
=
_triple_nonzero
(
stride
)
dilate
=
_triple_nonzero
(
dilation
)
dtype
=
dtype_promotion
(
inp
,
weight
)
if
inp
.
dtype
!=
dtype
:
inp
=
inp
.
astype
(
dtype
)
if
weight
.
dtype
!=
dtype
:
weight
=
weight
.
astype
(
dtype
)
sparse_type
=
"dense"
if
groups
==
1
else
"group"
op
=
builtin
.
Convolution3D
(
pad_d
=
pad
[
D
],
...
...
@@ -389,15 +383,6 @@ def conv_transpose2d(
conv_mode
.
lower
()
==
"cross_correlation"
or
conv_mode
.
name
==
"CROSS_CORRELATION"
)
if
amp
.
_enabled
:
compute_mode
=
"float32"
inp
,
weight
,
bias
=
cast_tensors
(
inp
,
weight
,
bias
)
else
:
dtype
=
dtype_promotion
(
inp
,
weight
)
if
inp
.
dtype
!=
dtype
:
inp
=
inp
.
astype
(
dtype
)
if
weight
.
dtype
!=
dtype
:
weight
=
weight
.
astype
(
dtype
)
stride_h
,
stride_w
=
expand_hw
(
stride
)
pad_h
,
pad_w
=
expand_hw
(
padding
)
...
...
@@ -418,6 +403,8 @@ def conv_transpose2d(
)
(
output
,)
=
apply
(
op
,
weight
,
inp
)
if
bias
is
not
None
:
if
amp
.
_enabled
:
bias
=
cast_tensors
(
bias
)
output
+=
bias
return
output
...
...
@@ -591,12 +578,6 @@ def conv_transpose3d(
stride
=
_triple_nonzero
(
stride
)
dilate
=
_triple_nonzero
(
dilation
)
dtype
=
dtype_promotion
(
inp
,
weight
)
if
inp
.
dtype
!=
dtype
:
inp
=
inp
.
astype
(
dtype
)
if
weight
.
dtype
!=
dtype
:
weight
=
weight
.
astype
(
dtype
)
sparse_type
=
"dense"
if
groups
==
1
else
"group"
op
=
builtin
.
Convolution3DBackwardData
(
pad_d
=
pad
[
D
],
...
...
imperative/src/impl/transformations/dtype_promote.cpp
浏览文件 @
729242f9
...
...
@@ -183,6 +183,38 @@ ValueRefList convolution_rule(const OpDef& op, Span<ValueRef> inputs) {
return
imperative
::
apply
(
op
,
converted
);
}
// differ from Convolution, ConvolutionBackwardData is used in both
// functional.conv_transpose2d and quantize.conv_transpose2d
ValueRefList
convolution_backward_rule
(
const
OpDef
&
op
,
Span
<
ValueRef
>
inputs
)
{
auto
&&
conv_op
=
const_cast
<
ConvolutionBackwardData
&>
(
op
.
cast_final_safe
<
ConvolutionBackwardData
>
());
SmallVector
<
DType
>
dtypes
=
get_value_dtypes
(
inputs
);
if
(
is_quantized_dtype
(
dtypes
[
0
])
&&
is_quantized_dtype
(
dtypes
[
1
]))
{
return
imperative
::
apply
(
op
,
inputs
);
}
mgb
::
DType
target_dtype
;
if
(
DTypePromoteCfg
::
amp_dtype_autocast_enabled
)
{
conv_op
.
compute_mode
=
ConvolutionBackwardData
::
ComputeMode
::
FLOAT32
;
target_dtype
=
DTypePromoteCfg
::
amp_low_prec_dtype
;
}
else
{
target_dtype
=
get_promoted_dtype
(
dtypes
);
}
ValueRefList
converted
(
inputs
.
size
());
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
++
i
)
{
if
(
dtypes
[
i
]
!=
target_dtype
)
{
converted
[
i
]
=
imperative
::
apply
(
ApplyOp
(
*
TypeCvt
::
make
(
target_dtype
)),
inputs
[
i
])[
0
];
}
else
{
converted
[
i
]
=
inputs
[
i
];
}
}
return
imperative
::
apply
(
op
,
converted
);
}
ValueRefList
batch_norm_rule
(
const
OpDef
&
op
,
Span
<
ValueRef
>
inputs
)
{
if
(
DTypePromoteCfg
::
amp_dtype_autocast_enabled
)
{
mgb_assert
(
inputs
.
size
()
>
0
);
...
...
@@ -208,12 +240,32 @@ ValueRefList batch_norm_rule(const OpDef& op, Span<ValueRef> inputs) {
return
imperative
::
apply
(
op
,
inputs
);
}
ValueRefList
convolution3d_rule
(
const
OpDef
&
op
,
Span
<
ValueRef
>
inputs
)
{
SmallVector
<
DType
>
dtypes
=
get_value_dtypes
(
inputs
);
mgb
::
DType
target_dtype
=
get_promoted_dtype
(
dtypes
);
ValueRefList
converted
(
inputs
.
size
());
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
++
i
)
{
if
(
dtypes
[
i
]
!=
target_dtype
)
{
converted
[
i
]
=
imperative
::
apply
(
ApplyOp
(
*
TypeCvt
::
make
(
target_dtype
)),
inputs
[
i
])[
0
];
}
else
{
converted
[
i
]
=
inputs
[
i
];
}
}
return
imperative
::
apply
(
op
,
converted
);
}
struct
DTypePromoteRuleRegistry
{
DTypePromoteRuleRegistry
()
{
register_dtype_promote_rule
<
Elemwise
>
(
elemwise_rule
);
register_dtype_promote_rule
<
Reduce
>
(
reduce_rule
);
register_dtype_promote_rule
<
Convolution
>
(
convolution_rule
);
register_dtype_promote_rule
<
ConvolutionBackwardData
>
(
convolution_backward_rule
);
register_dtype_promote_rule
<
BatchNorm
>
(
batch_norm_rule
);
register_dtype_promote_rule
<
Convolution3D
>
(
convolution3d_rule
);
register_dtype_promote_rule
<
Convolution3DBackwardData
>
(
convolution3d_rule
);
}
}
register_helper
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录