Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
dc96f6aa
MegEngine
项目概览
MegEngine 天元
/
MegEngine
大约 1 年 前同步成功
通知
395
Star
4704
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,发现更多精彩内容 >>
提交
dc96f6aa
编写于
3月 30, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mge/quantization): fix quantized concat forward problem
GitOrigin-RevId: dc21b340d1732fa5c1c186904d2a6c1b13e10121
上级
33da8de1
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
56 addition
and
4 deletion
+56
-4
imperative/python/megengine/module/quantized/concat.py
imperative/python/megengine/module/quantized/concat.py
+1
-1
imperative/python/megengine/quantization/__init__.py
imperative/python/megengine/quantization/__init__.py
+10
-2
imperative/python/test/unit/quantization/test_module.py
imperative/python/test/unit/quantization/test_module.py
+45
-1
未找到文件。
imperative/python/megengine/module/quantized/concat.py
浏览文件 @
dc96f6aa
...
...
@@ -23,7 +23,7 @@ class Concat(QuantizedModule):
self
.
output_dtype
=
dtype
def
forward
(
self
,
inps
:
Iterable
[
Tensor
],
axis
:
int
=
0
):
new_inps
=
(
x
.
astype
(
self
.
output_dtype
)
for
x
in
inps
)
new_inps
=
tuple
(
x
.
astype
(
self
.
output_dtype
)
for
x
in
inps
)
return
F
.
concat
(
new_inps
,
axis
)
@
classmethod
...
...
imperative/python/megengine/quantization/__init__.py
浏览文件 @
dc96f6aa
...
...
@@ -6,8 +6,16 @@
# 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
Observer
from
.fake_quant
import
TQT
,
FakeQuantize
from
.observer
import
(
ExponentialMovingAverageObserver
,
HistogramObserver
,
MinMaxObserver
,
Observer
,
PassiveObserver
,
SyncExponentialMovingAverageObserver
,
SyncMinMaxObserver
,
)
from
.qconfig
import
(
QConfig
,
calibration_qconfig
,
...
...
imperative/python/test/unit/quantization/test_module.py
浏览文件 @
dc96f6aa
...
...
@@ -30,7 +30,10 @@ min_max_fakequant_qconfig = QConfig(
act_fake_quant
=
partial
(
FakeQuantize
,
dtype
=
"qint8"
),
)
inp_scale
=
np
.
float32
(
np
.
random
.
rand
()
+
1
)
def
gen_inp_scale
():
return
np
.
float32
(
np
.
random
.
rand
()
+
1
)
min_val
=
np
.
random
.
randint
(
-
127
,
0
,
size
=
(
2
,)).
astype
(
"float32"
)
max_val
=
np
.
random
.
randint
(
1
,
127
,
size
=
(
2
,)).
astype
(
"float32"
)
...
...
@@ -116,6 +119,7 @@ def test_dequant_stub():
q_net
.
eval
()
x
=
mge
.
tensor
(
np
.
random
.
normal
(
size
=
(
3
,
3
)).
astype
(
"float32"
))
inp_scale
=
gen_inp_scale
()
x
=
fake_quant_act
(
x
,
inp_scale
)
x
.
qparams
.
scale
=
inp_scale
...
...
@@ -192,6 +196,7 @@ def test_linear():
init_qat_net
(
qat_net
)
x
=
mge
.
tensor
(
np
.
random
.
normal
(
size
=
(
3
,
3
)).
astype
(
"float32"
))
inp_scale
=
gen_inp_scale
()
x
=
fake_quant_act
(
x
,
inp_scale
)
x
.
qparams
.
update
(
create_qparams
(
QuantMode
.
SYMMERTIC
,
"qint8"
,
inp_scale
))
...
...
@@ -235,6 +240,7 @@ def test_conv(module):
init_qat_net
(
qat_net
)
x
=
mge
.
tensor
(
np
.
random
.
normal
(
size
=
(
1
,
3
,
3
,
3
)).
astype
(
"float32"
))
inp_scale
=
gen_inp_scale
()
x
=
fake_quant_act
(
x
,
inp_scale
)
x
.
qparams
.
update
(
create_qparams
(
QuantMode
.
SYMMERTIC
,
"qint8"
,
inp_scale
))
...
...
@@ -269,3 +275,41 @@ def test_conv(module):
np
.
testing
.
assert_allclose
(
qat_without_fakequant
,
normal
,
atol
=
1e-5
)
np
.
testing
.
assert_allclose
(
qat
,
fake_quant_normal
,
atol
=
act_scale
)
np
.
testing
.
assert_allclose
(
q
,
fake_quant_normal
.
numpy
(),
atol
=
act_scale
)
def
test_concat
():
normal_net
=
Float
.
Concat
()
normal_net
.
eval
()
qat_net
=
QAT
.
Concat
()
qat_net
.
eval
()
disable_observer
(
qat_net
)
propagate_qconfig
(
qat_net
,
min_max_fakequant_qconfig
)
init_qat_net
(
qat_net
)
inps
=
[]
inps_int8
=
[]
for
i
in
range
(
3
):
inp_scale
=
gen_inp_scale
()
inps
.
append
(
mge
.
tensor
(
np
.
random
.
normal
(
size
=
(
3
,
3
)).
astype
(
"float32"
)))
inps
[
i
]
=
fake_quant_act
(
inps
[
i
],
inp_scale
)
inps
[
i
].
qparams
.
update
(
create_qparams
(
QuantMode
.
SYMMERTIC
,
"qint8"
,
inp_scale
))
inps_int8
.
append
(
quant
(
inps
[
i
],
inp_scale
))
qat_from_float
=
QAT
.
Concat
.
from_float_module
(
normal_net
)
qat_from_float
.
eval
()
disable_fake_quant
(
qat_from_float
)
disable_observer
(
qat_from_float
)
q_net
=
Q
.
Concat
.
from_qat_module
(
qat_net
)
q_net
.
eval
()
normal
=
normal_net
(
inps
)
qat_without_fakequant
=
qat_from_float
(
inps
)
fake_quant_normal
=
fake_quant_act
(
normal_net
(
inps
),
act_scale
)
qat
=
qat_net
(
inps
)
q
=
q_net
(
inps_int8
).
numpy
()
*
act_scale
np
.
testing
.
assert_allclose
(
qat_without_fakequant
,
normal
)
np
.
testing
.
assert_allclose
(
qat
,
fake_quant_normal
.
numpy
())
np
.
testing
.
assert_allclose
(
q
,
fake_quant_normal
.
numpy
())
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录