Skip to content

Commit

Permalink
build sdk python api in standard-alone manner
Browse files Browse the repository at this point in the history
  • Loading branch information
lvhan028 committed Jul 25, 2022
1 parent 36c35b6 commit ea4ea44
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 31 deletions.
5 changes: 3 additions & 2 deletions tools/package_tools/configs/linux_x64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ global_config:
cmake_envs:
CMAKE_CXX_COMPILER: "g++-7"
MMDEPLOY_BUILD_SDK: "ON"
MMDEPLOY_BUILD_SDK_PYTHON_API: "ON"
MMDEPLOY_SHARED_LIBS: "OFF"
OpenCV_DIR: "${OpenCV_DIR}/install/lib/cmake/opencv4"

local_configs:
- BUILD_NAME: "mmdeploy-{mmdeploy_v}-{system}-{machine}-onnxruntime{ort_v}"
Expand All @@ -15,6 +16,6 @@ local_configs:
MMDEPLOY_TARGET_DEVICES: '"cuda"'
MMDEPLOY_TARGET_BACKENDS: "trt"
TENSORRT_DIR: "${TENSORRT_DIR}"
CUDA_TOOLKIT_ROOT_DIR: "/usr/local/cuda-11.3"
CUDA_TOOLKIT_ROOT_DIR: "${CUDA_TOOLKIT_ROOT_DIR}"
CUDNN_DIR: "${CUDNN_DIR}"
pplcv_DIR: ${pplcv_DIR}/cuda-build/install/lib/cmake/ppl
3 changes: 1 addition & 2 deletions tools/package_tools/configs/windows_x64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ global_config:
cmake_flags: ['-G "Visual Studio 16 2019" -A x64 -T v142']
cmake_envs:
MMDEPLOY_BUILD_SDK: "ON"
MMDEPLOY_BUILD_SDK_PYTHON_API: "ON"
MMDEPLOY_SHARED_LIBS: "OFF"
MMDEPLOY_CODEBASES: "all"
OpenCV_DIR: "%OpenCV_DIR%"
spdlog_DIR: '"%spdlog_DIR%"'

local_configs:
- BUILD_NAME: "mmdeploy-{mmdeploy_v}-{system}-{machine}-onnxruntime{ort_v}"
Expand Down
97 changes: 70 additions & 27 deletions tools/package_tools/mmdeploy_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,30 @@ def build_mmdeploy(cfg, mmdeploy_dir, dist_dir=None):
_call_command(bdist_cmd, mmdeploy_dir)


def build_mmdeploy_python(python_executable, cfg, mmdeploy_dir):
cmake_envs = cfg.get('cmake_envs', dict())

args = [f'-D{k}={v}' for k, v in cmake_envs.items()]
args.append(
f'-DMMDeploy_DIR={mmdeploy_dir}/build/install/lib/cmake/MMDeploy')

if sys.platform == 'win32':
build_cmd = 'cmake --build . --config Release -- /m'
pass
else:
args.append(f'-DPYTHON_EXECUTABLE={python_executable}')
build_cmd = 'cmake --build . -- -j$(nproc)'

cmake_cmd = ' '.join(['cmake ../csrc/mmdeploy/apis/python'] + args)

build_dir = osp.join(mmdeploy_dir, 'build_python')
_remove_if_exist(build_dir)
os.mkdir(build_dir)

_call_command(cmake_cmd, build_dir)
_call_command(build_cmd, build_dir)


def get_dir_name(cfg, tag, default_name):
if tag not in cfg:
logging.warning(f'{tag} not found, use `{default_name}` as default.')
Expand Down Expand Up @@ -286,33 +310,52 @@ def create_package(cfg: Dict, mmdeploy_dir: str):
_copy(install_dir, sdk_tar_dir)
_remove_if_exist(osp.join(sdk_tar_dir, 'example', 'build'))

# create sdk python api wheel
# for linux
python_api_lib_path = glob(
osp.join(mmdeploy_dir, 'build/lib/mmdeploy_python.*.so'))
# for windows
python_api_lib_path += glob(
osp.join(mmdeploy_dir, 'build/bin/*/mmdeploy_python.*.pyd'))
num_libs = len(python_api_lib_path)
if num_libs != 1:
logging.info('find multiple mmdeploy_python libraries.')
python_api_lib_path = python_api_lib_path[0]

sdk_python_package_dir = osp.join(build_dir, '.mmdeploy_python')
_copy(PACKAGING_DIR, sdk_python_package_dir)
_copy(
osp.join(mmdeploy_dir, 'mmdeploy', 'version.py'),
osp.join(sdk_python_package_dir, 'mmdeploy_python',
'version.py'))
_copy(python_api_lib_path,
osp.join(sdk_python_package_dir, 'mmdeploy_python'))
sdk_wheel_dir = osp.abspath(osp.join(sdk_tar_dir, 'python'))
bdist_cmd = _create_bdist_cmd(
cfg, c_ext=True, dist_dir=sdk_wheel_dir)
_call_command(bdist_cmd, sdk_python_package_dir)

# remove temp package dir
_remove_if_exist(sdk_python_package_dir)
# build SDK Python API according to different python version
# 1. get conda env path from python's path.
# We assume python is managed by conda
python_versions = ['3.6', '3.7', '3.8', '3.9']
for python_version in python_versions:
python_executable = shutil.which(f'python{python_version}')
if python_executable:
logging.info(python_executable)
conda_env_path = osp.dirname(
osp.dirname(osp.dirname(python_executable)))
logging.info(conda_env_path)
break

for python_version in python_versions:
python_major, _, python_minor = list(python_version)

# create sdk python api wheel
sdk_python_package_dir = osp.join(build_dir,
'.mmdeploy_python')
_copy(PACKAGING_DIR, sdk_python_package_dir)
_copy(
osp.join(mmdeploy_dir, 'mmdeploy', 'version.py'),
osp.join(sdk_python_package_dir, 'mmdeploy_python',
'version.py'))

# build mmdeploy sdk python api
build_mmdeploy_python(
f'{conda_env_path}/mmdeploy-{python_version}/bin/'
f'python{python_version}', cfg, mmdeploy_dir)

python_api_lib_path = glob(
osp.join(mmdeploy_dir, 'build_python/mmdeploy_python*'))
_copy(python_api_lib_path[0],
osp.join(sdk_python_package_dir, 'mmdeploy_python'))
_remove_if_exist(osp.join(mmdeploy_dir, 'build_python'))

sdk_wheel_dir = osp.abspath(osp.join(sdk_tar_dir, 'python'))

bdist_cmd = f'python setup.py bdist_wheel --plat-name ' \
f'{PLATFORM_TAG} --python-tag ' \
f'cp{python_major}{python_minor} ' \
f'--dist-dir {sdk_wheel_dir}'
_call_command(bdist_cmd, sdk_python_package_dir)

# remove temp package dir
_remove_if_exist(sdk_python_package_dir)

logging.info('build finish.')

Expand Down

0 comments on commit ea4ea44

Please sign in to comment.