Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
82c30f71
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
82c30f71
编写于
3月 11, 2022
作者:
G
Guanghua Yu
提交者:
GitHub
3月 11, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add EMD method of post_quant (#40421)
上级
1593c7ca
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
92 addition
and
7 deletion
+92
-7
python/paddle/fluid/contrib/slim/quantization/post_training_quantization.py
...d/contrib/slim/quantization/post_training_quantization.py
+50
-7
python/paddle/fluid/contrib/slim/tests/test_post_training_quantization_mnist.py
...ntrib/slim/tests/test_post_training_quantization_mnist.py
+20
-0
python/paddle/fluid/contrib/slim/tests/test_post_training_quantization_mobilenetv1.py
...slim/tests/test_post_training_quantization_mobilenetv1.py
+22
-0
未找到文件。
python/paddle/fluid/contrib/slim/quantization/post_training_quantization.py
浏览文件 @
82c30f71
...
@@ -272,7 +272,7 @@ class PostTrainingQuantization(object):
...
@@ -272,7 +272,7 @@ class PostTrainingQuantization(object):
]
]
self
.
_support_weight_quantize_type
=
[
'abs_max'
,
'channel_wise_abs_max'
]
self
.
_support_weight_quantize_type
=
[
'abs_max'
,
'channel_wise_abs_max'
]
self
.
_support_algo_type
=
[
self
.
_support_algo_type
=
[
'KL'
,
'hist'
,
'avg'
,
'mse'
,
'abs_max'
,
'min_max'
'KL'
,
'hist'
,
'avg'
,
'mse'
,
'
emd'
,
'
abs_max'
,
'min_max'
]
]
self
.
_dynamic_quantize_op_type
=
[
'lstm'
]
self
.
_dynamic_quantize_op_type
=
[
'lstm'
]
self
.
_support_quantize_op_type
=
\
self
.
_support_quantize_op_type
=
\
...
@@ -349,7 +349,7 @@ class PostTrainingQuantization(object):
...
@@ -349,7 +349,7 @@ class PostTrainingQuantization(object):
# The vars for algo = avg
# The vars for algo = avg
self
.
_quantized_var_avg
=
{}
self
.
_quantized_var_avg
=
{}
# The best loss of algo = mse
# The best loss of algo = mse
self
.
_best_
mse
_loss
=
{}
self
.
_best_
calibration
_loss
=
{}
# The threshold for algo = abs_max, mse or avg
# The threshold for algo = abs_max, mse or avg
self
.
_quantized_threshold
=
{}
self
.
_quantized_threshold
=
{}
...
@@ -408,7 +408,7 @@ class PostTrainingQuantization(object):
...
@@ -408,7 +408,7 @@ class PostTrainingQuantization(object):
np
.
array
(
self
.
_quantized_var_avg
[
var_name
]).
mean
()
np
.
array
(
self
.
_quantized_var_avg
[
var_name
]).
mean
()
if
self
.
_algo
in
[
"KL"
,
"hist"
]:
if
self
.
_algo
in
[
"KL"
,
"hist"
]:
self
.
_calculate_kl_hist_threshold
()
self
.
_calculate_kl_hist_threshold
()
if
self
.
_algo
in
[
"KL"
,
"abs_max"
,
"hist"
,
"avg"
,
"mse"
]:
if
self
.
_algo
in
[
"KL"
,
"abs_max"
,
"hist"
,
"avg"
,
"mse"
,
"emd"
]:
self
.
_update_program
()
self
.
_update_program
()
else
:
else
:
self
.
_save_input_threhold
()
self
.
_save_input_threhold
()
...
@@ -582,6 +582,8 @@ class PostTrainingQuantization(object):
...
@@ -582,6 +582,8 @@ class PostTrainingQuantization(object):
self
.
_sample_min_max
()
self
.
_sample_min_max
()
elif
self
.
_algo
==
"mse"
:
elif
self
.
_algo
==
"mse"
:
self
.
_sample_mse
()
self
.
_sample_mse
()
elif
self
.
_algo
==
"emd"
:
self
.
_sample_emd
()
elif
self
.
_algo
in
[
"KL"
,
"hist"
]:
elif
self
.
_algo
in
[
"KL"
,
"hist"
]:
self
.
_sample_histogram
()
self
.
_sample_histogram
()
...
@@ -610,8 +612,8 @@ class PostTrainingQuantization(object):
...
@@ -610,8 +612,8 @@ class PostTrainingQuantization(object):
abs_max_value
=
float
(
np
.
max
(
np
.
abs
(
var_tensor
)))
abs_max_value
=
float
(
np
.
max
(
np
.
abs
(
var_tensor
)))
abs_max_value
=
1e-8
if
abs_max_value
==
0.0
else
abs_max_value
abs_max_value
=
1e-8
if
abs_max_value
==
0.0
else
abs_max_value
s
=
0.3
s
=
0.3
if
var_name
not
in
self
.
_best_
mse
_loss
:
if
var_name
not
in
self
.
_best_
calibration
_loss
:
self
.
_best_
mse
_loss
[
var_name
]
=
float
(
'inf'
)
self
.
_best_
calibration
_loss
[
var_name
]
=
float
(
'inf'
)
while
s
<=
1.0
:
while
s
<=
1.0
:
scale
=
s
*
abs_max_value
scale
=
s
*
abs_max_value
s
+=
0.02
s
+=
0.02
...
@@ -620,8 +622,49 @@ class PostTrainingQuantization(object):
...
@@ -620,8 +622,49 @@ class PostTrainingQuantization(object):
np
.
clip
(
var_tensor
,
0.0
,
scale
)
/
scale
*
np
.
clip
(
var_tensor
,
0.0
,
scale
)
/
scale
*
bins
)
/
bins
*
scale
bins
)
/
bins
*
scale
mse_loss
=
((
var_tensor
-
quant_dequant_var
)
**
2
).
mean
()
mse_loss
=
((
var_tensor
-
quant_dequant_var
)
**
2
).
mean
()
if
mse_loss
<=
self
.
_best_mse_loss
[
var_name
]:
if
mse_loss
<=
self
.
_best_calibration_loss
[
var_name
]:
self
.
_best_mse_loss
[
var_name
]
=
mse_loss
self
.
_best_calibration_loss
[
var_name
]
=
mse_loss
self
.
_quantized_threshold
[
var_name
]
=
scale
def
_sample_emd
(
self
):
if
self
.
_quantized_threshold
==
{}:
for
var_name
in
self
.
_quantized_weight_var_name
:
var_tensor
=
_load_variable_data
(
self
.
_scope
,
var_name
)
if
self
.
_weight_quantize_type
==
"abs_max"
:
abs_max_value
=
float
(
np
.
max
(
np
.
abs
(
var_tensor
)))
elif
self
.
_weight_quantize_type
==
"channel_wise_abs_max"
:
abs_max_value
=
[]
if
self
.
_weight_op_pairs
[
var_name
]
in
_channelwise_quant_axis1_ops
:
for
i
in
range
(
var_tensor
.
shape
[
1
]):
abs_max_value
.
append
(
float
(
np
.
max
(
np
.
abs
(
var_tensor
[:,
i
]))))
else
:
for
i
in
range
(
var_tensor
.
shape
[
0
]):
abs_max_value
.
append
(
float
(
np
.
max
(
np
.
abs
(
var_tensor
[
i
]))))
self
.
_quantized_threshold
[
var_name
]
=
abs_max_value
_logger
.
info
(
"EMD searching stage ..."
)
for
var_name
in
self
.
_quantized_act_var_name
:
var_tensor
=
_load_variable_data
(
self
.
_scope
,
var_name
)
var_tensor
=
var_tensor
.
flatten
()
abs_max_value
=
float
(
np
.
max
(
np
.
abs
(
var_tensor
)))
abs_max_value
=
1e-8
if
abs_max_value
==
0.0
else
abs_max_value
s
=
0.3
if
var_name
not
in
self
.
_best_calibration_loss
:
self
.
_best_calibration_loss
[
var_name
]
=
float
(
'inf'
)
while
s
<=
1.0
:
scale
=
s
*
abs_max_value
s
+=
0.02
bins
=
2
**
(
self
.
_activation_bits
-
1
)
-
1
quant_dequant_var
=
np
.
round
(
np
.
clip
(
var_tensor
,
0.0
,
scale
)
/
scale
*
bins
)
/
bins
*
scale
emd_loss
=
np
.
abs
(
np
.
mean
(
var_tensor
)
-
np
.
mean
(
quant_dequant_var
))
+
np
.
abs
(
np
.
std
(
var_tensor
)
-
np
.
std
(
quant_dequant_var
))
if
emd_loss
<=
self
.
_best_calibration_loss
[
var_name
]:
self
.
_best_calibration_loss
[
var_name
]
=
emd_loss
self
.
_quantized_threshold
[
var_name
]
=
scale
self
.
_quantized_threshold
[
var_name
]
=
scale
def
_sample_avg
(
self
):
def
_sample_avg
(
self
):
...
...
python/paddle/fluid/contrib/slim/tests/test_post_training_quantization_mnist.py
浏览文件 @
82c30f71
...
@@ -244,6 +244,26 @@ class TestPostTrainingmseForMnist(TestPostTrainingQuantization):
...
@@ -244,6 +244,26 @@ class TestPostTrainingmseForMnist(TestPostTrainingQuantization):
quant_iterations
)
quant_iterations
)
class
TestPostTrainingemdForMnist
(
TestPostTrainingQuantization
):
def
test_post_training_mse
(
self
):
model_name
=
"mnist_model"
data_url
=
"http://paddle-inference-dist.bj.bcebos.com/int8/mnist_model.tar.gz"
data_md5
=
"be71d3997ec35ac2a65ae8a145e2887c"
algo
=
"emd"
quantizable_op_type
=
[
"conv2d"
,
"depthwise_conv2d"
,
"mul"
]
is_full_quantize
=
False
is_use_cache_file
=
False
is_optimize_model
=
True
diff_threshold
=
0.01
batch_size
=
10
infer_iterations
=
50
quant_iterations
=
5
self
.
run_test
(
model_name
,
data_url
,
data_md5
,
algo
,
quantizable_op_type
,
is_full_quantize
,
is_use_cache_file
,
is_optimize_model
,
diff_threshold
,
batch_size
,
infer_iterations
,
quant_iterations
)
class
TestPostTrainingavgForMnist
(
TestPostTrainingQuantization
):
class
TestPostTrainingavgForMnist
(
TestPostTrainingQuantization
):
def
test_post_training_avg
(
self
):
def
test_post_training_avg
(
self
):
model_name
=
"mnist_model"
model_name
=
"mnist_model"
...
...
python/paddle/fluid/contrib/slim/tests/test_post_training_quantization_mobilenetv1.py
浏览文件 @
82c30f71
...
@@ -394,5 +394,27 @@ class TestPostTrainingAbsMaxForMobilenetv1(TestPostTrainingQuantization):
...
@@ -394,5 +394,27 @@ class TestPostTrainingAbsMaxForMobilenetv1(TestPostTrainingQuantization):
diff_threshold
)
diff_threshold
)
class
TestPostTrainingEMDForMobilenetv1
(
TestPostTrainingQuantization
):
def
test_post_training_avg_mobilenetv1
(
self
):
model
=
"MobileNet-V1"
algo
=
"emd"
data_urls
=
[
'http://paddle-inference-dist.bj.bcebos.com/int8/mobilenetv1_int8_model.tar.gz'
]
data_md5s
=
[
'13892b0716d26443a8cdea15b3c6438b'
]
quantizable_op_type
=
[
"conv2d"
,
"depthwise_conv2d"
,
"mul"
,
]
is_full_quantize
=
False
is_use_cache_file
=
False
is_optimize_model
=
True
diff_threshold
=
0.025
self
.
run_test
(
model
,
algo
,
data_urls
,
data_md5s
,
quantizable_op_type
,
is_full_quantize
,
is_use_cache_file
,
is_optimize_model
,
diff_threshold
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录