Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
35c71276
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看板
提交
35c71276
编写于
8月 04, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mge/quant): fix TQT epoch scale change bug
GitOrigin-RevId: 6e39de9cecbfae2b4f1e0acdf34f821af0e339bb
上级
e6e41242
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
39 addition
and
63 deletion
+39
-63
python_module/megengine/module/qat/elemwise.py
python_module/megengine/module/qat/elemwise.py
+1
-3
python_module/megengine/module/qat/module.py
python_module/megengine/module/qat/module.py
+15
-23
python_module/megengine/module/qat/quant_dequant.py
python_module/megengine/module/qat/quant_dequant.py
+3
-7
python_module/megengine/quantization/fake_quant.py
python_module/megengine/quantization/fake_quant.py
+5
-4
python_module/megengine/quantization/observer.py
python_module/megengine/quantization/observer.py
+1
-0
python_module/megengine/quantization/quantize.py
python_module/megengine/quantization/quantize.py
+14
-26
未找到文件。
python_module/megengine/module/qat/elemwise.py
浏览文件 @
35c71276
...
...
@@ -17,9 +17,7 @@ class Elemwise(Float.Elemwise, QATModule):
:param method: the elemwise method, see :class:`~.module.elemwise.Elemwise` for detail.
"""
def
__init__
(
self
,
method
):
super
().
__init__
(
method
)
self
.
with_weight
=
False
with_weight
=
False
def
forward
(
self
,
*
inps
):
return
self
.
apply_quant_activation
(
super
().
forward
(
*
inps
))
...
...
python_module/megengine/module/qat/module.py
浏览文件 @
35c71276
...
...
@@ -23,6 +23,9 @@ class QATModule(Module):
:func:`~.quantize.quantize` further.
"""
with_weight
=
True
with_act
=
True
def
__init__
(
self
):
super
().
__init__
()
...
...
@@ -32,9 +35,6 @@ class QATModule(Module):
self
.
weight_fake_quant
=
None
# type: FakeQuantize
self
.
act_fake_quant
=
None
# type: FakeQuantize
self
.
with_weight
=
True
self
.
with_act
=
True
def
set_qconfig
(
self
,
qconfig
:
QConfig
):
r
"""
Set quantization related configs with ``qconfig``, including
...
...
@@ -51,29 +51,21 @@ class QATModule(Module):
self
.
weight_observer
=
safe_call
(
qconfig
.
weight_observer
)
self
.
weight_fake_quant
=
safe_call
(
qconfig
.
weight_fake_quant
)
def
_enable_exec
(
self
,
with_module
,
func
,
enable
):
if
not
with_module
:
return
if
enable
:
func
.
enable
()
else
:
func
.
disable
()
def
set_fake_quant
(
self
,
enable
):
if
self
.
with_act
:
if
enable
:
self
.
act_fake_quant
.
enable
()
else
:
self
.
act_fake_quant
.
disable
()
if
self
.
with_weight
:
if
enable
:
self
.
weight_fake_quant
.
enable
()
else
:
self
.
weight_fake_quant
.
disable
()
self
.
_enable_exec
(
self
.
with_act
,
self
.
act_fake_quant
,
enable
)
self
.
_enable_exec
(
self
.
with_weight
,
self
.
weight_fake_quant
,
enable
)
def
set_observer
(
self
,
enable
):
if
self
.
with_act
:
if
enable
:
self
.
act_observer
.
enable
()
else
:
self
.
act_observer
.
disable
()
if
self
.
with_weight
:
if
enable
:
self
.
weight_observer
.
enable
()
else
:
self
.
weight_observer
.
disable
()
self
.
_enable_exec
(
self
.
with_act
,
self
.
act_observer
,
enable
)
self
.
_enable_exec
(
self
.
with_weight
,
self
.
weight_observer
,
enable
)
def
_apply_fakequant_with_observer
(
self
,
target
:
Tensor
,
fake_quant
:
FakeQuantize
,
observer
:
Observer
...
...
python_module/megengine/module/qat/quant_dequant.py
浏览文件 @
35c71276
...
...
@@ -15,9 +15,7 @@ class QuantStub(Float.QuantStub, QATModule):
input after converted to :class:`~.QuantizedModule`.
"""
def
__init__
(
self
):
super
().
__init__
()
self
.
with_weight
=
False
with_weight
=
False
def
forward
(
self
,
inp
):
return
self
.
apply_quant_activation
(
inp
)
...
...
@@ -37,10 +35,8 @@ class DequantStub(Float.DequantStub, QATModule):
input after converted to :class:`~.QuantizedModule`.
"""
def
__init__
(
self
):
super
().
__init__
()
self
.
with_weight
=
False
self
.
with_act
=
False
with_weight
=
False
with_act
=
False
def
forward
(
self
,
inp
):
return
inp
...
...
python_module/megengine/quantization/fake_quant.py
浏览文件 @
35c71276
...
...
@@ -116,10 +116,11 @@ class TQT(_FakeQuantize):
return
TQT_Function
(
self
.
qmin
,
self
.
qmax
)(
inp
,
self
.
scale
)
def
normal_foward
(
self
,
inp
,
q_dict
):
# when disable, TQT will do normal forward, initialize scale weight
tmp_scale
=
F
.
maximum
(
F
.
abs
(
q_dict
[
"min_val"
]),
F
.
abs
(
q_dict
[
"max_val"
]))
tmp_scale
=
F
.
log
(
tmp_scale
/
127
)
/
F
.
log
(
2
)
F
.
add_update
(
self
.
scale
,
tmp_scale
,
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
)
if
q_dict
[
"enable_observer"
]:
# when disable, TQT will do normal forward, initialize scale weight
tmp_scale
=
F
.
maximum
(
F
.
abs
(
q_dict
[
"min_val"
]),
F
.
abs
(
q_dict
[
"max_val"
]))
tmp_scale
=
F
.
log
(
tmp_scale
/
127
)
/
F
.
log
(
2
)
F
.
add_update
(
self
.
scale
,
tmp_scale
,
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
)
return
inp
def
get_qparams
(
self
):
...
...
python_module/megengine/quantization/observer.py
浏览文件 @
35c71276
...
...
@@ -102,6 +102,7 @@ class MinMaxObserver(Observer):
q_dict
=
get_qparam_dict
(
self
.
mode
)
q_dict
[
"min_val"
]
=
inp_min_val
q_dict
[
"max_val"
]
=
inp_max_val
q_dict
[
"enable_observer"
]
=
self
.
enable
if
self
.
mode
==
QuantMode
.
SYMMERTIC
:
symmetric_max_vals
=
F
.
maximum
(
-
min_val
,
max_val
)
# use maximun to avoid scale too small at the begin
...
...
python_module/megengine/quantization/quantize.py
浏览文件 @
35c71276
...
...
@@ -14,6 +14,7 @@ from ..module import qat as QAT
from
..module
import
quantized
as
Quantized
from
..module.qat
import
QATModule
from
..module.quantized
import
QuantizedModule
from
.fake_quant
import
TQT
from
.qconfig
import
QConfig
,
ema_fakequant_qconfig
...
...
@@ -119,6 +120,14 @@ def quantize_qat(
return
module
def
_propagate
(
module
:
Module
,
func_str
:
str
,
*
args
,
**
kargs
):
def
fn
(
mod
:
Module
):
if
isinstance
(
mod
,
QATModule
):
getattr
(
mod
,
func_str
)(
*
args
,
**
kargs
)
module
.
apply
(
fn
)
def
propagate_qconfig
(
module
:
QATModule
,
qconfig
:
QConfig
):
r
"""
Recursively set ``module``'s qconfig through :meth:`~.Module.apply`.
...
...
@@ -126,12 +135,7 @@ def propagate_qconfig(module: QATModule, qconfig: QConfig):
:param module: root module to traverse recursively.
:param qconfig: a instance of :class:`~.QConfig` to be set as submodules' qconfig.
"""
def
fn
(
mod
:
Module
):
if
isinstance
(
mod
,
QATModule
):
mod
.
set_qconfig
(
qconfig
)
module
.
apply
(
fn
)
_propagate
(
module
,
"set_qconfig"
,
qconfig
)
def
disable_fake_quant
(
module
:
Module
):
...
...
@@ -141,11 +145,7 @@ def disable_fake_quant(module: Module):
:param module: root module to do disable fake quantization recursively.
"""
def
fn
(
mod
:
Module
):
if
isinstance
(
mod
,
QATModule
):
mod
.
set_fake_quant
(
False
)
module
.
apply
(
fn
)
_propagate
(
module
,
"set_fake_quant"
,
False
)
def
disable_observer
(
module
:
Module
):
...
...
@@ -155,11 +155,7 @@ def disable_observer(module: Module):
:param module: root module to do disable observer recursively.
"""
def
fn
(
mod
:
Module
):
if
isinstance
(
mod
,
QATModule
):
self
.
set_observer
(
False
)
module
.
apply
(
fn
)
_propagate
(
module
,
"set_observer"
,
False
)
def
enable_fake_quant
(
module
:
Module
):
...
...
@@ -169,11 +165,7 @@ def enable_fake_quant(module: Module):
:param module: root module to do enable fake quantization recursively.
"""
def
fn
(
mod
:
Module
):
if
isinstance
(
mod
,
QATModule
):
mod
.
set_fake_quant
(
True
)
module
.
apply
(
fn
)
_propagate
(
module
,
"set_fake_quant"
,
True
)
def
enable_observer
(
module
:
Module
):
...
...
@@ -183,8 +175,4 @@ def enable_observer(module: Module):
:param module: root module to do enable observer recursively.
"""
def
fn
(
mod
:
Module
):
if
isinstance
(
mod
,
QATModule
):
mod
.
set_observer
(
True
)
module
.
apply
(
fn
)
_propagate
(
module
,
"set_observer"
,
False
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录