Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
7a8a2830
MegEngine
项目概览
MegEngine 天元
/
MegEngine
接近 2 年 前同步成功
通知
414
Star
4708
Fork
583
代码
文件
提交
分支
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看板
提交
7a8a2830
编写于
6月 28, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(quant): support nnie quant
GitOrigin-RevId: 8ca3f828bdd55e9bfcec072831aa161e8b961fa8
上级
d49a2971
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
38 addition
and
8 deletion
+38
-8
python_module/megengine/module/qat/module.py
python_module/megengine/module/qat/module.py
+3
-1
python_module/megengine/quantization/__init__.py
python_module/megengine/quantization/__init__.py
+2
-0
python_module/megengine/quantization/fake_quant.py
python_module/megengine/quantization/fake_quant.py
+11
-7
python_module/megengine/quantization/internal_fake_quant.py
python_module/megengine/quantization/internal_fake_quant.py
+19
-0
python_module/test/unit/quantization/test_fake_quant.py
python_module/test/unit/quantization/test_fake_quant.py
+3
-0
未找到文件。
python_module/megengine/module/qat/module.py
浏览文件 @
7a8a2830
...
...
@@ -49,6 +49,8 @@ class QATModule(Module):
def
_apply_fakequant_with_observer
(
self
,
target
:
Tensor
,
fake_quant
:
FakeQuantize
,
observer
:
Observer
):
if
observer
is
None
:
return
target
oup
=
observer
(
target
)
if
fake_quant
is
None
:
return
oup
...
...
@@ -76,7 +78,7 @@ class QATModule(Module):
r
"""
Get weight's quantization dtype as the method from ``qconfig``.
"""
if
hasattr
(
self
.
ac
t_fake_quant
,
"get_dtype"
):
if
hasattr
(
self
.
weigh
t_fake_quant
,
"get_dtype"
):
return
self
.
weight_fake_quant
.
get_dtype
()
else
:
return
self
.
weight_observer
.
get_dtype
()
...
...
python_module/megengine/quantization/__init__.py
浏览文件 @
7a8a2830
...
...
@@ -5,7 +5,9 @@
# Unless required by applicable law or agreed to in writing,
# 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
.internal_fake_quant
import
*
from
.observer
import
HistogramObserver
,
Observer
,
ObserverMode
from
.qconfig
import
(
QConfig
,
...
...
python_module/megengine/quantization/fake_quant.py
浏览文件 @
7a8a2830
...
...
@@ -19,6 +19,15 @@ from .observer import ObserverMode, Round
class
_FakeQuantize
(
Module
):
r
"""
A Basic Fake Quant module.
:param dtype: A string indicating the target quantization type of input.
:param narrow_range: Whether the absolute value of ``qmin`` is the same as ``qmax``,
instead of 1 greater. Usually True for weight and False for activation.
:param enable: Whether do ``normal_forward`` or ``fake_quant_forward``.
"""
def
__init__
(
self
,
dtype
:
str
,
narrow_range
:
bool
=
False
,
enable
:
bool
=
True
):
super
().
__init__
()
if
not
dtype
in
_metadata_dict
.
keys
():
...
...
@@ -92,9 +101,9 @@ class TQT_Function(Function):
class
TQT
(
_FakeQuantize
):
"""
r
"""
TQT: https://arxiv.org/abs/1903.08066 Trained Quantization Thresholds
for Accurate and Efficient Fixed-Point Inference of Deep Neural Networks
for Accurate and Efficient Fixed-Point Inference of Deep Neural Networks
.
"""
def
__init__
(
self
,
dtype
:
str
,
narrow_range
:
bool
=
False
,
enable
:
bool
=
True
):
...
...
@@ -119,11 +128,6 @@ class TQT(_FakeQuantize):
class
FakeQuantize
(
_FakeQuantize
):
r
"""
A module to do quant and dequant according to observer's scale and zero_point.
:param dtype: A string indicating the target quantization type of input.
:param narrow_range: Whether the absolute value of ``qmin`` is the same as ``qmax``,
instead of 1 greater. Usually True for weight and False for activation.
:param enable: Whether do ``normal_forward`` or ``fake_quant_forward``.
"""
def
fake_quant_forward
(
self
,
inp
,
q_dict
):
...
...
python_module/megengine/quantization/internal_fake_quant.py
0 → 100644
浏览文件 @
7a8a2830
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import
copy
import
math
from
functools
import
partial
import
numpy
as
np
from
..
import
functional
as
F
from
..core
import
Function
from
.fake_quant
import
_FakeQuantize
from
.observer
import
MinMaxObserver
from
.qconfig
import
QConfig
python_module/test/unit/quantization/test_
TQT
.py
→
python_module/test/unit/quantization/test_
fake_quant
.py
浏览文件 @
7a8a2830
...
...
@@ -13,6 +13,7 @@ import megengine as mge
import
megengine._internal
as
mgb
from
megengine.core
import
tensor
from
megengine.quantization.fake_quant
import
TQT_Function
from
megengine.quantization.internal_fake_quant
import
*
from
megengine.test
import
assertTensorClose
...
...
@@ -75,3 +76,5 @@ def test_TQT():
a
.
set_value
(
a_np
)
b
.
set_value
(
b_np
)
check_inp
(
a
,
b
,
b
,
a_np
,
b_np
,
b_np
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录