setup.py 18 KB

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