Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
b22dbf3a
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看板
未验证
提交
b22dbf3a
编写于
6月 27, 2022
作者:
C
ceci3
提交者:
GitHub
6月 27, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename fluid API to static API (#1193)
上级
2d4964bc
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
190 addition
and
93 deletion
+190
-93
demo/auto_compression/README.md
demo/auto_compression/README.md
+2
-1
demo/auto_compression/convert.py
demo/auto_compression/convert.py
+37
-0
demo/auto_compression/detection/configs/ppyoloe_l_qat_dis.yaml
...auto_compression/detection/configs/ppyoloe_l_qat_dis.yaml
+1
-1
demo/auto_compression/detection/configs/ssd_mbv1_voc_qat_dis.yaml
...o_compression/detection/configs/ssd_mbv1_voc_qat_dis.yaml
+1
-1
demo/auto_compression/detection/configs/tinypose_qat_dis.yaml
.../auto_compression/detection/configs/tinypose_qat_dis.yaml
+1
-1
demo/auto_compression/detection/configs/yolov3_mbv1_qat_dis.yaml
...to_compression/detection/configs/yolov3_mbv1_qat_dis.yaml
+1
-1
demo/auto_compression/detection/configs/yolov5s_qat_dis.yaml
demo/auto_compression/detection/configs/yolov5s_qat_dis.yaml
+1
-1
demo/auto_compression/nlp/README.md
demo/auto_compression/nlp/README.md
+2
-2
demo/auto_compression/nlp/run.sh
demo/auto_compression/nlp/run.sh
+4
-4
paddleslim/auto_compression/compressor.py
paddleslim/auto_compression/compressor.py
+48
-30
paddleslim/auto_compression/create_compressed_program.py
paddleslim/auto_compression/create_compressed_program.py
+17
-15
paddleslim/auto_compression/utils/__init__.py
paddleslim/auto_compression/utils/__init__.py
+3
-0
paddleslim/auto_compression/utils/fake_ptq.py
paddleslim/auto_compression/utils/fake_ptq.py
+10
-7
paddleslim/auto_compression/utils/load_model.py
paddleslim/auto_compression/utils/load_model.py
+36
-0
paddleslim/auto_compression/utils/predict.py
paddleslim/auto_compression/utils/predict.py
+6
-6
paddleslim/auto_compression/utils/prune_model.py
paddleslim/auto_compression/utils/prune_model.py
+19
-21
tests/act/test_act_api.py
tests/act/test_act_api.py
+1
-2
未找到文件。
demo/auto_compression/README.md
浏览文件 @
b22dbf3a
...
...
@@ -61,7 +61,7 @@ image = paddle.static.data(
train_loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
feed_list
=
[
image
],
batch_size
=
32
,
return_list
=
False
)
# 开始自动压缩
ac
=
AutoCompression
(
model_dir
=
"./MobileNetV1_infer
/
"
,
model_dir
=
"./MobileNetV1_infer"
,
model_filename
=
"inference.pdmodel"
,
params_filename
=
"inference.pdiparams"
,
save_dir
=
"output"
,
...
...
@@ -76,6 +76,7 @@ ac.compress()
-
DataLoader传入的数据集是待压缩模型所用的数据集,DataLoader继承自
`paddle.io.DataLoader`
。
-
如无需验证自动化压缩过程中模型的精度,
`eval_callback`
可不传入function,程序会自动根据损失来选择最优模型。
-
自动化压缩Config中定义量化、蒸馏、剪枝等压缩算法会合并执行,压缩策略有:量化+蒸馏,剪枝+蒸馏等等。
-
如果要压缩的模型参数是存储在各自分离的文件中,需要先通过
[
convert.py
](
./convert.py
)
脚本将其保存成一个单独的二进制文件。
## 应用示例
...
...
demo/auto_compression/convert.py
0 → 100644
浏览文件 @
b22dbf3a
import
argparse
import
paddle
from
..utility
import
add_arguments
,
print_arguments
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
add_arg
=
functools
.
partial
(
add_arguments
,
argparser
=
parser
)
# yapf: disable
add_arg
(
'model_dir'
,
str
,
None
,
"inference model directory."
)
add_arg
(
'save_dir'
,
str
,
None
,
"directory to save compressed model."
)
add_arg
(
'model_filename'
,
str
,
None
,
"inference model filename."
)
# yapf: enable
if
__name__
==
'__main__'
:
args
=
parser
.
parse_args
()
print_arguments
(
args
)
paddle
.
enable_static
()
place
=
fluid
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
paddle
.
CPUPlace
())
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
paddle
.
fluid
.
io
.
load_inference_model
(
dirname
=
args
.
model_dir
,
executor
=
exe
,
model_filename
=
args
.
model_filename
,
params_filename
=
None
)
feed_vars
=
[
inference_program
.
global_block
().
var
(
name
)
for
name
in
feed_target_names
]
paddle
.
static
.
save_inference_model
(
args
.
save_dir
,
executor
=
exe
,
model_filename
=
'model.pdmodel'
,
params_filename
=
'model.pdiparams'
,
feed_vars
=
feed_vars
,
fetch_vars
=
fetch_targets
)
demo/auto_compression/detection/configs/ppyoloe_l_qat_dis.yaml
浏览文件 @
b22dbf3a
...
...
@@ -3,7 +3,7 @@ Global:
reader_config
:
configs/yolo_reader.yml
input_list
:
[
'
image'
,
'
scale_factor'
]
Evaluation
:
True
model_dir
:
./ppyoloe_crn_l_300e_coco
/
model_dir
:
./ppyoloe_crn_l_300e_coco
model_filename
:
model.pdmodel
params_filename
:
model.pdiparams
...
...
demo/auto_compression/detection/configs/ssd_mbv1_voc_qat_dis.yaml
浏览文件 @
b22dbf3a
...
...
@@ -2,7 +2,7 @@ Global:
reader_config
:
configs/ssd_reader.yml
input_list
:
[
'
image'
,
'
scale_factor'
,
'
im_shape'
]
Evaluation
:
True
model_dir
:
./ssd_mobilenet_v1_300_120e_voc
/
model_dir
:
./ssd_mobilenet_v1_300_120e_voc
model_filename
:
model.pdmodel
params_filename
:
model.pdiparams
...
...
demo/auto_compression/detection/configs/tinypose_qat_dis.yaml
浏览文件 @
b22dbf3a
...
...
@@ -2,7 +2,7 @@ Global:
reader_config
:
configs/tinypose_reader.yml
input_list
:
[
'
image'
]
Evaluation
:
False
model_dir
:
./tinypose_128x96
/
model_dir
:
./tinypose_128x96
model_filename
:
model.pdmodel
params_filename
:
model.pdiparams
...
...
demo/auto_compression/detection/configs/yolov3_mbv1_qat_dis.yaml
浏览文件 @
b22dbf3a
...
...
@@ -2,7 +2,7 @@ Global:
reader_config
:
configs/yolo_reader.yml
input_list
:
[
'
image'
,
'
im_shape'
,
'
scale_factor'
]
Evaluation
:
True
model_dir
:
./yolov3_mobilenet_v1_270e_coco
/
# Model Link: https://paddledet.bj.bcebos.com/models/slim/yolov3_mobilenet_v1_270e_coco.tar
model_dir
:
./yolov3_mobilenet_v1_270e_coco
# Model Link: https://paddledet.bj.bcebos.com/models/slim/yolov3_mobilenet_v1_270e_coco.tar
model_filename
:
model.pdmodel
params_filename
:
model.pdiparams
...
...
demo/auto_compression/detection/configs/yolov5s_qat_dis.yaml
浏览文件 @
b22dbf3a
...
...
@@ -4,7 +4,7 @@ Global:
input_list
:
{
'
image'
:
'
x2paddle_images'
}
Evaluation
:
True
arch
:
'
YOLOv5'
model_dir
:
./yolov5s_infer
/
model_dir
:
./yolov5s_infer
model_filename
:
model.pdmodel
params_filename
:
model.pdiparams
...
...
demo/auto_compression/nlp/README.md
浏览文件 @
b22dbf3a
...
...
@@ -109,7 +109,7 @@ tar -zxvf afqmc.tar
export
CUDA_VISIBLE_DEVICES
=
0
python run.py
\
--model_type
=
'ppminilm'
\
--model_dir
=
'./afqmc
/
'
\
--model_dir
=
'./afqmc'
\
--model_filename
=
'inference.pdmodel'
\
--params_filename
=
'inference.pdiparams'
\
--dataset
=
'clue'
\
...
...
@@ -147,7 +147,7 @@ TrainConfig:
```
yaml
Distillation
:
teacher_model_dir
:
./afqmc
/
teacher_model_dir
:
./afqmc
teacher_model_filename
:
inference.pdmodel
teacher_params_filename
:
inference.pdiparams
```
...
...
demo/auto_compression/nlp/run.sh
浏览文件 @
b22dbf3a
export
FLAGS_cudnn_deterministic
=
True
python run.py
\
--model_type
=
'ppminilm'
\
--model_dir
=
'./a
ll_original_models/AFQMC
'
\
--model_filename
=
'infer.pdmodel'
\
--params_filename
=
'infer.pdiparams'
\
--model_dir
=
'./a
fqmc
'
\
--model_filename
=
'infer
ence
.pdmodel'
\
--params_filename
=
'infer
ence
.pdiparams'
\
--dataset
=
'clue'
\
--save_dir
=
'./save_afqmc_pruned/'
\
--batch_size
=
16
\
--max_seq_length
=
128
\
--task_name
=
'afqmc'
\
--config_path
=
'./configs/afqmc.yaml'
--config_path
=
'./configs/
pp-minilm/auto/
afqmc.yaml'
paddleslim/auto_compression/compressor.py
浏览文件 @
b22dbf3a
...
...
@@ -35,7 +35,7 @@ from .strategy_config import TrainConfig, ProgramInfo, merge_config
from
.auto_strategy
import
prepare_strategy
,
get_final_quant_config
,
create_strategy_config
,
create_train_config
from
.config_helpers
import
load_config
,
extract_strategy_config
,
extract_train_config
from
.utils.predict
import
with_variable_shape
from
.utils
import
get_feed_vars
,
wrap_dataloader
from
.utils
import
get_feed_vars
,
wrap_dataloader
,
load_inference_model
_logger
=
get_logger
(
__name__
,
level
=
logging
.
INFO
)
...
...
@@ -64,14 +64,10 @@ class AutoCompression:
Args:
model_dir(str): The path of inference model that will be compressed, and
the model and params that saved by ``paddle.static.
io.
save_inference_model``
the model and params that saved by ``paddle.static.save_inference_model``
are under the path.
model_filename(str, optional): The name of model file. If parameters
are saved in separate files, set it as 'None'. Default: '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 : 'None'.
model_filename(str): The name of model file.
params_filename(str): The name of params file.
save_dir(str): The path to save compressed model. The models in this directory will be overwrited
after calling 'compress()' function.
train_data_loader(Python Generator, Paddle.io.DataLoader): The
...
...
@@ -121,12 +117,19 @@ class AutoCompression:
deploy_hardware(str, optional): The hardware you want to deploy. Default: 'gpu'.
"""
self
.
model_dir
=
model_dir
if
model_filename
==
'None'
:
model_filename
=
None
self
.
model_filename
=
model_filename
if
params_filename
==
'None'
:
params_filename
=
None
self
.
params_filename
=
params_filename
if
params_filename
is
None
and
model_filename
is
not
None
:
raise
NotImplementedError
(
"NOT SUPPORT parameters saved in separate files. Please convert it to single binary file first."
)
self
.
final_dir
=
save_dir
if
not
os
.
path
.
exists
(
self
.
final_dir
):
os
.
makedirs
(
self
.
final_dir
)
...
...
@@ -241,12 +244,9 @@ class AutoCompression:
],
f
'Type of input_shapes should be in [dict, tuple or list] but got
{
type
(
input_shapes
)
}
.'
paddle
.
enable_static
()
exe
=
paddle
.
static
.
Executor
(
paddle
.
CPUPlace
())
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
(
paddle
.
static
.
load_inference_model
(
model_dir
,
exe
,
model_filename
=
model_filename
,
params_filename
=
params_filename
))
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
(
load_inference_model
(
model_dir
,
exe
,
model_filename
,
params_filename
))
if
type
(
input_shapes
)
in
[
list
,
tuple
]:
assert
len
(
...
...
@@ -307,10 +307,10 @@ class AutoCompression:
return
exe
,
places
def
_get_model_type
(
self
,
exe
,
model_dir
,
model_filename
,
params_filename
):
[
inference_program
,
_
,
_
]
=
paddle
.
fluid
.
io
.
load_inference_model
(
\
dirname
=
model_dir
,
\
[
inference_program
,
_
,
_
]
=
(
load_inference_model
(
\
model_dir
,
\
model_filename
=
model_filename
,
params_filename
=
params_filename
,
executor
=
exe
)
executor
=
exe
)
)
_
,
_
,
model_type
=
get_patterns
(
inference_program
)
_logger
.
info
(
f
"Detect model type:
{
model_type
}
"
)
return
model_type
...
...
@@ -575,10 +575,23 @@ class AutoCompression:
final_model_path
=
os
.
path
.
join
(
self
.
final_dir
)
if
not
os
.
path
.
exists
(
final_model_path
):
os
.
makedirs
(
final_model_path
)
tmp_model_file
=
os
.
path
.
join
(
tmp_model_path
,
self
.
model_filename
)
tmp_params_file
=
os
.
path
.
join
(
tmp_model_path
,
self
.
params_filename
)
tmp_model_file
=
"."
.
join
([
tmp_model_path
,
"pdmodel"
])
if
not
os
.
path
.
exists
(
tmp_model_file
):
tmp_model_file
=
os
.
path
.
join
(
tmp_model_path
,
self
.
model_filename
)
tmp_params_file
=
"."
.
join
([
tmp_model_path
,
"pdiparams"
])
if
not
os
.
path
.
exists
(
tmp_params_file
):
tmp_params_file
=
os
.
path
.
join
(
tmp_model_path
,
self
.
params_filename
)
if
self
.
model_filename
is
None
:
self
.
model_filename
=
"infer.pdmodel"
if
self
.
params_filename
is
None
:
self
.
params_filename
=
"infer.pdiparams"
final_model_file
=
os
.
path
.
join
(
final_model_path
,
self
.
model_filename
)
final_params_file
=
os
.
path
.
join
(
final_model_path
,
self
.
params_filename
)
if
paddle
.
distributed
.
get_rank
()
==
0
:
shutil
.
move
(
tmp_model_file
,
final_model_file
)
shutil
.
move
(
tmp_params_file
,
final_params_file
)
...
...
@@ -656,8 +669,8 @@ class AutoCompression:
model_dir
=
os
.
path
.
join
(
self
.
tmp_dir
,
'strategy_{}'
.
format
(
str
(
strategy_idx
)))
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
paddle
.
fluid
.
io
.
load_inference_model
(
\
dirname
=
model_dir
,
\
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
load_inference_model
(
\
model_dir
,
\
model_filename
=
self
.
model_filename
,
params_filename
=
self
.
params_filename
,
executor
=
self
.
_exe
)
...
...
@@ -694,7 +707,7 @@ class AutoCompression:
def
_start_train
(
self
,
train_program_info
,
test_program_info
,
strategy
,
train_config
):
best_metric
=
-
1.0
total_epochs
=
self
.
train_config
.
epochs
if
self
.
train_config
.
epochs
else
100
total_epochs
=
train_config
.
epochs
if
train_config
.
epochs
else
100
total_train_iter
=
0
for
epoch_id
in
range
(
total_epochs
):
for
batch_id
,
data
in
enumerate
(
self
.
train_dataloader
()):
...
...
@@ -716,8 +729,8 @@ class AutoCompression:
total_train_iter
,
epoch_id
,
batch_id
,
np_probs_float
))
total_train_iter
+=
1
if
total_train_iter
%
int
(
self
.
train_config
.
eval_iter
)
==
0
and
total_train_iter
!=
0
:
if
total_train_iter
%
int
(
train_config
.
eval_iter
)
==
0
and
total_train_iter
!=
0
:
if
self
.
eval_function
is
not
None
:
# GMP pruner step 3: update params before summrizing sparsity, saving model or evaluation.
...
...
@@ -755,7 +768,7 @@ class AutoCompression:
_logger
.
warning
(
"Not set eval function, so unable to test accuracy performance."
)
if
self
.
train_config
.
train_iter
and
total_train_iter
>=
self
.
train_config
.
train_iter
:
if
train_config
.
train_iter
and
total_train_iter
>=
train_config
.
train_iter
:
break
if
'unstructure'
in
self
.
_strategy
or
train_config
.
sparse_model
:
...
...
@@ -784,11 +797,16 @@ class AutoCompression:
'strategy_{}'
.
format
(
str
(
strategy_idx
+
1
)))
if
not
os
.
path
.
exists
(
model_dir
):
os
.
makedirs
(
model_dir
)
paddle
.
fluid
.
io
.
save_inference_model
(
dirname
=
str
(
model_dir
),
feeded_var_names
=
test_program_info
.
feed_target_names
,
target_vars
=
test_program_info
.
fetch_targets
,
feed_vars
=
[
test_program
.
global_block
().
var
(
name
)
for
name
in
test_program_info
.
feed_target_names
]
paddle
.
static
.
save_inference_model
(
path_prefix
=
str
(
model_dir
),
feed_vars
=
feed_vars
,
fetch_vars
=
test_program_info
.
fetch_targets
,
executor
=
self
.
_exe
,
main_
program
=
test_program
,
program
=
test_program
,
model_filename
=
self
.
model_filename
,
params_filename
=
self
.
params_filename
)
paddleslim/auto_compression/create_compressed_program.py
浏览文件 @
b22dbf3a
...
...
@@ -23,6 +23,7 @@ from ..dist import *
from
..common.recover_program
import
recover_inference_program
,
_remove_fetch_node
from
..common
import
get_logger
from
.strategy_config
import
ProgramInfo
from
.utils
import
load_inference_model
_logger
=
get_logger
(
__name__
,
level
=
logging
.
INFO
)
__all__
=
[
...
...
@@ -151,20 +152,21 @@ def _load_program_and_merge(executor,
feed_target_names
=
None
):
scope
=
paddle
.
static
.
global_scope
()
new_scope
=
paddle
.
static
.
Scope
()
if
params_filename
==
'None'
:
params_filename
=
None
try
:
with
paddle
.
static
.
scope_guard
(
new_scope
)
:
[
teacher_program
,
teacher_feed_target_names
,
teacher_fetch_targets
]
=
paddle
.
fluid
.
io
.
load_inference_model
(
\
dirname
=
model_dir
,
\
model_filename
=
model_filename
,
\
params_filename
=
params_filename
,
\
executor
=
executor
)
except
:
with
paddle
.
static
.
scope_guard
(
new_scope
):
[
teacher_program
,
teacher_feed_target_names
,
teacher_fetch_targets
]
=
paddle
.
static
.
load_inference_model
(
\
path_prefix
=
model_dir
,
\
executor
=
executor
)
if
params_filename
is
None
and
model_filename
is
not
None
:
raise
NotImplementedError
(
"NOT SUPPORT parameters saved in separate files. Please convert it to single binary file first."
)
with
paddle
.
static
.
scope_guard
(
new_scope
):
[
teacher_program
,
teacher_feed_target_names
,
teacher_fetch_targets
]
=
(
load_inference_model
(
\
model_dir
,
\
model_filename
=
model_filename
,
\
params_filename
=
params_filename
,
\
executor
=
executor
)
)
_remove_fetch_node
(
teacher_program
)
...
...
@@ -209,9 +211,9 @@ def build_distill_program(executor,
"""build distill program with infermodel"""
startup_program
=
paddle
.
static
.
Program
()
if
train_program_info
is
None
:
[
train_program
,
feed_target_names
,
fetch_targets
]
=
paddle
.
static
.
load_inference_model
(
\
[
train_program
,
feed_target_names
,
fetch_targets
]
=
(
load_inference_model
(
\
path_prefix
=
config
[
"model_dir"
]
if
"model_dir"
in
config
else
config
[
"model_path_prefix"
],
\
executor
=
executor
)
executor
=
executor
)
)
train_program
=
recover_inference_program
(
train_program
)
else
:
train_program
=
train_program_info
.
program
...
...
@@ -497,7 +499,7 @@ def remove_unused_var_nodes(program):
Returns:
program(paddle.static.Program): The sparse model.
'''
from
paddle.f
luid
import
core
from
paddle.f
ramework
import
core
from
paddle.fluid.framework
import
IrGraph
graph
=
IrGraph
(
core
.
Graph
(
program
.
desc
),
for_test
=
True
)
removed_nodes
=
set
()
...
...
paddleslim/auto_compression/utils/__init__.py
浏览文件 @
b22dbf3a
...
...
@@ -16,6 +16,9 @@ from __future__ import absolute_import
from
.predict
import
predict_compressed_model
from
.dataloader
import
*
from
.
import
dataloader
from
.load_model
import
*
from
.
import
load_model
__all__
=
[
"predict_compressed_model"
]
__all__
+=
dataloader
.
__all__
__all__
+=
load_model
.
__all__
paddleslim/auto_compression/utils/fake_ptq.py
浏览文件 @
b22dbf3a
import
paddle
from
paddle.fluid.framework
import
IrGraph
from
paddle.f
luid
import
core
from
paddle.f
ramework
import
core
from
paddle.fluid.contrib.slim.quantization
import
QuantizationTransformPass
,
AddQuantDequantPass
,
QuantizationFreezePass
try
:
...
...
@@ -11,6 +11,8 @@ except:
TRANSFORM_PASS_OP_TYPES
=
QuantizationTransformPass
.
_supported_quantizable_op_type
QUANT_DEQUANT_PASS_OP_TYPES
=
AddQuantDequantPass
.
_supported_quantizable_op_type
from
.load_model
import
load_inference_model
def
post_quant_fake
(
executor
,
model_dir
,
...
...
@@ -51,7 +53,7 @@ def post_quant_fake(executor,
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
(
_program
,
_feed_list
,
_fetch_list
=
load_inference_model
(
model_dir
,
executor
,
model_filename
=
model_filename
,
...
...
@@ -108,12 +110,13 @@ def post_quant_fake(executor,
_program
=
graph
.
to_program
()
paddle
.
fluid
.
io
.
save_inference_model
(
dirname
=
save_model_path
,
feed_vars
=
[
_program
.
global_block
().
var
(
name
)
for
name
in
_feed_list
]
paddle
.
static
.
save_inference_model
(
path_prefix
=
save_model_path
,
model_filename
=
model_filename
,
params_filename
=
params_filename
,
feed
ed_var_names
=
_feed_list
,
target
_vars
=
_fetch_list
,
feed
_vars
=
feed_vars
,
fetch
_vars
=
_fetch_list
,
executor
=
executor
,
main_
program
=
_program
)
program
=
_program
)
print
(
"The quantized model is saved in: "
+
save_model_path
)
paddleslim/auto_compression/utils/load_model.py
0 → 100644
浏览文件 @
b22dbf3a
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
paddle
__all__
=
[
'load_inference_model'
]
def
load_inference_model
(
path_prefix
,
executor
,
model_filename
=
None
,
params_filename
=
None
):
if
model_filename
is
not
None
and
params_filename
is
not
None
:
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
(
paddle
.
static
.
load_inference_model
(
path_prefix
=
path_prefix
,
executor
=
executor
,
model_filename
=
model_filename
,
params_filename
=
params_filename
))
else
:
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
(
paddle
.
static
.
load_inference_model
(
path_prefix
=
path_prefix
,
executor
=
executor
))
return
[
inference_program
,
feed_target_names
,
fetch_targets
]
paddleslim/auto_compression/utils/predict.py
浏览文件 @
b22dbf3a
...
...
@@ -4,6 +4,7 @@ import paddle
from
...analysis
import
TableLatencyPredictor
from
.prune_model
import
get_sparse_model
,
get_prune_model
from
.fake_ptq
import
post_quant_fake
from
.load_model
import
load_inference_model
def
with_variable_shape
(
model_dir
,
model_filename
=
None
,
params_filename
=
None
):
...
...
@@ -18,12 +19,11 @@ def with_variable_shape(model_dir, model_filename=None, params_filename=None):
"""
paddle
.
enable_static
()
exe
=
paddle
.
static
.
Executor
(
paddle
.
CPUPlace
())
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
(
paddle
.
fluid
.
io
.
load_inference_model
(
model_dir
,
exe
,
model_filename
=
model_filename
,
params_filename
=
params_filename
))
inference_program
,
feed_target_names
,
fetch_targets
=
load_inference_model
(
model_dir
,
exe
,
model_filename
=
model_filename
,
params_filename
=
params_filename
)
for
var_
in
inference_program
.
list_vars
():
if
var_
.
name
in
feed_target_names
:
if
var_
.
shape
.
count
(
-
1
)
>
1
:
...
...
paddleslim/auto_compression/utils/prune_model.py
浏览文件 @
b22dbf3a
...
...
@@ -2,10 +2,10 @@ import os
import
time
import
numpy
as
np
import
paddle
import
paddle.fluid
as
fluid
import
paddle.static
as
static
from
...prune
import
Pruner
from
...core
import
GraphWrapper
from
.load_model
import
load_inference_model
__all__
=
[
"get_sparse_model"
,
"get_prune_model"
]
...
...
@@ -37,12 +37,8 @@ def get_sparse_model(executor, places, model_file, param_file, ratio,
startup_prog
=
static
.
Program
()
executor
.
run
(
startup_prog
)
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
(
fluid
.
io
.
load_inference_model
(
folder
,
executor
,
model_filename
=
model_name
,
params_filename
=
param_name
))
inference_program
,
feed_target_names
,
fetch_targets
=
load_inference_model
(
folder
,
executor
,
model_filename
=
model_name
,
params_filename
=
param_name
)
thresholds
=
{}
graph
=
GraphWrapper
(
inference_program
)
...
...
@@ -87,12 +83,15 @@ def get_sparse_model(executor, places, model_file, param_file, ratio,
paddle
.
static
.
global_scope
().
find_var
(
name
).
get_tensor
().
set
(
array
,
paddle
.
CPUPlace
())
fluid
.
io
.
save_inference_model
(
feed_vars
=
[
inference_program
.
global_block
().
var
(
name
)
for
name
in
feed_target_names
]
static
.
save_inference_model
(
save_path
,
feed
ed_var_names
=
feed_target_name
s
,
target
_vars
=
fetch_targets
,
feed
_vars
=
feed_var
s
,
fetch
_vars
=
fetch_targets
,
executor
=
executor
,
main_
program
=
inference_program
,
program
=
inference_program
,
model_filename
=
model_name
,
params_filename
=
param_name
)
print
(
"The pruned model is saved in: "
,
save_path
)
...
...
@@ -127,12 +126,8 @@ def get_prune_model(executor, places, model_file, param_file, ratio, save_path):
scope
=
static
.
global_scope
()
executor
.
run
(
startup_prog
)
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
(
fluid
.
io
.
load_inference_model
(
folder
,
executor
,
model_filename
=
model_name
,
params_filename
=
param_name
))
inference_program
,
feed_target_names
,
fetch_targets
=
load_inference_model
(
folder
,
executor
,
model_filename
=
model_name
,
params_filename
=
param_name
)
prune_params
=
[]
graph
=
GraphWrapper
(
inference_program
)
...
...
@@ -162,11 +157,14 @@ def get_prune_model(executor, places, model_file, param_file, ratio, save_path):
param_backup
=
None
,
param_shape_backup
=
None
)
fluid
.
io
.
save_inference_model
(
feed_vars
=
[
main_program
.
global_block
().
var
(
name
)
for
name
in
feed_target_names
]
static
.
save_inference_model
(
save_path
,
feed
ed_var_names
=
feed_target_name
s
,
target
_vars
=
fetch_targets
,
feed
_vars
=
feed_var
s
,
fetch
_vars
=
fetch_targets
,
executor
=
executor
,
main_
program
=
main_program
,
program
=
main_program
,
model_filename
=
model_name
,
params_filename
=
param_name
)
tests/act/test_act_api.py
浏览文件 @
b22dbf3a
import
sys
import
os
sys
.
path
.
append
(
"../"
)
sys
.
path
.
append
(
"../
../
"
)
import
unittest
import
tempfile
import
paddle
import
unittest
import
numpy
as
np
from
static_case
import
StaticCase
from
paddle.io
import
Dataset
from
paddleslim.auto_compression
import
AutoCompression
from
paddleslim.auto_compression.config_helpers
import
load_config
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录