setup.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import codecs
  2. import glob
  3. import hashlib
  4. import os
  5. import re
  6. import subprocess
  7. import tarfile
  8. import tempfile
  9. import urllib.request
  10. from pkg_resources import parse_requirements, parse_version
  11. from setuptools import find_packages, setup
  12. from setuptools.command.build_py import build_py
  13. from setuptools.command.develop import develop
  14. P2PD_VERSION = "v0.3.16"
  15. P2PD_SOURCE_URL = f"https://github.com/learning-at-home/go-libp2p-daemon/archive/refs/tags/{P2PD_VERSION}.tar.gz"
  16. P2PD_BINARY_URL = f"https://github.com/learning-at-home/go-libp2p-daemon/releases/download/{P2PD_VERSION}/"
  17. # The value is sha256 of the binary from the release page
  18. EXECUTABLES = {
  19. "p2pd": "057ec61edbe926cf049e9532d43ea9540da55db7b2d8c816d2bbdddce23f3cdf",
  20. }
  21. here = os.path.abspath(os.path.dirname(__file__))
  22. def sha256(path):
  23. if not os.path.exists(path):
  24. return None
  25. with open(path, "rb") as f:
  26. return hashlib.sha256(f.read()).hexdigest()
  27. def proto_compile(output_path):
  28. import grpc_tools.protoc
  29. cli_args = [
  30. "grpc_tools.protoc",
  31. "--proto_path=hivemind/proto",
  32. f"--python_out={output_path}",
  33. ] + glob.glob("hivemind/proto/*.proto")
  34. code = grpc_tools.protoc.main(cli_args)
  35. if code: # hint: if you get this error in jupyter, run in console for richer error message
  36. raise ValueError(f"{' '.join(cli_args)} finished with exit code {code}")
  37. # Make pb2 imports in generated scripts relative
  38. for script in glob.iglob(f"{output_path}/*.py"):
  39. with open(script, "r+") as file:
  40. code = file.read()
  41. file.seek(0)
  42. file.write(re.sub(r"\n(import .+_pb2.*)", "from . \\1", code))
  43. file.truncate()
  44. def build_p2p_daemon():
  45. result = subprocess.run("go version", capture_output=True, shell=True).stdout.decode("ascii", "replace")
  46. m = re.search(r"^go version go([\d.]+)", result)
  47. if m is None:
  48. raise FileNotFoundError("Could not find golang installation")
  49. version = parse_version(m.group(1))
  50. if version < parse_version("1.13"):
  51. raise EnvironmentError(f"Newer version of go required: must be >= 1.13, found {version}")
  52. with tempfile.TemporaryDirectory() as tempdir:
  53. dest = os.path.join(tempdir, "libp2p-daemon.tar.gz")
  54. urllib.request.urlretrieve(P2PD_SOURCE_URL, dest)
  55. with tarfile.open(dest, "r:gz") as tar:
  56. tar.extractall(tempdir)
  57. for executable in EXECUTABLES:
  58. result = subprocess.run(
  59. ["go", "build", "-o", os.path.join(here, "hivemind", "hivemind_cli", executable)],
  60. cwd=os.path.join(tempdir, f"go-libp2p-daemon-{P2PD_VERSION.lstrip('v')}", executable),
  61. )
  62. if result.returncode != 0:
  63. raise RuntimeError(f"Failed to build {executable}: exited with status code: {result.returncode}")
  64. def download_p2p_daemon():
  65. for executable, expected_hash in EXECUTABLES.items():
  66. binary_path = os.path.join(here, "hivemind", "hivemind_cli", executable)
  67. if sha256(binary_path) != expected_hash:
  68. binary_url = os.path.join(P2PD_BINARY_URL, executable)
  69. print(f"Downloading {binary_url}")
  70. urllib.request.urlretrieve(binary_url, binary_path)
  71. os.chmod(binary_path, 0o777)
  72. actual_hash = sha256(binary_path)
  73. if actual_hash != expected_hash:
  74. raise RuntimeError(
  75. f"The sha256 checksum for {executable} does not match (expected: {expected_hash}, actual: {actual_hash})"
  76. )
  77. class BuildPy(build_py):
  78. user_options = build_py.user_options + [("buildgo", None, "Builds p2pd from source")]
  79. def initialize_options(self):
  80. super().initialize_options()
  81. self.buildgo = False
  82. def run(self):
  83. if self.buildgo:
  84. build_p2p_daemon()
  85. else:
  86. download_p2p_daemon()
  87. super().run()
  88. proto_compile(os.path.join(self.build_lib, "hivemind", "proto"))
  89. class Develop(develop):
  90. def run(self):
  91. self.reinitialize_command("build_py", build_lib=here)
  92. self.run_command("build_py")
  93. super().run()
  94. with open("requirements.txt") as requirements_file:
  95. install_requires = list(map(str, parse_requirements(requirements_file)))
  96. # loading version from setup.py
  97. with codecs.open(os.path.join(here, "hivemind/__init__.py"), encoding="utf-8") as init_file:
  98. version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", init_file.read(), re.M)
  99. version_string = version_match.group(1)
  100. extras = {}
  101. with open("requirements-dev.txt") as dev_requirements_file:
  102. extras["dev"] = list(map(str, parse_requirements(dev_requirements_file)))
  103. with open("requirements-docs.txt") as docs_requirements_file:
  104. extras["docs"] = list(map(str, parse_requirements(docs_requirements_file)))
  105. extras["bitsandbytes"] = ["bitsandbytes~=0.34.0"]
  106. extras["all"] = extras["dev"] + extras["docs"] + extras["bitsandbytes"]
  107. setup(
  108. name="hivemind",
  109. version=version_string,
  110. cmdclass={"build_py": BuildPy, "develop": Develop},
  111. description="Decentralized deep learning in PyTorch",
  112. long_description="Decentralized deep learning in PyTorch. Built to train models on thousands of volunteers "
  113. "across the world.",
  114. author="Learning@home & contributors",
  115. author_email="hivemind-team@hotmail.com",
  116. url="https://github.com/learning-at-home/hivemind",
  117. packages=find_packages(exclude=["tests"]),
  118. package_data={"hivemind": ["proto/*", "hivemind_cli/*"]},
  119. include_package_data=True,
  120. license="MIT",
  121. setup_requires=["grpcio-tools>=1.33.2"],
  122. install_requires=install_requires,
  123. extras_require=extras,
  124. classifiers=[
  125. "Development Status :: 4 - Beta",
  126. "Intended Audience :: Developers",
  127. "Intended Audience :: Science/Research",
  128. "License :: OSI Approved :: MIT License",
  129. "Programming Language :: Python :: 3",
  130. "Programming Language :: Python :: 3.7",
  131. "Programming Language :: Python :: 3.8",
  132. "Programming Language :: Python :: 3.9",
  133. "Topic :: Scientific/Engineering",
  134. "Topic :: Scientific/Engineering :: Mathematics",
  135. "Topic :: Scientific/Engineering :: Artificial Intelligence",
  136. "Topic :: Software Development",
  137. "Topic :: Software Development :: Libraries",
  138. "Topic :: Software Development :: Libraries :: Python Modules",
  139. ],
  140. entry_points={
  141. "console_scripts": [
  142. "hivemind-dht = hivemind.hivemind_cli.run_dht:main",
  143. "hivemind-server = hivemind.hivemind_cli.run_server:main",
  144. ]
  145. },
  146. # What does your project relate to?
  147. keywords="pytorch, deep learning, machine learning, gpu, distributed computing, volunteer computing, dht",
  148. )