You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

group.py 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. from typing import List, Optional, Tuple
  10. from ..device import set_default_device
  11. from .server import Client, Server
  12. class StaticData:
  13. server = None
  14. client = None
  15. master_ip = None
  16. py_server_port = None
  17. mm_server_port = None
  18. world_size = None
  19. proc_rank = None
  20. device = None
  21. backend = None
  22. next_stream = None
  23. _sd = None
  24. class Group:
  25. def __init__(self, proc_ranks):
  26. if len(proc_ranks) == 0: # empty group
  27. self.proc_ranks = None
  28. self.stream = None
  29. else:
  30. self.reset(proc_ranks)
  31. def reset(self, proc_ranks):
  32. self.check(proc_ranks)
  33. self.proc_ranks = proc_ranks
  34. self.stream = _sd.next_stream
  35. _sd.next_stream += 1
  36. def check(self, proc_ranks):
  37. assert _sd is not None, "please call init_process_group first"
  38. for rank in proc_ranks:
  39. assert isinstance(rank, int)
  40. assert rank >= 0 and rank < _sd.world_size
  41. assert _sd.proc_rank in proc_ranks
  42. @property
  43. def size(self):
  44. assert len(self.proc_ranks) > 0, "invalid group"
  45. return len(self.proc_ranks)
  46. @property
  47. def key(self):
  48. assert len(self.proc_ranks) > 0, "invalid group"
  49. return ",".join(map(str, self.proc_ranks))
  50. @property
  51. def rank(self):
  52. assert len(self.proc_ranks) > 0, "invalid group"
  53. return self.proc_ranks.index(_sd.proc_rank)
  54. @property
  55. def comp_node(self):
  56. assert len(self.proc_ranks) > 0, "invalid group"
  57. return "gpu{}:{}".format(_sd.device, self.stream)
  58. WORLD = Group([])
  59. def init_process_group(
  60. master_ip: str,
  61. port: int,
  62. world_size: int,
  63. rank: int,
  64. device: int,
  65. backend: Optional[str] = "nccl",
  66. ) -> None:
  67. """Initialize the distributed process group and specify the device used in the current process
  68. :param master_ip: ip address of the master node.
  69. :param port: port available for all processes to communicate.
  70. :param world_size: total number of processes participating in the job.
  71. :param rank: rank of the current process.
  72. :param device: the GPU device id to bind this process to.
  73. :param backend: communicator backend, currently support 'nccl' and 'ucx'.
  74. """
  75. if not isinstance(master_ip, str):
  76. raise TypeError("Expect type str but got {}".format(type(master_ip)))
  77. if not isinstance(port, int):
  78. raise TypeError("Expect type int but got {}".format(type(port)))
  79. if not isinstance(world_size, int):
  80. raise TypeError("Expect type int but got {}".format(type(world_size)))
  81. if not isinstance(rank, int):
  82. raise TypeError("Expect type int but got {}".format(type(rank)))
  83. if not isinstance(device, int):
  84. raise TypeError("Expect type int but got {}".format(type(backend)))
  85. if not isinstance(backend, str):
  86. raise TypeError("Expect type str but got {}".format(type(backend)))
  87. global _sd
  88. assert _sd is None, "init_process_group should be called only once"
  89. _sd = StaticData()
  90. assert world_size > 1
  91. assert rank >= 0 and rank < world_size
  92. assert port > 0
  93. _sd.client = Client(master_ip, port)
  94. _sd.master_ip = master_ip
  95. _sd.py_server_port = port
  96. _sd.mm_server_port = _sd.client.get_mm_server_port()
  97. _sd.world_size = world_size
  98. _sd.proc_rank = rank
  99. _sd.device = device
  100. _sd.backend = backend
  101. _sd.next_stream = 1
  102. WORLD.reset(list(range(world_size)))
  103. set_default_device("gpu{}".format(device))
  104. def is_distributed() -> bool:
  105. """Return True if the distributed process group has been initialized."""
  106. return _sd is not None
  107. def get_rank() -> int:
  108. """Get the rank of the current process."""
  109. return _sd.proc_rank if _sd is not None else 0
  110. def get_world_size() -> int:
  111. """Get the total number of processes participating in the job."""
  112. return _sd.world_size if _sd is not None else 1
  113. def get_backend() -> str:
  114. """Get the backend str."""
  115. assert _sd is not None, "please call init_process_group first"
  116. return _sd.backend if _sd is not None else None
  117. def get_py_server_addr() -> Tuple[str, int]:
  118. """Get master_ip and port of python XML RPC server."""
  119. assert _sd is not None, "please call init_process_group first"
  120. return _sd.master_ip, _sd.py_server_port
  121. def get_mm_server_addr() -> Tuple[str, int]:
  122. """Get master_ip and port of C++ mm_server."""
  123. assert _sd is not None, "please call init_process_group first"
  124. return _sd.master_ip, _sd.mm_server_port
  125. def get_client() -> Client:
  126. """Get client of python XML RPC server."""
  127. assert _sd is not None, "please call init_process_group first"
  128. return _sd.client
  129. def new_group(proc_ranks: List[int]) -> Group:
  130. """Build a subgroup containing certain ranks."""
  131. return Group(proc_ranks)
  132. def group_barrier(group: Optional[Group] = WORLD) -> None:
  133. """Block until all ranks in the group reach this barrier."""
  134. assert isinstance(group, Group)
  135. _sd.client.group_barrier(group.key, group.size)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台