Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix quant tipc docs #5517

Open
wants to merge 2 commits into
base: release/2.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 27 additions & 102 deletions tutorials/tipc/ptq_infer_python/ptq_infer_python.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,69 +46,44 @@ Paddle 离线量化开发可以分为个步骤,如下图所示。

以ImageNet1k数据集为例,可参考[数据准备文档](https://github.com/PaddlePaddle/models/tree/release/2.2/tutorials/mobilenetv3_prod/Step6#32-%E5%87%86%E5%A4%87%E6%95%B0%E6%8D%AE)。

用于校准的数据最好选择训练集,数据量在500个样本左右即可。

**【准备开发环境】**

- 确定已安装paddle,通过pip安装linux版本paddle命令如下,更多的版本安装方法可查看飞桨[官网](https://www.paddlepaddle.org.cn/)
- 确定已安装paddleslim,通过pip安装linux版本paddle命令如下,更多的版本安装方法可查看[PaddleSlim](https://github.com/PaddlePaddle/PaddleSlim)
- 确定已安装PaddlePaddle最新版本,通过pip安装linux版本paddle命令如下,更多的版本安装方法可查看飞桨[官网](https://www.paddlepaddle.org.cn/)
- 确定已安装paddleslim最新版本,通过pip安装linux版本paddle命令如下,更多的版本安装方法可查看[PaddleSlim](https://github.com/PaddlePaddle/PaddleSlim)

```
pip install paddlepaddle-gpu==2.2.1.post112 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
pip install paddleslim==2.2.1
pip install paddlepaddle-gpu
pip install paddleslim
```

<a name="2.2"></a>

### 2.2 准备推理模型

**【基本流程】**

准备推理模型分为三步:

- Step1:定义继承自`paddle.nn.Layer`的网络模型

- Step2:使用`paddle.jit.save`接口对模型进行动转静,导出成Inference模型

- Step3:检查导出的路径下是否生成 `model.pdmodel` 和 `model.pdiparams` 文件

**【实战】**

模型组网可以参考[mobilenet_v3](https://github.com/PaddlePaddle/models/blob/release/2.2/tutorials/mobilenetv3_prod/Step6/paddlevision/models/mobilenet_v3.py)

```python
fp32_model = mobilenet_v3_small()
fp32_model.eval()
```

然后将模型进行动转静:

```python
# save inference model
input_spec = paddle.static.InputSpec(
shape=[None, 3, 224, 224], dtype='float32')
fp32_output_model_path = os.path.join("mv3_fp32_infer", "model")
paddle.jit.save(fp32_model, fp32_output_model_path, [input_spec])
```
准备推理模型可参考[准备推理模型教程](https://github.com/PaddlePaddle/models/blob/release/2.3/tutorials/tipc/train_infer_python/infer_python.md#22-%E5%87%86%E5%A4%87%E6%8E%A8%E7%90%86%E6%A8%A1%E5%9E%8B)

会在`mv3_fp32_infer`文件夹下生成`model.pdmodel` 和 `model.pdiparams`两个文件
最终会在`mv3_fp32_infer`文件夹下生成`model.pdmodel` 和 `model.pdiparams`两个预测模型文件

<a name="2.3"></a>

### 2.3 准备离线量化代码
### 2.3 开始离线量化

**【基本流程】**

基于PaddleSlim,使用接口``paddleslim.quant.quant_post_static``对模型进行离线量化:

- Step1:定义`sample_generator`,传入paddle.io.Dataloader实例化对象,用来遍历校准数据集

- Step2:定义Executor,由于离线量化模型是Inference模型,量化校准过程也需要在静态图下执行,所以需要定义静态图Executor,用来执行离线量化校准执行
- Step2:开始离线量化


**【实战】**

1)定义数据集,可以参考[Datasets定义](https://github.com/PaddlePaddle/models/blob/release/2.2/tutorials/mobilenetv3_prod/Step6/paddlevision/datasets/vision.py)
1)定义DataLoader,数据集定义可以参考[Datasets定义](https://github.com/PaddlePaddle/models/blob/release/2.2/tutorials/mobilenetv3_prod/Step6/paddlevision/datasets/vision.py)

2)定义`sample_generator`:
包装DataLoader,定义`sample_generator`:

```python
def sample_generator(loader):
Expand All @@ -120,46 +95,28 @@ def sample_generator(loader):
return __reader__
```

2)定义Executor:
2)开始离线量化

```python
use_gpu = True
place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace()
exe = paddle.static.Executor(place)
```


<a name="2.4"></a>

### 2.4 开始离线量化

**【基本流程】**

使用飞桨PaddleSlim中的`quant_post_static`接口开始进行离线量化:

- Step1:导入`quant_post_static`接口
```python
from paddleslim.quant import quant_post_static
```

- Step2:配置传入`quant_post_static`接口参数,开始离线量化

```python
fp32_model_dir = 'mv3_fp32_infer'
quant_output_dir = 'quant_model'
use_gpu = True
place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace()
exe = paddle.static.Executor(place)
quant_post_static(
executor=exe,
model_dir=fp32_model_dir,
quantize_model_path=quant_output_dir,
sample_generator=sample_generator(data_loader),
model_filename='model.pdmodel',
params_filename='model.pdiparams',
batch_size=32,
batch_nums=10,
algo='KL')
model_dir=fp32_model_dir,
quantize_model_path=quant_output_dir,
sample_generator=sample_generator(data_loader),
model_filename='model.pdmodel',
params_filename='model.pdiparams',
batch_size=32,
batch_nums=10,
algo='KL')
```

- Step3:检查输出结果,确保离线量化后生成`__model__`和`__params__`文件。
- 检查输出结果,确保离线量化后生成`__model__`和`__params__`文件。


**【实战】**
Expand All @@ -169,43 +126,11 @@ quant_post_static(

<a name="2.5"></a>

### 2.5 验证推理结果正确性
### 2.5 通过Paddle Inference验证量化前模型和量化后模型的精度差异

**【基本流程】**

使用Paddle Inference库测试离线量化模型,确保模型精度符合预期。

- Step1:初始化`paddle.inference`库并配置相应参数

```python
import paddle.inference as paddle_infer
model_file = os.path.join('quant_model', '__model__')
params_file = os.path.join('quant_model', '__params__')
config = paddle_infer.Config(model_file, params_file)
if FLAGS.use_gpu:
config.enable_use_gpu(1000, 0)
if not FLAGS.ir_optim:
config.switch_ir_optim(False)

predictor = paddle_infer.create_predictor(config)
```

- Step2:配置预测库输入输出

```python
input_names = predictor.get_input_names()
input_handle = predictor.get_input_handle(input_names[0])
output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0])
```

- Step3:开始预测并检验结果正确性

```python
input_handle.copy_from_cpu(img_np)
predictor.run()
output_data = output_handle.copy_to_cpu()
```
可参考[开发推理程序流程](https://github.com/PaddlePaddle/models/blob/release/2.3/tutorials/tipc/train_infer_python/infer_python.md#26-%E5%BC%80%E5%8F%91%E6%8E%A8%E7%90%86%E7%A8%8B%E5%BA%8F)

**【实战】**

Expand Down
33 changes: 10 additions & 23 deletions tutorials/tipc/ptq_infer_python/test_ptq_infer_python.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

为了一键跑通上述所有功能,本文档提供了`训推一体全流程`功能自动化测试工具,它包含3个脚本文件和1个配置文件,分别是:

* `test_ptq_inference_python.sh`: 测试Linux上离线量化训练、推理功能的脚本,会对`train_ptq_infer_python.txt`进行解析,得到具体的执行命令。**该脚本无需修改**。
* `test_ptq_inference_python.sh`: 测试Linux上离线量化、推理功能的脚本,会对`train_ptq_infer_python.txt`进行解析,得到具体的执行命令。**该脚本无需修改**。
* `prepare.sh`: 准备测试需要的数据或需要的预训练模型。
* `common_func.sh`: 在配置文件一些通用的函数,如配置文件的解析函数等,**该脚本无需修改**。
* `train_ptq_infer_python.txt`: 配置文件,其中的内容会被`test_ptq_inference_python.sh`解析成具体的执行命令字段。
Expand Down Expand Up @@ -155,9 +155,9 @@ python run_script set_configs

<a name="3"></a>

## 3. 离线PACT量化训练推理功能测试开发
## 3. 离线量化推理功能测试开发

PACT量化训练推理功能测试开发过程主要分为以下6个步骤
离线量化推理功能测试开发过程主要分为以下6个步骤

<div align="center">
<img src="../images/post_infer_quant_guide.png" width="800">
Expand All @@ -172,14 +172,14 @@ PACT量化训练推理功能测试开发过程主要分为以下6个步骤。

**【基本内容】**

准备离线量化训练、模型推理的命令,后续会将这些命令按照[第2节](#2)所述内容,映射到配置文件中。
准备离线量化、模型推理的命令,后续会将这些命令按照[第2节](#2)所述内容,映射到配置文件中。

**【实战】**

MobileNetV3的训练、动转静、推理示例运行命令如下所示。
MobileNetV3的离线量化、动转静、推理示例运行命令如下所示。

```bash
# 模型训练
# 模型离线量化
python3.7 deploy/ptq_python/post_quant.py --use-gpu=True --model_path=mobilenet_v3_small_infer/ --batch_num=2 --batch-size=2 --data_dir=./test_images/lite_data/ --output_dir=./mobilenet_v3_small_infer_ptq/ # 模型动转静
# 推理
python3.7 deploy/inference_python/infer.py --use-gpu=False --model-dir=./mobilenet_v3_small_infer_ptq/ --batch-size=1 --img-path=./images/demo.jpg --benchmark=True
Expand All @@ -189,20 +189,7 @@ python3.7 deploy/inference_python/infer.py --use-gpu=False --model-dir=./mobilen

### 3.2 准备数据与环境

**【基本内容】**

1. 数据集:为方便快速验证训练/评估/推理过程,需要准备一个小数据集(训练集和验证集各8~16张图像即可,压缩后数据大小建议在`20M`以内),放在`lite_data`文件夹下。

相关文档可以参考[论文复现赛指南3.2章节](../../../docs/lwfx/ArticleReproduction_CV.md),代码可以参考`基于ImageNet准备小数据集的脚本`:[prepare.py](https://github.com/littletomatodonkey/AlexNet-Prod/blob/tipc/pipeline/Step2/prepare.py)。

2. 环境:安装好PaddlePaddle即可进行离线量化训练推理测试开发

**【注意事项】**

* 为方便管理,建议在上传至github前,首先将lite_data文件夹压缩为tar包,直接上传tar包即可,在测试训练评估与推理过程时,可以首先对数据进行解压。
* 压缩命令: `tar -zcf lite_data.tar lite_data`
* 解压命令: `tar -xf lite_data.tar`

可参考[TIPC 数据准备教程](https://github.com/PaddlePaddle/models/blob/release/2.2/tutorials/tipc/train_infer_python/test_train_infer_python.md#32-%E5%87%86%E5%A4%87%E6%95%B0%E6%8D%AE%E4%B8%8E%E7%8E%AF%E5%A2%83)

<a name="3.3"></a>

Expand Down Expand Up @@ -254,7 +241,7 @@ Run failed with command - python3.7 deploy/ptq_python/post_quant.py --use-gpu=Tr

**【实战】**

以mobilenet_v3_small的`Linux GPU/CPU 离线量化训练推理功能测试` 为例,命令如下所示。
以mobilenet_v3_small的`Linux GPU/CPU 离线量化推理功能测试` 为例,命令如下所示。

```bash
bash test_tipc/test_train_ptq_python.sh ./test_tipc/configs/mobilenet_v3_small/train_ptq_infer_python.txt whole_infer
Expand All @@ -281,7 +268,7 @@ bash test_tipc/test_train_ptq_python.sh ./test_tipc/configs/mobilenet_v3_small/t
撰写TIPC功能总览和测试流程说明文档,分别为

1. TIPC功能总览文档:test_tipc/README.md
2. Linux GPU/CPU 离线量化训练推理功能测试说明文档:test_tipc/docs/test_ptq_train_infer_python.md
2. Linux GPU/CPU 离线量化及推理功能测试说明文档:test_tipc/docs/test_ptq_train_infer_python.md

2个文档模板分别位于下述位置,可以直接拷贝到自己的repo中,根据自己的模型进行修改。

Expand All @@ -293,7 +280,7 @@ bash test_tipc/test_train_ptq_python.sh ./test_tipc/configs/mobilenet_v3_small/t
mobilenet_v3_small中`test_tipc`文档如下所示。

1. TIPC功能总览文档:[README.md](../../mobilenetv3_prod/Step6/test_tipc/README.md)
2. Linux GPU/CPU PACT量化训练推理测试说明文档:[test_train_ptq_inference_python.md](../../mobilenetv3_prod/Step6/test_tipc/docs/test_train_ptq_inference_python.md)
2. Linux GPU/CPU 离线量化及推理测试说明文档:[test_train_ptq_inference_python.md](../../mobilenetv3_prod/Step6/test_tipc/docs/test_train_ptq_inference_python.md)

**【核验】**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,7 @@ python deploy/inference_python/infer.py --model-dir=./mobilenet_v3_small_infer/

### 3.2 准备数据与环境

**【基本内容】**

1. 数据集:为方便快速验证训练/评估/推理过程,需要准备一个小数据集(训练集和验证集各8~16张图像即可,压缩后数据大小建议在`20M`以内),放在`lite_data`文件夹下。

相关文档可以参考[论文复现赛指南3.2章节](../../../docs/lwfx/ArticleReproduction_CV.md),代码可以参考`基于ImageNet准备小数据集的脚本`:[prepare.py](https://github.com/littletomatodonkey/AlexNet-Prod/blob/tipc/pipeline/Step2/prepare.py)。

2. 环境:安装好PaddlePaddle即可进行PACT量化训练推理测试开发

**【注意事项】**

* 为方便管理,建议在上传至github前,首先将lite_data文件夹压缩为tar包,直接上传tar包即可,在测试训练评估与推理过程时,可以首先对数据进行解压。
* 压缩命令: `tar -zcf lite_data.tar lite_data`
* 解压命令: `tar -xf lite_data.tar`
可参考[TIPC 数据准备教程](https://github.com/PaddlePaddle/models/blob/release/2.2/tutorials/tipc/train_infer_python/test_train_infer_python.md#32-%E5%87%86%E5%A4%87%E6%95%B0%E6%8D%AE%E4%B8%8E%E7%8E%AF%E5%A2%83)


<a name="3.3"></a>
Expand Down Expand Up @@ -343,4 +331,4 @@ test_tipc

<a name="4"></a>

## 4. FAQ
## 4. FAQ
52 changes: 8 additions & 44 deletions tutorials/tipc/train_pact_infer_python/train_pact_infer_python.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ Linux GPU/CPU PACT量化训练功能开发可以分为5个步骤,如下图所

**【准备开发环境】**

- 确定已安装paddle,通过pip安装linux版本paddle命令如下,更多的版本安装方法可查看飞桨[官网](https://www.paddlepaddle.org.cn/)
- 确定已安装paddleslim,通过pip安装linux版本paddle命令如下,更多的版本安装方法可查看[PaddleSlim](https://github.com/PaddlePaddle/PaddleSlim)
- 确定已安装PaddlePaddle最新版本,通过pip安装linux版本paddle命令如下,更多的版本安装方法可查看飞桨[官网](https://www.paddlepaddle.org.cn/)
- 确定已安装paddleslim最新版本,通过pip安装linux版本paddle命令如下,更多的版本安装方法可查看[PaddleSlim](https://github.com/PaddlePaddle/PaddleSlim)

```
pip install paddlepaddle-gpu==2.2.1.post112 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
pip install paddleslim==2.2.1
pip install paddlepaddle-gpu
pip install paddleslim
```

<a name="2.2"></a>
Expand Down Expand Up @@ -101,7 +101,7 @@ PACT在线量化训练开发之前,要求首先有Linux GPU/CPU基础训练的
```python
quant_config = {
'weight_preprocess_type': None,
'activation_preprocess_type': None,
'activation_preprocess_type': PACT, #None,
'weight_quantize_type': 'channel_wise_abs_max',
'activation_quantize_type': 'moving_average_abs_max',
'weight_bits': 8,
Expand All @@ -113,11 +113,7 @@ quant_config = {
}
```

- `activation_preprocess_type`':代表对量化模型激活值预处理的方法,目前支持PACT方法,如需使用可以改为'PACT';默认为None,代表不对激活值进行任何预处理。
- `weight_preprocess_type`:代表对量化模型权重参数预处理的方法;默认为None,代表不对权重进行任何预处理。
- `weight_quantize_type`:代表模型权重的量化方式,可选的有['abs_max', 'moving_average_abs_max', 'channel_wise_abs_max'],默认为channel_wise_abs_max
- `activation_quantize_type`:代表模型激活值的量化方式,可选的有['abs_max', 'moving_average_abs_max'],默认为moving_average_abs_max
- `quantizable_layer_type`:代表量化OP的类型,目前支持Conv2D和Linear
**注意**:保持以上量化配置,无需改动


- Step2:插入量化算子,得到量化训练模型
Expand All @@ -143,43 +139,11 @@ quanter.save_quantized_model(net, 'save_dir', input_spec=[paddle.static.InputSpe

<a name="2.5"></a>

### 2.5 验证推理结果正确性
### 2.5 通过Paddle Inference验证量化前模型和量化后模型的精度差异

**【基本流程】**

使用Paddle Inference库测试离线量化模型,确保模型精度符合预期。

- Step1:初始化`paddle.inference`库并配置相应参数

```python
import paddle.inference as paddle_infer
model_file = os.path.join('quant_model', 'qat_inference.pdmodel')
params_file = os.path.join('quant_model', 'qat_inference.pdiparams')
config = paddle_infer.Config(model_file, params_file)
if FLAGS.use_gpu:
config.enable_use_gpu(1000, 0)
if not FLAGS.ir_optim:
config.switch_ir_optim(False)

predictor = paddle_infer.create_predictor(config)
```

- Step2:配置预测库输入输出

```python
input_names = predictor.get_input_names()
input_handle = predictor.get_input_handle(input_names[0])
output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0])
```

- Step3:开始预测并检验结果正确性

```python
input_handle.copy_from_cpu(img_np)
predictor.run()
output_data = output_handle.copy_to_cpu()
```
可参考[开发推理程序流程](https://github.com/PaddlePaddle/models/blob/release/2.3/tutorials/tipc/train_infer_python/infer_python.md#26-%E5%BC%80%E5%8F%91%E6%8E%A8%E7%90%86%E7%A8%8B%E5%BA%8F)

**【实战】**

Expand Down