Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Build][Update] Update the project's build process to pyproject. #1221

Merged
merged 11 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 53 additions & 38 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,59 @@ permissions:

jobs:
build:

# For the sake of simplicity in testing, the Paddle2ONNX packaging program will temporarily run on Ubuntu-latest.
# However, to ensure the stability of the packaging system, we will need to fix the Ubuntu version in the future.
runs-on: ubuntu-latest

# For the sake of simplicity in testing, the Paddle2ONNX packaging program will temporarily only package executable files for Python 3.8.
# In the future, we will need to extend support to cover Python 3.8 through Python 3.10.
steps:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Checkout Paddle2ONNX
uses: actions/checkout@v4
with:
submodules: true
path: paddle2onnx

- name: Checkout Protobuf
uses: actions/checkout@v4
with:
repository: protocolbuffers/protobuf
ref: v3.16.0
path: protobuf

- name: Build Protobuf
working-directory: ./protobuf
run: |
#============
cmake ./cmake -B build_wd -Dprotobuf_BUILD_SHARED_LIBS=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=./install_dir
cmake --build ./build_wd
cmake --build ./build_wd -- install
echo "$PWD/install_dir/bin" >> $GITHUB_PATH

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel auditwheel auditwheel-symbols
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: Build Paddle2ONNX
working-directory: ./paddle2onnx
run: |
python setup.py install
- name: Setup Python 3.8
uses: actions/setup-python@v5
with:
python-version: '3.8'

# Checkout the latest branch of Paddle2ONNX.
- name: Checkout Paddle2ONNX
uses: actions/checkout@v4
with:
submodules: true
path: paddle2onnx

# Checkout the branch(v3.16.0) of Paddle2ONNX.
- name: Checkout Protobuf
uses: actions/checkout@v4
with:
repository: protocolbuffers/protobuf
ref: v3.16.0
path: protobuf

# Build Protobuf, detailed process can refer to the [Paddle2ONNX construction documentation](https://github.com/PaddlePaddle/Paddle2ONNX/blob/develop/docs/zh/compile.md).
- name: Build Protobuf
working-directory: ./protobuf
run: |
#============
cmake ./cmake -B build_wd -Dprotobuf_BUILD_SHARED_LIBS=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=./install_dir
cmake --build ./build_wd
cmake --build ./build_wd -- install
echo "$PWD/install_dir/bin" >> $GITHUB_PATH

# Install Dependencies for Python
- name: Install Dependencies for Python
run: |
python -m pip install --upgrade pip
pip install setuptools wheel auditwheel auditwheel-symbols build
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

# Build Paddle2ONNX
- name: Build Paddle2ONNX
working-directory: ./paddle2onnx
run: |
python -m build
python -m pip install dist/*.whl

# Install Test
- name: Run Test
working-directory: ./paddle2onnx/tests
run: |
bash run.sh python
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ paddle2onnx.egg-info/*
*_*.onnx
*.log
version.py
paddle2onnx/mappers_registry.h
paddle2onnx/mappers_registry.h
13 changes: 6 additions & 7 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[submodule "third/onnx"]
path = third/onnx
[submodule "third_party/onnx"]
path = third_party/onnx
url = https://github.com/onnx/onnx.git
branch = da889e6b95750350726d149bf447bf0cd1245964
[submodule "third/optimizer"]
path = third/optimizer
[submodule "third_party/optimizer"]
path = third_party/optimizer
url = https://github.com/onnx/optimizer.git
branch = a37748b2c3a80dad4274401c45c5026c7a506730
[submodule "third/pybind11"]
path = third/pybind11
[submodule "third_party/pybind11"]
path = third_party/pybind11
url = https://github.com/pybind/pybind11.git
branch = master
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ if(NOT TARGET onnx_proto)
endif()

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_subdirectory(${PROJECT_SOURCE_DIR}/third/onnx)
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/onnx)
endif()

include_directories(${PROJECT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${PROJECT_SOURCE_DIR}/third/optimizer)
include_directories(${PROJECT_SOURCE_DIR}/third_party/optimizer)
add_subdirectory(${PROJECT_SOURCE_DIR}/paddle2onnx/proto)

file(GLOB_RECURSE ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/*.cc ${PROJECT_SOURCE_DIR}/third/optimizer/onnxoptimizer/*.cc)
file(GLOB_RECURSE ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/*.cc ${PROJECT_SOURCE_DIR}/third_party/optimizer/onnxoptimizer/*.cc)
list(REMOVE_ITEM ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/cpp2py_export.cc)
list(REMOVE_ITEM ALL_SRCS ${PROJECT_SOURCE_DIR}/third/optimizer/onnxoptimizer/cpp2py_export.cc)
list(REMOVE_ITEM ALL_SRCS ${PROJECT_SOURCE_DIR}/third_party/optimizer/onnxoptimizer/cpp2py_export.cc)

include_directories(${CMAKE_CURRENT_BINARY_DIR})
file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" PADDLE2ONNX_VERSION)
Expand Down Expand Up @@ -189,9 +189,9 @@ if(BUILD_PADDLE2ONNX_PYTHON)
target_include_directories(paddle2onnx_cpp2py_export PUBLIC
${pybind11_INCLUDE_DIRS})
else()
if(EXISTS ${PROJECT_SOURCE_DIR}/third/pybind11/include/pybind11/pybind11.h)
if(EXISTS ${PROJECT_SOURCE_DIR}/third_party/pybind11/include/pybind11/pybind11.h)
target_include_directories(paddle2onnx_cpp2py_export PUBLIC
${PROJECT_SOURCE_DIR}/third/pybind11/include)
${PROJECT_SOURCE_DIR}/third_party/pybind11/include)
else()
message(FATAL_ERROR "cannot find pybind")
endif()
Expand Down
8 changes: 8 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
recursive-include cmake *
recursive-include paddle2onnx *
recursive-include third_party *
recursive-include tools *

include LICENSE
include VERSION_NUMBER
include CMakeLists.txt
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ TensorRT/OpenVINO/MNN/TNN/NCNN,以及其它对 ONNX 开源格式进行支持

# 2 环境依赖

- PaddlePaddle 2.6.0
- PaddlePaddle == 2.6.0
- onnxruntime >= 1.10.0

# 3 安装
Expand Down
2 changes: 0 additions & 2 deletions paddle2onnx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
from .convert import dygraph2onnx
from .convert import program2onnx
from .version import version
from .version import git_version

__version__ = version
__commit_id__ = git_version


def export(model_file,
Expand Down
46 changes: 46 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[build-system]
requires = [
"setuptools>=42",
"wheel",
"cmake>=3.16",
"setuptools-scm"
]
build-backend = "setuptools.build_meta"

[project]
name = "paddle2onnx"
dynamic = ["version"]
description = "Export PaddlePaddle to ONNX"
readme = "README.md"
authors = [
{name = "paddle-infer", email = "[email protected]"},
]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
]
license = {text = "Apache License v2.0"}
requires-python = ">=3.8"
dependencies = [
"six",
Zheng-Bicheng marked this conversation as resolved.
Show resolved Hide resolved
# "paddlepaddle==2.6.0",
"onnxruntime>=1.10.0"
]

[tool.setuptools.dynamic]
version = {file = "VERSION_NUMBER"}

[tool.setuptools.packages.find]
include = ["paddle2onnx*"]

[tool.setuptools_scm]
write_to = "paddle2onnx/version.py"

[tool.mypy]
files = "setup.py"
python_version = "3.8"
strict = true
show_error_codes = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
warn_unreachable = true
Empty file removed requirements.txt
Empty file.
68 changes: 4 additions & 64 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
CMAKE = find_executable('cmake3') or find_executable('cmake')
MAKE = find_executable('make')

setup_requires = []
extras_require = {}

################################################################################
# Global variables for controlling the build variant
################################################################################
Expand All @@ -60,15 +57,9 @@
# Version
################################################################################

try:
git_version = subprocess.check_output(
['git', 'rev-parse', 'HEAD'], cwd=TOP_DIR).decode('ascii').strip()
except (OSError, subprocess.CalledProcessError):
git_version = None

with open(os.path.join(TOP_DIR, 'VERSION_NUMBER')) as version_file:
VersionInfo = namedtuple('VersionInfo', ['version', 'git_version'])(
version=version_file.read().strip(), git_version=git_version)
_version = version_file.read().strip()
VERSION_INFO = {"version": _version}
Zheng-Bicheng marked this conversation as resolved.
Show resolved Hide resolved

################################################################################
# Pre Check
Expand Down Expand Up @@ -108,21 +99,6 @@ def finalize_options(self):
pass


class create_version(ONNXCommand):
def run(self):
with open(os.path.join(SRC_DIR, 'version.py'), 'w') as f:
f.write(
dedent('''\
# This file is generated by setup.py. DO NOT EDIT!
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
version = '{version}'
git_version = '{git_version}'
'''.format(**dict(VersionInfo._asdict()))))


class cmake_build(setuptools.Command):
"""
Compiles everything when `python setupmnm.py build` is run using cmake.
Expand Down Expand Up @@ -164,8 +140,7 @@ def run(self):
'-DBUILD_PADDLE2ONNX_PYTHON=ON',
'-DCMAKE_EXPORT_COMPILE_COMMANDS=ON',
'-DONNX_NAMESPACE={}'.format(ONNX_NAMESPACE),
'-DPY_EXT_SUFFIX={}'.format(
sysconfig.get_config_var('EXT_SUFFIX') or ''),
'-DPY_EXT_SUFFIX={}'.format(sysconfig.get_config_var('EXT_SUFFIX') or ''),
]
cmake_args.append('-DCMAKE_BUILD_TYPE=%s' % build_type)
if WINDOWS:
Expand Down Expand Up @@ -200,7 +175,6 @@ def run(self):

class build_py(setuptools.command.build_py.build_py):
def run(self):
self.run_command('create_version')
self.run_command('cmake_build')

generated_python_files = \
Expand Down Expand Up @@ -258,7 +232,6 @@ def run(self):


cmdclass = {
'create_version': create_version,
'cmake_build': cmake_build,
'build_py': build_py,
'develop': develop,
Expand All @@ -275,46 +248,13 @@ def run(self):
name=str('paddle2onnx.paddle2onnx_cpp2py_export'), sources=[])
]

################################################################################
# Packages
################################################################################

# no need to do fancy stuff so far
packages = setuptools.find_packages()

################################################################################
# Test
################################################################################

setup_requires.append('pytest-runner')

if sys.version_info[0] == 3:
# Mypy doesn't work with Python 2
extras_require['mypy'] = ['mypy==0.600']

################################################################################
# Final
################################################################################

setuptools.setup(
name="paddle2onnx",
version=VersionInfo.version,
description="Export PaddlePaddle to ONNX",
version=VERSION_INFO["version"],
ext_modules=ext_modules,
cmdclass=cmdclass,
packages=packages,
include_package_data=True,
setup_requires=setup_requires,
extras_require=extras_require,
author='paddle-infer',
author_email='[email protected]',
url='https://github.com/PaddlePaddle/Paddle2ONNX.git',
# install_requires=['six', 'protobuf', 'onnx<=1.9.0'],
install_requires=['six'],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
],
license='Apache 2.0',
entry_points={'console_scripts': ['paddle2onnx=paddle2onnx.command:main']})
Loading
Loading