Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
7b2c5a73
MegEngine
项目概览
MegEngine 天元
/
MegEngine
大约 1 年 前同步成功
通知
399
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7b2c5a73
编写于
6月 01, 2020
作者:
M
Megvii Engine Team
提交者:
Xu Xinran
6月 19, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mge/quantization): fix histogram observer load and store issue
GitOrigin-RevId: b0a2b476e4490f960fc6fde48a715bbdab5ce128
上级
e6820b91
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
33 addition
and
40 deletion
+33
-40
python_module/megengine/quantization/__init__.py
python_module/megengine/quantization/__init__.py
+1
-1
python_module/megengine/quantization/observer.py
python_module/megengine/quantization/observer.py
+32
-39
未找到文件。
python_module/megengine/quantization/__init__.py
浏览文件 @
7b2c5a73
...
...
@@ -6,7 +6,7 @@
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from
.fake_quant
import
FakeQuantize
from
.observer
import
HistogramObserver
,
Observer
from
.observer
import
HistogramObserver
,
Observer
,
ObserverMode
from
.qconfig
import
(
QConfig
,
calibration_qconfig
,
...
...
python_module/megengine/quantization/observer.py
浏览文件 @
7b2c5a73
...
...
@@ -132,7 +132,7 @@ class MinMaxObserver(Observer):
(
max_val
-
min_val
)
/
(
self
.
qmax
-
self
.
qmin
),
self
.
scale_limit
,
)
# caculate zero_point
q_dict
[
"zero_point"
]
=
self
.
qmin
-
Round
()((
min_val
/
scale
))
q_dict
[
"zero_point"
]
=
self
.
qmin
-
Round
()((
min_val
/
q_dict
[
"scale"
]
))
return
q_dict
...
...
@@ -204,7 +204,7 @@ class HistogramObserver(MinMaxObserver):
self
.
bins
=
bins
self
.
upsample_rate
=
upsample_rate
self
.
dst_nbins
=
_metadata_dict
[
dtype
].
qmax
-
_metadata_dict
[
dtype
].
qmin
+
1
self
.
histogram
=
None
self
.
histogram
=
Buffer
([
0.0
]
*
bins
)
def
_non_linear_param_search
(
self
):
r
"""Non-linear parameter search.
...
...
@@ -212,6 +212,12 @@ class HistogramObserver(MinMaxObserver):
By selecting new min/max, we filter out outliers in input distribution.
"""
np_min_val
=
self
.
min_val
.
numpy
()[
0
]
np_max_val
=
self
.
max_val
.
numpy
()[
0
]
np_histogram
=
self
.
histogram
.
numpy
()
assert
len
(
np_histogram
)
==
self
.
bins
,
"bins mistmatch"
bin_width
=
(
np_max_val
-
np_min_val
)
/
self
.
bins
def
_get_norm
(
delta_begin
,
delta_end
,
density
,
norm_type
):
r
"""
Compute the norm of the values uniformaly distributed between
...
...
@@ -233,9 +239,6 @@ class HistogramObserver(MinMaxObserver):
Compute the quantization error if we use start_bin to end_bin as the
min and max to do the quantization.
"""
np_min_val
=
self
.
min_val
.
numpy
()[
0
]
np_max_val
=
self
.
max_val
.
numpy
()[
0
]
bin_width
=
(
np_max_val
-
np_min_val
)
/
self
.
bins
norm
=
0.0
dst_bin_width
=
(
...
...
@@ -262,7 +265,7 @@ class HistogramObserver(MinMaxObserver):
dst_bin_of_begin
*
dst_bin_width
+
dst_bin_width
/
2
)
density
=
self
.
histogram
[
src_bin
]
/
bin_width
density
=
np_
histogram
[
src_bin
]
/
bin_width
if
dst_bin_of_begin
==
dst_bin_of_end
:
# if src_bin is entirely within 1 dst_bin
delta_begin
=
src_bin_begin
-
dst_bin_of_begin_center
...
...
@@ -286,12 +289,9 @@ class HistogramObserver(MinMaxObserver):
norm
=
norm
+
_get_norm
(
delta_begin
,
delta_end
,
density
,
norm_type
)
return
norm
assert
len
(
self
.
histogram
)
==
self
.
bins
,
"bins mistmatch"
bin_width
=
(
self
.
max_val
-
self
.
min_val
)
/
self
.
bins
# cumulative sum
total
=
sum
(
self
.
histogram
)
cSum
=
np
.
cumsum
(
self
.
histogram
,
axis
=
0
)
total
=
sum
(
np_
histogram
)
cSum
=
np
.
cumsum
(
np_
histogram
,
axis
=
0
)
stepsize
=
1e-5
# granularity
alpha
=
0.0
# lower bound
...
...
@@ -400,46 +400,39 @@ class HistogramObserver(MinMaxObserver):
x
=
x_orig
.
numpy
()
min_val
=
self
.
min_val
.
numpy
()[
0
]
max_val
=
self
.
max_val
.
numpy
()[
0
]
histogram
=
self
.
histogram
.
numpy
()
new_min
=
x
.
min
()
new_max
=
x
.
max
()
if
min_val
==
0
or
max_val
==
0
:
min_val
=
x
.
min
()
max_val
=
x
.
max
()
self
.
min_val
.
set_value
(
min_val
)
self
.
max_val
.
set_value
(
max_val
)
self
.
histogram
,
_
=
np
.
histogram
(
x
,
self
.
bins
,
(
min_val
,
max_val
))
self
.
histogram
=
self
.
histogram
.
astype
(
np
.
float64
)
new_histogram
,
_
=
np
.
histogram
(
x
,
self
.
bins
,
(
new_min
,
new_max
))
else
:
new_min
=
x
.
min
()
new_max
=
x
.
max
()
combined_min
=
min
(
new_min
,
min_val
)
combined_max
=
max
(
new_max
,
max_val
)
new_min
=
min
(
new_min
,
min_val
)
new_max
=
max
(
new_max
,
max_val
)
# combine the existing histogram and new histogram into 1 histogram
# We do this by first upsampling the histogram to a dense grid
# and then downsampling the histogram efficiently
(
combined_min
,
combined_max
,
downsample_rate
,
start_idx
,
)
=
self
.
_adjust_min_max
(
combined_min
,
combined_max
,
self
.
upsample_rate
)
combined_histogram
,
_
=
np
.
histogram
(
x
,
self
.
bins
,
(
combined_min
,
combined_max
)
(
new_min
,
new_max
,
downsample_rate
,
start_idx
,)
=
self
.
_adjust_min_max
(
new_min
,
new_max
,
self
.
upsample_rate
)
combined_histogram
=
combined_histogram
.
astype
(
np
.
float64
)
if
combined_min
==
min_val
and
combined_max
==
max_val
:
combined_histogram
+=
self
.
histogram
new_histogram
,
_
=
np
.
histogram
(
x
,
self
.
bins
,
(
new_min
,
new_max
))
new_histogram
=
new_histogram
.
astype
(
np
.
float64
)
if
new_min
==
min_val
and
new_max
==
max_val
:
new_histogram
+=
histogram
else
:
combined
_histogram
=
self
.
_combine_histograms
(
combined
_histogram
,
self
.
histogram
,
new
_histogram
=
self
.
_combine_histograms
(
new
_histogram
,
histogram
,
self
.
upsample_rate
,
downsample_rate
,
start_idx
,
self
.
bins
,
)
self
.
histogram
=
combined_histogram
self
.
min_val
.
set_value
(
combined_min
)
self
.
max_val
.
set_value
(
combined_max
)
self
.
histogram
.
set_value
(
new_histogram
)
self
.
min_val
.
set_value
(
new_min
)
self
.
max_val
.
set_value
(
new_max
)
def
forward
(
self
,
x_orig
):
self
.
sideeffect_forward
(
x_orig
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录