已关闭
[WIP][Feature][build]Support PyPI-compatible MindIE-SD wheel publishing #308
guowenna1创建于 5月25日关闭于 6月10日
[WIP][Feature][build]Support PyPI-compatible MindIE-SD wheel publishing #308
已关闭
共 1 个文件变更+82-8
| @@ -16,9 +16,9 @@ | |||
| 16 | import os | 16 | import os |
| 17 | import sys | 17 | import sys |
| 18 | import logging | 18 | import logging |
| 19 | -import runpy | ||
| 20 | import subprocess | 19 | import subprocess |
| 21 | import shutil | 20 | import shutil |
| 21 | +import platform | ||
| 22 | from setuptools import setup, find_packages | 22 | from setuptools import setup, find_packages |
| 23 | from setuptools.command.build_py import build_py as _build_py | 23 | from setuptools.command.build_py import build_py as _build_py |
| 24 | from wheel.bdist_wheel import bdist_wheel as _bdist_wheel | 24 | from wheel.bdist_wheel import bdist_wheel as _bdist_wheel |
| @@ -26,19 +26,41 @@ from wheel.bdist_wheel import bdist_wheel as _bdist_wheel | |||
| 26 | logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') | 26 | logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') |
| 27 | 27 | ||
| 28 | os.environ["SOURCE_DATE_EPOCH"] = "315532800" | 28 | os.environ["SOURCE_DATE_EPOCH"] = "315532800" |
| 29 | -VERSION_FILE = os.path.join(os.path.abspath(os.path.dirname(__file__)), "version.py") | ||
| 30 | 29 | ||
| 31 | 30 | ||
| 32 | def get_mindiesd_version(): | 31 | def get_mindiesd_version(): |
| 33 | - version_ns = runpy.run_path(VERSION_FILE) | 32 | + version = os.environ.get("MINDIE_SD_VERSION_OVERRIDE") |
| 34 | - version = version_ns.get("__version__") | 33 | + if version: |
| 35 | - if not version: | 34 | + version = version.replace("T", "post") |
| 36 | - raise RuntimeError(f"Failed to get version from {VERSION_FILE}") | 35 | + else: |
| 36 | + version = get_torch_version() | ||
| 37 | 37 | ||
| 38 | + if not version: | ||
| 39 | + raise RuntimeError("Failed to get MindIE-SD build version") | ||
| 40 | + | ||
| 41 | + os.environ["MINDIE_SD_VERSION_OVERRIDE"] = version | ||
| 38 | logging.info(f"Build version is: {version}") | 42 | logging.info(f"Build version is: {version}") |
| 39 | return version | 43 | return version |
| 40 | 44 | ||
| 41 | 45 | ||
| 46 | +def get_torch_version(): | ||
| 47 | + torch_version = os.environ.get("MINDIE_SD_TORCH_VERSION") | ||
| 48 | + if torch_version: | ||
| 49 | + return torch_version.split("+", 1)[0] | ||
| 50 | + | ||
| 51 | + import torch | ||
| 52 | + return torch.__version__.split("+", 1)[0] | ||
| 53 | + | ||
| 54 | + | ||
| 55 | +def get_torch_npu_version(): | ||
| 56 | + torch_npu_version = os.environ.get("MINDIE_SD_TORCH_NPU_VERSION") | ||
| 57 | + if torch_npu_version: | ||
| 58 | + return torch_npu_version.split("+", 1)[0] | ||
| 59 | + | ||
| 60 | + import torch_npu | ||
| 61 | + return torch_npu.__version__.split("+", 1)[0] | ||
| 62 | + | ||
| 63 | + | ||
| 42 | def get_python_version(): | 64 | def get_python_version(): |
| 43 | """获取 Python 版本字符串,如 py310""" | 65 | """获取 Python 版本字符串,如 py310""" |
| 44 | try: | 66 | try: |
| @@ -56,6 +78,28 @@ def get_python_version(): | |||
| 56 | raise RuntimeError("Cannot get Python version. Please ensure Python is properly installed.") from e | 78 | raise RuntimeError("Cannot get Python version. Please ensure Python is properly installed.") from e |
| 57 | 79 | ||
| 58 | 80 | ||
| 81 | +def get_python_requires(): | ||
| 82 | + override = os.environ.get("MINDIE_SD_PYTHON_REQUIRES") | ||
| 83 | + if override: | ||
| 84 | + return override | ||
| 85 | + | ||
| 86 | + major = sys.version_info.major | ||
| 87 | + minor = sys.version_info.minor | ||
| 88 | + return f">={major}.{minor},<{major}.{minor + 1}" | ||
| 89 | + | ||
| 90 | + | ||
| 91 | +def get_runtime_requirements(): | ||
| 92 | + torch_version = get_torch_version() | ||
| 93 | + torch_npu_version = get_torch_npu_version() | ||
| 94 | + | ||
| 95 | + logging.info(f"Runtime dependency torch is: {torch_version}") | ||
| 96 | + logging.info(f"Runtime dependency torch-npu is: {torch_npu_version}") | ||
| 97 | + return [ | ||
| 98 | + f"torch=={torch_version}", | ||
| 99 | + f"torch-npu=={torch_npu_version}", | ||
| 100 | + ] | ||
| 101 | + | ||
| 102 | + | ||
| 59 | def copy_so_files(src_dir, dest_dir): | 103 | def copy_so_files(src_dir, dest_dir): |
| 60 | if not os.path.exists(dest_dir): | 104 | if not os.path.exists(dest_dir): |
| 61 | os.makedirs(dest_dir) | 105 | os.makedirs(dest_dir) |
| @@ -244,10 +288,40 @@ class BDistWheel(_bdist_wheel): | |||
| 244 | def finalize_options(self): | 288 | def finalize_options(self): |
| 245 | super().finalize_options() | 289 | super().finalize_options() |
| 246 | self.root_is_pure = False | 290 | self.root_is_pure = False |
| 291 | + if not getattr(self, "plat_name_supplied", False): | ||
| 292 | + self.plat_name = get_binary_wheel_plat_name() | ||
| 293 | + logging.info(f"Wheel platform tag is: {self.plat_name}") | ||
| 294 | + | ||
| 295 | + | ||
| 296 | +def get_binary_wheel_plat_name(): | ||
| 297 | + override = os.environ.get("MINDIE_SD_PLAT_NAME") | ||
| 298 | + if override: | ||
| 299 | + return override | ||
| 300 | + | ||
| 301 | + system = platform.system().lower() | ||
| 302 | + machine = platform.machine().lower().replace("-", "_") | ||
| 303 | + if system != "linux": | ||
| 304 | + return None | ||
| 305 | + | ||
| 306 | + arch_map = { | ||
| 307 | + "aarch64": "aarch64", | ||
| 308 | + "arm64": "aarch64", | ||
| 309 | + "x86_64": "x86_64", | ||
| 310 | + "amd64": "x86_64", | ||
| 311 | + } | ||
| 312 | + arch = arch_map.get(machine) | ||
| 313 | + libc_name, libc_version = platform.libc_ver() | ||
| 314 | + if arch and libc_name == "glibc" and libc_version: | ||
| 315 | + major_minor = libc_version.split(".")[:2] | ||
| 316 | + if len(major_minor) == 2 and all(part.isdigit() for part in major_minor): | ||
| 317 | + return f"manylinux_{major_minor[0]}_{major_minor[1]}_{arch}" | ||
| 318 | + | ||
| 319 | + return None | ||
| 247 | 320 | ||
| 248 | 321 | ||
| 249 | if __name__ == "__main__": | 322 | if __name__ == "__main__": |
| 250 | - requirements = ["torch", "torch_npu"] | 323 | + requirements = get_runtime_requirements() |
| 324 | + python_requires = get_python_requires() | ||
| 251 | mindie_sd_version = get_mindiesd_version() | 325 | mindie_sd_version = get_mindiesd_version() |
| 252 | ensure_plugin_init() | 326 | ensure_plugin_init() |
| 253 | 327 | ||
| @@ -259,7 +333,7 @@ if __name__ == "__main__": | |||
| 259 | setup_requires=[], | 333 | setup_requires=[], |
| 260 | install_requires=requirements, | 334 | install_requires=requirements, |
| 261 | zip_safe=False, | 335 | zip_safe=False, |
| 262 | - python_requires=">=3.10", | 336 | + python_requires=python_requires, |
| 263 | include_package_data=True, | 337 | include_package_data=True, |
| 264 | packages=find_packages(), | 338 | packages=find_packages(), |
| 265 | package_data={ | 339 | package_data={ |