Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
7fa70df9
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看板
提交
7fa70df9
编写于
12月 06, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
style(imperative): remove absolute imports in megengine
GitOrigin-RevId: 700492906963551974b40f694949a1f80b67ab95
上级
cf1db261
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
38 addition
and
45 deletion
+38
-45
imperative/python/megengine/core/autodiff/grad.py
imperative/python/megengine/core/autodiff/grad.py
+0
-2
imperative/python/megengine/data/sampler.py
imperative/python/megengine/data/sampler.py
+1
-1
imperative/python/megengine/experimental/weight_scaler.py
imperative/python/megengine/experimental/weight_scaler.py
+4
-5
imperative/python/megengine/utils/module_stats.py
imperative/python/megengine/utils/module_stats.py
+33
-37
未找到文件。
imperative/python/megengine/core/autodiff/grad.py
浏览文件 @
7fa70df9
...
...
@@ -14,8 +14,6 @@ import weakref
import
numpy
as
np
import
megengine
as
mge
from
.._imperative_rt
import
core2
,
ops
from
..ops.builtin
import
Elemwise
,
OpDef
,
RemoteSend
from
..ops.special
import
Const
...
...
imperative/python/megengine/data/sampler.py
浏览文件 @
7fa70df9
...
...
@@ -13,7 +13,7 @@ from typing import Any, Generator, Iterator, List, Union
import
numpy
as
np
import
megengine.
distributed
as
dist
from
..
import
distributed
as
dist
class
Sampler
(
ABC
):
...
...
imperative/python/megengine/experimental/weight_scaler.py
浏览文件 @
7fa70df9
import
types
from
functools
import
partial
import
megengine.functional
as
F
import
megengine.module
as
M
from
megengine.functional.tensor
import
zeros
from
megengine.utils.module_utils
import
set_module_mode_safe
from
..
import
functional
as
F
from
..
import
module
as
M
from
..utils.module_utils
import
set_module_mode_safe
def
get_norm_mod_value
(
weight
,
norm_value
):
...
...
@@ -81,7 +80,7 @@ def get_scaled_model(model, scale_submodel, input_shape=None):
if
input_shape
is
None
:
raise
ValueError
(
"input_shape is required for calculating scale value"
)
input
=
zeros
(
input_shape
)
input
=
F
.
zeros
(
input_shape
)
hooks
=
[]
for
modelname
,
submodel
in
model
.
named_modules
():
...
...
imperative/python/megengine/utils/module_stats.py
浏览文件 @
7fa70df9
...
...
@@ -13,24 +13,20 @@ from typing import Iterable
import
numpy
as
np
import
tabulate
import
megengine
as
mge
import
megengine.module
as
m
import
megengine.module.qat
as
qatm
import
megengine.module.quantized
as
qm
from
megengine
import
Tensor
from
megengine
import
functional
as
F
from
megengine.core.tensor.dtype
import
get_dtype_bit
from
megengine.functional.tensor
import
zeros
from
megengine.tensor
import
Tensor
from
..
import
Tensor
from
..
import
functional
as
F
from
..
import
get_logger
from
..
import
module
as
M
from
..core.tensor.dtype
import
get_dtype_bit
from
..logger
import
MegEngineLogFormatter
from
.module_utils
import
set_module_mode_safe
try
:
mge
.
logger
.
MegEngineLogFormatter
.
max_lines
=
float
(
"inf"
)
MegEngineLogFormatter
.
max_lines
=
float
(
"inf"
)
except
AttributeError
as
e
:
raise
ValueError
(
"set logger max lines failed"
)
logger
=
mge
.
get_logger
(
__name__
)
logger
=
get_logger
(
__name__
)
logger
.
setLevel
(
"INFO"
)
...
...
@@ -95,9 +91,9 @@ def disable_receptive_field():
@
register_flops
(
m
.
Conv1d
,
m
.
Conv2d
,
m
.
Conv3d
,
m
.
ConvTranspose2d
,
m
.
LocalConv2d
,
m
.
DeformableConv2d
M
.
Conv1d
,
M
.
Conv2d
,
M
.
Conv3d
,
M
.
ConvTranspose2d
,
M
.
LocalConv2d
,
M
.
DeformableConv2d
)
def
flops_convNd
(
module
:
m
.
Conv2d
,
inputs
,
outputs
):
def
flops_convNd
(
module
:
M
.
Conv2d
,
inputs
,
outputs
):
bias
=
1
if
module
.
bias
is
not
None
else
0
# N x Cout x H x W x (Cin x Kw x Kh + bias)
return
np
.
prod
(
outputs
[
0
].
shape
)
*
(
...
...
@@ -106,14 +102,14 @@ def flops_convNd(module: m.Conv2d, inputs, outputs):
@
register_flops
(
m
.
batchnorm
.
_BatchNorm
,
m
.
SyncBatchNorm
,
m
.
GroupNorm
,
m
.
LayerNorm
,
m
.
InstanceNorm
,
M
.
batchnorm
.
_BatchNorm
,
M
.
SyncBatchNorm
,
M
.
GroupNorm
,
M
.
LayerNorm
,
M
.
InstanceNorm
,
)
def
flops_norm
(
module
:
m
.
Linear
,
inputs
,
outputs
):
def
flops_norm
(
module
:
M
.
Linear
,
inputs
,
outputs
):
return
np
.
prod
(
inputs
[
0
].
shape
)
*
7
@
register_flops
(
m
.
AvgPool2d
,
m
.
MaxPool2d
)
def
flops_pool
(
module
:
m
.
AvgPool2d
,
inputs
,
outputs
):
@
register_flops
(
M
.
AvgPool2d
,
M
.
MaxPool2d
)
def
flops_pool
(
module
:
M
.
AvgPool2d
,
inputs
,
outputs
):
kernel_sum
=
0
if
isinstance
(
module
.
kernel_size
,
tuple
)
and
len
(
module
.
kernel_size
)
==
2
:
kernel_sum
=
np
.
prod
(
module
.
kernel_size
)
...
...
@@ -122,8 +118,8 @@ def flops_pool(module: m.AvgPool2d, inputs, outputs):
return
np
.
prod
(
outputs
[
0
].
shape
)
*
kernel_sum
@
register_flops
(
m
.
AdaptiveAvgPool2d
,
m
.
AdaptiveMaxPool2d
)
def
flops_adaptivePool
(
module
:
m
.
AdaptiveAvgPool2d
,
inputs
,
outputs
):
@
register_flops
(
M
.
AdaptiveAvgPool2d
,
M
.
AdaptiveMaxPool2d
)
def
flops_adaptivePool
(
module
:
M
.
AdaptiveAvgPool2d
,
inputs
,
outputs
):
stride_h
=
np
.
floor
(
inputs
[
0
].
shape
[
2
]
/
(
inputs
[
0
].
shape
[
2
]
-
1
))
kernel_h
=
inputs
[
0
].
shape
[
2
]
-
(
inputs
[
0
].
shape
[
2
]
-
1
)
*
stride_h
stride_w
=
np
.
floor
(
inputs
[
0
].
shape
[
3
]
/
(
inputs
[
0
].
shape
[
3
]
-
1
))
...
...
@@ -131,14 +127,14 @@ def flops_adaptivePool(module: m.AdaptiveAvgPool2d, inputs, outputs):
return
np
.
prod
(
outputs
[
0
].
shape
)
*
kernel_h
*
kernel_w
@
register_flops
(
m
.
Linear
)
def
flops_linear
(
module
:
m
.
Linear
,
inputs
,
outputs
):
@
register_flops
(
M
.
Linear
)
def
flops_linear
(
module
:
M
.
Linear
,
inputs
,
outputs
):
bias
=
module
.
out_features
if
module
.
bias
is
not
None
else
0
return
np
.
prod
(
outputs
[
0
].
shape
)
*
module
.
in_features
+
bias
@
register_flops
(
m
.
BatchMatMulActivation
)
def
flops_batchmatmul
(
module
:
m
.
BatchMatMulActivation
,
inputs
,
outputs
):
@
register_flops
(
M
.
BatchMatMulActivation
)
def
flops_batchmatmul
(
module
:
M
.
BatchMatMulActivation
,
inputs
,
outputs
):
bias
=
1
if
module
.
bias
is
not
None
else
0
x
=
inputs
[
0
]
w
=
module
.
weight
...
...
@@ -150,25 +146,25 @@ def flops_batchmatmul(module: m.BatchMatMulActivation, inputs, outputs):
# does not need import qat and quantized module since they inherit from float module.
hook_modules
=
(
m
.
conv
.
_ConvNd
,
m
.
Linear
,
m
.
BatchMatMulActivation
,
m
.
batchnorm
.
_BatchNorm
,
m
.
LayerNorm
,
m
.
GroupNorm
,
m
.
InstanceNorm
,
m
.
pooling
.
_PoolNd
,
m
.
adaptive_pooling
.
_AdaptivePoolNd
,
M
.
conv
.
_ConvNd
,
M
.
Linear
,
M
.
BatchMatMulActivation
,
M
.
batchnorm
.
_BatchNorm
,
M
.
LayerNorm
,
M
.
GroupNorm
,
M
.
InstanceNorm
,
M
.
pooling
.
_PoolNd
,
M
.
adaptive_pooling
.
_AdaptivePoolNd
,
)
def
_mean
(
inp
):
inp
=
mge
.
t
ensor
(
inp
).
astype
(
np
.
float32
)
inp
=
T
ensor
(
inp
).
astype
(
np
.
float32
)
return
F
.
mean
(
inp
).
numpy
()
def
_std
(
inp
):
inp
=
mge
.
t
ensor
(
inp
).
astype
(
np
.
float32
)
inp
=
T
ensor
(
inp
).
astype
(
np
.
float32
)
return
F
.
std
(
inp
).
numpy
()
...
...
@@ -412,7 +408,7 @@ def print_summary(**kwargs):
def
module_stats
(
model
:
m
.
Module
,
model
:
M
.
Module
,
inputs
:
Iterable
[
np
.
ndarray
]
=
None
,
input_shapes
:
list
=
None
,
cal_params
:
bool
=
True
,
...
...
@@ -457,7 +453,7 @@ def module_stats(
if
input_shapes
:
if
not
isinstance
(
input_shapes
[
0
],
tuple
):
input_shapes
=
[
input_shapes
]
inputs
=
[
zeros
(
in_size
,
dtype
=
np
.
float32
)
for
in_size
in
input_shapes
]
inputs
=
[
F
.
zeros
(
in_size
,
dtype
=
np
.
float32
)
for
in_size
in
input_shapes
]
else
:
logger
.
error
(
"Inputs or input_shapes is required for running model and calculating stats."
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录