Skip to content

Commit

Permalink
support editable install and cleanup to entry scripts (#1285)
Browse files Browse the repository at this point in the history
Signed-off-by: Zhang Jun <jzhang533@gmail.com>
  • Loading branch information
jzhang533 authored Jun 17, 2024
1 parent 5ea417b commit ae46d7c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 64 deletions.
2 changes: 2 additions & 0 deletions docs/en/compile_local.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/zh/compile_local.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ python -m build
pip install dist/*.whl
```

如果你是在本地开发 Paddle2ONNX 项目,推荐使用 `pip install -e .` 命令,以 editable mode 来安装。

## Windows编译安装

注意Windows编译安装先验条件是系统中已安装好Visual Studio 2019
Expand Down
5 changes: 0 additions & 5 deletions paddle2onnx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 14 additions & 53 deletions paddle2onnx/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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."
)
Expand All @@ -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(
Expand All @@ -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

Expand All @@ -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,
Expand All @@ -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__":
Expand Down
14 changes: 8 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit ae46d7c

Please sign in to comment.