You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup.py 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import io
  2. import os
  3. import sys
  4. import time
  5. import re
  6. import subprocess
  7. from setuptools import setup, find_packages, Extension
  8. from setuptools.command.build_ext import build_ext
  9. from setuptools.command.install import install
  10. def set_version():
  11. pnnx_version = time.strftime("%Y%m%d", time.localtime())
  12. return pnnx_version
  13. # Parse environment variables
  14. TORCH_INSTALL_DIR = os.environ.get("TORCH_INSTALL_DIR", "")
  15. TORCHVISION_INSTALL_DIR = os.environ.get("TORCHVISION_INSTALL_DIR", "")
  16. PROTOBUF_INCLUDE_DIR = os.environ.get("PROTOBUF_INCLUDE_DIR", "")
  17. PROTOBUF_LIBRARIES = os.environ.get("PROTOBUF_LIBRARIES", "")
  18. PROTOBUF_PROTOC_EXECUTABLE = os.environ.get("PROTOBUF_PROTOC_EXECUTABLE", "")
  19. CMAKE_BUILD_TYPE = os.environ.get("CMAKE_BUILD_TYPE", "")
  20. PNNX_BUILD_WITH_STATIC_CRT = os.environ.get("PNNX_BUILD_WITH_STATIC_CRT", "")
  21. PNNX_WHEEL_WITHOUT_BUILD = os.environ.get("PNNX_WHEEL_WITHOUT_BUILD", "")
  22. # Convert distutils Windows platform specifiers to CMake -A arguments
  23. PLAT_TO_CMAKE = {
  24. "win32": "Win32",
  25. "win-amd64": "x64",
  26. "win-arm32": "ARM",
  27. "win-arm64": "ARM64",
  28. }
  29. # A CMakeExtension needs a sourcedir instead of a file list.
  30. # The name must be the _single_ output extension from the CMake build.
  31. # If you need multiple extensions, see scikit-build.
  32. class CMakeExtension(Extension):
  33. def __init__(self, name, sourcedir=""):
  34. Extension.__init__(self, name, sources=[])
  35. self.sourcedir = os.path.abspath(sourcedir)
  36. class CMakeBuild(build_ext):
  37. def build_extension(self, ext):
  38. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  39. extdir = os.path.join(extdir, "pnnx")
  40. # required for auto-detection of auxiliary "native" libs
  41. if not extdir.endswith(os.path.sep):
  42. extdir += os.path.sep
  43. cfg = "Debug" if self.debug else "Release"
  44. # CMake lets you override the generator - we need to check this.
  45. # Can be set with Conda-Build, for example.
  46. cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
  47. # Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
  48. # EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
  49. # from Python.
  50. cmake_args = [
  51. "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY={}".format(extdir),
  52. "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE={}".format(extdir),
  53. "-DPython3_EXECUTABLE={}".format(sys.executable),
  54. "-DCMAKE_BUILD_TYPE={}".format(cfg), # not used on MSVC, but no harm
  55. ]
  56. if TORCH_INSTALL_DIR != "":
  57. cmake_args.append("-DTorch_INSTALL_DIR=" + TORCH_INSTALL_DIR)
  58. if TORCHVISION_INSTALL_DIR != "":
  59. cmake_args.append("-DTorchVision_INSTALL_DIR=" + TORCHVISION_INSTALL_DIR)
  60. if PROTOBUF_INCLUDE_DIR != "":
  61. cmake_args.append("-DProtobuf_INCLUDE_DIR=" + PROTOBUF_INCLUDE_DIR)
  62. if PROTOBUF_LIBRARIES != "":
  63. cmake_args.append("-DProtobuf_LIBRARIES=" + PROTOBUF_LIBRARIES)
  64. if PROTOBUF_PROTOC_EXECUTABLE != "":
  65. cmake_args.append("-DProtobuf_PROTOC_EXECUTABLE=" + PROTOBUF_PROTOC_EXECUTABLE)
  66. if CMAKE_BUILD_TYPE != "":
  67. cmake_args.append("-DCMAKE_BUILD_TYPE=" + CMAKE_BUILD_TYPE)
  68. if PNNX_BUILD_WITH_STATIC_CRT != "":
  69. cmake_args.append("-DPNNX_BUILD_WITH_STATIC_CRT=" + PNNX_BUILD_WITH_STATIC_CRT)
  70. build_args = []
  71. if self.compiler.compiler_type == "msvc":
  72. # Single config generators are handled "normally"
  73. single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
  74. # CMake allows an arch-in-generator style for backward compatibility
  75. contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
  76. # Specify the arch if using MSVC generator, but only if it doesn't
  77. # contain a backward-compatibility arch spec already in the
  78. # generator name.
  79. if not single_config and not contains_arch:
  80. cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
  81. # Multi-config generators have a different way to specify configs
  82. if not single_config:
  83. cmake_args += [
  84. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)
  85. ]
  86. build_args += ["--config", cfg]
  87. # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
  88. # across all generators.
  89. if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
  90. # self.parallel is a Python 3 only way to set parallel jobs by hand
  91. # using -j in the build_ext call, not supported by pip or PyPA-build.
  92. if hasattr(self, "parallel") and self.parallel:
  93. # CMake 3.12+ only.
  94. build_args += ["-j{}".format(self.parallel)]
  95. else:
  96. build_args += ["-j2"]
  97. if not os.path.exists(self.build_temp):
  98. os.makedirs(self.build_temp)
  99. if not (PNNX_WHEEL_WITHOUT_BUILD == "ON"):
  100. subprocess.check_call(
  101. ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp
  102. )
  103. subprocess.check_call(
  104. ["cmake", "--build", "."] + build_args, cwd=self.build_temp
  105. )
  106. else:
  107. pass
  108. if sys.version_info < (3, 0):
  109. sys.exit("Sorry, Python < 3.0 is not supported")
  110. requirements = ["torch"]
  111. with io.open("README.md", encoding="utf-8") as h:
  112. long_description = h.read()
  113. setup(
  114. name="pnnx",
  115. version=set_version(),
  116. author="nihui",
  117. author_email="nihuini@tencent.com",
  118. description="pnnx is an open standard for PyTorch model interoperability.",
  119. long_description=long_description,
  120. long_description_content_type="text/markdown",
  121. url="https://github.com/Tencent/ncnn/tree/master/tools/pnnx",
  122. classifiers=[
  123. "Programming Language :: C++",
  124. "Programming Language :: Python :: 3",
  125. "Programming Language :: Python :: 3.7",
  126. "Programming Language :: Python :: 3.8",
  127. "Programming Language :: Python :: 3.9",
  128. "Programming Language :: Python :: 3.10",
  129. "Programming Language :: Python :: 3.11",
  130. "License :: OSI Approved :: BSD License",
  131. "Operating System :: OS Independent",
  132. ],
  133. license="BSD-3",
  134. python_requires=">=3.7",
  135. packages=find_packages(),
  136. package_data={"pnnx": ["pnnx", "pnnx.exe"]},
  137. package_dir={"": "."},
  138. install_requires=requirements,
  139. ext_modules=None if PNNX_WHEEL_WITHOUT_BUILD == 'ON' else [CMakeExtension("pnnx", "..")],
  140. cmdclass={"build_ext": CMakeBuild},
  141. entry_points={"console_scripts": ["pnnx=pnnx:pnnx"]},
  142. )