Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
44e69a8d
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看板
未验证
提交
44e69a8d
编写于
5月 20, 2022
作者:
C
ceci3
提交者:
GitHub
5月 20, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update figure in README (#1117) (#1125)
Co-authored-by:
N
whs
<
wanghaoshuang@baidu.com
>
上级
d98c7738
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
101 addition
and
79 deletion
+101
-79
demo/auto_compression/detection/README.md
demo/auto_compression/detection/README.md
+5
-3
demo/auto_compression/detection/run.py
demo/auto_compression/detection/run.py
+14
-13
demo/auto_compression/image_classification/README.md
demo/auto_compression/image_classification/README.md
+2
-0
demo/auto_compression/image_classification/run.py
demo/auto_compression/image_classification/run.py
+3
-2
demo/auto_compression/image_classification/run.sh
demo/auto_compression/image_classification/run.sh
+1
-0
demo/auto_compression/nlp/README.md
demo/auto_compression/nlp/README.md
+3
-3
demo/auto_compression/nlp/run.py
demo/auto_compression/nlp/run.py
+19
-16
demo/auto_compression/semantic_segmentation/README.md
demo/auto_compression/semantic_segmentation/README.md
+5
-2
demo/auto_compression/semantic_segmentation/configs/pp_humanseg_auto.yaml
...ssion/semantic_segmentation/configs/pp_humanseg_auto.yaml
+2
-2
demo/auto_compression/semantic_segmentation/configs/pp_humanseg_lite.yaml
...ssion/semantic_segmentation/configs/pp_humanseg_lite.yaml
+1
-1
demo/auto_compression/semantic_segmentation/configs/pp_humanseg_quant_dis.yaml
.../semantic_segmentation/configs/pp_humanseg_quant_dis.yaml
+2
-2
demo/auto_compression/semantic_segmentation/configs/pp_humanseg_sparse_dis.yaml
...semantic_segmentation/configs/pp_humanseg_sparse_dis.yaml
+2
-2
demo/auto_compression/semantic_segmentation/run.py
demo/auto_compression/semantic_segmentation/run.py
+3
-3
paddleslim/auto_compression/auto_strategy.py
paddleslim/auto_compression/auto_strategy.py
+11
-5
paddleslim/auto_compression/compressor.py
paddleslim/auto_compression/compressor.py
+18
-17
paddleslim/auto_compression/config_helpers.py
paddleslim/auto_compression/config_helpers.py
+9
-7
paddleslim/version.py
paddleslim/version.py
+1
-1
未找到文件。
demo/auto_compression/detection/README.md
浏览文件 @
44e69a8d
...
@@ -23,7 +23,7 @@
...
@@ -23,7 +23,7 @@
| 模型 | 策略 | 输入尺寸 | mAP
<sup>
val
<br>
0.5:0.95 | 预测时延
<sup><small>
FP32
</small><sup><br><sup>
(ms) |预测时延
<sup><small>
FP32
</small><sup><br><sup>
(ms) | 预测时延
<sup><small>
INT8
</small><sup><br><sup>
(ms) | 配置文件 | Inference模型 |
| 模型 | 策略 | 输入尺寸 | mAP
<sup>
val
<br>
0.5:0.95 | 预测时延
<sup><small>
FP32
</small><sup><br><sup>
(ms) |预测时延
<sup><small>
FP32
</small><sup><br><sup>
(ms) | 预测时延
<sup><small>
INT8
</small><sup><br><sup>
(ms) | 配置文件 | Inference模型 |
| :-------- |:-------- |:--------: | :---------------------: | :----------------: | :----------------: | :---------------: | :-----------------------------: | :-----------------------------: |
| :-------- |:-------- |:--------: | :---------------------: | :----------------: | :----------------: | :---------------: | :-----------------------------: | :-----------------------------: |
| PP-YOLOE-l | Base模型 | 640
*
640 | 50.9 | 11.2 | 7.7ms | - |
[
config
](
https://github.com/PaddlePaddle/PaddleDetection/blob/develop/configs/ppyoloe/ppyoloe_crn_l_300e_coco.yml
)
|
[
Model
](
https://bj.bcebos.com/v1/paddle-slim-models/detection/ppyoloe_crn_l_300e_coco.tar
)
|
| PP-YOLOE-l | Base模型 | 640
*
640 | 50.9 | 11.2 | 7.7ms | - |
[
config
](
https://github.com/PaddlePaddle/PaddleDetection/blob/develop/configs/ppyoloe/ppyoloe_crn_l_300e_coco.yml
)
|
[
Model
](
https://bj.bcebos.com/v1/paddle-slim-models/detection/ppyoloe_crn_l_300e_coco.tar
)
|
| PP-YOLOE-l | 量化+蒸馏 | 640
*
640 | 50.6 | - | - | 6.7ms |
[
config
](
https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/auto_compression/detection/configs/ppyoloe_l_qat_dis
t
.yaml
)
|
[
Model
](
https://bj.bcebos.com/v1/paddle-slim-models/act/ppyoloe_crn_l_300e_coco_quant.tar
)
|
| PP-YOLOE-l | 量化+蒸馏 | 640
*
640 | 50.6 | - | - | 6.7ms |
[
config
](
https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/auto_compression/detection/configs/ppyoloe_l_qat_dis.yaml
)
|
[
Model
](
https://bj.bcebos.com/v1/paddle-slim-models/act/ppyoloe_crn_l_300e_coco_quant.tar
)
|
-
mAP的指标均在COCO val2017数据集中评测得到。
-
mAP的指标均在COCO val2017数据集中评测得到。
-
PP-YOLOE模型在Tesla V100的GPU环境下测试,测试脚本是
[
benchmark demo
](
https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/deploy/python
)
-
PP-YOLOE模型在Tesla V100的GPU环境下测试,测试脚本是
[
benchmark demo
](
https://github.com/PaddlePaddle/PaddleDetection/tree/release/2.4/deploy/python
)
...
@@ -95,7 +95,8 @@ tar -xf ppyoloe_crn_l_300e_coco.tar
...
@@ -95,7 +95,8 @@ tar -xf ppyoloe_crn_l_300e_coco.tar
使用run.py脚本得到模型的mAP:
使用run.py脚本得到模型的mAP:
```
```
python run.py --config_path=./configs/ppyoloe_l_qat_dist.yaml --eval=True
export CUDA_VISIBLE_DEVEICES=0
python run.py --config_path=./configs/ppyoloe_l_qat_dis.yaml --eval=True
```
```
**注意**
:TinyPose模型暂不支持精度测试。
**注意**
:TinyPose模型暂不支持精度测试。
...
@@ -104,7 +105,8 @@ python run.py --config_path=./configs/ppyoloe_l_qat_dist.yaml --eval=True
...
@@ -104,7 +105,8 @@ python run.py --config_path=./configs/ppyoloe_l_qat_dist.yaml --eval=True
蒸馏量化自动压缩示例通过run.py脚本启动,会使用接口
```paddleslim.auto_compression.AutoCompression```
对模型进行自动压缩。配置config文件中模型路径、蒸馏、量化、和训练等部分的参数,配置完成后便可对模型进行量化和蒸馏。具体运行命令为:
蒸馏量化自动压缩示例通过run.py脚本启动,会使用接口
```paddleslim.auto_compression.AutoCompression```
对模型进行自动压缩。配置config文件中模型路径、蒸馏、量化、和训练等部分的参数,配置完成后便可对模型进行量化和蒸馏。具体运行命令为:
```
```
python run.py --config_path=./configs/ppyoloe_l_qat_dist.yaml --save_dir='./output/'
export CUDA_VISIBLE_DEVEICES=0
python run.py --config_path=./configs/ppyoloe_l_qat_dis.yaml --save_dir='./output/'
```
```
...
...
demo/auto_compression/detection/run.py
浏览文件 @
44e69a8d
...
@@ -66,16 +66,16 @@ def reader_wrapper(reader, input_list):
...
@@ -66,16 +66,16 @@ def reader_wrapper(reader, input_list):
return
gen
return
gen
def
eval
(
co
mpress_co
nfig
):
def
eval
(
config
):
place
=
paddle
.
CUDAPlace
(
0
)
if
FLAGS
.
devices
==
'gpu'
else
paddle
.
CPUPlace
()
place
=
paddle
.
CUDAPlace
(
0
)
if
FLAGS
.
devices
==
'gpu'
else
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
val_program
,
feed_target_names
,
fetch_targets
=
paddle
.
fluid
.
io
.
load_inference_model
(
val_program
,
feed_target_names
,
fetch_targets
=
paddle
.
fluid
.
io
.
load_inference_model
(
co
mpress_co
nfig
[
"model_dir"
],
config
[
"model_dir"
],
exe
,
exe
,
model_filename
=
co
mpress_co
nfig
[
"model_filename"
],
model_filename
=
config
[
"model_filename"
],
params_filename
=
co
mpress_co
nfig
[
"params_filename"
],
)
params_filename
=
config
[
"params_filename"
],
)
clsid2catid
=
{
v
:
k
for
k
,
v
in
dataset
.
catid2clsid
.
items
()}
clsid2catid
=
{
v
:
k
for
k
,
v
in
dataset
.
catid2clsid
.
items
()}
anno_file
=
dataset
.
get_anno
()
anno_file
=
dataset
.
get_anno
()
...
@@ -85,7 +85,7 @@ def eval(compress_config):
...
@@ -85,7 +85,7 @@ def eval(compress_config):
data_all
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
data
.
items
()}
data_all
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
data
.
items
()}
data_input
=
{}
data_input
=
{}
for
k
,
v
in
data
.
items
():
for
k
,
v
in
data
.
items
():
if
k
in
co
mpress_co
nfig
[
'input_list'
]:
if
k
in
config
[
'input_list'
]:
data_input
[
k
]
=
np
.
array
(
v
)
data_input
[
k
]
=
np
.
array
(
v
)
outs
=
exe
.
run
(
val_program
,
outs
=
exe
.
run
(
val_program
,
feed
=
data_input
,
feed
=
data_input
,
...
@@ -142,13 +142,14 @@ def eval_function(exe, compiled_test_program, test_feed_names, test_fetch_list):
...
@@ -142,13 +142,14 @@ def eval_function(exe, compiled_test_program, test_feed_names, test_fetch_list):
def
main
():
def
main
():
compress_config
,
train_config
=
load_slim_config
(
FLAGS
.
config_path
)
compress_config
,
train_config
,
global_config
=
load_slim_config
(
reader_cfg
=
load_config
(
compress_config
[
'reader_config'
])
FLAGS
.
config_path
)
reader_cfg
=
load_config
(
global_config
[
'reader_config'
])
train_loader
=
create
(
'EvalReader'
)(
reader_cfg
[
'TrainDataset'
],
train_loader
=
create
(
'EvalReader'
)(
reader_cfg
[
'TrainDataset'
],
reader_cfg
[
'worker_num'
],
reader_cfg
[
'worker_num'
],
return_list
=
True
)
return_list
=
True
)
train_loader
=
reader_wrapper
(
train_loader
,
compress
_config
[
'input_list'
])
train_loader
=
reader_wrapper
(
train_loader
,
global
_config
[
'input_list'
])
global
dataset
global
dataset
dataset
=
reader_cfg
[
'EvalDataset'
]
dataset
=
reader_cfg
[
'EvalDataset'
]
...
@@ -158,18 +159,18 @@ def main():
...
@@ -158,18 +159,18 @@ def main():
return_list
=
True
)
return_list
=
True
)
if
FLAGS
.
eval
:
if
FLAGS
.
eval
:
eval
(
compress
_config
)
eval
(
global
_config
)
sys
.
exit
(
0
)
sys
.
exit
(
0
)
if
'Evaluation'
in
compress_config
.
keys
()
and
compress
_config
[
'Evaluation'
]:
if
'Evaluation'
in
global_config
.
keys
()
and
global
_config
[
'Evaluation'
]:
eval_func
=
eval_function
eval_func
=
eval_function
else
:
else
:
eval_func
=
None
eval_func
=
None
ac
=
AutoCompression
(
ac
=
AutoCompression
(
model_dir
=
compress
_config
[
"model_dir"
],
model_dir
=
global
_config
[
"model_dir"
],
model_filename
=
compress
_config
[
"model_filename"
],
model_filename
=
global
_config
[
"model_filename"
],
params_filename
=
compress
_config
[
"params_filename"
],
params_filename
=
global
_config
[
"params_filename"
],
save_dir
=
FLAGS
.
save_dir
,
save_dir
=
FLAGS
.
save_dir
,
strategy_config
=
compress_config
,
strategy_config
=
compress_config
,
train_config
=
train_config
,
train_config
=
train_config
,
...
...
demo/auto_compression/image_classification/README.md
浏览文件 @
44e69a8d
...
@@ -69,6 +69,7 @@ tar -xf MobileNetV1_infer.tar
...
@@ -69,6 +69,7 @@ tar -xf MobileNetV1_infer.tar
```
shell
```
shell
# 单卡启动
# 单卡启动
export
CUDA_VISIBLE_DEVEICES
=
0
python run.py
\
python run.py
\
--model_dir
=
'MobileNetV1_infer'
\
--model_dir
=
'MobileNetV1_infer'
\
--model_filename
=
'inference.pdmodel'
\
--model_filename
=
'inference.pdmodel'
\
...
@@ -97,3 +98,4 @@ python -m paddle.distributed.launch run.py \
...
@@ -97,3 +98,4 @@ python -m paddle.distributed.launch run.py \
-
[
Paddle Lite部署
](
https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.5/docs/deployment/lite/lite.md
)
-
[
Paddle Lite部署
](
https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.5/docs/deployment/lite/lite.md
)
## 5.FAQ
## 5.FAQ
[1.] 如果遇到报错
```ValueError: var inputs not in this block```
,则说明模型中的输入变量的名字不是
```inputs```
,可以先用netron可视化查看输入变量的名称,然后修改
```run.py```
中的第35行中
``` yield {"inputs": imgs}```
为
```yield {${input_tensor_name}: imgs}```
。一般PaddleClas产出部署模型的输入名字如果不是
```inputs```
,则是
```x```
。
demo/auto_compression/image_classification/run.py
浏览文件 @
44e69a8d
import
os
import
os
import
sys
import
sys
sys
.
path
[
0
]
=
os
.
path
.
join
(
os
.
path
.
dirname
(
"__file__"
),
os
.
path
.
pardir
,
os
.
path
.
pardir
)
sys
.
path
[
0
]
=
os
.
path
.
join
(
os
.
path
.
dirname
(
"__file__"
),
os
.
path
.
pardir
,
os
.
path
.
pardir
)
import
argparse
import
argparse
import
functools
import
functools
from
functools
import
partial
from
functools
import
partial
...
@@ -32,7 +33,7 @@ def reader_wrapper(reader):
...
@@ -32,7 +33,7 @@ def reader_wrapper(reader):
def
gen
():
def
gen
():
for
i
,
data
in
enumerate
(
reader
()):
for
i
,
data
in
enumerate
(
reader
()):
imgs
=
np
.
float32
([
item
[
0
]
for
item
in
data
])
imgs
=
np
.
float32
([
item
[
0
]
for
item
in
data
])
yield
{
"
x
"
:
imgs
}
yield
{
"
inputs
"
:
imgs
}
return
gen
return
gen
...
...
demo/auto_compression/image_classification/run.sh
浏览文件 @
44e69a8d
# 单卡启动
# 单卡启动
export
CUDA_VISIBLE_DEVEICES
=
0
python run.py
\
python run.py
\
--model_dir
=
'MobileNetV1_infer'
\
--model_dir
=
'MobileNetV1_infer'
\
--model_filename
=
'inference.pdmodel'
\
--model_filename
=
'inference.pdmodel'
\
...
...
demo/auto_compression/nlp/README.md
浏览文件 @
44e69a8d
...
@@ -62,7 +62,7 @@ pip install paddleslim
...
@@ -62,7 +62,7 @@ pip install paddleslim
安装paddlenlp:
安装paddlenlp:
```
shell
```
shell
pip
install
paddlenlp
pip
install
paddlenlp
```
```
注:安装PaddleNLP的目的是为了下载PaddleNLP中的数据集和Tokenizer。
注:安装PaddleNLP的目的是为了下载PaddleNLP中的数据集和Tokenizer。
...
@@ -88,6 +88,7 @@ tar -zxvf afqmc.tar
...
@@ -88,6 +88,7 @@ tar -zxvf afqmc.tar
数据集为CLUE,不同任务名称代表CLUE上不同的任务,可选择的任务名称有:afqmc, tnews, iflytek, ocnli, cmnli, cluewsc2020, csl。具体运行命令为
数据集为CLUE,不同任务名称代表CLUE上不同的任务,可选择的任务名称有:afqmc, tnews, iflytek, ocnli, cmnli, cluewsc2020, csl。具体运行命令为
:
:
```
shell
```
shell
export
CUDA_VISIBLE_DEVEICES
=
0
python run.py
\
python run.py
\
--model_type
=
'ppminilm'
\
--model_type
=
'ppminilm'
\
--model_dir
=
'./afqmc/'
\
--model_dir
=
'./afqmc/'
\
...
@@ -98,7 +99,7 @@ python run.py \
...
@@ -98,7 +99,7 @@ python run.py \
--batch_size
=
16
\
--batch_size
=
16
\
--max_seq_length
=
128
\
--max_seq_length
=
128
\
--task_name
=
'afqmc'
\
--task_name
=
'afqmc'
\
--config_path
=
'./configs/afqmc.yaml'
--config_path
=
'./configs/afqmc.yaml'
```
```
## 4. 压缩配置介绍
## 4. 压缩配置介绍
...
@@ -184,4 +185,3 @@ Quantization:
...
@@ -184,4 +185,3 @@ Quantization:
-
[
Paddle Lite部署
](
https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.5/docs/deployment/lite/lite.md
)
-
[
Paddle Lite部署
](
https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.5/docs/deployment/lite/lite.md
)
## 6. FAQ
## 6. FAQ
demo/auto_compression/nlp/run.py
浏览文件 @
44e69a8d
import
os
import
os
import
sys
import
sys
sys
.
path
[
0
]
=
os
.
path
.
join
(
os
.
path
.
dirname
(
"__file__"
),
os
.
path
.
pardir
,
os
.
path
.
pardir
)
sys
.
path
[
0
]
=
os
.
path
.
join
(
os
.
path
.
dirname
(
"__file__"
),
os
.
path
.
pardir
,
os
.
path
.
pardir
)
import
argparse
import
argparse
import
functools
import
functools
from
functools
import
partial
from
functools
import
partial
...
@@ -58,7 +59,9 @@ def convert_example(example,
...
@@ -58,7 +59,9 @@ def convert_example(example,
label_list
,
label_list
,
max_seq_length
=
512
,
max_seq_length
=
512
,
is_test
=
False
):
is_test
=
False
):
assert
args
.
dataset
in
[
'glue'
,
'clue'
],
"This demo only supports for dataset glue or clue"
assert
args
.
dataset
in
[
'glue'
,
'clue'
],
"This demo only supports for dataset glue or clue"
"""Convert a glue example into necessary features."""
"""Convert a glue example into necessary features."""
if
args
.
dataset
==
'glue'
:
if
args
.
dataset
==
'glue'
:
if
not
is_test
:
if
not
is_test
:
...
@@ -74,13 +77,14 @@ def convert_example(example,
...
@@ -74,13 +77,14 @@ def convert_example(example,
return
example
[
'input_ids'
],
example
[
'token_type_ids'
],
label
return
example
[
'input_ids'
],
example
[
'token_type_ids'
],
label
else
:
else
:
return
example
[
'input_ids'
],
example
[
'token_type_ids'
]
return
example
[
'input_ids'
],
example
[
'token_type_ids'
]
else
:
#if args.dataset == 'clue':
else
:
#if args.dataset == 'clue':
if
not
is_test
:
if
not
is_test
:
# `label_list == None` is for regression task
# `label_list == None` is for regression task
label_dtype
=
"int64"
if
label_list
else
"float32"
label_dtype
=
"int64"
if
label_list
else
"float32"
# Get the label
# Get the label
example
[
'label'
]
=
np
.
array
(
example
[
"label"
],
dtype
=
"int64"
).
reshape
((
-
1
,
1
))
example
[
'label'
]
=
np
.
array
(
example
[
"label"
],
dtype
=
"int64"
).
reshape
((
-
1
,
1
))
label
=
example
[
'label'
]
label
=
example
[
'label'
]
# Convert raw text to feature
# Convert raw text to feature
if
'keyword'
in
example
:
# CSL
if
'keyword'
in
example
:
# CSL
...
@@ -91,12 +95,13 @@ def convert_example(example,
...
@@ -91,12 +95,13 @@ def convert_example(example,
'label'
:
example
[
'label'
]
'label'
:
example
[
'label'
]
}
}
elif
'target'
in
example
:
# wsc
elif
'target'
in
example
:
# wsc
text
,
query
,
pronoun
,
query_idx
,
pronoun_idx
=
example
[
'text'
],
example
[
text
,
query
,
pronoun
,
query_idx
,
pronoun_idx
=
example
[
'target'
][
'span1_text'
],
example
[
'target'
][
'span2_text'
],
example
[
'text'
],
example
[
'target'
][
'span1_text'
],
example
[
'target'
][
'target'
][
'span1_index'
],
example
[
'target'
][
'span2_index'
]
'span2_text'
],
example
[
'target'
][
'span1_index'
],
example
[
'target'
][
'span2_index'
]
text_list
=
list
(
text
)
text_list
=
list
(
text
)
assert
text
[
pronoun_idx
:(
pronoun_idx
+
len
(
pronoun
)
assert
text
[
pronoun_idx
:(
pronoun_idx
+
len
(
)]
==
pronoun
,
"pronoun: {}"
.
format
(
pronoun
)
pronoun
)
)]
==
pronoun
,
"pronoun: {}"
.
format
(
pronoun
)
assert
text
[
query_idx
:(
query_idx
+
len
(
query
)
assert
text
[
query_idx
:(
query_idx
+
len
(
query
)
)]
==
query
,
"query: {}"
.
format
(
query
)
)]
==
query
,
"query: {}"
.
format
(
query
)
if
pronoun_idx
>
query_idx
:
if
pronoun_idx
>
query_idx
:
...
@@ -125,8 +130,6 @@ def convert_example(example,
...
@@ -125,8 +130,6 @@ def convert_example(example,
else
:
else
:
return
example
[
'input_ids'
],
example
[
'token_type_ids'
]
return
example
[
'input_ids'
],
example
[
'token_type_ids'
]
def
create_data_holder
(
task_name
):
def
create_data_holder
(
task_name
):
"""
"""
...
@@ -148,7 +151,7 @@ def reader():
...
@@ -148,7 +151,7 @@ def reader():
# Create the tokenizer and dataset
# Create the tokenizer and dataset
if
args
.
model_type
==
'bert'
:
if
args
.
model_type
==
'bert'
:
tokenizer
=
BertTokenizer
.
from_pretrained
(
args
.
model_dir
)
tokenizer
=
BertTokenizer
.
from_pretrained
(
args
.
model_dir
)
else
:
# ppminilm
else
:
# ppminilm
tokenizer
=
PPMiniLMTokenizer
.
from_pretrained
(
args
.
model_dir
)
tokenizer
=
PPMiniLMTokenizer
.
from_pretrained
(
args
.
model_dir
)
train_ds
,
dev_ds
=
load_dataset
(
train_ds
,
dev_ds
=
load_dataset
(
args
.
dataset
,
args
.
task_name
,
splits
=
(
'train'
,
'dev'
))
args
.
dataset
,
args
.
task_name
,
splits
=
(
'train'
,
'dev'
))
...
@@ -239,7 +242,7 @@ if __name__ == '__main__':
...
@@ -239,7 +242,7 @@ if __name__ == '__main__':
print_arguments
(
args
)
print_arguments
(
args
)
paddle
.
enable_static
()
paddle
.
enable_static
()
compress_config
,
train_config
=
load_config
(
args
.
config_path
)
compress_config
,
train_config
,
_
=
load_config
(
args
.
config_path
)
if
train_config
is
not
None
and
'optim_args'
in
train_config
:
if
train_config
is
not
None
and
'optim_args'
in
train_config
:
train_config
[
'optim_args'
][
train_config
[
'optim_args'
][
'apply_decay_param_fun'
]
=
apply_decay_param_fun
'apply_decay_param_fun'
]
=
apply_decay_param_fun
...
@@ -256,8 +259,8 @@ if __name__ == '__main__':
...
@@ -256,8 +259,8 @@ if __name__ == '__main__':
strategy_config
=
compress_config
,
strategy_config
=
compress_config
,
train_config
=
train_config
,
train_config
=
train_config
,
train_dataloader
=
train_dataloader
,
train_dataloader
=
train_dataloader
,
eval_callback
=
eval_function
eval_callback
=
eval_function
if
compress_config
is
None
or
if
compress_config
is
None
or
'HyperParameterOptimization'
not
in
compress_config
else
'HyperParameterOptimization'
not
in
compress_config
else
eval_dataloader
,
eval_dataloader
,
eval_dataloader
=
eval_dataloader
)
eval_dataloader
=
eval_dataloader
)
...
...
demo/auto_compression/semantic_segmentation/README.md
浏览文件 @
44e69a8d
...
@@ -74,7 +74,7 @@ pip install paddleseg
...
@@ -74,7 +74,7 @@ pip install paddleseg
-
如果想快速体验,可直接下载PP-HumanSeg-Lite 的预测模型:
-
如果想快速体验,可直接下载PP-HumanSeg-Lite 的预测模型:
```
shell
```
shell
w
eg
t https://paddleseg.bj.bcebos.com/dygraph/ppseg/ppseg_lite_portrait_398x224_with_softmax.tar.gz
w
ge
t https://paddleseg.bj.bcebos.com/dygraph/ppseg/ppseg_lite_portrait_398x224_with_softmax.tar.gz
tar
-xzf
ppseg_lite_portrait_398x224_with_softmax.tar.gz
tar
-xzf
ppseg_lite_portrait_398x224_with_softmax.tar.gz
```
```
...
@@ -87,16 +87,18 @@ tar -xzf ppseg_lite_portrait_398x224_with_softmax.tar.gz
...
@@ -87,16 +87,18 @@ tar -xzf ppseg_lite_portrait_398x224_with_softmax.tar.gz
当只设置训练参数,并传入
``deploy_hardware``
字段时,将自动搜索压缩策略进行压缩。以骁龙710(SD710)为部署硬件,进行自动压缩的运行命令如下:
当只设置训练参数,并传入
``deploy_hardware``
字段时,将自动搜索压缩策略进行压缩。以骁龙710(SD710)为部署硬件,进行自动压缩的运行命令如下:
```
shell
```
shell
export
CUDA_VISIBLE_DEVICES
=
0
python run.py
\
python run.py
\
--model_dir
=
'./ppseg_lite_portrait_398x224_with_softmax'
\
--model_dir
=
'./ppseg_lite_portrait_398x224_with_softmax'
\
--model_filename
=
'model.pdmodel'
\
--model_filename
=
'model.pdmodel'
\
--params_filename
=
'model.pdiparams'
\
--params_filename
=
'model.pdiparams'
\
--save_dir
=
'./save_model'
\
--save_dir
=
'./save_model'
\
--config_path
=
'configs/pp_humanseg_auto.yaml'
--config_path
=
'configs/pp_humanseg_auto.yaml'
\
--deploy_hardware
=
'SD710'
--deploy_hardware
=
'SD710'
```
```
-
自行配置稀疏参数进行非结构化稀疏和蒸馏训练,配置参数含义详见
[
自动压缩超参文档
](
https://github.com/PaddlePaddle/PaddleSlim/blob/27dafe1c722476f1b16879f7045e9215b6f37559/demo/auto_compression/hyperparameter_tutorial.md
)
。具体命令如下所示:
-
自行配置稀疏参数进行非结构化稀疏和蒸馏训练,配置参数含义详见
[
自动压缩超参文档
](
https://github.com/PaddlePaddle/PaddleSlim/blob/27dafe1c722476f1b16879f7045e9215b6f37559/demo/auto_compression/hyperparameter_tutorial.md
)
。具体命令如下所示:
```
shell
```
shell
export
CUDA_VISIBLE_DEVICES
=
0
python run.py
\
python run.py
\
--model_dir
=
'./ppseg_lite_portrait_398x224_with_softmax'
\
--model_dir
=
'./ppseg_lite_portrait_398x224_with_softmax'
\
--model_filename
=
'model.pdmodel'
\
--model_filename
=
'model.pdmodel'
\
...
@@ -107,6 +109,7 @@ python run.py \
...
@@ -107,6 +109,7 @@ python run.py \
-
自行配置量化参数进行量化和蒸馏训练,配置参数含义详见
[
自动压缩超参文档
](
https://github.com/PaddlePaddle/PaddleSlim/blob/27dafe1c722476f1b16879f7045e9215b6f37559/demo/auto_compression/hyperparameter_tutorial.md
)
。具体命令如下所示:
-
自行配置量化参数进行量化和蒸馏训练,配置参数含义详见
[
自动压缩超参文档
](
https://github.com/PaddlePaddle/PaddleSlim/blob/27dafe1c722476f1b16879f7045e9215b6f37559/demo/auto_compression/hyperparameter_tutorial.md
)
。具体命令如下所示:
```
shell
```
shell
export
CUDA_VISIBLE_DEVICES
=
0
python run.py
\
python run.py
\
--model_dir
=
'./ppseg_lite_portrait_398x224_with_softmax'
\
--model_dir
=
'./ppseg_lite_portrait_398x224_with_softmax'
\
--model_filename
=
'model.pdmodel'
\
--model_filename
=
'model.pdmodel'
\
...
...
demo/auto_compression/semantic_segmentation/configs/pp_humanseg_auto.yaml
浏览文件 @
44e69a8d
Global
:
Global
:
reader_config
:
configs/pp_humanseg_lite.yml
reader_config
:
configs/pp_humanseg_lite.y
a
ml
TrainConfig
:
TrainConfig
:
epochs
:
14
epochs
:
14
...
@@ -8,4 +8,4 @@ TrainConfig:
...
@@ -8,4 +8,4 @@ TrainConfig:
optim_args
:
optim_args
:
weight_decay
:
0.0005
weight_decay
:
0.0005
optimizer
:
SGD
optimizer
:
SGD
\ No newline at end of file
demo/auto_compression/semantic_segmentation/configs/pp_humanseg_lite.yaml
浏览文件 @
44e69a8d
...
@@ -25,7 +25,7 @@ train_dataset:
...
@@ -25,7 +25,7 @@ train_dataset:
val_dataset
:
val_dataset
:
type
:
Dataset
type
:
Dataset
dataset_root
:
data/humanseg
dataset_root
:
data/humanseg
train
_path
:
data/humanseg/val.txt
val
_path
:
data/humanseg/val.txt
num_classes
:
2
num_classes
:
2
transforms
:
transforms
:
-
type
:
PaddingByAspectRatio
-
type
:
PaddingByAspectRatio
...
...
demo/auto_compression/semantic_segmentation/configs/pp_humanseg_quant_dis.yaml
浏览文件 @
44e69a8d
Global
:
Global
:
reader_config
:
configs/pp_humanseg_lite.yml
reader_config
:
configs/pp_humanseg_lite.y
a
ml
Distillation
:
Distillation
:
distill_lambda
:
1.0
distill_lambda
:
1.0
...
@@ -26,4 +26,4 @@ TrainConfig:
...
@@ -26,4 +26,4 @@ TrainConfig:
learning_rate
:
0.0005
learning_rate
:
0.0005
optimizer
:
SGD
optimizer
:
SGD
optim_args
:
optim_args
:
weight_decay
:
4.0e-05
weight_decay
:
4.0e-05
\ No newline at end of file
demo/auto_compression/semantic_segmentation/configs/pp_humanseg_sparse_dis.yaml
浏览文件 @
44e69a8d
Global
:
Global
:
reader_config
:
configs/pp_humanseg_lite.yml
reader_config
:
configs/pp_humanseg_lite.y
a
ml
Distillation
:
Distillation
:
distill_lambda
:
1.0
distill_lambda
:
1.0
...
@@ -31,4 +31,4 @@ TrainConfig:
...
@@ -31,4 +31,4 @@ TrainConfig:
optim_args
:
optim_args
:
weight_decay
:
0.0005
weight_decay
:
0.0005
optimizer
:
SGD
optimizer
:
SGD
\ No newline at end of file
demo/auto_compression/semantic_segmentation/run.py
浏览文件 @
44e69a8d
...
@@ -148,8 +148,8 @@ if __name__ == '__main__':
...
@@ -148,8 +148,8 @@ if __name__ == '__main__':
args
=
parse_args
()
args
=
parse_args
()
compress_config
,
train_config
=
load_config
(
args
.
config_path
)
compress_config
,
train_config
,
global_config
=
load_config
(
args
.
config_path
)
cfg
=
Config
(
compress
_config
[
'reader_config'
])
cfg
=
Config
(
global
_config
[
'reader_config'
])
train_dataset
=
cfg
.
train_dataset
train_dataset
=
cfg
.
train_dataset
eval_dataset
=
cfg
.
val_dataset
eval_dataset
=
cfg
.
val_dataset
...
@@ -167,7 +167,7 @@ if __name__ == '__main__':
...
@@ -167,7 +167,7 @@ if __name__ == '__main__':
ac
=
AutoCompression
(
ac
=
AutoCompression
(
model_dir
=
args
.
model_dir
,
model_dir
=
args
.
model_dir
,
model_filename
=
args
.
model_filename
,
model_filename
=
args
.
model_filename
,
params_filename
=
args
.
param_filename
,
params_filename
=
args
.
param
s
_filename
,
save_dir
=
args
.
save_dir
,
save_dir
=
args
.
save_dir
,
strategy_config
=
compress_config
,
strategy_config
=
compress_config
,
train_config
=
train_config
,
train_config
=
train_config
,
...
...
paddleslim/auto_compression/auto_strategy.py
浏览文件 @
44e69a8d
...
@@ -27,7 +27,7 @@ __all__ = [
...
@@ -27,7 +27,7 @@ __all__ = [
# config tester to test the loss of quant_post
# config tester to test the loss of quant_post
hpo_config_tester
=
{
hpo_config_tester
=
{
"ptq_algo"
:
[
"avg"
],
"ptq_algo"
:
[
"avg"
,
"mse"
,
"KL"
],
"weight_quantize_type"
:
[
'channel_wise_abs_max'
,
'abs_max'
],
"weight_quantize_type"
:
[
'channel_wise_abs_max'
,
'abs_max'
],
"bias_correct"
:
[
False
],
"bias_correct"
:
[
False
],
"batch_num"
:
[
5
],
"batch_num"
:
[
5
],
...
@@ -72,7 +72,8 @@ EXPERIENCE_STRATEGY_WITHOUT_LOSS = [
...
@@ -72,7 +72,8 @@ EXPERIENCE_STRATEGY_WITHOUT_LOSS = [
]
]
MAGIC_SPARSE_RATIO
=
0.75
MAGIC_SPARSE_RATIO
=
0.75
### TODO: 0.03 threshold maybe not suitable, need to check
### TODO: 0.03 threshold maybe not suitable, need to check
MAGIC_EMD_DISTANCE
=
0.03
MAGIC_MAX_EMD_DISTANCE
=
0.03
MAGIC_MIN_EMD_DISTANCE
=
0.01
DEFAULT_TRANSFORMER_STRATEGY
=
'prune_0.25_int8'
DEFAULT_TRANSFORMER_STRATEGY
=
'prune_0.25_int8'
DEFAULT_STRATEGY
=
'origin_int8'
DEFAULT_STRATEGY
=
'origin_int8'
...
@@ -224,9 +225,13 @@ def prepare_strategy(model_dir,
...
@@ -224,9 +225,13 @@ def prepare_strategy(model_dir,
return
strategy_config
return
strategy_config
def
get_final_quant_config
(
ptq_loss
,
mode
=
'DistilQuant'
):
def
get_final_quant_config
(
ptq_loss
):
""" transform quantization tester config to real quantization config """
""" transform quantization tester config to real quantization config """
if
mode
==
'HPO'
:
### if emd loss less than MAGIC_MIN_EMD_DISTANCE, final compress.
if
ptq_loss
<
MAGIC_MIN_EMD_DISTANCE
:
return
None
### if emd loss less than MAGIC_MAX_EMD_DISTANCE, select quant_post & hpo.
elif
ptq_loss
<
MAGIC_MAX_EMD_DISTANCE
:
quant_config
=
Quantization
(
**
default_quant_config
)
quant_config
=
Quantization
(
**
default_quant_config
)
hpo_config
=
HyperParameterOptimization
(
**
default_hpo_config
)
hpo_config
=
HyperParameterOptimization
(
**
default_hpo_config
)
configs
=
[{
configs
=
[{
...
@@ -234,7 +239,8 @@ def get_final_quant_config(ptq_loss, mode='DistilQuant'):
...
@@ -234,7 +239,8 @@ def get_final_quant_config(ptq_loss, mode='DistilQuant'):
'HyperParameterOptimization'
:
hpo_config
'HyperParameterOptimization'
:
hpo_config
}]
}]
if
mode
==
'DistilQuant'
:
### if emd loss greater than MAGIC_MAX_EMD_DISTANCE, select qat & dist.
else
:
quant_config
=
Quantization
(
**
default_quant_config
)
quant_config
=
Quantization
(
**
default_quant_config
)
dis_config
=
Distillation
()
dis_config
=
Distillation
()
configs
=
[{
'Quantization'
:
quant_config
,
'Distillation'
:
dis_config
}]
configs
=
[{
'Quantization'
:
quant_config
,
'Distillation'
:
dis_config
}]
...
...
paddleslim/auto_compression/compressor.py
浏览文件 @
44e69a8d
...
@@ -357,26 +357,27 @@ class AutoCompression:
...
@@ -357,26 +357,27 @@ class AutoCompression:
).
lower
()
==
'linux'
:
).
lower
()
==
'linux'
:
ptq_loss
=
quant_post_hpo
.
g_min_emd_loss
ptq_loss
=
quant_post_hpo
.
g_min_emd_loss
final_quant_config
=
get_final_quant_config
(
final_quant_config
=
get_final_quant_config
(
ptq_loss
)
ptq_loss
,
mode
=
'DistilQuant'
)
if
final_quant_config
is
not
None
:
quant_strategy
,
quant_config
=
self
.
_prepare_strategy
(
quant_strategy
,
quant_config
=
self
.
_prepare_strategy
(
final_quant_config
)
final_quant_config
)
self
.
single_strategy_compress
(
quant_strategy
[
0
],
quant_config
[
0
],
self
.
single_strategy_compress
(
quant_strategy
[
0
],
strategy_idx
)
quant_config
[
0
],
strategy_idx
)
tmp_model_path
=
os
.
path
.
join
(
tmp_model_path
=
os
.
path
.
join
(
self
.
save_dir
,
'strategy_{}'
.
format
(
str
(
strategy_idx
+
1
)))
self
.
save_dir
,
'strategy_{}'
.
format
(
str
(
strategy_idx
+
1
)))
final_model_path
=
os
.
path
.
join
(
self
.
final_dir
)
final_model_path
=
os
.
path
.
join
(
self
.
final_dir
)
if
not
os
.
path
.
exists
(
final_model_path
):
if
not
os
.
path
.
exists
(
final_model_path
):
os
.
makedirs
(
final_model_path
)
os
.
makedirs
(
final_model_path
)
tmp_model_file
=
os
.
path
.
join
(
tmp_model_path
,
'model.pdmodel'
)
tmp_model_file
=
os
.
path
.
join
(
tmp_model_path
,
self
.
model_filename
)
tmp_params_file
=
os
.
path
.
join
(
tmp_model_path
,
'model.pdiparams'
)
tmp_params_file
=
os
.
path
.
join
(
tmp_model_path
,
self
.
params_filename
)
final_model_file
=
os
.
path
.
join
(
final_model_path
,
'model.pdmodel'
)
final_model_file
=
os
.
path
.
join
(
final_model_path
,
self
.
model_filename
)
final_params_file
=
os
.
path
.
join
(
final_model_path
,
'model.pdiparams'
)
final_params_file
=
os
.
path
.
join
(
final_model_path
,
self
.
params_filename
)
shutil
.
move
(
tmp_model_file
,
final_model_file
)
if
paddle
.
distributed
.
get_rank
()
==
0
:
shutil
.
move
(
tmp_params_file
,
final_params_file
)
shutil
.
move
(
tmp_model_file
,
final_model_file
)
_logger
.
info
(
shutil
.
move
(
tmp_params_file
,
final_params_file
)
"==> Finished the ACT process and the final model is saved in:{}"
.
_logger
.
info
(
format
(
final_model_path
))
"==> Finished the ACT process and the final model is saved in:{}"
.
format
(
final_model_path
))
os
.
_exit
(
0
)
os
.
_exit
(
0
)
def
single_strategy_compress
(
self
,
strategy
,
config
,
strategy_idx
):
def
single_strategy_compress
(
self
,
strategy
,
config
,
strategy_idx
):
...
@@ -569,5 +570,5 @@ class AutoCompression:
...
@@ -569,5 +570,5 @@ class AutoCompression:
target_vars
=
test_program_info
.
fetch_targets
,
target_vars
=
test_program_info
.
fetch_targets
,
executor
=
self
.
_exe
,
executor
=
self
.
_exe
,
main_program
=
test_program
,
main_program
=
test_program
,
model_filename
=
'model.pdmodel'
,
model_filename
=
self
.
model_filename
,
params_filename
=
'model.pdiparams'
)
params_filename
=
self
.
params_filename
)
paddleslim/auto_compression/config_helpers.py
浏览文件 @
44e69a8d
...
@@ -25,14 +25,16 @@ def load_config(config_path):
...
@@ -25,14 +25,16 @@ def load_config(config_path):
cfg
=
yaml
.
load
(
f
,
Loader
=
yaml
.
FullLoader
)
cfg
=
yaml
.
load
(
f
,
Loader
=
yaml
.
FullLoader
)
f
.
close
()
f
.
close
()
global_config
=
{}
if
'Global'
in
cfg
:
for
g_key
,
g_value
in
cfg
[
"Global"
].
items
():
global_config
[
g_key
]
=
g_value
cfg
.
pop
(
'Global'
)
compress_config
=
{}
compress_config
=
{}
for
key
,
value
in
cfg
.
items
():
for
key
,
value
in
cfg
.
items
():
if
key
==
"Global"
:
default_key
=
eval
(
key
)(
**
value
)
for
g_key
,
g_value
in
cfg
[
"Global"
].
items
():
compress_config
[
key
]
=
default_key
compress_config
[
g_key
]
=
g_value
else
:
default_key
=
eval
(
key
)(
**
value
)
compress_config
[
key
]
=
default_key
if
compress_config
.
get
(
'TrainConfig'
)
!=
None
:
if
compress_config
.
get
(
'TrainConfig'
)
!=
None
:
train_config
=
compress_config
.
pop
(
'TrainConfig'
)
train_config
=
compress_config
.
pop
(
'TrainConfig'
)
...
@@ -42,7 +44,7 @@ def load_config(config_path):
...
@@ -42,7 +44,7 @@ def load_config(config_path):
if
len
(
compress_config
)
==
0
:
if
len
(
compress_config
)
==
0
:
compress_config
=
None
compress_config
=
None
return
compress_config
,
train_config
return
compress_config
,
train_config
,
global_config
def
save_config
(
config
,
config_path
):
def
save_config
(
config
,
config_path
):
...
...
paddleslim/version.py
浏览文件 @
44e69a8d
...
@@ -14,4 +14,4 @@
...
@@ -14,4 +14,4 @@
# limitations under the License.
# limitations under the License.
""" PaddleSlim version string """
""" PaddleSlim version string """
__all__
=
[
"slim_version"
]
__all__
=
[
"slim_version"
]
slim_version
=
"
1.0
.0"
slim_version
=
"
2.3
.0"
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录