Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
3892aa0b
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
404
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看板
提交
3892aa0b
编写于
4月 29, 2022
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(imperative/amp): fix bn params for nhwc amp
GitOrigin-RevId: 57a3b9d4181ba8110ad736a80b54f43c3d239962
上级
6f0b5820
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
17 addition
and
55 deletion
+17
-55
imperative/python/megengine/core/tensor/array_method.py
imperative/python/megengine/core/tensor/array_method.py
+1
-8
imperative/python/megengine/functional/nn.py
imperative/python/megengine/functional/nn.py
+3
-2
imperative/python/megengine/functional/tensor.py
imperative/python/megengine/functional/tensor.py
+3
-0
imperative/python/megengine/optimizer/optimizer.py
imperative/python/megengine/optimizer/optimizer.py
+8
-7
imperative/python/test/conftest.py
imperative/python/test/conftest.py
+0
-2
imperative/python/test/unit/core/test_formatted_tensor.py
imperative/python/test/unit/core/test_formatted_tensor.py
+2
-36
未找到文件。
imperative/python/megengine/core/tensor/array_method.py
浏览文件 @
3892aa0b
...
...
@@ -51,14 +51,7 @@ class _Hashable:
return
self
.
value
==
o
.
value
def
_matmul
(
inp1
,
inp2
,
transpose_a
=
False
,
transpose_b
=
False
,
compute_mode
=
"default"
,
format
=
"default"
,
):
def
_matmul
(
inp1
,
inp2
,
transpose_a
=
False
,
transpose_b
=
False
,
compute_mode
=
"default"
):
dim1
,
dim2
=
inp1
.
ndim
,
inp2
.
ndim
assert
dim1
>
0
and
dim2
>
0
maxdim
=
dim1
if
dim1
>
dim2
else
dim2
...
...
imperative/python/megengine/functional/nn.py
浏览文件 @
3892aa0b
...
...
@@ -1206,6 +1206,7 @@ def batch_norm(
if
x
is
None
:
x
=
Const
(
value
,
inp
.
dtype
,
inp
.
device
)
x
.
format
=
inp
.
format
shape
=
astensor1d
(
pshape
,
inp
,
dtype
=
"int32"
,
device
=
inp
.
device
)
(
result
,)
=
apply
(
builtin
.
Broadcast
(),
x
,
shape
)
return
result
...
...
@@ -1227,14 +1228,14 @@ def batch_norm(
if
not
training
:
op
=
builtin
.
BatchNorm
(
fwd_mode
=
BatchNorm
.
FwdMode
.
INFERENCE
,
param_dim
=
"dim_1c11"
,
epsilon
=
eps
fwd_mode
=
BatchNorm
.
FwdMode
.
INFERENCE
,
epsilon
=
eps
,
param_dim
=
"dim_1c11"
)
ret
=
apply
(
op
,
inp
,
weight
,
bias
,
running_mean
,
running_var
)[
-
1
]
return
ret
else
:
op
=
builtin
.
BatchNorm
(
avg_factor
=
1
-
momentum
,
param_dim
=
"dim_1c11"
,
epsilon
=
eps
avg_factor
=
1
-
momentum
,
epsilon
=
eps
,
param_dim
=
"dim_1c11"
)
if
has_mean
or
has_var
:
running_mean
=
make_full_if_none
(
running_mean
,
0
)
...
...
imperative/python/megengine/functional/tensor.py
浏览文件 @
3892aa0b
...
...
@@ -272,6 +272,9 @@ def full_like(inp: Tensor, value: Union[int, float]) -> Tensor:
x
=
Const
(
value
,
inp
.
dtype
,
inp
.
device
)
if
inp
.
ndim
==
0
:
return
x
# set x's format to use FormatTransformation rule for Broadcast.
x
.
format
=
inp
.
format
return
broadcast_to
(
x
,
inp
.
shape
)
...
...
imperative/python/megengine/optimizer/optimizer.py
浏览文件 @
3892aa0b
...
...
@@ -91,13 +91,14 @@ class Optimizer(metaclass=ABCMeta):
else
:
param_group
[
"params"
]
=
list
(
param_group
[
"params"
])
for
param
in
param_group
[
"params"
]:
if
not
isinstance
(
param
,
Parameter
):
raise
TypeError
(
"optimizer can only optimize Parameters, but one of the params is "
+
str
(
type
(
param
))
)
param
.
_reset
(
Tensor
(
param
.
numpy
(),
no_cache
=
True
,
format
=
param
.
format
))
with
_config
.
_override
(
auto_format_convert
=
False
):
for
param
in
param_group
[
"params"
]:
if
not
isinstance
(
param
,
Parameter
):
raise
TypeError
(
"optimizer can only optimize Parameters, but one of the params is "
+
str
(
type
(
param
))
)
param
.
_reset
(
Tensor
(
param
.
numpy
(),
no_cache
=
True
,
format
=
param
.
format
))
for
name
,
default
in
self
.
_defaults
.
items
():
if
default
is
required
and
name
not
in
param_group
:
...
...
imperative/python/test/conftest.py
浏览文件 @
3892aa0b
...
...
@@ -58,7 +58,6 @@ def run_around_tests():
"benchmark_kernel"
:
config
.
benchmark_kernel
,
"deterministic_kernel"
:
config
.
deterministic_kernel
,
"compute_mode"
:
config
.
_compute_mode
,
"conv_format"
:
config
.
_conv_format
,
"amp_enabled"
:
amp
.
enabled
,
"convert_inputs"
:
_get_convert_inputs
(),
"amp_dtype_autocast"
:
_get_amp_dtype_autocast
(),
...
...
@@ -82,7 +81,6 @@ def run_around_tests():
"benchmark_kernel"
:
config
.
benchmark_kernel
,
"deterministic_kernel"
:
config
.
deterministic_kernel
,
"compute_mode"
:
config
.
_compute_mode
,
"conv_format"
:
config
.
_conv_format
,
"amp_enabled"
:
amp
.
enabled
,
"convert_inputs"
:
_get_convert_inputs
(),
"amp_dtype_autocast"
:
_get_amp_dtype_autocast
(),
...
...
imperative/python/test/unit/core/test_formatted_tensor.py
浏览文件 @
3892aa0b
...
...
@@ -386,13 +386,6 @@ def test_backward_conv2d_dimshuffle(is_symbolic):
return
F
.
transpose
(
self
.
conv
(
inp
),
(
0
,
2
,
3
,
1
)).
reshape
(
1
,
18
,
2
)
inp
=
mge
.
tensor
(
np
.
arange
(
0
,
24
).
reshape
((
1
,
2
,
3
,
4
)))
# x = tensor(data.transpose(0, 2, 3, 1), format="nhwc")
# w = mge.tensor(np.ones((3, 1, 1, 2)), format="nhwc")
# b = mge.tensor(np.ones((1, 1, 1, 3)), format="nhwc")
# grads = [
# np.array([66, 210, 66, 210, 66, 210]).reshape((3, 1, 1, 2)),
# np.array([12, 12, 12]).reshape((1, 1, 1, 3)),
# ]
_compare_backward
([
inp
],
Net
(),
is_symbolic
)
...
...
@@ -403,37 +396,10 @@ def test_backward_groupconv2d_bn(is_symbolic):
super
().
__init__
()
self
.
conv0
=
M
.
Conv2d
(
32
,
256
,
3
,
groups
=
32
,
stride
=
2
)
self
.
conv1
=
M
.
Conv2d
(
256
,
2048
,
3
,
groups
=
32
,
stride
=
2
)
#
self.bn = M.BatchNorm2d(2048)
self
.
bn
=
M
.
BatchNorm2d
(
2048
)
def
forward
(
self
,
inp
):
# test manually convert to NHWC, usually used in detection head
return
self
.
conv1
(
self
.
conv0
(
inp
))
return
self
.
bn
(
self
.
conv1
(
self
.
conv0
(
inp
)))
inp
=
mge
.
tensor
(
np
.
ones
(
shape
=
(
32
,
32
,
56
,
56
)).
astype
(
"float32"
))
_compare_backward
([
inp
],
Net
(),
is_symbolic
)
# def func(x, w, b, bn_w, bn_b):
# x = F.conv2d(x, w, b, groups=2)
# x = F.batch_norm(
# x,
# 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=bn_w,
# bias=bn_b,
# training=True,
# inplace=True,
# )
# return x
# data = np.arange(0, 24).reshape((1, 2, 3, 4))
# x = tensor(data.transpose(0, 2, 3, 1), format="nhwc")
# w = tensor(np.ones((2, 1, 1, 1, 1)), format="nhwc")
# b = tensor(np.ones((1, 1, 1, 2)), format="nhwc")
# bn_w = tensor(np.ones((1, 1, 1, 2)), format="nhwc")
# bn_b = tensor(np.ones((1, 1, 1, 2)), format="nhwc")
# grads = [
# np.array([66, 210]).reshape((2, 1, 1, 1, 1)),
# np.array([12, 12]).reshape((1, 1, 1, 2)),
# np.array([12, 12]).reshape((1, 1, 1, 2)),
# np.array([12, 12]).reshape((1, 1, 1, 2)),
# ]
# _compare_backward(x, func, [w, b, bn_w, bn_b], grads, is_symbolic)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录