Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
ae8c3c81
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看板
提交
ae8c3c81
编写于
11月 10, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/functional): add python wrapper for fake quant opr
GitOrigin-RevId: 5f205198be9af2990d51984e1dac582b157d56a0
上级
b60cc8ca
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
83 addition
and
8 deletion
+83
-8
imperative/python/megengine/quantization/utils.py
imperative/python/megengine/quantization/utils.py
+17
-8
imperative/python/test/unit/quantization/test_fake_quant.py
imperative/python/test/unit/quantization/test_fake_quant.py
+66
-0
未找到文件。
imperative/python/megengine/quantization/utils.py
浏览文件 @
ae8c3c81
...
...
@@ -9,7 +9,12 @@ from enum import Enum
from
functools
import
partial
,
update_wrapper
,
wraps
from
typing
import
Dict
import
numpy
as
np
from
..
import
functional
as
F
from
..core.ops
import
builtin
from
..core.tensor
import
megbrain_graph
from
..core.tensor.core
import
apply
from
..core.tensor.dtype
import
_metadata_dict
from
..core.tensor.function
import
Function
from
..tensor
import
Tensor
...
...
@@ -81,16 +86,20 @@ def fake_quant_tensor(inp: Tensor, qmin: int, qmax: int, q_dict: Dict) -> Tensor
"""
scale
=
q_dict
[
"scale"
]
zero_point
=
0
zero_point
=
Tensor
([
0.0
],
dtype
=
np
.
float32
)
if
q_dict
[
"mode"
]
==
QuantMode
.
ASYMMERTIC
:
zero_point
=
q_dict
[
"zero_point"
]
# Quant
oup
=
Round
()(
inp
/
scale
)
+
zero_point
# Clip
oup
=
F
.
minimum
(
F
.
maximum
(
oup
,
qmin
),
qmax
)
# Dequant
oup
=
(
oup
-
zero_point
)
*
scale
return
oup
assert
isinstance
(
inp
,
(
Tensor
,
megbrain_graph
.
VarNode
)),
"inp must be Tensor type"
assert
isinstance
(
scale
,
(
Tensor
,
megbrain_graph
.
VarNode
)
),
"scale must be Tensor type"
assert
isinstance
(
zero_point
,
(
Tensor
,
megbrain_graph
.
VarNode
)
),
"zero point must be Tensor type"
op
=
builtin
.
FakeQuant
(
qmin
=
qmin
,
qmax
=
qmax
)
return
apply
(
op
,
inp
,
scale
,
zero_point
)[
0
]
def
fake_quant_bias
(
bias
:
Tensor
,
inp
:
Tensor
,
w_qat
:
Tensor
)
->
Tensor
:
...
...
imperative/python/test/unit/quantization/test_fake_quant.py
浏览文件 @
ae8c3c81
...
...
@@ -11,8 +11,12 @@ import pytest
import
megengine
as
mge
from
megengine
import
tensor
from
megengine.core.autodiff.grad
import
Grad
from
megengine.core.tensor.function
import
Function
from
megengine.core.tensor.utils
import
make_shape_tuple
from
megengine.quantization.fake_quant
import
TQT_Function
from
megengine.quantization.internal_fake_quant
import
*
from
megengine.quantization.utils
import
QuantMode
,
fake_quant_tensor
class
numpy_TQT_Function
:
...
...
@@ -77,3 +81,65 @@ def test_TQT():
check_inp
(
a
,
b
,
b
,
a_np
,
b_np
,
b_np
)
def
_save_to
(
self
,
name
=
"grad"
):
def
callback
(
tensor
,
grad
):
setattr
(
self
,
name
,
grad
)
return
callback
class
Round
(
Function
):
def
forward
(
self
,
x
):
return
F
.
round
(
x
)
def
backward
(
self
,
output_grads
):
return
output_grads
def
fake_quant_tensor_gt
(
inp
,
scale
,
zero_point
,
qmin
,
qmax
):
oup
=
Round
()(
inp
/
scale
)
+
zero_point
oup
=
F
.
minimum
(
F
.
maximum
(
oup
,
qmin
),
qmax
)
oup
=
(
oup
-
zero_point
)
*
scale
return
oup
def
test_fakequant
():
qmin
=
-
126
qmax
=
129
def
run
(
zero_point
,
scale
):
q_dict
=
{}
q_dict
[
"mode"
]
=
QuantMode
.
ASYMMERTIC
q_dict
[
"scale"
]
=
scale
q_dict
[
"zero_point"
]
=
zero_point
inp_data
=
np
.
random
.
uniform
(
low
=-
512.0
,
high
=
512.0
,
size
=
(
1
,
32
,
32
,
32
))
inp
=
tensor
(
inp_data
,
dtype
=
np
.
float32
)
# test forward
oup
=
fake_quant_tensor
(
inp
,
qmin
,
qmax
,
q_dict
).
numpy
()
oup_gt
=
fake_quant_tensor_gt
(
inp
,
scale
,
zero_point
,
qmin
,
qmax
).
numpy
()
assert
np
.
allclose
(
oup
,
oup_gt
)
assert
oup
.
shape
==
oup_gt
.
shape
# test backward
x
=
tensor
(
inp_data
,
dtype
=
np
.
float32
)
grad
=
Grad
().
wrt
(
x
,
callback
=
_save_to
(
x
))
y
=
fake_quant_tensor
(
x
,
qmin
,
qmax
,
q_dict
)
grad
(
y
,
tensor
(
F
.
ones_like
(
x
)))
x1
=
tensor
(
inp_data
,
dtype
=
np
.
float32
)
grad
=
Grad
().
wrt
(
x1
,
callback
=
_save_to
(
x1
))
y1
=
fake_quant_tensor_gt
(
x1
,
scale
,
zero_point
,
qmin
,
qmax
)
grad
(
y1
,
tensor
(
F
.
ones_like
(
x1
)))
assert
np
.
allclose
(
x
.
grad
.
numpy
(),
x1
.
grad
.
numpy
())
assert
make_shape_tuple
(
x
.
grad
.
shape
)
==
make_shape_tuple
(
x1
.
grad
.
shape
)
zero_point
=
tensor
([
1.0
],
dtype
=
np
.
float32
)
scale
=
tensor
([
4.0
],
dtype
=
np
.
float32
)
run
(
zero_point
,
scale
)
zero_point
=
tensor
(
1.0
*
np
.
ones
((
1
,
32
,
1
,
1
)),
dtype
=
np
.
float32
)
scale
=
tensor
(
4.0
*
np
.
ones
((
1
,
32
,
1
,
1
)),
dtype
=
np
.
float32
)
run
(
zero_point
,
scale
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录