Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
72b9dcd8
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看板
未验证
提交
72b9dcd8
编写于
8月 03, 2020
作者:
B
Bai Yifan
提交者:
GitHub
8月 03, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop' into pact_clip
上级
f934b004
fad86fc1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
61 addition
and
32 deletion
+61
-32
paddleslim/quant/quanter.py
paddleslim/quant/quanter.py
+61
-32
未找到文件。
paddleslim/quant/quanter.py
浏览文件 @
72b9dcd8
...
...
@@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
copy
import
json
import
logging
import
paddle
...
...
@@ -33,8 +35,7 @@ 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'
'abs_max'
,
'channel_wise_abs_max'
,
'range_abs_max'
,
'moving_average_abs_max'
]
WEIGHT_QUANTIZATION_TYPES_TENSORRT
=
[
'channel_wise_abs_max'
]
...
...
@@ -55,6 +56,8 @@ TENSORRT_OP_TYPES = [
'leaky_relu'
]
VARS_MAPPING_TABLE
=
'./mapping_table_for_saving_inference_model'
_quant_config_default
=
{
# weight quantize type, default is 'channel_wise_abs_max'
'weight_quantize_type'
:
'channel_wise_abs_max'
,
...
...
@@ -81,6 +84,18 @@ _quant_config_default = {
}
def
load_dict
():
with
open
(
VARS_MAPPING_TABLE
,
'r'
)
as
file
:
data
=
file
.
read
()
data
=
json
.
loads
(
data
)
return
data
def
save_dict
(
table
):
with
open
(
VARS_MAPPING_TABLE
,
'w'
)
as
file
:
file
.
write
(
json
.
dumps
(
table
))
def
_parse_configs
(
user_config
):
"""
check if user's configs are valid.
...
...
@@ -267,6 +282,15 @@ def quant_aware(program,
scope
=
scope
,
place
=
place
,
moving_rate
=
config
[
'moving_rate'
])
out_scale_training_pass
.
apply
(
main_graph
)
if
(
weight_preprocess_func
is
not
None
or
act_preprocess_func
is
not
None
)
and
not
for_test
:
_logger
.
info
(
"When a preprocess_func is used in quant_aware, Need to save a mapping table to match variable names in the convert phase."
)
_logger
.
info
(
"The mapping table is saved as '{}'."
.
format
(
VARS_MAPPING_TABLE
))
save_dict
(
main_graph
.
out_node_mapping_table
)
if
for_test
:
quant_program
=
main_graph
.
to_program
()
else
:
...
...
@@ -274,27 +298,28 @@ def quant_aware(program,
return
quant_program
def
quant_post_static
(
executor
,
model_dir
,
quantize_model_path
,
batch_generator
=
None
,
sample_generator
=
None
,
model_filename
=
None
,
params_filename
=
None
,
save_model_filename
=
'__model__'
,
save_params_filename
=
'__params__'
,
batch_size
=
16
,
batch_nums
=
None
,
scope
=
None
,
algo
=
'KL'
,
quantizable_op_type
=
[
"conv2d"
,
"depthwise_conv2d"
,
"mul"
],
is_full_quantize
=
False
,
weight_bits
=
8
,
activation_bits
=
8
,
activation_quantize_type
=
'range_abs_max'
,
weight_quantize_type
=
'channel_wise_abs_max'
,
is_use_cache_file
=
False
,
cache_dir
=
"./temp_post_training"
):
def
quant_post_static
(
executor
,
model_dir
,
quantize_model_path
,
batch_generator
=
None
,
sample_generator
=
None
,
model_filename
=
None
,
params_filename
=
None
,
save_model_filename
=
'__model__'
,
save_params_filename
=
'__params__'
,
batch_size
=
16
,
batch_nums
=
None
,
scope
=
None
,
algo
=
'KL'
,
quantizable_op_type
=
[
"conv2d"
,
"depthwise_conv2d"
,
"mul"
],
is_full_quantize
=
False
,
weight_bits
=
8
,
activation_bits
=
8
,
activation_quantize_type
=
'range_abs_max'
,
weight_quantize_type
=
'channel_wise_abs_max'
,
is_use_cache_file
=
False
,
cache_dir
=
"./temp_post_training"
):
"""
The function utilizes static post training quantization method to
quantize the fp32 model. It uses calibrate data to calculate the
...
...
@@ -381,6 +406,7 @@ def quant_post_static(executor,
model_filename
=
save_model_filename
,
params_filename
=
save_params_filename
)
# We have changed the quant_post to quant_post_static.
# For compatibility, we keep quant_post api for now, and it will be
# deprecated in the future.
...
...
@@ -438,6 +464,9 @@ def convert(program, place, config=None, scope=None, save_int8=False):
activation_bits
=
config
[
'activation_bits'
],
weight_quantize_type
=
config
[
'weight_quantize_type'
])
if
os
.
path
.
exists
(
VARS_MAPPING_TABLE
):
test_graph
.
out_node_mapping_table
=
load_dict
()
freeze_pass
.
apply
(
test_graph
)
freezed_program
=
test_graph
.
to_program
()
...
...
@@ -451,14 +480,14 @@ def convert(program, place, config=None, scope=None, save_int8=False):
def
quant_post_dynamic
(
model_dir
,
save_model_dir
,
model_filename
=
None
,
params_filename
=
None
,
save_model_filename
=
None
,
save_params_filename
=
None
,
quantizable_op_type
=
[
"conv2d"
,
"mul"
],
weight_bits
=
8
,
generate_test_model
=
False
):
save_model_dir
,
model_filename
=
None
,
params_filename
=
None
,
save_model_filename
=
None
,
save_params_filename
=
None
,
quantizable_op_type
=
[
"conv2d"
,
"mul"
],
weight_bits
=
8
,
generate_test_model
=
False
):
'''
The function utilizes static post training quantization method to
quantize the fp32 model. In details, it quantizes the weight of some
...
...
@@ -517,4 +546,4 @@ def quant_post_dynamic(model_dir,
# We have changed the quant_post_only_weight to quant_post_dynamic.
# For compatibility, we keep quant_post_only_weight api for now,
# and it will be deprecated in the future.
quant_post_only_weight
=
quant_post_dynamic
\ No newline at end of file
quant_post_only_weight
=
quant_post_dynamic
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录