setup.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. from packaging import version
  10. from pkg_resources import parse_requirements
  11. from setuptools import setup, find_packages
  12. from setuptools.command.develop import develop
  13. from setuptools.command.install import install
  14. here = os.path.abspath(os.path.dirname(__file__))
  15. class cd:
  16. """Context manager for changing the current working directory"""
  17. def __init__(self, newPath):
  18. self.newPath = os.path.expanduser(newPath)
  19. def __enter__(self):
  20. self.savedPath = os.getcwd()
  21. os.chdir(self.newPath)
  22. def __exit__(self, etype, value, traceback):
  23. os.chdir(self.savedPath)
  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 install_libp2p_daemon():
  40. # check go version:
  41. try:
  42. proc = subprocess.Popen(['go', 'version'],
  43. stdout=subprocess.PIPE)
  44. result, _ = proc.communicate()
  45. result = result.decode('ascii', 'replace')
  46. _, _, v, _ = result.split(' ')
  47. v = v.lstrip('go')
  48. if version.parse(v) < version.parse("1.13"):
  49. raise EnvironmentError(f'newer version of go required: must be >= 1.13, found {version}')
  50. except FileNotFoundError:
  51. raise FileNotFoundError('could not find golang installation')
  52. with tempfile.TemporaryDirectory() as tempdir:
  53. url = 'https://github.com/libp2p/go-libp2p-daemon/archive/master.tar.gz'
  54. dest = os.path.join(tempdir, 'libp2p-daemon.tar.gz')
  55. urllib.request.urlretrieve(url, os.path.join(tempdir, dest))
  56. tar = tarfile.open(dest, 'r:gz')
  57. tar.extractall(tempdir)
  58. tar.close()
  59. with cd(os.path.join(tempdir, 'go-libp2p-daemon-master', 'p2pd')):
  60. status = os.system(f'go build -o {os.path.join(here, "hivemind/hivemind_cli", "p2pd")}')
  61. if status:
  62. raise RuntimeError('Failed to build or install libp2p-daemon:'\
  63. f' exited with status code :{status}')
  64. class ProtoCompileInstall(install):
  65. def run(self):
  66. proto_compile(os.path.join(self.build_lib, 'hivemind', 'proto'))
  67. super().run()
  68. class ProtoCompileDevelop(develop):
  69. def run(self):
  70. proto_compile(os.path.join('hivemind', 'proto'))
  71. super().run()
  72. class LibP2PInstall(install):
  73. def run(self):
  74. install_libp2p_daemon()
  75. with open('requirements.txt') as requirements_file:
  76. install_requires = list(map(str, parse_requirements(requirements_file)))
  77. # loading version from setup.py
  78. with codecs.open(os.path.join(here, 'hivemind/__init__.py'), encoding='utf-8') as init_file:
  79. version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", init_file.read(), re.M)
  80. version_string = version_match.group(1)
  81. extras = {}
  82. with open('requirements-dev.txt') as dev_requirements_file:
  83. extras['dev'] = list(map(str, parse_requirements(dev_requirements_file)))
  84. with open('requirements-docs.txt') as docs_requirements_file:
  85. extras['docs'] = list(map(str, parse_requirements(docs_requirements_file)))
  86. extras['all'] = extras['dev'] + extras['docs']
  87. setup(
  88. name='hivemind',
  89. version=version_string,
  90. cmdclass={'install': ProtoCompileInstall, 'develop': ProtoCompileDevelop, 'libp2p': LibP2PInstall},
  91. description='Decentralized deep learning in PyTorch',
  92. long_description='Decentralized deep learning in PyTorch. Built to train giant models on '
  93. 'thousands of volunteers across the world.',
  94. author='Learning@home & contributors',
  95. author_email='mryabinin0@gmail.com',
  96. url="https://github.com/learning-at-home/hivemind",
  97. packages=find_packages(exclude=['tests']),
  98. package_data={'hivemind': ['proto/*']},
  99. include_package_data=True,
  100. license='MIT',
  101. setup_requires=['grpcio-tools'],
  102. install_requires=install_requires,
  103. extras_require=extras,
  104. classifiers=[
  105. 'Development Status :: 4 - Beta',
  106. 'Intended Audience :: Developers',
  107. 'Intended Audience :: Science/Research',
  108. 'License :: OSI Approved :: MIT License',
  109. 'Programming Language :: Python :: 3',
  110. 'Programming Language :: Python :: 3.7',
  111. 'Programming Language :: Python :: 3.8',
  112. 'Programming Language :: Python :: 3.9',
  113. 'Topic :: Scientific/Engineering',
  114. 'Topic :: Scientific/Engineering :: Mathematics',
  115. 'Topic :: Scientific/Engineering :: Artificial Intelligence',
  116. 'Topic :: Software Development',
  117. 'Topic :: Software Development :: Libraries',
  118. 'Topic :: Software Development :: Libraries :: Python Modules',
  119. ],
  120. entry_points={
  121. 'console_scripts': ['hivemind-server = hivemind.hivemind_cli.run_server:main', ]
  122. },
  123. # What does your project relate to?
  124. keywords='pytorch, deep learning, machine learning, gpu, distributed computing, volunteer computing, dht',
  125. )