Skip to content

Commit 9b9246b

Browse files
[Fea] Support onnx in pir mode (#1133)
* fix version control for develop and release * restore silu from x*sigmoid * update paddle2onnx * update code * fix * fix root * restore files
1 parent 07e9f72 commit 9b9246b

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

docs/zh/user_guide.md

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -216,39 +216,46 @@ ppsci MESSAGE: Inference model has been exported to: ./inference/aneurysm, inclu
216216
217217
#### 1.2.2 ONNX 推理模型导出
218218
219-
在导出 ONNX 推理模型前,需要完成 [1.2.1 Paddle 推理模型导出](#121-paddle) 的步骤,得到`inference/aneurysm.pdiparams`和`inference/aneurysm.pdmodel`。
219+
在导出 ONNX 推理模型前,需要完成 [1.2.1 Paddle 推理模型导出](#121-paddle) 的步骤,得到`inference/aneurysm.json`和`inference/aneurysm.pdiparams`。
220220
221-
然后安装 paddle2onnx。
221+
接着安装 `paddle2onnx>=2.0.0`
222+
223+
> 更多详细使用方式请参考 [**paddle2onnx 官方文档**](https://github.com/PaddlePaddle/Paddle2ONNX)。
222224
223225
``` sh
224-
pip install paddle2onnx
226+
pip install "paddle2onnx>=2.0.0"
225227
```
226228
227-
接下来仍然以 aneurysm 案例为例,介绍命令行直接导出和 PaddleScience 导出两种方式。
229+
aneurysm 案例为例,介绍命令行直接导出和 PaddleScience 导出两种方式。
228230
229231
=== "命令行导出"
230232
231233
``` sh
232234
paddle2onnx \
233235
--model_dir=./inference/ \
234-
--model_filename=aneurysm.pdmodel \
236+
--model_filename=aneurysm.json \
235237
--params_filename=aneurysm.pdiparams \
236238
--save_file=./inference/aneurysm.onnx \
237-
--opset_version=13 \
239+
--opset_version=19 \
238240
--enable_onnx_checker=True
239241
```
240242
241-
若导出成功,输出信息如下所示
243+
若导出成功,输出信息如下所示
242244
243245
``` log
244246
[Paddle2ONNX] Start to parse PaddlePaddle model...
245-
[Paddle2ONNX] Model file path: ./inference/aneurysm.pdmodel
246-
[Paddle2ONNX] Paramters file path: ./inference/aneurysm.pdiparams
247-
[Paddle2ONNX] Start to parsing Paddle model...
248-
[Paddle2ONNX] Use opset_version = 13 for ONNX export.
247+
[Paddle2ONNX] Model file path: ./inference/aneurysm.json
248+
[Paddle2ONNX] Parameters file path: ./inference/aneurysm.pdiparams
249+
[Paddle2ONNX] Start to parsing Paddle model saved in pir program format...
250+
[Paddle2ONNX] Start to parsing Paddle Pir model...
251+
[Paddle2ONNX] PIR Program:
252+
...
253+
254+
[Paddle2ONNX] Load PaddlePaddle pir model successfully
255+
[Paddle2ONNX] Start getting paramas value name from pir::program
256+
...
257+
[Paddle2ONNX] Construct operation : builtin_split
249258
[Paddle2ONNX] PaddlePaddle model is exported as ONNX format now.
250-
2024-03-02 05:45:12 [INFO] ===============Make PaddlePaddle Better!================
251-
2024-03-02 05:45:12 [INFO] A little survey: https://iwenjuan.baidu.com/?code=r8hu2s
252259
```
253260
254261
=== "PaddleScience 导出"
@@ -271,12 +278,25 @@ pip install paddle2onnx
271278
若导出成功,输出信息如下所示。
272279
273280
``` log
274-
...
281+
ppsci MESSAGE: Found /root/.paddlesci/weights/aneurysm_pretrained.pdparams already in /root/.paddlesci/weights, skip downloading.
282+
ppsci MESSAGE: Finish loading pretrained model from: /root/.paddlesci/weights/aneurysm_pretrained.pdparams
283+
ppsci INFO: Using paddlepaddle 3.0.0 on device Place(gpu:0)
284+
ppsci MESSAGE: Set to_static=False for computational optimization.
285+
/workspace/hesensen/anaconda3/envs/conda_py310/lib/python3.10/site-packages/paddle/jit/api.py:662: UserWarning: Found 'dict' in given outputs, the values will be returned in a sequence sorted in lexicographical order by their keys.
286+
warnings.warn(
287+
ppsci MESSAGE: Inference model has been exported to: ./inference/aneurysm, including *.json, *.pdiparams files.
275288
[Paddle2ONNX] Start to parse PaddlePaddle model...
276-
[Paddle2ONNX] Model file path: ./inference/aneurysm.pdmodel
277-
[Paddle2ONNX] Paramters file path: ./inference/aneurysm.pdiparams
278-
[Paddle2ONNX] Start to parsing Paddle model...
279-
[Paddle2ONNX] Use opset_version = 13 for ONNX export.
289+
[Paddle2ONNX] Model file path: ./inference/aneurysm.json
290+
[Paddle2ONNX] Parameters file path: ./inference/aneurysm.pdiparams
291+
[Paddle2ONNX] Start to parsing Paddle model saved in pir program format...
292+
[Paddle2ONNX] Start to parsing Paddle Pir model...
293+
[Paddle2ONNX] PIR Program:
294+
...
295+
296+
[Paddle2ONNX] Load PaddlePaddle pir model successfully
297+
[Paddle2ONNX] Start getting paramas value name from pir::program
298+
[Paddle2ONNX] Getting paramas value name from pir::program successfully
299+
...
280300
[Paddle2ONNX] PaddlePaddle model is exported as ONNX format now.
281301
ppsci MESSAGE: ONNX model has been exported to: ./inference/aneurysm.onnx
282302
```

ppsci/arch/activation.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,6 @@ def forward(self, x):
7474
return paddle.sin(x)
7575

7676

77-
class Silu(nn.Layer):
78-
"""
79-
FIXME: This activation function is a workaround for the potential occurrence of NaNs
80-
during the computation of the native SiLU function via using x*sigmoid(x) instead of
81-
silu(x)
82-
"""
83-
84-
def __init__(self):
85-
super().__init__()
86-
87-
def forward(self, x):
88-
return x * F.sigmoid(x)
89-
90-
9177
class Siren(nn.Layer):
9278
"""Implicit Neural Representations with Periodic Activation Functions.
9379
paper link: https://arxiv.org/abs/2006.09661
@@ -143,7 +129,7 @@ def init_for_hidden_layer(layer: nn.Linear, w0: float = 30):
143129
"gelu": nn.GELU(),
144130
"leaky_relu": nn.LeakyReLU(),
145131
"sigmoid": nn.Sigmoid(),
146-
"silu": Silu(),
132+
"silu": nn.Silu(),
147133
"sin": Sin(),
148134
"cos": Cos(),
149135
"swish": Swish,

ppsci/solver/solver.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -948,20 +948,20 @@ def export(
948948

949949
if with_onnx:
950950
# TODO: support pir + onnx
951-
if paddle.framework.use_pir_api():
952-
raise ValueError("paddle2onnx does not support PIR mode yet.")
953951
if not importlib.util.find_spec("paddle2onnx"):
954952
raise ModuleNotFoundError(
955953
"Please install paddle2onnx with `pip install paddle2onnx`"
956954
" before exporting onnx model."
957955
)
958956
import paddle2onnx
959957

960-
DEFAULT_OPSET_VERSION = 13
958+
DEFAULT_OPSET_VERSION = 19
961959

962960
paddle2onnx.export(
963-
model_file=export_path + ".pdmodel",
964-
params_file=export_path + ".pdiparams",
961+
model_filename=export_path + ".json"
962+
if paddle.framework.use_pir_api()
963+
else ".pdmodel",
964+
params_filename=export_path + ".pdiparams",
965965
save_file=export_path + ".onnx",
966966
opset_version=DEFAULT_OPSET_VERSION,
967967
enable_onnx_checker=True,

0 commit comments

Comments
 (0)