setup.py 18 KB

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