Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
233fdb26
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
233fdb26
编写于
6月 17, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
6月 17, 2020
浏览文件
操作
浏览文件
下载
差异文件
!2181 add quant utils
Merge pull request !2181 from chenzhongming/quant_utils
上级
ca94916e
70f499ba
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
86 addition
and
0 deletion
+86
-0
mindspore/train/quant/quant_utils.py
mindspore/train/quant/quant_utils.py
+86
-0
未找到文件。
mindspore/train/quant/quant_utils.py
0 → 100644
浏览文件 @
233fdb26
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""quantization utils."""
import
numpy
as
np
def
cal_quantization_params
(
input_min
,
input_max
,
num_bits
=
8
,
symmetric
=
False
,
narrow_range
=
False
):
r
"""
calculate quantization params for scale and zero point.
Args:
input_min (int, list): The dimension of channel or 1.
input_max (int, list): The dimension of channel or 1.
num_bits (int): Quantization number bit, support 4 and 8bit. Default: 8.
symmetric (bool): Quantization algorithm use symmetric or not. Default: False.
narrow_range (bool): Quantization algorithm use narrow range or not. Default: False.
Outputs:
scale (int, list): quantization param.
zero point (int, list): quantization param.
Examples:
>>> scale, zp = cal_quantization_params([1, 2, 1], [-2, 0, -1], 8, False, False)
"""
input_max
=
np
.
maximum
(
0.0
,
input_max
)
input_min
=
np
.
minimum
(
0.0
,
input_min
)
if
input_min
.
shape
!=
input_max
.
shape
:
raise
ValueError
(
"input min shape should equal to input max."
)
if
len
(
input_min
.
shape
)
>
1
:
raise
ValueError
(
"input min and max shape should be one dim."
)
if
input_min
>
input_max
:
raise
ValueError
(
"input_min min should less than input max."
)
if
(
input_max
==
input_min
).
all
():
# scale = 1.0, zp = 0.0
return
np
.
ones
(
input_min
.
shape
),
np
.
zeros
(
input_min
.
shape
)
if
symmetric
:
quant_min
=
0
-
2
**
(
num_bits
-
1
)
quant_max
=
2
**
(
num_bits
-
1
)
else
:
quant_min
=
0
quant_max
=
2
**
num_bits
-
1
if
narrow_range
:
quant_min
=
quant_min
+
1
# calculate scale
if
symmetric
:
input_max
=
np
.
maximum
(
-
input_min
,
input_max
)
input_min
=
-
input_max
scale
=
(
input_max
-
input_min
)
/
(
quant_max
-
quant_min
)
# calculate zero point
if
symmetric
:
zp
=
np
.
zeros
(
input_min
.
shape
)
else
:
zp_from_min
=
quant_min
-
input_min
/
scale
zp_from_max
=
quant_max
-
input_max
/
scale
zp_from_min_error
=
np
.
abs
(
quant_min
)
+
np
.
abs
(
input_min
/
scale
)
zp_from_max_error
=
np
.
abs
(
quant_max
)
+
np
.
abs
(
input_max
/
scale
)
zp_double
=
zp_from_min
if
zp_from_min_error
<
zp_from_max_error
else
zp_from_max
if
zp_double
<
quant_min
:
zp
=
quant_min
elif
zp_double
>
quant_max
:
zp
=
quant_max
else
:
zp
=
np
.
floor
(
zp_double
+
0.5
)
return
scale
,
zp
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录