From ae46d7c6696a03f1e26ddaeb44104c141de78595 Mon Sep 17 00:00:00 2001 From: jzhang533 Date: Mon, 17 Jun 2024 09:11:12 +0800 Subject: [PATCH] support editable install and cleanup to entry scripts (#1285) Signed-off-by: Zhang Jun --- docs/en/compile_local.md | 2 ++ docs/zh/compile_local.md | 2 ++ paddle2onnx/__init__.py | 5 --- paddle2onnx/command.py | 67 +++++++++------------------------------- setup.py | 14 +++++---- 5 files changed, 26 insertions(+), 64 deletions(-) diff --git a/docs/en/compile_local.md b/docs/en/compile_local.md index eb28d0e1e..07348645f 100644 --- a/docs/en/compile_local.md +++ b/docs/en/compile_local.md @@ -35,6 +35,8 @@ python -m build pip install dist/*.whl ``` +If you are developing the Paddle2ONNX project locally, you can use `pip install -e .` to install it in editable mode. + ## 2 Install on Windows Make sure you have installed Visual Studio 2019 first diff --git a/docs/zh/compile_local.md b/docs/zh/compile_local.md index 0268d4f73..4482891ca 100644 --- a/docs/zh/compile_local.md +++ b/docs/zh/compile_local.md @@ -35,6 +35,8 @@ python -m build pip install dist/*.whl ``` +如果你是在本地开发 Paddle2ONNX 项目,推荐使用 `pip install -e .` 命令,以 editable mode 来安装。 + ## Windows编译安装 注意Windows编译安装先验条件是系统中已安装好Visual Studio 2019 diff --git a/paddle2onnx/__init__.py b/paddle2onnx/__init__.py index b9aa96382..e09fd8865 100755 --- a/paddle2onnx/__init__.py +++ b/paddle2onnx/__init__.py @@ -11,11 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -from paddle2onnx.utils import logging -from . import command -from .convert import dygraph2onnx -from .convert import program2onnx from .version import version __version__ = version diff --git a/paddle2onnx/command.py b/paddle2onnx/command.py index ba3b2e98d..39b1438a7 100755 --- a/paddle2onnx/command.py +++ b/paddle2onnx/command.py @@ -12,50 +12,41 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import absolute_import -from six import text_type as _text_type import argparse import ast import sys import os +import paddle2onnx from paddle2onnx.utils import logging -def str2list(v): - if len(v) == 0: - return None - v = v.replace(" ", "") - v = eval(v) - return v - - def arg_parser(): parser = argparse.ArgumentParser() parser.add_argument( "--model_dir", "-m", - type=_text_type, + type=str, default=None, help="PaddlePaddle model directory, if params stored in single file, you need define '--model_filename' and 'params_filename'." ) parser.add_argument( "--model_filename", "-mf", - type=_text_type, + type=str, default=None, help="PaddlePaddle model's network file name, which under directory seted by --model_dir" ) parser.add_argument( "--params_filename", "-pf", - type=_text_type, + type=str, default=None, help="PaddlePaddle model's param file name(param files combined in single file), which under directory seted by --model_dir." ) parser.add_argument( "--save_file", "-s", - type=_text_type, + type=str, default=None, help="file path to save onnx model") parser.add_argument( @@ -67,13 +58,13 @@ def arg_parser(): parser.add_argument( "--deploy_backend", "-d", - type=_text_type, + type=str, default="onnxruntime", choices=["onnxruntime", "tensorrt", "rknn", "others"], help="Quantize model deploy backend, default onnxruntime.") parser.add_argument( "--save_calibration_file", - type=_text_type, + type=str, default="calibration.cache", help="The calibration cache for TensorRT deploy, default calibration.cache." ) @@ -100,7 +91,7 @@ def arg_parser(): help="whether enable auto_update_opset, default is True") parser.add_argument( "--external_filename", - type=_text_type, + type=str, default=None, help="The filename of external_data when the model is bigger than 2G.") parser.add_argument( @@ -110,54 +101,26 @@ def arg_parser(): help="Whether export FP16 model for ORT-GPU, default False") parser.add_argument( "--custom_ops", - type=_text_type, + type=str, default="{}", help="Ops that needs to be converted to custom op, e.g --custom_ops '{\"paddle_op\":\"onnx_op\"}', default {}" ) return parser -def c_paddle_to_onnx(model_file, - params_file="", - save_file=None, - opset_version=7, - auto_upgrade_opset=True, - verbose=True, - enable_onnx_checker=True, - enable_experimental_op=True, - enable_optimize=True, - deploy_backend="onnxruntime", - calibration_file="", - external_file="", - export_fp16_model=False, - custom_ops={}): - import paddle2onnx.paddle2onnx_cpp2py_export as c_p2o - onnx_model_str = c_p2o.export( - model_file, params_file, opset_version, auto_upgrade_opset, verbose, - enable_onnx_checker, enable_experimental_op, enable_optimize, - custom_ops, deploy_backend, calibration_file, external_file, - export_fp16_model) - if save_file is not None: - with open(save_file, "wb") as f: - f.write(onnx_model_str) - else: - return onnx_model_str - - def main(): if len(sys.argv) < 2: logging.info("Use \"paddle2onnx -h\" to print the help information") logging.info( "For more information, please follow our github repo below:") - logging.info("Github: https://github.com/PaddlePaddle/paddle2onnx.git") + logging.info("Github: https://github.com/PaddlePaddle/paddle2onnx") return parser = arg_parser() args = parser.parse_args() if args.version: - import paddle2onnx - logging.info("paddle2onnx-{} with python>=3.6, paddlepaddle>=2.0.0". + logging.info("paddle2onnx-{} with python>=3.8, paddlepaddle>=2.0.0". format(paddle2onnx.__version__)) return @@ -181,7 +144,7 @@ def main(): custom_ops_dict = eval(args.custom_ops) calibration_file = args.save_calibration_file - c_paddle_to_onnx( + paddle2onnx.export( model_file=model_file, params_file=params_file, save_file=args.save_file, @@ -191,13 +154,11 @@ def main(): enable_onnx_checker=args.enable_onnx_checker, enable_experimental_op=True, enable_optimize=True, + custom_op_info=custom_ops_dict, deploy_backend=args.deploy_backend, calibration_file=calibration_file, external_file=external_file, - export_fp16_model=args.export_fp16_model, - custom_ops=custom_ops_dict) - logging.info("===============Make PaddlePaddle Better!================") - logging.info("A little survey: https://iwenjuan.baidu.com/?code=r8hu2s") + export_fp16_model=args.export_fp16_model) if __name__ == "__main__": diff --git a/setup.py b/setup.py index 1e26e1914..4f14dbeab 100644 --- a/setup.py +++ b/setup.py @@ -98,11 +98,7 @@ def finalize_options(self): self.jobs) def run(self): - if cmake_build.built: - return - cmake_build.built = True - if not os.path.exists(CMAKE_BUILD_DIR): - os.makedirs(CMAKE_BUILD_DIR) + os.makedirs(CMAKE_BUILD_DIR, exist_ok=True) with cd(CMAKE_BUILD_DIR): build_type = 'Release' @@ -145,12 +141,18 @@ def run(self): else: build_args.extend(['--', '-j', str(self.jobs)]) subprocess.check_call(build_args) + + class build_ext(setuptools.command.build_ext.build_ext): def run(self): self.run_command('cmake_build') - setuptools.command.build_ext.build_ext.run(self) + return super().run() def build_extensions(self): + build_lib = self.build_lib + extension_dst_dir = os.path.join(build_lib, "paddle2onnx") + os.makedirs(extension_dst_dir, exist_ok=True) + for ext in self.extensions: fullname = self.get_ext_fullname(ext.name) filename = os.path.basename(self.get_ext_filename(fullname))