setup.py 6.0 KB

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