Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
6742a58b
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看板
提交
6742a58b
编写于
6月 09, 2020
作者:
M
Megvii Engine Team
提交者:
Xu Xinran
6月 19, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(quant): observer do not use cond_take
GitOrigin-RevId: a954814bcbbd81736da5dd383d9492efc1ef8ed1
上级
9e876203
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
26 addition
and
36 deletion
+26
-36
python_module/megengine/quantization/observer.py
python_module/megengine/quantization/observer.py
+26
-36
未找到文件。
python_module/megengine/quantization/observer.py
浏览文件 @
6742a58b
...
...
@@ -99,21 +99,9 @@ class MinMaxObserver(Observer):
def
__init__
(
self
,
mode
=
ObserverMode
.
SYMMERTIC
,
eps
=
0.00001
,
dtype
=
"qint8"
):
super
().
__init__
(
dtype
)
self
.
mode
=
mode
self
.
min_val
=
Buffer
(
0.0
,
dtype
=
np
.
float32
)
self
.
max_val
=
Buffer
(
0.0
,
dtype
=
np
.
float32
)
self
.
min_val
=
Buffer
(
np
.
finfo
(
np
.
float32
).
max
,
dtype
=
np
.
float32
)
self
.
max_val
=
Buffer
(
np
.
finfo
(
np
.
float32
).
min
,
dtype
=
np
.
float32
)
self
.
scale_limit
=
eps
# flag is used by cond_take, first time will be first flag, and after will be set as not_flag
self
.
first_flag
=
Buffer
(
np
.
array
([
1
,
0
],
dtype
=
np
.
int32
))
self
.
not_flag
=
Buffer
(
np
.
array
([
0
,
1
],
dtype
=
np
.
int32
))
def
set_min_max
(
self
,
tmp_min
,
tmp_max
):
# FIXME: cond_take will destory shape, use reshape to reset shape
tmp_min
=
tmp_min
.
reshape
(
1
)
tmp_max
=
tmp_max
.
reshape
(
1
)
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
)
def
_calculate_qparams
(
self
,
inp_min_val
,
inp_max_val
):
min_val
=
F
.
minimum
(
0.0
,
inp_min_val
)
...
...
@@ -144,13 +132,20 @@ class MinMaxObserver(Observer):
# stop gradient
x
=
F
.
zero_grad
(
x_orig
)
# find max and min
tmp_min
,
_
=
F
.
cond_take
(
self
.
first_flag
,
F
.
concat
([
x
.
min
(),
F
.
minimum
(
self
.
min_val
,
x
.
min
())])
F
.
add_update
(
self
.
min_val
,
F
.
minimum
(
self
.
min_val
,
x
.
min
()),
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
,
)
tmp_max
,
_
=
F
.
cond_take
(
self
.
first_flag
,
F
.
concat
([
x
.
max
(),
F
.
maximum
(
self
.
max_val
,
x
.
max
())])
F
.
add_update
(
self
.
max_val
,
F
.
maximum
(
self
.
max_val
,
x
.
max
()),
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
,
)
self
.
set_min_max
(
tmp_min
,
tmp_max
)
return
x_orig
...
...
@@ -160,6 +155,7 @@ class ExponentialMovingAverageObserver(MinMaxObserver):
):
super
().
__init__
(
mode
,
eps
,
dtype
)
self
.
momentum
=
Buffer
(
momentum
)
self
.
runtime_momentum
=
Buffer
(
0.0
)
def
set_momentum
(
self
,
momentum
):
self
.
momentum
.
set_value
(
momentum
)
...
...
@@ -169,25 +165,19 @@ class ExponentialMovingAverageObserver(MinMaxObserver):
# stop gradient
x
=
F
.
zero_grad
(
x_orig
)
# Exponential Moving Average
tmp_min
,
_
=
F
.
cond_take
(
self
.
first_flag
,
F
.
concat
(
[
x
.
min
(),
self
.
momentum
*
self
.
min_val
+
(
1
-
self
.
momentum
)
*
x
.
min
(),
]
),
tmp_min
=
(
self
.
min_val
*
self
.
runtime_momentum
+
(
1
-
self
.
runtime_momentum
)
*
x
.
min
()
)
tmp_max
=
(
self
.
max_val
*
self
.
runtime_momentum
+
(
1
-
self
.
runtime_momentum
)
*
x
.
max
()
)
tmp_max
,
_
=
F
.
cond_take
(
self
.
first_flag
,
F
.
concat
(
[
x
.
max
(),
self
.
momentum
*
self
.
max_val
+
(
1
-
self
.
momentum
)
*
x
.
max
(),
]
),
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
.
runtime_momentum
,
self
.
momentum
,
alpha
=
0.0
,
beta
=
1.0
,
bias
=
0.0
)
self
.
set_min_max
(
tmp_min
,
tmp_max
)
return
x_orig
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录