Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
70a36428
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看板
未验证
提交
70a36428
编写于
3月 31, 2022
作者:
G
Guanghua Yu
提交者:
GitHub
3月 31, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add ptq data-free method (#1026)
* add ptq data-free method
上级
d31a202a
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
112 addition
and
0 deletion
+112
-0
paddleslim/auto_compression/utils/fake_ptq.py
paddleslim/auto_compression/utils/fake_ptq.py
+112
-0
未找到文件。
paddleslim/auto_compression/utils/fake_ptq.py
0 → 100644
浏览文件 @
70a36428
import
paddle
from
paddle.fluid.framework
import
IrGraph
from
paddle.fluid
import
core
from
paddle.fluid.contrib.slim.quantization
import
QuantizationTransformPass
,
AddQuantDequantPass
,
QuantizationFreezePass
def
post_quant_fake
(
executor
,
model_dir
,
model_filename
=
None
,
params_filename
=
None
,
save_model_path
=
None
,
quantizable_op_type
=
[
"conv2d"
,
"depthwise_conv2d"
,
"mul"
],
is_full_quantize
=
False
,
activation_bits
=
8
,
weight_bits
=
8
):
"""
Utilizing post training quantization methon to quantize the FP32 model,
and it not uses calibrate data and the fake model cannot be used in practice.
Usage:
paddle.enable_static()
place = paddle.CPUPlace()
exe = paddle.static.Executor(place)
post_quant_fake(executor=exe,
model_dir='./inference_model/MobileNet/',
model_filename='model',
params_filename='params',
save_model_path='fake_quant')
"""
activation_quantize_type
=
'range_abs_max'
weight_quantize_type
=
'channel_wise_abs_max'
_dynamic_quantize_op_type
=
[
'lstm'
]
_weight_supported_quantizable_op_type
=
QuantizationTransformPass
.
_supported_quantizable_op_type
_act_supported_quantizable_op_type
=
AddQuantDequantPass
.
_supported_quantizable_op_type
_support_quantize_op_type
=
list
(
set
(
_weight_supported_quantizable_op_type
+
_act_supported_quantizable_op_type
+
_dynamic_quantize_op_type
))
_place
=
executor
.
place
_scope
=
paddle
.
static
.
global_scope
()
if
is_full_quantize
:
_quantizable_op_type
=
_support_quantize_op_type
else
:
_quantizable_op_type
=
quantizable_op_type
for
op_type
in
_quantizable_op_type
:
assert
op_type
in
_support_quantize_op_type
,
\
op_type
+
" is not supported for quantization."
_program
,
_feed_list
,
_fetch_list
=
paddle
.
fluid
.
io
.
load_inference_model
(
model_dir
,
executor
,
model_filename
=
model_filename
,
params_filename
=
params_filename
)
graph
=
IrGraph
(
core
.
Graph
(
_program
.
desc
),
for_test
=
True
)
# use QuantizationTransformPass to insert fake_quant/fake_dequantize op
major_quantizable_op_types
=
[]
for
op_type
in
_weight_supported_quantizable_op_type
:
if
op_type
in
_quantizable_op_type
:
major_quantizable_op_types
.
append
(
op_type
)
transform_pass
=
QuantizationTransformPass
(
scope
=
_scope
,
place
=
_place
,
weight_bits
=
weight_bits
,
activation_bits
=
activation_bits
,
activation_quantize_type
=
activation_quantize_type
,
weight_quantize_type
=
weight_quantize_type
,
quantizable_op_type
=
major_quantizable_op_types
)
for
sub_graph
in
graph
.
all_sub_graphs
():
# Insert fake_quant/fake_dequantize op must in test graph, so
# set per graph's _for_test is True.
sub_graph
.
_for_test
=
True
transform_pass
.
apply
(
sub_graph
)
# use AddQuantDequantPass to insert fake_quant_dequant op
minor_quantizable_op_types
=
[]
for
op_type
in
_act_supported_quantizable_op_type
:
if
op_type
in
_quantizable_op_type
:
minor_quantizable_op_types
.
append
(
op_type
)
add_quant_dequant_pass
=
AddQuantDequantPass
(
scope
=
_scope
,
place
=
_place
,
quantizable_op_type
=
minor_quantizable_op_types
)
for
sub_graph
in
graph
.
all_sub_graphs
():
sub_graph
.
_for_test
=
True
add_quant_dequant_pass
.
apply
(
sub_graph
)
# apply QuantizationFreezePass, and obtain the final quant model
freeze_pass
=
QuantizationFreezePass
(
scope
=
_scope
,
place
=
_place
,
weight_bits
=
weight_bits
,
activation_bits
=
activation_bits
,
weight_quantize_type
=
weight_quantize_type
,
quantizable_op_type
=
major_quantizable_op_types
)
for
sub_graph
in
graph
.
all_sub_graphs
():
sub_graph
.
_for_test
=
True
freeze_pass
.
apply
(
sub_graph
)
_program
=
graph
.
to_program
()
paddle
.
fluid
.
io
.
save_inference_model
(
dirname
=
save_model_path
,
model_filename
=
model_filename
,
params_filename
=
params_filename
,
feeded_var_names
=
_feed_list
,
target_vars
=
_fetch_list
,
executor
=
executor
,
main_program
=
_program
)
print
(
"The quantized model is saved in: "
+
save_model_path
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录