|
1 |
| -# FastDeploy BERT 模型 Python 部署示例 |
2 |
| - |
3 |
| -在部署前,参考 [FastDeploy SDK 安装文档](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/download_prebuilt_libraries.md)安装 FastDeploy Python SDK。 |
4 |
| - |
5 |
| -本目录下分别提供 `seq_cls_infer.py` 快速完成在 CPU/GPU 的 GLUE 文本分类任务的 Python 部署示例。 |
| 1 | +# BERT 模型 Python 推理示例 |
| 2 | +本目录下提供 `seq_cls_infer.py` 快速完成在 CPU/GPU 的 GLUE 文本分类任务的 Python 示例。 |
6 | 3 |
|
7 | 4 | ## 快速开始
|
8 | 5 |
|
9 |
| -以下示例展示如何基于 FastDeploy 库完成 BERT 模型在 GLUE SST-2 数据集上进行自然语言推断任务的 Python 预测部署,可通过命令行参数`--device`以及`--backend`指定运行在不同的硬件以及推理引擎后端,并使用`--model_dir`参数指定运行的模型,具体参数设置可查看下面[参数说明](#参数说明)。示例中的模型是按照 [BERT 训练文档](../../README.md)导出得到的部署模型,其模型目录为`model_zoo/bert/infer_model`(用户可按实际情况设置)。 |
| 6 | +可通过命令行参数`--device`指定运行在不同的硬件,并使用`--model_dir`参数指定运行的模型,具体参数设置可查看下面[参数说明](#参数说明)。示例中的模型是按照 [BERT 训练文档](../../README.md)导出得到的部署模型,其模型目录为`model_zoo/bert/infer_model`(用户可按实际情况设置)。 |
10 | 7 |
|
11 | 8 |
|
12 | 9 | ```bash
|
13 | 10 | # CPU 推理
|
14 |
| -python seq_cls_infer.py --model_dir ../../infer_model/ --device cpu --backend paddle |
| 11 | +python infer.py --model_dir ../../infer_model/ --device cpu |
15 | 12 | # GPU 推理
|
16 |
| -python seq_cls_infer.py --model_dir ../../infer_model/ --device gpu --backend paddle |
| 13 | +python infer.py --model_dir ../../infer_model/ --device gpu |
17 | 14 | ```
|
18 | 15 |
|
19 | 16 | 运行完成后返回的结果如下:
|
20 | 17 |
|
21 | 18 | ```bash
|
22 |
| -[INFO] fastdeploy/runtime/runtime.cc(266)::CreatePaddleBackend Runtime initialized with Backend::PDINFER in Device::GPU. |
23 |
| -Batch id: 0, example id: 0, sentence1: against shimmering cinematography that lends the setting the ethereal beauty of an asian landscape painting, label: positive, negative prob: 0.0003, positive prob: 0.9997. |
24 |
| -Batch id: 1, example id: 0, sentence1: the situation in a well-balanced fashion, label: positive, negative prob: 0.0002, positive prob: 0.9998. |
25 |
| -Batch id: 2, example id: 0, sentence1: at achieving the modest , crowd-pleasing goals it sets for itself, label: positive, negative prob: 0.0017, positive prob: 0.9983. |
26 |
| -Batch id: 3, example id: 0, sentence1: so pat it makes your teeth hurt, label: negative, negative prob: 0.9986, positive prob: 0.0014. |
27 |
| -Batch id: 4, example id: 0, sentence1: this new jangle of noise , mayhem and stupidity must be a serious contender for the title ., label: negative, negative prob: 0.9806, positive prob: 0.0194. |
| 19 | +Batch id: 0, example id: 0, sentence: against shimmering cinematography that lends the setting the ethereal beauty of an asian landscape painting, label: positive, negative prob: 0.4623, positive prob: 0.5377. |
| 20 | +Batch id: 0, example id: 1, sentence: the situation in a well-balanced fashion, label: positive, negative prob: 0.3500, positive prob: 0.6500. |
| 21 | +Batch id: 1, example id: 0, sentence: at achieving the modest , crowd-pleasing goals it sets for itself, label: positive, negative prob: 0.4530, positive prob: 0.5470. |
| 22 | +Batch id: 1, example id: 1, sentence: so pat it makes your teeth hurt, label: positive, negative prob: 0.3816, positive prob: 0.6184. |
| 23 | +Batch id: 2, example id: 0, sentence: this new jangle of noise , mayhem and stupidity must be a serious contender for the title ., label: positive, negative prob: 0.3650, positive prob: 0.6350. |
28 | 24 | ```
|
29 | 25 |
|
30 | 26 | ## 参数说明
|
31 | 27 |
|
32 | 28 | | 参数 |参数说明 |
|
33 | 29 | |----------|--------------|
|
34 | 30 | |--model_dir | 指定部署模型的目录, |
|
35 |
| -|--batch_size |输入的 batch size,默认为 1| |
| 31 | +|--batch_size |输入的 batch size,默认为 2| |
36 | 32 | |--max_length |最大序列长度,默认为 128|
|
37 | 33 | |--device | 运行的设备,可选范围: ['cpu', 'gpu'],默认为'cpu' |
|
38 | 34 | |--device_id | 运行设备的 id。默认为0。 |
|
39 |
| -|--cpu_threads | 当使用 cpu 推理时,指定推理的 cpu 线程数,默认为1。| |
40 |
| -|--backend | 支持的推理后端,可选范围: ['onnx_runtime', 'paddle', 'openvino', 'tensorrt', 'paddle_tensorrt'],默认为'paddle' | |
41 |
| -|--use_fp16 | 是否使用 FP16模式进行推理。使用 tensorrt 和 paddle_tensorrt 后端时可开启,默认为 False | |
42 |
| - |
43 |
| -## FastDeploy 高阶用法 |
44 |
| - |
45 |
| -FastDeploy 在 Python 端上,提供 `fastdeploy.RuntimeOption.use_xxx()` 以及 `fastdeploy.RuntimeOption.use_xxx_backend()` 接口支持开发者选择不同的硬件、不同的推理引擎进行部署。在不同的硬件上部署 BERT 模型,需要选择硬件所支持的推理引擎进行部署,下表展示如何在不同的硬件上选择可用的推理引擎部署 BERT 模型。 |
46 |
| - |
47 |
| -符号说明: (1) ✅: 已经支持; (2) ❔: 正在进行中; (3) N/A: 暂不支持; |
48 |
| - |
49 |
| -<table> |
50 |
| - <tr> |
51 |
| - <td align=center> 硬件</td> |
52 |
| - <td align=center> 硬件对应的接口</td> |
53 |
| - <td align=center> 可用的推理引擎 </td> |
54 |
| - <td align=center> 推理引擎对应的接口 </td> |
55 |
| - <td align=center> 是否支持 Paddle 新格式量化模型 </td> |
56 |
| - <td align=center> 是否支持 FP16 模式 </td> |
57 |
| - </tr> |
58 |
| - <tr> |
59 |
| - <td rowspan=3 align=center> CPU </td> |
60 |
| - <td rowspan=3 align=center> use_cpu() </td> |
61 |
| - <td align=center> Paddle Inference </td> |
62 |
| - <td align=center> use_paddle_infer_backend() </td> |
63 |
| - <td align=center> ✅ </td> |
64 |
| - <td align=center> N/A </td> |
65 |
| - </tr> |
66 |
| - <tr> |
67 |
| - <td align=center> ONNX Runtime </td> |
68 |
| - <td align=center> use_ort_backend() </td> |
69 |
| - <td align=center> ✅ </td> |
70 |
| - <td align=center> N/A </td> |
71 |
| - </tr> |
72 |
| - <tr> |
73 |
| - <td align=center> OpenVINO </td> |
74 |
| - <td align=center> use_openvino_backend() </td> |
75 |
| - <td align=center> ❔ </td> |
76 |
| - <td align=center> N/A </td> |
77 |
| - </tr> |
78 |
| - <tr> |
79 |
| - <td rowspan=4 align=center> GPU </td> |
80 |
| - <td rowspan=4 align=center> use_gpu() </td> |
81 |
| - <td align=center> Paddle Inference </td> |
82 |
| - <td align=center> use_paddle_infer_backend() </td> |
83 |
| - <td align=center> ✅ </td> |
84 |
| - <td align=center> N/A </td> |
85 |
| - </tr> |
86 |
| - <tr> |
87 |
| - <td align=center> ONNX Runtime </td> |
88 |
| - <td align=center> use_ort_backend() </td> |
89 |
| - <td align=center> ✅ </td> |
90 |
| - <td align=center> ❔ </td> |
91 |
| - </tr> |
92 |
| - <tr> |
93 |
| - <td align=center> Paddle TensorRT </td> |
94 |
| - <td align=center> use_paddle_infer_backend() + paddle_infer_option.enable_trt = True </td> |
95 |
| - <td align=center> ✅ </td> |
96 |
| - <td align=center> ✅ </td> |
97 |
| - </tr> |
98 |
| - <tr> |
99 |
| - <td align=center> TensorRT </td> |
100 |
| - <td align=center> use_trt_backend() </td> |
101 |
| - <td align=center> ✅ </td> |
102 |
| - <td align=center> ✅ </td> |
103 |
| - </tr> |
104 |
| - <tr> |
105 |
| - <td align=center> 昆仑芯 XPU </td> |
106 |
| - <td align=center> use_kunlunxin() </td> |
107 |
| - <td align=center> Paddle Lite </td> |
108 |
| - <td align=center> use_paddle_lite_backend() </td> |
109 |
| - <td align=center> N/A </td> |
110 |
| - <td align=center> ✅ </td> |
111 |
| - </tr> |
112 |
| - <tr> |
113 |
| - <td align=center> 华为 昇腾 </td> |
114 |
| - <td align=center> use_ascend() </td> |
115 |
| - <td align=center> Paddle Lite </td> |
116 |
| - <td align=center> use_paddle_lite_backend() </td> |
117 |
| - <td align=center> ❔ </td> |
118 |
| - <td align=center> ✅ </td> |
119 |
| - </tr> |
120 |
| - <tr> |
121 |
| - <td align=center> Graphcore IPU </td> |
122 |
| - <td align=center> use_ipu() </td> |
123 |
| - <td align=center> Paddle Inference </td> |
124 |
| - <td align=center> use_paddle_infer_backend() </td> |
125 |
| - <td align=center> ❔ </td> |
126 |
| - <td align=center> N/A </td> |
127 |
| - </tr> |
128 |
| -</table> |
| 35 | +|--cpu_threads | 当使用 cpu 推理时,指定推理的 cpu 线程数,默认为4。| |
0 commit comments