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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. """
  68. Initialize the distributed process group and specify the device used in the current process
  69. :param master_ip: ip address of the master node.
  70. :param port: port available for all processes to communicate.
  71. :param world_size: total number of processes participating in the job.
  72. :param rank: rank of the current process.
  73. :param device: the GPU device id to bind this process to.
  74. :param backend: communicator backend, currently support 'nccl' and 'ucx'.
  75. """
  76. if not isinstance(master_ip, str):
  77. raise TypeError("Expect type str but got {}".format(type(master_ip)))
  78. if not isinstance(port, int):
  79. raise TypeError("Expect type int but got {}".format(type(port)))
  80. if not isinstance(world_size, int):
  81. raise TypeError("Expect type int but got {}".format(type(world_size)))
  82. if not isinstance(rank, int):
  83. raise TypeError("Expect type int but got {}".format(type(rank)))
  84. if not isinstance(device, int):
  85. raise TypeError("Expect type int but got {}".format(type(backend)))
  86. if not isinstance(backend, str):
  87. raise TypeError("Expect type str but got {}".format(type(backend)))
  88. global _sd
  89. assert _sd is None, "init_process_group should be called only once"
  90. _sd = StaticData()
  91. assert world_size > 1
  92. assert rank >= 0 and rank < world_size
  93. assert port > 0
  94. _sd.client = Client(master_ip, port)
  95. _sd.master_ip = master_ip
  96. _sd.py_server_port = port
  97. _sd.mm_server_port = _sd.client.get_mm_server_port()
  98. _sd.world_size = world_size
  99. _sd.proc_rank = rank
  100. _sd.device = device
  101. _sd.backend = backend
  102. _sd.next_stream = 1
  103. WORLD.reset(list(range(world_size)))
  104. set_default_device("gpu{}".format(device))
  105. def is_distributed() -> bool:
  106. """Return True if the distributed process group has been initialized."""
  107. return _sd is not None
  108. def get_rank() -> int:
  109. """Get the rank of the current process."""
  110. return _sd.proc_rank if _sd is not None else 0
  111. def get_world_size() -> int:
  112. """Get the total number of processes participating in the job."""
  113. return _sd.world_size if _sd is not None else 1
  114. def get_backend() -> str:
  115. """Get the backend str."""
  116. assert _sd is not None, "please call init_process_group first"
  117. return _sd.backend if _sd is not None else None
  118. def get_py_server_addr() -> Tuple[str, int]:
  119. """Get master_ip and port of python XML RPC server."""
  120. assert _sd is not None, "please call init_process_group first"
  121. return _sd.master_ip, _sd.py_server_port
  122. def get_mm_server_addr() -> Tuple[str, int]:
  123. """Get master_ip and port of C++ mm_server."""
  124. assert _sd is not None, "please call init_process_group first"
  125. return _sd.master_ip, _sd.mm_server_port
  126. def get_client() -> Client:
  127. """Get client of python XML RPC server."""
  128. assert _sd is not None, "please call init_process_group first"
  129. return _sd.client
  130. def new_group(proc_ranks: List[int]) -> Group:
  131. """Build a subgroup containing certain ranks."""
  132. return Group(proc_ranks)
  133. def group_barrier(group: Optional[Group] = WORLD) -> None:
  134. """Block until all ranks in the group reach this barrier."""
  135. assert isinstance(group, Group)
  136. _sd.client.group_barrier(group.key, group.size)

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