Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
11814d1c
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
大约 2 年 前同步成功
通知
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看板
未验证
提交
11814d1c
编写于
8月 29, 2022
作者:
G
Guanghua Yu
提交者:
GitHub
8月 29, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix fake quant demo (#1397)
上级
a0d87b27
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
71 addition
and
24 deletion
+71
-24
paddleslim/auto_compression/utils/fake_ptq.py
paddleslim/auto_compression/utils/fake_ptq.py
+71
-24
未找到文件。
paddleslim/auto_compression/utils/fake_ptq.py
浏览文件 @
11814d1c
...
@@ -2,7 +2,7 @@ import os
...
@@ -2,7 +2,7 @@ import os
import
paddle
import
paddle
from
paddle.fluid.framework
import
IrGraph
from
paddle.fluid.framework
import
IrGraph
from
paddle.framework
import
core
from
paddle.framework
import
core
from
paddle.fluid.contrib.slim.quantization
import
QuantizationTransformPass
,
AddQuantDequantPass
,
QuantizationFreeze
Pass
from
paddle.fluid.contrib.slim.quantization
import
QuantizationTransformPass
,
QuantizationTransformPassV2
,
AddQuantDequantPass
,
AddQuantDequantPassV2
,
QuantizationFreezePass
,
QuantWeight
Pass
try
:
try
:
from
paddle.fluid.contrib.slim.quantization
import
utils
from
paddle.fluid.contrib.slim.quantization
import
utils
...
@@ -23,7 +23,8 @@ def post_quant_fake(executor,
...
@@ -23,7 +23,8 @@ def post_quant_fake(executor,
quantizable_op_type
=
[
"conv2d"
,
"depthwise_conv2d"
,
"mul"
],
quantizable_op_type
=
[
"conv2d"
,
"depthwise_conv2d"
,
"mul"
],
is_full_quantize
=
False
,
is_full_quantize
=
False
,
activation_bits
=
8
,
activation_bits
=
8
,
weight_bits
=
8
):
weight_bits
=
8
,
onnx_format
=
False
):
"""
"""
Utilizing post training quantization methon to quantize the FP32 model,
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.
and it not uses calibrate data and the fake model cannot be used in practice.
...
@@ -67,14 +68,24 @@ def post_quant_fake(executor,
...
@@ -67,14 +68,24 @@ def post_quant_fake(executor,
for
op_type
in
_weight_supported_quantizable_op_type
:
for
op_type
in
_weight_supported_quantizable_op_type
:
if
op_type
in
_quantizable_op_type
:
if
op_type
in
_quantizable_op_type
:
major_quantizable_op_types
.
append
(
op_type
)
major_quantizable_op_types
.
append
(
op_type
)
transform_pass
=
QuantizationTransformPass
(
if
onnx_format
:
scope
=
_scope
,
transform_pass
=
QuantizationTransformPassV2
(
place
=
_place
,
scope
=
_scope
,
weight_bits
=
weight_bits
,
place
=
_place
,
activation_bits
=
activation_bits
,
weight_bits
=
weight_bits
,
activation_quantize_type
=
activation_quantize_type
,
activation_bits
=
activation_bits
,
weight_quantize_type
=
weight_quantize_type
,
activation_quantize_type
=
activation_quantize_type
,
quantizable_op_type
=
major_quantizable_op_types
)
weight_quantize_type
=
weight_quantize_type
,
quantizable_op_type
=
major_quantizable_op_types
)
else
:
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
():
for
sub_graph
in
graph
.
all_sub_graphs
():
# Insert fake_quant/fake_dequantize op must in test graph, so
# Insert fake_quant/fake_dequantize op must in test graph, so
...
@@ -87,30 +98,66 @@ def post_quant_fake(executor,
...
@@ -87,30 +98,66 @@ def post_quant_fake(executor,
for
op_type
in
_act_supported_quantizable_op_type
:
for
op_type
in
_act_supported_quantizable_op_type
:
if
op_type
in
_quantizable_op_type
:
if
op_type
in
_quantizable_op_type
:
minor_quantizable_op_types
.
append
(
op_type
)
minor_quantizable_op_types
.
append
(
op_type
)
add_quant_dequant_pass
=
AddQuantDequantPass
(
if
onnx_format
:
scope
=
_scope
,
add_quant_dequant_pass
=
AddQuantDequantPassV2
(
place
=
_place
,
scope
=
_scope
,
quantizable_op_type
=
minor_quantizable_op_types
)
place
=
_place
,
quantizable_op_type
=
minor_quantizable_op_types
,
is_full_quantized
=
is_full_quantize
)
else
:
add_quant_dequant_pass
=
AddQuantDequantPass
(
scope
=
_scope
,
place
=
_place
,
quantizable_op_type
=
minor_quantizable_op_types
)
for
sub_graph
in
graph
.
all_sub_graphs
():
for
sub_graph
in
graph
.
all_sub_graphs
():
sub_graph
.
_for_test
=
True
sub_graph
.
_for_test
=
True
add_quant_dequant_pass
.
apply
(
sub_graph
)
add_quant_dequant_pass
.
apply
(
sub_graph
)
# apply QuantizationFreezePass, and obtain the final quant model
# apply QuantizationFreezePass, and obtain the final quant model
freeze_pass
=
QuantizationFreezePass
(
if
onnx_format
:
scope
=
_scope
,
quant_weight_pass
=
QuantWeightPass
(
_scope
,
_place
)
place
=
_place
,
for
sub_graph
in
graph
.
all_sub_graphs
():
weight_bits
=
weight_bits
,
sub_graph
.
_for_test
=
True
activation_bits
=
activation_bits
,
quant_weight_pass
.
apply
(
sub_graph
)
weight_quantize_type
=
weight_quantize_type
,
else
:
quantizable_op_type
=
major_quantizable_op_types
)
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
():
for
sub_graph
in
graph
.
all_sub_graphs
():
sub_graph
.
_for_test
=
True
sub_graph
.
_for_test
=
True
freeze_pass
.
apply
(
sub_graph
)
freeze_pass
.
apply
(
sub_graph
)
_program
=
graph
.
to_program
()
_program
=
graph
.
to_program
()
def
save_info
(
op_node
,
out_var_name
,
out_info_name
,
quantized_type
):
op_node
.
_set_attr
(
out_info_name
,
0.001
)
op_node
.
_set_attr
(
"with_quant_attr"
,
True
)
if
op_node
.
type
in
_quantizable_op_type
:
op
.
_set_attr
(
"quantization_type"
,
quantized_type
)
def
analysis_and_save_info
(
op_node
,
out_var_name
):
argname_index
=
utils
.
_get_output_name_index
(
op_node
,
out_var_name
)
assert
argname_index
is
not
None
,
\
out_var_name
+
" is not the output of the op"
save_info
(
op_node
,
out_var_name
,
"out_threshold"
,
"post_avg"
)
save_info
(
op_node
,
out_var_name
,
argname_index
[
0
]
+
str
(
argname_index
[
1
])
+
"_threshold"
,
"post_avg"
)
for
block_id
in
range
(
len
(
_program
.
blocks
)):
for
op
in
_program
.
blocks
[
block_id
].
ops
:
if
op
.
type
in
(
_quantizable_op_type
+
utils
.
_out_scale_op_list
):
out_var_names
=
utils
.
_get_op_output_var_names
(
op
)
for
var_name
in
out_var_names
:
analysis_and_save_info
(
op
,
var_name
)
feed_vars
=
[
_program
.
global_block
().
var
(
name
)
for
name
in
_feed_list
]
feed_vars
=
[
_program
.
global_block
().
var
(
name
)
for
name
in
_feed_list
]
model_name
=
model_filename
.
split
(
'.'
)[
model_name
=
model_filename
.
split
(
'.'
)[
0
]
if
model_filename
is
not
None
else
'model'
0
]
if
model_filename
is
not
None
else
'model'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录