|
16 | 16 | import paddle
|
17 | 17 | import paddle2onnx.paddle2onnx_cpp2py_export as c_p2o
|
18 | 18 | from paddle2onnx.utils import logging, paddle_jit_save_configs
|
| 19 | +from contextlib import contextmanager |
| 20 | + |
| 21 | + |
| 22 | +def get_old_ir_guard(): |
| 23 | + # For old version of PaddlePaddle, donothing guard is returned. |
| 24 | + @contextmanager |
| 25 | + def dummy_guard(): |
| 26 | + yield |
| 27 | + |
| 28 | + if not hasattr(paddle, "pir_utils"): |
| 29 | + return dummy_guard |
| 30 | + pir_utils = paddle.pir_utils |
| 31 | + if not hasattr(pir_utils, "DygraphOldIrGuard"): |
| 32 | + return dummy_guard |
| 33 | + return pir_utils.DygraphOldIrGuard |
19 | 34 |
|
20 | 35 | def export(model_filename,
|
21 | 36 | params_filename,
|
@@ -66,9 +81,13 @@ def dygraph2onnx(layer, save_file, input_spec=None, opset_version=9, **configs):
|
66 | 81 | if os.path.isfile(params_file):
|
67 | 82 | os.remove(params_file)
|
68 | 83 | save_configs = paddle_jit_save_configs(configs)
|
69 |
| - paddle.jit.save( |
70 |
| - layer, os.path.join(paddle_model_dir, "model"), input_spec, **save_configs |
71 |
| - ) |
| 84 | + with get_old_ir_guard()(): |
| 85 | + # In PaddlePaddle 3.0.0b2, PIR becomes the default IR, but PIR export still in development. |
| 86 | + # So we need to use the old IR to export the model, avoid make users confused. |
| 87 | + # In the future, we will remove this guard and recommend users to use PIR. |
| 88 | + paddle.jit.save( |
| 89 | + layer, os.path.join(paddle_model_dir, "model"), input_spec, **save_configs |
| 90 | + ) |
72 | 91 | logging.info("Static PaddlePaddle model saved in {}.".format(paddle_model_dir))
|
73 | 92 | if not os.path.isfile(params_file):
|
74 | 93 | params_file = ""
|
|
0 commit comments