setup.py 6.1 KB

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