setup.py 6.3 KB

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