Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
1a2575bf
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
1 年多 前同步成功
通知
51
Star
1434
Fork
344
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
16
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSlim
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
16
合并请求
16
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
1a2575bf
编写于
9月 02, 2020
作者:
H
huangxu96
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added dygraph quantization.
上级
36b38fc3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
136 addition
and
0 deletion
+136
-0
paddleslim/quant/dy_quanter.py
paddleslim/quant/dy_quanter.py
+136
-0
未找到文件。
paddleslim/quant/dy_quanter.py
0 → 100644
浏览文件 @
1a2575bf
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
import
copy
import
logging
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.contrib.slim.quantization
import
ImperativeQuantAware
from
..common
import
get_logger
_logger
=
get_logger
(
__name__
,
level
=
logging
.
INFO
)
WEIGHT_QUANTIZATION_TYPES
=
[
'abs_max'
,
'channel_wise_abs_max'
,
'range_abs_max'
,
'moving_average_abs_max'
]
WEIGHT_QUANTIZATION_TYPES_TENSORRT
=
[
'channel_wise_abs_max'
]
ACTIVATION_QUANTIZATION_TYPES
=
[
'abs_max'
,
'range_abs_max'
,
'moving_average_abs_max'
]
ACTIVATION_QUANTIZATION_TYPES_TENSORRT
=
[
'range_abs_max'
,
'moving_average_abs_max'
]
_quant_config_default
=
{
# weight quantize type, default is 'channel_wise_abs_max'
'weight_quantize_type'
:
'channel_wise_abs_max'
,
# activation quantize type, default is 'moving_average_abs_max'
'activation_quantize_type'
:
'moving_average_abs_max'
,
# weight quantize bit num, default is 8
'weight_bits'
:
8
,
# activation quantize bit num, default is 8
'activation_bits'
:
8
,
# Layer of type in quantize_layer_types, will be quantized
'quantize_layer_types'
:
[
'Conv2D'
,
'Linear'
,
'ReLU'
,
'Pool2D'
,
'LeakyReLU'
],
# data type after quantization, such as 'uint8', 'int8', etc. default is 'int8'
'dtype'
:
'int8'
,
# window size for 'range_abs_max' quantization. defaulf is 10000
'window_size'
:
10000
,
# The decay coefficient of moving average, default is 0.9
'moving_rate'
:
0.9
,
}
def
_parse_configs
(
user_config
):
"""
check if user's configs are valid.
Args:
user_config(dict): user's config.
Return:
configs(dict): final configs will be used.
"""
configs
=
copy
.
deepcopy
(
_quant_config_default
)
configs
.
update
(
user_config
)
assert
isinstance
(
configs
[
'for_tensorrt'
],
bool
)
and
isinstance
(
configs
[
'is_full_quantize'
],
bool
),
"'for_tensorrt' and 'is_full_quantize' must both be bool'"
# check if configs is valid
if
configs
[
'for_tensorrt'
]:
weight_types
=
WEIGHT_QUANTIZATION_TYPES_TENSORRT
activation_types
=
ACTIVATION_QUANTIZATION_TYPES_TENSORRT
platform
=
'TensorRT'
else
:
weight_types
=
WEIGHT_QUANTIZATION_TYPES
activation_types
=
WEIGHT_QUANTIZATION_TYPES
platform
=
'PaddleLite'
assert
configs
[
'weight_quantize_type'
]
in
weight_types
,
\
"Unknown weight_quantize_type: {}. {} only supports {} "
.
format
(
configs
[
'weight_quantize_type'
],
platform
,
weight_types
)
assert
configs
[
'activation_quantize_type'
]
in
activation_types
,
\
"Unknown activation_quantize_type: {}. {} only supports {}"
.
format
(
configs
[
'activation_quantize_type'
],
platform
,
activation_types
)
assert
isinstance
(
configs
[
'weight_bits'
],
int
),
\
"weight_bits must be int value."
assert
(
configs
[
'weight_bits'
]
>=
1
and
configs
[
'weight_bits'
]
<=
16
),
\
"weight_bits should be between 1 and 16."
assert
isinstance
(
configs
[
'activation_bits'
],
int
),
\
"activation_bits must be int value."
assert
(
configs
[
'activation_bits'
]
>=
1
and
configs
[
'activation_bits'
]
<=
16
),
\
"activation_bits should be between 1 and 16."
assert
isinstance
(
configs
[
'dtype'
],
str
),
\
"dtype must be a str."
assert
isinstance
(
configs
[
'window_size'
],
int
),
\
"window_size must be int value, window size for 'range_abs_max' quantization, default is 10000."
assert
isinstance
(
configs
[
'moving_rate'
],
float
),
\
"moving_rate must be float value, The decay coefficient of moving average, default is 0.9."
return
configs
def
dy_quant_aware
(
model
,
config
=
None
,
scope
=
None
,
for_test
=
False
,
weight_quantize_func
=
None
,
act_quantize_func
=
None
,
weight_preprocess_func
=
None
,
act_preprocess_func
=
None
,
optimizer_func
=
None
):
imperative_qat
=
ImperativeQuantAware
(
weight_quantize_type
=
'abs_max'
,
activation_quantize_type
=
'moving_average_abs_max'
,
quantizable_layer_type
=
[
'Conv2D'
,
'Linear'
,
'ReLU'
,
'Pool2D'
,
'LeakyReLU'
])
imperative_qat
.
quantize
(
model
)
return
model
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录