Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
ce88e6c4
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看板
提交
ce88e6c4
编写于
2月 25, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/quantization): use extra act_fakequant to decide whether to do bias fakequant
GitOrigin-RevId: bf54012155292a5270db34d49cc5e761776b9ad3
上级
1d7dd001
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
32 addition
and
21 deletion
+32
-21
imperative/python/megengine/module/qat/batch_matmul_activation.py
...ve/python/megengine/module/qat/batch_matmul_activation.py
+1
-3
imperative/python/megengine/module/qat/conv.py
imperative/python/megengine/module/qat/conv.py
+1
-5
imperative/python/megengine/module/qat/conv_bn.py
imperative/python/megengine/module/qat/conv_bn.py
+1
-5
imperative/python/megengine/module/qat/linear.py
imperative/python/megengine/module/qat/linear.py
+3
-7
imperative/python/megengine/module/qat/module.py
imperative/python/megengine/module/qat/module.py
+19
-0
imperative/python/megengine/quantization/__init__.py
imperative/python/megengine/quantization/__init__.py
+7
-1
未找到文件。
imperative/python/megengine/module/qat/batch_matmul_activation.py
浏览文件 @
ce88e6c4
...
@@ -5,8 +5,6 @@
...
@@ -5,8 +5,6 @@
# Unless required by applicable law or agreed to in writing,
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from
...quantization.utils
import
fake_quant_bias
from
..
import
batch_matmul_activation
as
Float
from
..
import
batch_matmul_activation
as
Float
from
.module
import
QATModule
from
.module
import
QATModule
...
@@ -18,7 +16,7 @@ class BatchMatMulActivation(Float.BatchMatMulActivation, QATModule):
...
@@ -18,7 +16,7 @@ class BatchMatMulActivation(Float.BatchMatMulActivation, QATModule):
def
forward
(
self
,
inp
):
def
forward
(
self
,
inp
):
w_qat
=
self
.
apply_quant_weight
(
self
.
weight
)
w_qat
=
self
.
apply_quant_weight
(
self
.
weight
)
b_qat
=
fake
_quant_bias
(
self
.
bias
,
inp
,
w_qat
)
b_qat
=
self
.
apply
_quant_bias
(
self
.
bias
,
inp
,
w_qat
)
return
self
.
apply_quant_activation
(
self
.
_calc_linear
(
inp
,
w_qat
,
b_qat
))
return
self
.
apply_quant_activation
(
self
.
_calc_linear
(
inp
,
w_qat
,
b_qat
))
@
classmethod
@
classmethod
...
...
imperative/python/megengine/module/qat/conv.py
浏览文件 @
ce88e6c4
...
@@ -6,7 +6,6 @@
...
@@ -6,7 +6,6 @@
# software distributed under the License is distributed on an
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from
...
import
functional
as
F
from
...
import
functional
as
F
from
...quantization.utils
import
fake_quant_bias
from
..
import
conv
as
Float
from
..
import
conv
as
Float
from
.module
import
QATModule
from
.module
import
QATModule
...
@@ -19,10 +18,7 @@ class Conv2d(Float.Conv2d, QATModule):
...
@@ -19,10 +18,7 @@ class Conv2d(Float.Conv2d, QATModule):
def
calc_conv_qat
(
self
,
inp
):
def
calc_conv_qat
(
self
,
inp
):
w_qat
=
self
.
apply_quant_weight
(
self
.
weight
)
w_qat
=
self
.
apply_quant_weight
(
self
.
weight
)
if
self
.
weight_fake_quant
and
self
.
weight_fake_quant
.
enabled
:
b_qat
=
self
.
apply_quant_bias
(
self
.
bias
,
inp
,
w_qat
)
b_qat
=
fake_quant_bias
(
self
.
bias
,
inp
,
w_qat
)
else
:
b_qat
=
self
.
bias
conv
=
self
.
calc_conv
(
inp
,
w_qat
,
b_qat
)
conv
=
self
.
calc_conv
(
inp
,
w_qat
,
b_qat
)
return
conv
return
conv
...
...
imperative/python/megengine/module/qat/conv_bn.py
浏览文件 @
ce88e6c4
...
@@ -6,7 +6,6 @@
...
@@ -6,7 +6,6 @@
# software distributed under the License is distributed on an
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from
...functional
import
ones
,
relu
,
sqrt
,
sum
,
zeros
from
...functional
import
ones
,
relu
,
sqrt
,
sum
,
zeros
from
...quantization.utils
import
fake_quant_bias
from
..
import
conv_bn
as
Float
from
..
import
conv_bn
as
Float
from
.module
import
QATModule
from
.module
import
QATModule
...
@@ -122,10 +121,7 @@ class _ConvBnActivation2d(Float._ConvBnActivation2d, QATModule):
...
@@ -122,10 +121,7 @@ class _ConvBnActivation2d(Float._ConvBnActivation2d, QATModule):
b_fold
=
beta
+
gamma
*
(
conv_bias
-
bn_mean
)
*
bn_istd
b_fold
=
beta
+
gamma
*
(
conv_bias
-
bn_mean
)
*
bn_istd
w_qat
=
self
.
apply_quant_weight
(
w_fold
)
w_qat
=
self
.
apply_quant_weight
(
w_fold
)
if
self
.
weight_fake_quant
and
self
.
weight_fake_quant
.
enabled
:
b_qat
=
self
.
apply_quant_bias
(
b_fold
,
inp
,
w_qat
)
b_qat
=
fake_quant_bias
(
b_fold
,
inp
,
w_qat
)
else
:
b_qat
=
b_fold
conv
=
self
.
conv
.
calc_conv
(
inp
,
w_qat
,
b_qat
)
conv
=
self
.
conv
.
calc_conv
(
inp
,
w_qat
,
b_qat
)
if
not
(
self
.
training
and
approx
):
if
not
(
self
.
training
and
approx
):
return
conv
return
conv
...
...
imperative/python/megengine/module/qat/linear.py
浏览文件 @
ce88e6c4
...
@@ -5,7 +5,6 @@
...
@@ -5,7 +5,6 @@
# Unless required by applicable law or agreed to in writing,
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from
...quantization.utils
import
fake_quant_bias
from
..
import
linear
as
Float
from
..
import
linear
as
Float
from
.module
import
QATModule
from
.module
import
QATModule
...
@@ -22,13 +21,10 @@ class Linear(Float.Linear, QATModule):
...
@@ -22,13 +21,10 @@ class Linear(Float.Linear, QATModule):
"""
"""
def
forward
(
self
,
x
):
def
forward
(
self
,
inp
):
w_qat
=
self
.
apply_quant_weight
(
self
.
weight
)
w_qat
=
self
.
apply_quant_weight
(
self
.
weight
)
if
self
.
weight_fake_quant
and
self
.
weight_fake_quant
.
enabled
:
b_qat
=
self
.
apply_quant_bias
(
self
.
bias
,
inp
,
w_qat
)
b_qat
=
fake_quant_bias
(
self
.
bias
,
x
,
w_qat
)
return
self
.
apply_quant_activation
(
self
.
_calc_linear
(
inp
,
w_qat
,
b_qat
))
else
:
b_qat
=
self
.
bias
return
self
.
apply_quant_activation
(
self
.
_calc_linear
(
x
,
w_qat
,
b_qat
))
@
classmethod
@
classmethod
def
from_float_module
(
cls
,
float_module
:
Float
.
Linear
):
def
from_float_module
(
cls
,
float_module
:
Float
.
Linear
):
...
...
imperative/python/megengine/module/qat/module.py
浏览文件 @
ce88e6c4
...
@@ -11,6 +11,7 @@ from abc import abstractmethod
...
@@ -11,6 +11,7 @@ from abc import abstractmethod
from
...quantization.fake_quant
import
FakeQuantize
from
...quantization.fake_quant
import
FakeQuantize
from
...quantization.observer
import
Observer
from
...quantization.observer
import
Observer
from
...quantization.qconfig
import
QConfig
from
...quantization.qconfig
import
QConfig
from
...quantization.utils
import
fake_quant_bias
from
...tensor
import
Tensor
from
...tensor
import
Tensor
from
..module
import
Module
from
..module
import
Module
...
@@ -107,6 +108,24 @@ class QATModule(Module):
...
@@ -107,6 +108,24 @@ class QATModule(Module):
target
,
self
.
act_fake_quant
,
self
.
act_observer
target
,
self
.
act_fake_quant
,
self
.
act_observer
)
)
def
apply_quant_bias
(
self
,
target
:
Tensor
,
inp
:
Tensor
,
w_qat
:
Tensor
):
r
"""
Use :func:`~.fake_quant_bias` to process ``target``. Only valid when
``act_fake_quant`` and ``weight_fake_quant`` are both enabled.
"""
# bias should have the same dtype as activation, so act_fake_quant can also
# decide whether to do bias fakequant
if
(
self
.
act_fake_quant
and
self
.
act_fake_quant
.
enabled
and
self
.
weight_fake_quant
and
self
.
weight_fake_quant
.
enabled
):
b_qat
=
fake_quant_bias
(
target
,
inp
,
w_qat
)
else
:
b_qat
=
target
return
b_qat
def
_get_method_result
(
def
_get_method_result
(
self
,
method
:
str
,
fake_quant
:
FakeQuantize
,
observer
:
Observer
self
,
method
:
str
,
fake_quant
:
FakeQuantize
,
observer
:
Observer
):
):
...
...
imperative/python/megengine/quantization/__init__.py
浏览文件 @
ce88e6c4
...
@@ -30,4 +30,10 @@ from .quantize import (
...
@@ -30,4 +30,10 @@ from .quantize import (
quantize_qat
,
quantize_qat
,
reset_qconfig
,
reset_qconfig
,
)
)
from
.utils
import
QParams
,
QuantMode
,
create_qparams
from
.utils
import
(
QParams
,
QuantMode
,
create_qparams
,
fake_quant_bias
,
fake_quant_tensor
,
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录