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

Use manylinux2010 image to build linux python wheels #1282

Merged
merged 10 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions cmake/onnxruntime_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,18 @@ if(WIN32)
# Add Code Analysis properties to enable C++ Core checks. Have to do it via a props file include.
set_target_properties(onnxruntime_common PROPERTIES VS_USER_PROPS ${PROJECT_SOURCE_DIR}/EnableVisualStudioCodeAnalysis.props)
endif()

# check if we need to link against librt on Linux
include(CheckLibraryExists)
include(CheckFunctionExists)
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME)

if (NOT HAVE_CLOCK_GETTIME)
set(CMAKE_EXTRA_INCLUDE_FILES time.h)
check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
set(CMAKE_EXTRA_INCLUDE_FILES)
else()
target_link_libraries(onnxruntime_common rt)
snnn marked this conversation as resolved.
Show resolved Hide resolved
endif()
endif()
2 changes: 1 addition & 1 deletion cmake/onnxruntime_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ onnxruntime_add_include_to_target(onnxruntime_pybind11_state gsl)
if(APPLE)
set(ONNXRUNTIME_SO_LINK_FLAG "-Xlinker -exported_symbols_list ${ONNXRUNTIME_ROOT}/python/exported_symbols.lst")
elseif(UNIX)
set(ONNXRUNTIME_SO_LINK_FLAG "-Xlinker --version-script=${ONNXRUNTIME_ROOT}/python/version_script.lds -Xlinker --no-undefined -Xlinker --gc-sections")
set(ONNXRUNTIME_SO_LINK_FLAG "-Xlinker --version-script=${ONNXRUNTIME_ROOT}/python/version_script.lds -Xlinker --gc-sections")
snnn marked this conversation as resolved.
Show resolved Hide resolved
else()
set(ONNXRUNTIME_SO_LINK_FLAG "-DEF:${ONNXRUNTIME_ROOT}/python/pybind.def")
endif()
Expand Down
10 changes: 10 additions & 0 deletions onnxruntime/python/_ld_preload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
#--------------------------------------------------------------------------

# This file can be modified by setup.py when building a manylinux2010 wheel
# When modified, it will preload some libraries needed for the python C extension
# Do not remove or move the following comment

# LD_PRELOAD_BEGIN_MARK
1 change: 1 addition & 0 deletions onnxruntime/python/_pybind_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import os
import warnings
import onnxruntime.capi._ld_preload

try:
from onnxruntime.capi.onnxruntime_pybind11_state import * # noqa
Expand Down
14 changes: 0 additions & 14 deletions rename_manylinux.sh

This file was deleted.

84 changes: 79 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
# Licensed under the MIT License.
#--------------------------------------------------------------------------

from setuptools import setup, find_packages
from os import path, getcwd
from setuptools import setup, find_packages, Extension
from distutils import log as logger
from distutils.command.build_ext import build_ext as _build_ext
from glob import glob
from os import path, getcwd, environ, remove
from shutil import copyfile
import platform
import subprocess
import sys
import datetime

Expand Down Expand Up @@ -38,12 +43,69 @@
nightly_build = True
sys.argv.remove('--nightly_build')

is_manylinux2010 = False
if environ.get('AUDITWHEEL_PLAT', None) == 'manylinux2010_x86_64':
is_manylinux2010 = True


class build_ext(_build_ext):
def build_extension(self, ext):
dest_file = self.get_ext_fullpath(ext.name)
logger.info('copying %s -> %s', ext.sources[0], dest_file)
copyfile(ext.sources[0], dest_file)


try:
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
class bdist_wheel(_bdist_wheel):
def finalize_options(self):
_bdist_wheel.finalize_options(self)
self.root_is_pure = False
if not is_manylinux2010:
self.root_is_pure = False

def _rewrite_ld_preload(self, to_preload):
with open('onnxruntime/capi/_ld_preload.py', 'rt') as f:
ld_preload = f.read().splitlines()
with open('onnxruntime/capi/_ld_preload.py', 'wt') as f:
for line in ld_preload:
f.write(line)
f.write('\n')
if 'LD_PRELOAD_BEGIN_MARK' in line:
break
if len(to_preload) > 0:
f.write('from ctypes import CDLL, RTLD_GLOBAL\n')
for library in to_preload:
f.write('_{} = CDLL("{}", mode=RTLD_GLOBAL)\n'.format(library.split('.')[0], library))

def run(self):
if is_manylinux2010:
source = 'onnxruntime/capi/onnxruntime_pybind11_state.so'
dest = 'onnxruntime/capi/onnxruntime_pybind11_state_manylinux2010.so'
logger.info('copying %s -> %s', source, dest)
copyfile(source, dest)
result = subprocess.run(['patchelf', '--print-needed', dest], check=True, stdout=subprocess.PIPE, universal_newlines=True)
cuda_dependencies = ['libcublas.so', 'libcudnn.so', 'libcudart.so']
to_preload = []
mayeut marked this conversation as resolved.
Show resolved Hide resolved
args = ['patchelf', '--debug']
for line in result.stdout.split('\n'):
for dependency in cuda_dependencies:
if dependency in line:
to_preload.append(line)
args.extend(['--remove-needed', line])
args.append(dest)
if len(to_preload) > 0:
subprocess.run(args, check=True, stdout=subprocess.PIPE)
self._rewrite_ld_preload(to_preload)
_bdist_wheel.run(self)
if is_manylinux2010:
file = glob(path.join(self.dist_dir, '*linux*.whl'))[0]
logger.info('repairing %s for manylinux2010', file)
try:
subprocess.run(['auditwheel', 'repair', '--plat', 'manylinux2010_x86_64', '-w', self.dist_dir, file], check=True, stdout=subprocess.PIPE)
finally:
logger.info('removing %s', file)
remove(file)

except ImportError:
bdist_wheel = None

Expand All @@ -57,7 +119,18 @@ def finalize_options(self):
else:
libs = ['onnxruntime_pybind11_state.pyd', 'mkldnn.dll', 'mklml.dll', 'libiomp5md.dll']

data = [path.join('capi', x) for x in libs if path.isfile(path.join('onnxruntime', 'capi', x))]
if is_manylinux2010:
data = []
ext_modules = [
Extension(
'onnxruntime.capi.onnxruntime_pybind11_state',
['onnxruntime/capi/onnxruntime_pybind11_state_manylinux2010.so'],
),
]
else:
data = [path.join('capi', x) for x in libs if path.isfile(path.join('onnxruntime', 'capi', x))]
ext_modules = []


python_modules_list = list()
if '--use_openvino' in sys.argv:
Expand Down Expand Up @@ -98,14 +171,15 @@ def finalize_options(self):
long_description=long_description,
author='Microsoft Corporation',
author_email='[email protected]',
cmdclass={'bdist_wheel': bdist_wheel},
cmdclass={'bdist_wheel': bdist_wheel, 'build_ext': build_ext},
license="MIT License",
packages=['onnxruntime',
'onnxruntime.backend',
'onnxruntime.capi',
'onnxruntime.datasets',
'onnxruntime.tools',
],
ext_modules=ext_modules,
package_data={
'onnxruntime': data + examples + extra,
},
Expand Down
35 changes: 11 additions & 24 deletions tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,33 +683,20 @@ def run_server_model_tests(build_dir, configs):
def build_python_wheel(source_dir, build_dir, configs, use_cuda, use_ngraph, use_tensorrt, use_openvino, nightly_build = False):
for config in configs:
cwd = get_config_build_dir(build_dir, config)

if is_windows():
cwd = os.path.join(cwd, config)
args = [sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel']
if nightly_build:
if use_tensorrt:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel', '--use_tensorrt', '--nightly_build'], cwd=cwd)
elif use_cuda:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel', '--use_cuda', '--nightly_build'], cwd=cwd)
elif use_ngraph:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel', '--use_ngraph', '--nightly-build'], cwd=cwd)
elif use_openvino:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel', '--use_openvino', '--nightly-build'], cwd=cwd)
else:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel', '--nightly_build'], cwd=cwd)
else:
if use_tensorrt:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel', '--use_tensorrt'], cwd=cwd)
elif use_cuda:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel', '--use_cuda'], cwd=cwd)
elif use_ngraph:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel', '--use_ngraph'], cwd=cwd)
elif use_openvino:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel', '--use_openvino'], cwd=cwd)
else:
run_subprocess([sys.executable, os.path.join(source_dir, 'setup.py'), 'bdist_wheel'], cwd=cwd)
if is_ubuntu_1604():
run_subprocess([os.path.join(source_dir, 'rename_manylinux.sh')], cwd=cwd+'/dist')
args.append('--nightly_build')
if use_tensorrt:
args.append('--use_tensorrt')
elif use_cuda:
args.append('--use_cuda')
elif use_ngraph:
args.append('--use_ngraph')
elif use_openvino:
args.append('--use_openvino')
run_subprocess(args, cwd=cwd)

def build_protoc_for_windows_host(cmake_path, source_dir, build_dir):
if not is_windows():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jobs:
- job: Ubuntu1604_py_Wheels
- job: Manylinux2010_py_Wheels
pool: Linux-CPU
strategy:
matrix:
Expand Down Expand Up @@ -32,13 +32,13 @@ jobs:
displayName: 'Run build script'
inputs:
scriptPath: 'tools/ci_build/github/linux/run_dockerbuild.sh'
args: '-c Release -o ubuntu16.04 -d cpu -r $(Build.BinariesDirectory) -p $(python.version) -x "--use_openmp --build_wheel"'
args: '-c Release -o manylinux2010 -d cpu -r $(Build.BinariesDirectory) -p $(python.version) -x "--use_openmp --build_wheel"'

- task: CopyFiles@2
displayName: 'Copy Python Wheel to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: '$(Build.BinariesDirectory)'
Contents: 'Release/dist/*-manylinux1_x86_64.whl'
Contents: 'Release/dist/*-manylinux2010_x86_64.whl'
TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: PublishBuildArtifacts@1
Expand All @@ -51,7 +51,7 @@ jobs:

- template: templates/clean-agent-build-directory-step.yml

- job: Ubuntu1604_py_GPU_Wheels
- job: Manylinux2010_py_GPU_Wheels
pool: Linux-GPU
strategy:
matrix:
Expand Down Expand Up @@ -94,13 +94,13 @@ jobs:
displayName: 'Run build script'
inputs:
scriptPath: 'tools/ci_build/github/linux/run_dockerbuild.sh'
args: '-c Release -o ubuntu16.04 -d gpu -c cuda9.1-cudnn7.1 -r $(Build.BinariesDirectory) -p $(python.version) -x "--use_openmp --build_wheel"'
args: '-c Release -o manylinux2010 -d gpu -c cuda10.1 -r $(Build.BinariesDirectory) -p $(python.version) -x "--use_openmp --build_wheel"'

- task: CopyFiles@2
displayName: 'Copy Python Wheel to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: '$(Build.BinariesDirectory)'
Contents: 'Release/dist/*-manylinux1_x86_64.whl'
Contents: 'Release/dist/*-manylinux2010_x86_64.whl'
TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: PublishBuildArtifacts@1
Expand Down Expand Up @@ -161,7 +161,7 @@ jobs:
pool: Win-GPU
variables:
buildDirectory: '$(Build.SourcesDirectory)\build'
CUDA_VERSION: '9.1'
CUDA_VERSION: '10.0'
strategy:
matrix:
Python35:
Expand All @@ -178,12 +178,6 @@ jobs:
packageSpecs: 'python=$(python.version)'
cleanEnvironment: true

- task: PowerShell@1
displayName: 'Set CUDA path'
inputs:
scriptName: 'tools/ci_build/github/windows/set_cuda_path.ps1'
arguments: '-CudaMsbuildPath C:\local\cudaMsbuildIntegration-9.1.85-windows10-x64-0 -CudaVersion $(CUDA_VERSION)'

- task: BatchScript@1
displayName: 'Setup VS2017 env vars'
inputs:
Expand All @@ -195,8 +189,8 @@ jobs:
displayName: 'Run build script'
inputs:
filename: 'build.bat'
arguments: ' --use_cuda --cuda_home="C:\local\cuda-9.1.85-windows10-x64-0"
--cudnn_home="C:\local\cudnn-9.1-windows10-x64-v7.1\cuda" --build_dir $(buildDirectory) --config Release --use_openmp --build_wheel'
arguments: ' --use_cuda --cuda_home="C:\local\cuda_10.0.130_win10"
--cudnn_home="C:\local\cudnn-10.0-windows10-x64-v7.3.1.20\cuda" --build_dir $(buildDirectory) --config Release --use_openmp --build_wheel'
workingFolder: "$(Build.SourcesDirectory)"

- task: CopyFiles@2
Expand All @@ -215,45 +209,3 @@ jobs:
displayName: 'Component Detection'

- template: templates/clean-agent-build-directory-step.yml

- job: MacOS_py_Wheels
pool:
vmImage: 'macOS-10.13'
strategy:
matrix:
Python35:
python.version: '3.5'
Python36:
python.version: '3.6'
Python37:
python.version: '3.7'
steps:
- task: CondaEnvironment@1
inputs:
createCustomEnvironment: true
environmentName: 'py$(python.version)'
packageSpecs: 'python=$(python.version)'
cleanEnvironment: true

- script: |
sudo python -m pip install numpy==1.15.0
sudo xcode-select --switch /Applications/Xcode_10.app/Contents/Developer
./build.sh --config Release --skip_submodule_sync --parallel --use_openmp --build_wheel
displayName: 'Command Line Script'

- task: CopyFiles@2
displayName: 'Copy Python Wheel to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: '**/dist/*.whl'
TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: ONNXRuntime python wheel'
inputs:
ArtifactName: onnxruntime

- task: ms.vss-governance-buildtask.governance-build-task-component-detection.ComponentGovernanceComponentDetection@0
displayName: 'Component Detection'

- template: templates/clean-agent-build-directory-step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,4 @@ jobs:
artifactName: 'onnxruntime-win-$(buildArch)-gpu-$(OnnxRuntimeVersion)'
commitId: $(OnnxRuntimeGitCommitHash)

- task: PowerShell@2
displayName: 'Clean up Cuda Path 9.1'
inputs:
targetType: 'filePath'
filePath: '$(Build.SourcesDirectory)/tools/ci_build/github/windows/clean_up_cuda_prop_files.ps1'
arguments: '-CudaVersion 9.1'

- template: templates/clean-agent-build-directory-step.yml
Loading