setup.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # This file refered to github.com/onnx/onnx.git
  15. from __future__ import absolute_import, division, print_function, unicode_literals
  16. import os
  17. import shutil
  18. TOP_DIR = os.path.realpath(os.path.dirname(__file__))
  19. TOP_DIR = os.path.split(TOP_DIR)[0]
  20. PACKAGE_NAME = os.getenv("PACKAGE_NAME", "ultra_infer")
  21. wheel_name = os.getenv("WHEEL_NAME", "ultra-infer-python")
  22. if not os.path.exists(PACKAGE_NAME):
  23. shutil.copytree("ultra_infer", PACKAGE_NAME)
  24. import glob
  25. import multiprocessing
  26. import platform
  27. import shlex
  28. import subprocess
  29. import sys
  30. from collections import namedtuple
  31. from contextlib import contextmanager
  32. from distutils import log, sysconfig
  33. from distutils.spawn import find_executable
  34. from textwrap import dedent
  35. import setuptools
  36. import setuptools.command.build_ext
  37. import setuptools.command.build_py
  38. import setuptools.command.develop
  39. with open(os.path.join(TOP_DIR, "python", "requirements.txt")) as fin:
  40. REQUIRED_PACKAGES = fin.read()
  41. if os.getenv("BUILD_ON_CPU", "OFF") == "ON":
  42. os.environ["ENABLE_PADDLE_BACKEND"] = "ON"
  43. os.environ["ENABLE_ORT_BACKEND"] = "ON"
  44. os.environ["ENABLE_OPENVINO_BACKEND"] = "ON"
  45. os.environ["ENABLE_VISION"] = "ON"
  46. os.environ["ENABLE_TEXT"] = "ON"
  47. os.environ["WITH_GPU"] = "OFF"
  48. setup_configs = dict()
  49. setup_configs["LIBRARY_NAME"] = PACKAGE_NAME
  50. setup_configs["PY_LIBRARY_NAME"] = PACKAGE_NAME + "_main"
  51. # Backend options
  52. setup_configs["ENABLE_TVM_BACKEND"] = os.getenv("ENABLE_TVM_BACKEND", "OFF")
  53. setup_configs["ENABLE_RKNPU2_BACKEND"] = os.getenv("ENABLE_RKNPU2_BACKEND", "OFF")
  54. setup_configs["ENABLE_SOPHGO_BACKEND"] = os.getenv("ENABLE_SOPHGO_BACKEND", "OFF")
  55. setup_configs["ENABLE_ORT_BACKEND"] = os.getenv("ENABLE_ORT_BACKEND", "OFF")
  56. setup_configs["ENABLE_OPENVINO_BACKEND"] = os.getenv("ENABLE_OPENVINO_BACKEND", "OFF")
  57. setup_configs["ENABLE_PADDLE_BACKEND"] = os.getenv("ENABLE_PADDLE_BACKEND", "OFF")
  58. setup_configs["ENABLE_POROS_BACKEND"] = os.getenv("ENABLE_POROS_BACKEND", "OFF")
  59. setup_configs["ENABLE_TRT_BACKEND"] = os.getenv("ENABLE_TRT_BACKEND", "OFF")
  60. setup_configs["ENABLE_LITE_BACKEND"] = os.getenv("ENABLE_LITE_BACKEND", "OFF")
  61. setup_configs["ENABLE_OM_BACKEND"] = os.getenv("ENABLE_OM_BACKEND", "OFF")
  62. setup_configs["ENABLE_PADDLE2ONNX"] = os.getenv("ENABLE_PADDLE2ONNX", "OFF")
  63. setup_configs["ENABLE_VISION"] = os.getenv("ENABLE_VISION", "OFF")
  64. setup_configs["ENABLE_FLYCV"] = os.getenv("ENABLE_FLYCV", "OFF")
  65. setup_configs["ENABLE_CVCUDA"] = os.getenv("ENABLE_CVCUDA", "OFF")
  66. setup_configs["ENABLE_TEXT"] = os.getenv("ENABLE_TEXT", "OFF")
  67. setup_configs["ENABLE_BENCHMARK"] = os.getenv("ENABLE_BENCHMARK", "OFF")
  68. # Hardware options
  69. setup_configs["WITH_GPU"] = os.getenv("WITH_GPU", "OFF")
  70. setup_configs["WITH_IPU"] = os.getenv("WITH_IPU", "OFF")
  71. setup_configs["WITH_OPENCL"] = os.getenv("WITH_OPENCL", "OFF")
  72. setup_configs["WITH_TIMVX"] = os.getenv("WITH_TIMVX", "OFF")
  73. setup_configs["WITH_DIRECTML"] = os.getenv("WITH_DIRECTML", "OFF")
  74. setup_configs["WITH_ASCEND"] = os.getenv("WITH_ASCEND", "OFF")
  75. setup_configs["WITH_KUNLUNXIN"] = os.getenv("WITH_KUNLUNXIN", "OFF")
  76. setup_configs["RKNN2_TARGET_SOC"] = os.getenv("RKNN2_TARGET_SOC", "")
  77. # Custom deps settings
  78. setup_configs["TRT_DIRECTORY"] = os.getenv("TRT_DIRECTORY", "UNDEFINED")
  79. setup_configs["CUDA_DIRECTORY"] = os.getenv("CUDA_DIRECTORY", "/usr/local/cuda")
  80. setup_configs["OPENCV_DIRECTORY"] = os.getenv("OPENCV_DIRECTORY", "")
  81. setup_configs["ORT_DIRECTORY"] = os.getenv("ORT_DIRECTORY", "")
  82. setup_configs["OPENVINO_DIRECTORY"] = os.getenv("OPENVINO_DIRECTORY", "")
  83. setup_configs["PADDLEINFERENCE_DIRECTORY"] = os.getenv("PADDLEINFERENCE_DIRECTORY", "")
  84. setup_configs["PADDLEINFERENCE_VERSION"] = os.getenv("PADDLEINFERENCE_VERSION", "")
  85. setup_configs["PADDLEINFERENCE_URL"] = os.getenv("PADDLEINFERENCE_URL", "")
  86. setup_configs["PADDLEINFERENCE_API_COMPAT_2_4_x"] = os.getenv(
  87. "PADDLEINFERENCE_API_COMPAT_2_4_x", "OFF"
  88. )
  89. setup_configs["PADDLEINFERENCE_API_COMPAT_2_5_x"] = os.getenv(
  90. "PADDLEINFERENCE_API_COMPAT_2_5_x", "OFF"
  91. )
  92. setup_configs["PADDLEINFERENCE_API_COMPAT_2_6_x"] = os.getenv(
  93. "PADDLEINFERENCE_API_COMPAT_2_6_x", "OFF"
  94. )
  95. setup_configs["PADDLEINFERENCE_API_COMPAT_DEV"] = os.getenv(
  96. "PADDLEINFERENCE_API_COMPAT_DEV", "OFF"
  97. )
  98. setup_configs["PADDLEINFERENCE_API_CUSTOM_OP"] = os.getenv(
  99. "PADDLEINFERENCE_API_CUSTOM_OP", "OFF"
  100. )
  101. setup_configs["PADDLE2ONNX_URL"] = os.getenv("PADDLE2ONNX_URL", "")
  102. setup_configs["PADDLELITE_URL"] = os.getenv("PADDLELITE_URL", "")
  103. # Other settings
  104. setup_configs["BUILD_ON_JETSON"] = os.getenv("BUILD_ON_JETSON", "OFF")
  105. setup_configs["BUILD_PADDLE2ONNX"] = os.getenv("BUILD_PADDLE2ONNX", "OFF")
  106. if setup_configs["RKNN2_TARGET_SOC"] != "" or setup_configs["BUILD_ON_JETSON"] != "OFF":
  107. REQUIRED_PACKAGES = REQUIRED_PACKAGES.replace("opencv-contrib-python", "")
  108. if wheel_name == "ultra-infer-python":
  109. if setup_configs["WITH_GPU"] == "ON" or setup_configs["BUILD_ON_JETSON"] == "ON":
  110. wheel_name = "ultra-infer-gpu-python"
  111. elif setup_configs["WITH_IPU"] == "ON":
  112. wheel_name = "ultra-infer-ipu-python"
  113. if os.getenv("CMAKE_CXX_COMPILER", None) is not None:
  114. setup_configs["CMAKE_CXX_COMPILER"] = os.getenv("CMAKE_CXX_COMPILER")
  115. SRC_DIR = os.path.join(TOP_DIR, PACKAGE_NAME)
  116. PYTHON_SRC_DIR = os.path.join(TOP_DIR, "python", PACKAGE_NAME)
  117. CMAKE_BUILD_DIR = os.path.join(TOP_DIR, "python", ".setuptools-cmake-build")
  118. WINDOWS = os.name == "nt"
  119. CMAKE = find_executable("cmake3") or find_executable("cmake")
  120. MAKE = find_executable("make")
  121. setup_requires = []
  122. extras_require = {}
  123. ################################################################################
  124. # Global variables for controlling the build variant
  125. ################################################################################
  126. # Default value is set to TRUE\1 to keep the settings same as the current ones.
  127. # However going forward the recomemded way to is to set this to False\0
  128. USE_MSVC_STATIC_RUNTIME = bool(os.getenv("USE_MSVC_STATIC_RUNTIME", "1") == "1")
  129. ONNX_NAMESPACE = os.getenv("ONNX_NAMESPACE", "paddle2onnx")
  130. ################################################################################
  131. # Version
  132. ################################################################################
  133. try:
  134. git_version = (
  135. subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=TOP_DIR)
  136. .decode("ascii")
  137. .strip()
  138. )
  139. except (OSError, subprocess.CalledProcessError):
  140. git_version = None
  141. extra_version_info = ""
  142. if setup_configs["PADDLEINFERENCE_VERSION"] != "":
  143. extra_version_info += "." + setup_configs["PADDLEINFERENCE_VERSION"]
  144. with open(os.path.join(TOP_DIR, "VERSION_NUMBER")) as version_file:
  145. VersionInfo = namedtuple(
  146. "VersionInfo",
  147. [
  148. "version",
  149. "git_version",
  150. "extra_version_info",
  151. "enable_trt_backend",
  152. "enable_paddle_backend",
  153. "with_gpu",
  154. ],
  155. )(
  156. version=version_file.read().strip(),
  157. git_version=git_version,
  158. extra_version_info=extra_version_info.strip("."),
  159. enable_trt_backend=setup_configs["ENABLE_TRT_BACKEND"],
  160. enable_paddle_backend=setup_configs["ENABLE_PADDLE_BACKEND"],
  161. with_gpu=setup_configs["WITH_GPU"],
  162. )
  163. ################################################################################
  164. # Pre Check
  165. ################################################################################
  166. assert CMAKE, 'Could not find "cmake" executable!'
  167. ################################################################################
  168. # Utilities
  169. ################################################################################
  170. @contextmanager
  171. def cd(path):
  172. if not os.path.isabs(path):
  173. raise RuntimeError("Can only cd to absolute path, got: {}".format(path))
  174. orig_path = os.getcwd()
  175. os.chdir(path)
  176. try:
  177. yield
  178. finally:
  179. os.chdir(orig_path)
  180. ################################################################################
  181. # Customized commands
  182. ################################################################################
  183. class NoOptionCommand(setuptools.Command):
  184. user_options = []
  185. def initialize_options(self):
  186. pass
  187. def finalize_options(self):
  188. pass
  189. def get_all_files(dirname):
  190. files = list()
  191. for root, dirs, filenames in os.walk(dirname):
  192. for f in filenames:
  193. fullname = os.path.join(root, f)
  194. files.append(fullname)
  195. return files
  196. class create_version(NoOptionCommand):
  197. def run(self):
  198. with open(os.path.join(PYTHON_SRC_DIR, "code_version.py"), "w") as f:
  199. f.write(
  200. dedent(
  201. """\
  202. # This file is generated by setup.py. DO NOT EDIT!
  203. from __future__ import absolute_import
  204. from __future__ import division
  205. from __future__ import print_function
  206. from __future__ import unicode_literals
  207. version = '{version}'
  208. git_version = '{git_version}'
  209. extra_version_info = '{extra_version_info}'
  210. enable_trt_backend = '{enable_trt_backend}'
  211. enable_paddle_backend = '{enable_paddle_backend}'
  212. with_gpu = '{with_gpu}'
  213. """.format(
  214. **dict(VersionInfo._asdict())
  215. )
  216. )
  217. )
  218. class cmake_build(setuptools.Command):
  219. """
  220. Compiles everything when `python setupmnm.py build` is run using cmake.
  221. Custom args can be passed to cmake by specifying the `CMAKE_ARGS`
  222. environment variable.
  223. The number of CPUs used by `make` can be specified by passing `-j<ncpus>`
  224. to `setup.py build`. By default all CPUs are used.
  225. """
  226. user_options = [
  227. (str("jobs="), str("j"), str("Specifies the number of jobs to use with make"))
  228. ]
  229. built = False
  230. def initialize_options(self):
  231. self.jobs = None
  232. def finalize_options(self):
  233. if sys.version_info[0] >= 3:
  234. self.set_undefined_options("build", ("parallel", "jobs"))
  235. if self.jobs is None and os.getenv("MAX_JOBS") is not None:
  236. self.jobs = os.getenv("MAX_JOBS")
  237. self.jobs = multiprocessing.cpu_count() if self.jobs is None else int(self.jobs)
  238. def run(self):
  239. if cmake_build.built:
  240. return
  241. cmake_build.built = True
  242. if not os.path.exists(CMAKE_BUILD_DIR):
  243. os.makedirs(CMAKE_BUILD_DIR)
  244. with cd(CMAKE_BUILD_DIR):
  245. build_type = "Release"
  246. # configure
  247. cmake_args = [
  248. CMAKE,
  249. "-DPYTHON_INCLUDE_DIR={}".format(sysconfig.get_python_inc()),
  250. "-DPYTHON_EXECUTABLE={}".format(sys.executable),
  251. "-DBUILD_ULTRAINFER_PYTHON=ON",
  252. "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
  253. "-DONNX_NAMESPACE={}".format(ONNX_NAMESPACE),
  254. "-DPY_EXT_SUFFIX={}".format(
  255. sysconfig.get_config_var("EXT_SUFFIX") or ""
  256. ),
  257. ]
  258. cmake_args.append("-DCMAKE_BUILD_TYPE=%s" % build_type)
  259. for k, v in setup_configs.items():
  260. cmake_args.append("-D{}={}".format(k, v))
  261. if WINDOWS:
  262. cmake_args.extend(
  263. [
  264. # we need to link with libpython on windows, so
  265. # passing python version to window in order to
  266. # find python in cmake
  267. "-DPY_VERSION={}".format(
  268. "{0}.{1}".format(*sys.version_info[:2])
  269. ),
  270. ]
  271. )
  272. if platform.architecture()[0] == "64bit":
  273. cmake_args.extend(["-A", "x64", "-T", "host=x64"])
  274. else:
  275. cmake_args.extend(["-A", "Win32", "-T", "host=x86"])
  276. if "CMAKE_ARGS" in os.environ:
  277. extra_cmake_args = shlex.split(os.environ["CMAKE_ARGS"])
  278. # prevent crossfire with downstream scripts
  279. del os.environ["CMAKE_ARGS"]
  280. log.info("Extra cmake args: {}".format(extra_cmake_args))
  281. cmake_args.extend(extra_cmake_args)
  282. cmake_args.append(TOP_DIR)
  283. subprocess.check_call(cmake_args)
  284. build_args = [CMAKE, "--build", os.curdir]
  285. if WINDOWS:
  286. build_args.extend(["--config", build_type])
  287. build_args.extend(["--", "/maxcpucount:{}".format(self.jobs)])
  288. else:
  289. build_args.extend(["--", "-j", str(self.jobs)])
  290. subprocess.check_call(build_args)
  291. class build_py(setuptools.command.build_py.build_py):
  292. def run(self):
  293. self.run_command("create_version")
  294. self.run_command("cmake_build")
  295. generated_python_files = glob.glob(
  296. os.path.join(CMAKE_BUILD_DIR, PACKAGE_NAME, "*.py")
  297. ) + glob.glob(os.path.join(CMAKE_BUILD_DIR, PACKAGE_NAME, "*.pyi"))
  298. for src in generated_python_files:
  299. dst = os.path.join(TOP_DIR, os.path.relpath(src, CMAKE_BUILD_DIR))
  300. self.copy_file(src, dst)
  301. return setuptools.command.build_py.build_py.run(self)
  302. class develop(setuptools.command.develop.develop):
  303. def run(self):
  304. self.run_command("build_py")
  305. setuptools.command.develop.develop.run(self)
  306. class build_ext(setuptools.command.build_ext.build_ext):
  307. def run(self):
  308. self.run_command("cmake_build")
  309. setuptools.command.build_ext.build_ext.run(self)
  310. def build_extensions(self):
  311. for ext in self.extensions:
  312. fullname = self.get_ext_fullname(ext.name)
  313. filename = os.path.basename(self.get_ext_filename(fullname))
  314. lib_path = CMAKE_BUILD_DIR
  315. if os.name == "nt":
  316. debug_lib_dir = os.path.join(lib_path, "Debug")
  317. release_lib_dir = os.path.join(lib_path, "Release")
  318. if os.path.exists(debug_lib_dir):
  319. lib_path = debug_lib_dir
  320. elif os.path.exists(release_lib_dir):
  321. lib_path = release_lib_dir
  322. src = os.path.join(lib_path, filename)
  323. dst = os.path.join(os.path.realpath(self.build_lib), PACKAGE_NAME, filename)
  324. self.copy_file(src, dst)
  325. cmdclass = {
  326. "create_version": create_version,
  327. "cmake_build": cmake_build,
  328. "build_py": build_py,
  329. "develop": develop,
  330. "build_ext": build_ext,
  331. }
  332. ################################################################################
  333. # Extensions
  334. ################################################################################
  335. ext_modules = [
  336. setuptools.Extension(
  337. name=str(PACKAGE_NAME + "." + setup_configs["PY_LIBRARY_NAME"]), sources=[]
  338. ),
  339. ]
  340. ################################################################################
  341. # Packages
  342. ################################################################################
  343. # no need to do fancy stuff so far
  344. if PACKAGE_NAME != "ultra_infer":
  345. packages = setuptools.find_packages(exclude=["ultra_infer*", "scripts"])
  346. else:
  347. packages = setuptools.find_packages(exclude=["xencrypt*", "scripts"])
  348. ################################################################################
  349. # Test
  350. ################################################################################
  351. if sys.version_info[0] == 3:
  352. # Mypy doesn't work with Python 2
  353. extras_require["mypy"] = ["mypy==0.600"]
  354. ################################################################################
  355. # Final
  356. ################################################################################
  357. package_data = {PACKAGE_NAME: ["LICENSE", "ThirdPartyNotices.txt"]}
  358. if sys.argv[1] == "install" or sys.argv[1] == "bdist_wheel":
  359. shutil.copy(
  360. os.path.join(TOP_DIR, "ThirdPartyNotices.txt"),
  361. os.path.join(TOP_DIR, "python", PACKAGE_NAME),
  362. )
  363. shutil.copy(
  364. os.path.join(TOP_DIR, "LICENSE"), os.path.join(TOP_DIR, "python", PACKAGE_NAME)
  365. )
  366. if not os.path.exists(
  367. os.path.join(TOP_DIR, "python", PACKAGE_NAME, "libs", "third_libs")
  368. ):
  369. print(
  370. f"Didn't detect path: {PACKAGE_NAME}/libs/third_libs exist, please execute `python setup.py build` first"
  371. )
  372. sys.exit(0)
  373. from scripts.process_libraries import process_libraries
  374. all_lib_data = process_libraries(os.path.split(os.path.abspath(__file__))[0])
  375. package_data[PACKAGE_NAME].extend(all_lib_data)
  376. setuptools.setup(
  377. name=wheel_name,
  378. version=VersionInfo.version + extra_version_info,
  379. ext_modules=ext_modules,
  380. description="Deploy Kit Tool For Deeplearning models.",
  381. packages=packages,
  382. package_data=package_data,
  383. include_package_data=True,
  384. setup_requires=setup_requires,
  385. extras_require=extras_require,
  386. author="ultra_infer",
  387. install_requires=REQUIRED_PACKAGES,
  388. classifiers=[
  389. "Programming Language :: Python :: 3",
  390. "License :: OSI Approved :: Apache Software License",
  391. "Operating System :: OS Independent",
  392. ],
  393. license="Apache 2.0",
  394. )
  395. else:
  396. setuptools.setup(
  397. name=wheel_name,
  398. version=VersionInfo.version + extra_version_info,
  399. description="Deploy Kit Tool For Deeplearning models.",
  400. ext_modules=ext_modules,
  401. cmdclass=cmdclass,
  402. packages=packages,
  403. package_data=package_data,
  404. include_package_data=False,
  405. setup_requires=setup_requires,
  406. extras_require=extras_require,
  407. author="ultra_infer",
  408. install_requires=REQUIRED_PACKAGES,
  409. classifiers=[
  410. "Programming Language :: Python :: 3",
  411. "License :: OSI Approved :: Apache Software License",
  412. "Operating System :: OS Independent",
  413. ],
  414. license="Apache 2.0",
  415. )