Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
855c49ca
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
403
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看板
提交
855c49ca
编写于
5月 13, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/module): add linear quantization module
GitOrigin-RevId: d0c96a94112724afd263b56f866986872f48cf3d
上级
90107b6d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
78 addition
and
4 deletion
+78
-4
python_module/megengine/module/linear.py
python_module/megengine/module/linear.py
+17
-4
python_module/megengine/module/quantized/__init__.py
python_module/megengine/module/quantized/__init__.py
+1
-0
python_module/megengine/module/quantized/linear.py
python_module/megengine/module/quantized/linear.py
+60
-0
未找到文件。
python_module/megengine/module/linear.py
浏览文件 @
855c49ca
...
...
@@ -8,13 +8,13 @@
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import
numpy
as
np
from
..
import
functional
as
F
from
..core
import
Parameter
from
..functional
import
linear
from
.
import
init
from
.module
import
Module
from
.module
import
QAT
Module
class
Linear
(
Module
):
class
Linear
(
QAT
Module
):
r
"""Applies a linear transformation to the input. For instance, if input
is x, then output y is:
...
...
@@ -55,5 +55,18 @@ class Linear(Module):
if
self
.
bias
is
not
None
:
init
.
zeros_
(
self
.
bias
)
def
_calc_linear
(
self
,
x
,
weight
,
bias
):
return
F
.
linear
(
x
,
weight
,
bias
)
def
forward
(
self
,
x
):
return
linear
(
x
,
self
.
weight
,
self
.
bias
)
return
self
.
_calc_linear
(
x
,
self
.
weight
,
self
.
bias
)
def
forward_qat
(
self
,
x
):
w_qat
=
self
.
apply_fakequant_with_observer
(
self
.
weight
,
self
.
weight_fake_quant
,
self
.
weight_observer
)
return
self
.
apply_fakequant_with_observer
(
self
.
_calc_linear
(
x
,
w_qat
,
self
.
bias
),
self
.
act_fake_quant
,
self
.
act_observer
,
)
python_module/megengine/module/quantized/__init__.py
浏览文件 @
855c49ca
...
...
@@ -8,4 +8,5 @@
from
.concat
import
Concat
from
.conv_bn_relu
import
ConvBn2d
,
ConvBnRelu2d
from
.elemwise
import
Elemwise
from
.linear
import
Linear
from
.quant_dequant
import
DequantStub
,
QuantStub
python_module/megengine/module/quantized/linear.py
0 → 100644
浏览文件 @
855c49ca
# 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
numpy
as
np
import
megengine._internal
as
mgb
from
...
import
functional
as
F
from
...
import
module
as
Float
from
...core
import
Parameter
from
...quantization.utils
import
register_method_to_class
from
..module
import
Module
class
Linear
(
Module
):
r
"""Applies a quantized linear transformation to the input. The module
usually convert from QAT module by to_quantized method.
:param dtype: output data type.
"""
def
__init__
(
self
,
dtype
:
np
.
dtype
=
None
,
):
super
().
__init__
()
self
.
weight
=
None
self
.
bias
=
None
self
.
output_dtype
=
dtype
def
forward
(
self
,
inp
):
if
self
.
training
:
raise
ValueError
(
"quantized module only support inference."
)
inp_scale
=
mgb
.
dtype
.
get_scale
(
inp
.
dtype
)
w_scale
=
mgb
.
dtype
.
get_scale
(
self
.
weight
.
dtype
)
bias_dtype
=
mgb
.
dtype
.
qint32
(
inp_scale
*
w_scale
)
return
F
.
linear
(
inp
,
self
.
weight
,
None
if
self
.
bias
is
None
else
self
.
bias
.
astype
(
bias_dtype
),
).
astype
(
self
.
output_dtype
)
@
register_method_to_class
(
Float
.
Linear
)
def
to_quantized
(
float_module
):
r
"""
Replace :class:`~.module.QATModule`'s ``to_quantized`` method.
implemented here to avoid circular import.
"""
output_dtype
=
float_module
.
act_observer
.
get_dtype
()
qmod
=
Linear
(
dtype
=
output_dtype
,)
weight
=
float_module
.
weight
.
astype
(
float_module
.
weight_observer
.
get_dtype
())
qmod
.
weight
=
Parameter
(
weight
.
numpy
())
if
float_module
.
bias
is
not
None
:
qmod
.
bias
=
Parameter
(
float_module
.
bias
.
numpy
())
return
qmod
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录