Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
de75dae8
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看板
提交
de75dae8
编写于
5月 12, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mge/quantization): fix get scale issue
GitOrigin-RevId: 99068d74220ce4030bfe4ca050df46d8dd1fd590
上级
2f3d185d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
14 addition
and
50 deletion
+14
-50
python_module/megengine/module/module.py
python_module/megengine/module/module.py
+2
-1
python_module/megengine/quantization/observer.py
python_module/megengine/quantization/observer.py
+12
-49
未找到文件。
python_module/megengine/module/module.py
浏览文件 @
de75dae8
...
...
@@ -500,7 +500,8 @@ class QATModule(Module):
self
,
target
:
Tensor
,
fq
:
"FakeQuantize"
,
obs
:
"Observer"
):
oup
=
self
.
apply_observer
(
target
,
obs
)
return
fq
(
oup
,
obs
.
scale
,
obs
.
zero_point
)
scale
,
zero_point
=
obs
.
get_qparams
()
return
fq
(
oup
,
scale
,
zero_point
)
def
set_qat_mode
(
self
,
mode
:
QATMode
):
r
"""
...
...
python_module/megengine/quantization/observer.py
浏览文件 @
de75dae8
...
...
@@ -41,7 +41,6 @@ class Observer(Module):
self
.
dtype
=
dtype
self
.
qmin
=
_metadata_dict
[
dtype
].
qmin
self
.
qmax
=
_metadata_dict
[
dtype
].
qmax
self
.
zero_point
,
self
.
scale
=
None
,
None
self
.
enabled
=
True
def
get_dtype
(
self
):
...
...
@@ -72,23 +71,6 @@ class Observer(Module):
pass
class
IdentityObserver
(
Observer
):
r
"""
An test Observer that always return scale:1 and zero_point:0.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
self
.
zero_point
=
ones
((
1
),
dtype
=
"float32"
)
self
.
scale
=
zeros
((
1
),
dtype
=
"float32"
)
def
forward
(
self
,
x
):
return
x
def
get_qparams
(
self
):
return
self
.
scale
,
self
.
zero_point
class
MinMaxObserver
(
Observer
):
def
__init__
(
self
,
symmetric
=
True
,
eps
=
0.00001
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
...
...
@@ -108,47 +90,28 @@ class MinMaxObserver(Observer):
# FIXME: cond_take will destory shape, use reshape to reset shape
tmp_min
=
tmp_min
.
reshape
(
1
)
tmp_max
=
tmp_max
.
reshape
(
1
)
if
self
.
training
:
F
.
zero_grad
(
F
.
add_update
(
self
.
min_val
,
tmp_min
,
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
)
)
F
.
zero_grad
(
F
.
add_update
(
self
.
max_val
,
tmp_max
,
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
)
)
F
.
zero_grad
(
F
.
add_update
(
self
.
first_flag
,
self
.
not_flag
,
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
)
)
F
.
add_update
(
self
.
min_val
,
tmp_min
,
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
)
F
.
add_update
(
self
.
max_val
,
tmp_max
,
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
)
F
.
add_update
(
self
.
first_flag
,
self
.
not_flag
,
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
)
# FIXME: add_update is applied after the whole trace procedure in `symbolic=True`
# mode. So use tmp_min/tmp_max to calc and save scale/zero_point for further
# calculation in FakeQuant.
self
.
set_scale_zero_point
(
tmp_min
,
tmp_max
)
def
set_scale_zero_point
(
self
,
tmp_min
,
tmp_max
):
def
get_qparams
(
self
):
if
self
.
symmetric
:
symmetric_max_vals
=
F
.
maximum
(
-
tmp_min
,
tmp_max
)
symmetric_max_vals
=
F
.
maximum
(
-
self
.
min_val
,
self
.
max_val
)
# use maximun to avoid scale too small at the begin
s
elf
.
s
cale
=
F
.
maximum
(
scale
=
F
.
maximum
(
symmetric_max_vals
/
((
self
.
qmax
-
self
.
qmin
)
/
2
),
self
.
scale_limit
)
#
zero_point = self.zero_point
zero_point
=
self
.
zero_point
else
:
# use maximun to avoid scale too small at the begin
self
.
scale
=
F
.
maximum
(
(
tmp_max
-
tmp_min
)
/
(
self
.
qmax
-
self
.
qmin
),
self
.
scale_limit
scale
=
F
.
maximum
(
(
self
.
max_val
-
self
.
min_val
)
/
(
self
.
qmax
-
self
.
qmin
),
self
.
scale_limit
,
)
# caculate zero_point
self
.
zero_point
=
self
.
qmin
-
Round
()((
tmp_min
/
self
.
scale
))
def
get_qparams
(
self
):
# scale and zero_point is runtime tensor rather than Buffer,
# so need to re-calc if min_val and max_val are loaded.
if
self
.
scale
is
None
:
self
.
set_scale_zero_point
(
self
.
min_val
,
self
.
max_val
)
zero_point
=
self
.
qmin
-
Round
()((
self
.
min_val
/
scale
))
return
s
elf
.
scale
,
self
.
zero_point
return
s
cale
,
zero_point
def
forward
(
self
,
x_orig
):
if
self
.
enabled
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录