Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
19af2688
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
403
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看板
提交
19af2688
编写于
7月 05, 2022
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mge/tools): fix module_stats for duplicated module
GitOrigin-RevId: a15f17d6160a7ce80ec3f15b81822b476a7166c1
上级
4cd4a38a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
72 addition
and
3 deletion
+72
-3
imperative/python/megengine/utils/module_stats.py
imperative/python/megengine/utils/module_stats.py
+13
-3
imperative/python/test/unit/utils/test_module_stats.py
imperative/python/test/unit/utils/test_module_stats.py
+59
-0
未找到文件。
imperative/python/megengine/utils/module_stats.py
浏览文件 @
19af2688
...
...
@@ -457,6 +457,7 @@ def module_stats(
log_activations
=
False
disable_receptive_field
()
recorded_parameters
=
set
()
def
module_stats_hook
(
module
,
inputs
,
outputs
,
name
=
""
):
class_name
=
str
(
module
.
__class__
).
split
(
"."
)[
-
1
].
split
(
"'"
)[
0
]
...
...
@@ -468,17 +469,27 @@ def module_stats(
flops
.
append
(
flops_stats
)
if
cal_params
:
if
hasattr
(
module
,
"weight"
)
and
module
.
weight
is
not
None
:
if
(
hasattr
(
module
,
"weight"
)
and
(
module
.
weight
is
not
None
)
and
module
.
weight
not
in
recorded_parameters
):
w
=
module
.
weight
param_stats
=
get_param_stats
(
w
)
param_stats
[
"name"
]
=
name
+
"-w"
params
.
append
(
param_stats
)
recorded_parameters
.
add
(
w
)
if
hasattr
(
module
,
"bias"
)
and
module
.
bias
is
not
None
:
if
(
hasattr
(
module
,
"bias"
)
and
module
.
bias
is
not
None
and
module
.
bias
not
in
recorded_parameters
):
b
=
module
.
bias
param_stats
=
get_param_stats
(
b
)
param_stats
[
"name"
]
=
name
+
"-b"
params
.
append
(
param_stats
)
recorded_parameters
.
add
(
b
)
if
cal_activations
:
if
not
isinstance
(
outputs
,
(
tuple
,
list
)):
...
...
@@ -504,7 +515,6 @@ def module_stats(
hooks
.
append
(
module
.
register_forward_hook
(
partial
(
module_stats_hook
,
name
=
name
))
)
with
set_module_mode_safe
(
model
,
training
=
False
)
as
model
:
model
(
*
inputs
)
...
...
imperative/python/test/unit/utils/test_module_stats.py
浏览文件 @
19af2688
...
...
@@ -42,6 +42,65 @@ def test_other_input_module_state():
net
(
_nt
)
@
pytest
.
mark
.
skipif
(
use_symbolic_shape
(),
reason
=
"This test do not support symbolic shape."
,
)
def
test_duplicated_module
():
input_shape
=
(
1
,
3
,
224
,
224
)
net0
=
TestNet0
()
net0_stats
,
_
=
module_stats
(
net0
,
input_shapes
=
input_shape
)
net1
=
TestNet1
()
net1_stats
,
_
=
module_stats
(
net1
,
input_shapes
=
input_shape
)
net2
=
TestNet2
()
net2_stats
,
_
=
module_stats
(
net2
,
input_shapes
=
input_shape
)
assert
net0_stats
.
param_dims
==
net1_stats
.
param_dims
assert
net0_stats
.
param_size
==
net1_stats
.
param_size
assert
net0_stats
.
param_dims
==
net2_stats
.
param_dims
assert
net0_stats
.
param_size
==
net2_stats
.
param_size
class
TestNet0
(
M
.
Module
):
def
__init__
(
self
):
super
().
__init__
()
self
.
conv
=
M
.
Conv2d
(
3
,
3
,
3
,
padding
=
(
1
,
1
))
self
.
conv
.
bias
=
mge
.
Parameter
(
np
.
random
.
random
(
self
.
conv
.
bias
.
shape
).
astype
(
np
.
float32
)
)
def
forward
(
self
,
x
):
x
=
self
.
conv
(
x
)
return
x
class
TestNet1
(
TestNet0
):
def
__init__
(
self
):
super
().
__init__
()
self
.
conv1
=
self
.
conv
def
forward
(
self
,
x
):
x
=
self
.
conv
(
x
)
x
=
self
.
conv1
(
x
)
return
x
class
TestNet2
(
TestNet0
):
def
__init__
(
self
):
super
().
__init__
()
self
.
conv1
=
M
.
Conv2d
(
3
,
3
,
3
,
padding
=
(
1
,
1
))
self
.
conv1
.
weight
=
self
.
conv
.
weight
self
.
conv1
.
bias
=
self
.
conv
.
bias
def
forward
(
self
,
x
):
x
=
self
.
conv
(
x
)
x
=
self
.
conv1
(
x
)
return
x
class
FakeNet
(
M
.
Module
):
def
__init__
(
self
):
super
().
__init__
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录