Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
9fb9b6d2
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看板
提交
9fb9b6d2
编写于
11月 21, 2019
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'quant_post' into 'develop'
add post training quantization api quant_post See merge request
!24
上级
7d0e73e8
e904a37c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
60 addition
and
10 deletion
+60
-10
paddleslim/quant/quanter.py
paddleslim/quant/quanter.py
+60
-10
未找到文件。
paddleslim/quant/quanter.py
浏览文件 @
9fb9b6d2
...
@@ -20,6 +20,7 @@ from paddle.fluid.contrib.slim.quantization import QuantizationTransformPass
...
@@ -20,6 +20,7 @@ from paddle.fluid.contrib.slim.quantization import QuantizationTransformPass
from
paddle.fluid.contrib.slim.quantization
import
QuantizationFreezePass
from
paddle.fluid.contrib.slim.quantization
import
QuantizationFreezePass
from
paddle.fluid.contrib.slim.quantization
import
ConvertToInt8Pass
from
paddle.fluid.contrib.slim.quantization
import
ConvertToInt8Pass
from
paddle.fluid.contrib.slim.quantization
import
TransformForMobilePass
from
paddle.fluid.contrib.slim.quantization
import
TransformForMobilePass
from
paddle.fluid.contrib.slim.quantization
import
PostTrainingQuantization
from
paddle.fluid.contrib.slim.quantization
import
AddQuantDequantPass
from
paddle.fluid.contrib.slim.quantization
import
AddQuantDequantPass
from
paddle.fluid
import
core
from
paddle.fluid
import
core
...
@@ -186,19 +187,68 @@ def quant_aware(program, place, config, scope=None, for_test=False):
...
@@ -186,19 +187,68 @@ def quant_aware(program, place, config, scope=None, for_test=False):
return
quant_program
return
quant_program
def
quant_post
(
program
,
place
,
config
,
scope
=
None
):
def
quant_post
(
executor
,
model_dir
,
quantize_model_path
,
sample_generator
,
model_filename
=
None
,
params_filename
=
None
,
batch_size
=
16
,
batch_nums
=
None
,
scope
=
None
,
algo
=
'KL'
,
quantizable_op_type
=
[
"conv2d"
,
"depthwise_conv2d"
,
"mul"
]):
"""
"""
add quantization ops in program. the program returned is not trainable.
The function utilizes post training quantization method to quantize the
fp32 model. It uses calibrate data to calculate the scale factor of
quantized variables, and inserts fake quant/dequant op to obtain the
quantized model.
Args:
Args:
program(fluid.Program): program
executor(fluid.Executor): The executor to load, run and save the
scope(fluid.Scope): the scope to store var, it's should be the value of program's scope, usually it's fluid.global_scope().
quantized model.
place(fluid.CPUPlace or fluid.CUDAPlace): place
model_dir(str): The path of fp32 model that will be quantized, and
config(dict): configs for quantization, default values are in quant_config_default dict.
the model and params that saved by fluid.io.save_inference_model
for_test: is for test program.
are under the path.
Return:
quantize_model_path(str): The path to save quantized model using api
fluid.Program: the quantization program is not trainable.
fluid.io.save_inference_model.
sample_generator(Python Generator): The sample generator provides
calibrate data for DataLoader, and it only returns a sample every time.
model_filename(str, optional): The name of model file. If parameters
are saved in separate files, set it as 'None'. Default is 'None'.
params_filename(str, optional): The name of params file.
When all parameters are saved in a single file, set it
as filename. If parameters are saved in separate files,
set it as 'None'. Default is 'None'.
batch_size(int, optional): The batch size of DataLoader, default is 16.
batch_nums(int, optional): If batch_nums is not None, the number of calibrate
data is 'batch_size*batch_nums'. If batch_nums is None, use all data
generated by sample_generator as calibrate data.
scope(fluid.Scope, optional): The scope to run program, use it to load
and save variables. If scope is None, will use fluid.global_scope().
algo(str, optional): If algo=KL, use KL-divergenc method to
get the more precise scale factor. If algo='direct', use
abs_max method to get the scale factor. Default is 'KL'.
quantizable_op_type(list[str], optional): The list of op types
that will be quantized. Default is ["conv2d", "depthwise_conv2d",
"mul"].
Returns:
None
"""
"""
pass
post_training_quantization
=
PostTrainingQuantization
(
executor
=
executor
,
sample_generator
=
sample_generator
,
model_dir
=
model_dir
,
model_filename
=
model_filename
,
params_filename
=
params_filename
,
batch_size
=
batch_size
,
batch_nums
=
batch_nums
,
scope
=
scope
,
algo
=
algo
,
quantizable_op_type
=
quantizable_op_type
,
is_full_quantize
=
False
)
post_training_quantization
.
quantize
()
post_training_quantization
.
save_quantized_model
(
quantize_model_path
)
def
convert
(
program
,
place
,
config
,
scope
=
None
,
save_int8
=
False
):
def
convert
(
program
,
place
,
config
,
scope
=
None
,
save_int8
=
False
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录