Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
d8f4714b
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2307
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
d8f4714b
编写于
7月 06, 2020
作者:
C
cc
提交者:
GitHub
7月 06, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Quantization] Save output threshold by argname_index (#25272)
* Save output threshold by argname_index, test=develop
上级
64b46122
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
49 addition
and
7 deletion
+49
-7
python/paddle/fluid/contrib/slim/quantization/post_training_quantization.py
...d/contrib/slim/quantization/post_training_quantization.py
+20
-0
python/paddle/fluid/contrib/slim/quantization/quantization_pass.py
...ddle/fluid/contrib/slim/quantization/quantization_pass.py
+29
-7
未找到文件。
python/paddle/fluid/contrib/slim/quantization/post_training_quantization.py
浏览文件 @
d8f4714b
...
...
@@ -28,6 +28,7 @@ from .quantization_pass import AddQuantDequantPass
from
.quantization_pass
import
_out_scale_op_list
from
.quantization_pass
import
_get_op_input_var_names
from
.quantization_pass
import
_get_op_output_var_names
from
.quantization_pass
import
_get_output_name_index
__all__
=
[
'PostTrainingQuantization'
,
'WeightQuantization'
]
...
...
@@ -405,6 +406,10 @@ class PostTrainingQuantization(object):
model_filename
=
self
.
_model_filename
,
params_filename
=
self
.
_params_filename
)
if
self
.
_program
.
num_blocks
>
1
:
_logger
.
error
(
"The post training quantization requires that the "
"program only has one block."
)
if
self
.
_optimize_model
:
self
.
_optimize_fp32_model
()
...
...
@@ -450,6 +455,9 @@ class PostTrainingQuantization(object):
persistable_var_names
=
_all_persistable_var_names
(
self
.
_program
)
for
op
in
self
.
_program
.
global_block
().
ops
:
op_type
=
op
.
type
if
self
.
_is_full_quantize
and
\
op_type
not
in
self
.
_quantizable_op_type
:
_logger
.
warning
(
op_type
+
" is not supported for quantization."
)
# For quantized ops, sample inputs and outputs
if
op_type
in
self
.
_quantizable_op_type
:
collect_var_name
(
...
...
@@ -685,13 +693,25 @@ class PostTrainingQuantization(object):
op
.
_set_attr
(
"quantization_type"
,
quantized_type
)
def
analysis_and_save_info
(
op_node
,
out_var_name
):
argname_index
=
_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"
if
self
.
_algo
==
"KL"
:
# For compatibility, we save output threshold by two methods.
save_info
(
op_node
,
out_var_name
,
self
.
_quantized_var_kl_threshold
,
"out_threshold"
,
"post_kl"
)
save_info
(
op_node
,
out_var_name
,
self
.
_quantized_var_kl_threshold
,
argname_index
[
0
]
+
str
(
argname_index
[
1
])
+
"_threshold"
,
"post_kl"
)
elif
self
.
_algo
==
"abs_max"
:
save_info
(
op_node
,
out_var_name
,
self
.
_quantized_var_abs_max
,
"out_threshold"
,
"post_abs_max"
)
save_info
(
op_node
,
out_var_name
,
self
.
_quantized_var_abs_max
,
argname_index
[
0
]
+
str
(
argname_index
[
1
])
+
"_threshold"
,
"post_kl"
)
elif
self
.
_algo
==
"min_max"
:
save_info
(
op_node
,
out_var_name
,
self
.
_quantized_var_min
,
"out_min"
,
"post_min_max"
)
...
...
python/paddle/fluid/contrib/slim/quantization/quantization_pass.py
浏览文件 @
d8f4714b
...
...
@@ -127,6 +127,22 @@ def _get_op_output_var_names(op):
return
var_names
def
_get_output_name_index
(
op
,
output_var_name
):
"""Get the output name and index of the var_name in the op"""
assert
isinstance
(
op
,
(
IrNode
,
Operator
)),
\
"The input op should be IrNode or Operator."
op_name
=
op
.
name
()
if
isinstance
(
op
,
IrNode
)
\
else
op
.
type
name_list
=
_op_real_in_out_name
[
op_name
][
1
]
res
=
None
for
name
in
name_list
:
var_name
=
op
.
output
(
name
)
for
index
,
val
in
enumerate
(
var_name
):
if
val
==
output_var_name
:
res
=
(
name
,
index
)
return
res
def
_init_var_node
(
var_node
,
value
,
scope
,
place
):
assert
isinstance
(
value
,
np
.
ndarray
),
'The type of value should be numpy array.'
...
...
@@ -1528,13 +1544,19 @@ class OutScaleForInferencePass(object):
op_nodes
=
graph
.
all_op_nodes
()
for
op_node
in
op_nodes
:
if
op_node
.
name
()
in
self
.
_teller_set
:
output_var_name
=
_get_op_output_var_names
(
op_node
)
assert
len
(
output_var_name
)
==
1
,
"Only support collecting "
\
"output for op that only has an activation output for now."
scale_name
=
self
.
_scale_name
(
output_var_name
[
0
])
scale_v
=
np
.
array
(
self
.
_scope
.
find_var
(
scale_name
).
get_tensor
())[
0
]
op_node
.
op
().
_set_attr
(
"out_threshold"
,
float
(
scale_v
))
var_names
=
_get_op_output_var_names
(
op_node
)
for
var_name
in
var_names
:
# For compatibility, we save output threshold by two methods.
scale_name
=
self
.
_scale_name
(
var_name
)
scale_v
=
np
.
array
(
self
.
_scope
.
find_var
(
scale_name
).
get_tensor
())[
0
]
op_node
.
op
().
_set_attr
(
"out_threshold"
,
float
(
scale_v
))
argname_index
=
_get_output_name_index
(
op_node
,
var_name
)
assert
argname_index
is
not
None
,
\
var_name
+
" is not the output of the op"
op_node
.
op
().
_set_attr
(
argname_index
[
0
]
+
str
(
argname_index
[
1
])
\
+
"_threshold"
,
float
(
scale_v
))
graph
.
resolve_hazard
()
return
graph
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录