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.

functional.py 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 Optional, Tuple
  10. from ..core._imperative_rt.core2 import apply
  11. from ..core.autodiff.builtin_op_utils import builtin_op_get_backward_fn
  12. from ..core.autodiff.grad import (
  13. Tracer,
  14. check_backward_allow_noinput,
  15. get_grad_managers,
  16. get_op_has_grad_fn,
  17. tracer_apply,
  18. )
  19. from ..core.ops.builtin import CollectiveComm, Copy, RemoteRecv, RemoteSend
  20. from ..device import get_default_device
  21. from ..tensor import Tensor
  22. from .group import WORLD, Group, get_backend, get_client, get_mm_server_addr, get_rank
  23. __all__ = [
  24. "reduce_sum",
  25. "broadcast",
  26. "all_gather",
  27. "reduce_scatter_sum",
  28. "all_reduce_sum",
  29. "all_reduce_max",
  30. "all_reduce_min",
  31. "gather",
  32. "scatter",
  33. "all_to_all",
  34. "remote_send",
  35. "remote_recv",
  36. ]
  37. def collective_comm(inp, mode, group, device):
  38. """Helper function for applying collective communication functions."""
  39. assert isinstance(group, Group)
  40. if group is None:
  41. return inp
  42. addr, port = get_mm_server_addr()
  43. op = CollectiveComm(
  44. key=group.key,
  45. nr_devices=group.size,
  46. rank=group.rank,
  47. is_root=(group.rank == 0),
  48. local_grad=False,
  49. addr=addr,
  50. port=port,
  51. mode=mode,
  52. dtype=inp.dtype,
  53. backend=get_backend(),
  54. comp_node=device,
  55. )
  56. return apply(op, inp)[0]
  57. def reduce_sum(
  58. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  59. ) -> Tensor:
  60. """
  61. Create reduce_sum operator for collective communication.
  62. :param inp: input tensor.
  63. :param group: communication group.
  64. :param device: execution device.
  65. """
  66. mode = CollectiveComm.Mode.REDUCE_SUM
  67. return collective_comm(inp, mode, group, device)
  68. def broadcast(
  69. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  70. ) -> Tensor:
  71. """
  72. Create broadcast operator for collective communication.
  73. :param inp: input tensor.
  74. :param group: communication group.
  75. :param device: execution device.
  76. """
  77. mode = CollectiveComm.Mode.BROADCAST
  78. return collective_comm(inp, mode, group, device)
  79. def all_gather(
  80. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  81. ) -> Tensor:
  82. """
  83. Create all_gather operator for collective communication.
  84. :param inp: input tensor.
  85. :param group: communication group.
  86. :param device: execution device.
  87. """
  88. mode = CollectiveComm.Mode.ALL_GATHER
  89. return collective_comm(inp, mode, group, device)
  90. def reduce_scatter_sum(
  91. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  92. ) -> Tensor:
  93. """
  94. Create reduce_scatter_sum operator for collective communication.
  95. :param inp: input tensor.
  96. :param group: communication group.
  97. :param device: execution device.
  98. """
  99. mode = CollectiveComm.Mode.REDUCE_SCATTER_SUM
  100. return collective_comm(inp, mode, group, device)
  101. def all_reduce_sum(
  102. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  103. ) -> Tensor:
  104. """
  105. Create all_reduce_sum operator for collective communication.
  106. :param inp: input tensor.
  107. :param group: communication group.
  108. :param device: execution device.
  109. """
  110. mode = CollectiveComm.Mode.ALL_REDUCE_SUM
  111. return collective_comm(inp, mode, group, device)
  112. def all_reduce_max(
  113. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  114. ) -> Tensor:
  115. """
  116. Create all_reduce_max operator for collective communication.
  117. :param inp: input tensor.
  118. :param group: communication group.
  119. :param device: execution device.
  120. """
  121. mode = CollectiveComm.Mode.ALL_REDUCE_MAX
  122. return collective_comm(inp, mode, group, device)
  123. def all_reduce_min(
  124. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  125. ) -> Tensor:
  126. """
  127. Create all_reduce_min operator for collective communication.
  128. :param inp: input tensor.
  129. :param group: communication group.
  130. :param device: execution device.
  131. """
  132. mode = CollectiveComm.Mode.ALL_REDUCE_MIN
  133. return collective_comm(inp, mode, group, device)
  134. def gather(
  135. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  136. ) -> Tensor:
  137. """
  138. Create gather operator for collective communication.
  139. :param inp: input tensor.
  140. :param group: communication group.
  141. :param device: execution device.
  142. """
  143. mode = CollectiveComm.Mode.GATHER
  144. return collective_comm(inp, mode, group, device)
  145. def scatter(
  146. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  147. ) -> Tensor:
  148. """
  149. Create scatter operator for collective communication.
  150. :param inp: input tensor.
  151. :param group: communication group.
  152. :param device: execution device.
  153. """
  154. mode = CollectiveComm.Mode.SCATTER
  155. return collective_comm(inp, mode, group, device)
  156. def all_to_all(
  157. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  158. ) -> Tensor:
  159. """
  160. Create all_to_all operator for collective communication.
  161. :param inp: input tensor.
  162. :param group: communication group.
  163. :param device: execution device.
  164. """
  165. mode = CollectiveComm.Mode.ALL_TO_ALL
  166. return collective_comm(inp, mode, group, device)
  167. def remote_send(inp: Tensor, dest_rank: int) -> Tensor:
  168. """
  169. Send a Tensor to a remote process.
  170. :param inp: tensor to send.
  171. :param dest_rank: destination process rank.
  172. """
  173. op = RemoteSend()
  174. op.key = "{}->{}".format(get_rank(), dest_rank)
  175. op.addr, op.port = get_mm_server_addr()
  176. op.rank_to = dest_rank
  177. return apply(op, inp)[0]
  178. def remote_recv(
  179. src_rank: int,
  180. shape: Tuple[int],
  181. dtype: type,
  182. device: Optional[str] = None,
  183. inp=None,
  184. ) -> Tensor:
  185. """
  186. Receive a Tensor from a remote process.
  187. :param src_rank: source process rank.
  188. :param shape: the shape of the tensor to receive.
  189. :param dtype: the data type of the tensor to receive.
  190. :param device: the device to place the received tensor.
  191. :param inp: dummy input to determine recved tensor type
  192. """
  193. key = "{}->{}".format(src_rank, get_rank())
  194. if device is None:
  195. device = get_default_device()
  196. # dummy input
  197. if inp == None:
  198. inp = tensor([0], device=device)
  199. tracer_set = get_client().check_remote_tracer(key)
  200. for grad_manager in get_grad_managers():
  201. if grad_manager.name in tracer_set:
  202. grad_manager.wrt(inp)
  203. op = RemoteRecv()
  204. op.key = key
  205. op.cn = device
  206. op.shape = shape
  207. op.dtype = dtype
  208. op.addr, op.port = get_mm_server_addr()
  209. op.rank_from = src_rank
  210. return apply(op, inp)[0]

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