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 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.ops import CollectiveCommMode
  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 ..core.tensor.core import apply
  21. from ..core.tensor.tensor import Tensor, tensor_apply
  22. from ..device import get_default_device
  23. from ..tensor import tensor
  24. from .group import WORLD, Group, get_backend, get_client, get_mm_server_addr, get_rank
  25. __all__ = [
  26. "reduce_sum",
  27. "broadcast",
  28. "all_gather",
  29. "reduce_scatter_sum",
  30. "all_reduce_sum",
  31. "all_reduce_max",
  32. "all_reduce_min",
  33. "gather",
  34. "scatter",
  35. "all_to_all",
  36. "remote_send",
  37. "remote_recv",
  38. ]
  39. @apply.register()
  40. def _(op: RemoteSend, *args: Tensor):
  41. ret = tensor_apply(op, *args)
  42. # set extra information
  43. tracer_set = dict()
  44. for k in set().union(*(i._extra_data for i in args if isinstance(i, Tensor))):
  45. tracer_set[k.name] = True
  46. # check tracer_set in remote_recv
  47. get_client().set_remote_tracer(op.key, tracer_set)
  48. return ret
  49. @builtin_op_get_backward_fn.register(RemoteSend)
  50. def _(op: RemoteSend, inputs, outputs, input_requires_grad):
  51. def backward(*args):
  52. return [
  53. remote_recv(
  54. op.rank_to, inputs[0].shape, inputs[0].dtype, str(inputs[0].device)
  55. )
  56. ]
  57. return backward, [True]
  58. @get_op_has_grad_fn.register(RemoteSend)
  59. def _(op: RemoteSend):
  60. def has_grad(opnode, reached):
  61. return get_client().check_is_grad(op.key)
  62. return has_grad
  63. @check_backward_allow_noinput.register(RemoteSend)
  64. def _(op: RemoteSend):
  65. return True
  66. @builtin_op_get_backward_fn.register(RemoteRecv)
  67. def _(op: RemoteRecv, inputs, outputs, input_requires_grad):
  68. def backward(*output_grads):
  69. return [remote_send(output_grads[0], op.rank_from)]
  70. return backward, [True]
  71. @get_op_has_grad_fn.register(RemoteRecv)
  72. def _(op: RemoteRecv):
  73. def has_grad(opnode, reached):
  74. ret = False
  75. for v in opnode.outputs:
  76. if v() in reached:
  77. ret = True
  78. break
  79. get_client().set_is_grad(op.key, ret)
  80. return ret
  81. return has_grad
  82. def collective_comm(inp, mode, group, device):
  83. """Helper function for applying collective communication functions."""
  84. assert isinstance(group, Group)
  85. if group is None:
  86. return inp
  87. op = CollectiveComm()
  88. op.key = group.key
  89. op.nr_devices = group.size
  90. op.rank = group.rank
  91. op.is_root = op.rank == 0
  92. op.local_grad = False
  93. op.addr, op.port = get_mm_server_addr()
  94. op.mode = mode
  95. op.dtype = inp.dtype
  96. op.backend = get_backend()
  97. op.comp_node = device
  98. return apply(op, inp)[0]
  99. def reduce_sum(
  100. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  101. ) -> Tensor:
  102. """
  103. Create reduce_sum operator for collective communication.
  104. :param inp: input tensor.
  105. :param group: communication group.
  106. :param device: execution device.
  107. """
  108. mode = CollectiveCommMode.REDUCE_SUM
  109. return collective_comm(inp, mode, group, device)
  110. def broadcast(
  111. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  112. ) -> Tensor:
  113. """
  114. Create broadcast operator for collective communication.
  115. :param inp: input tensor.
  116. :param group: communication group.
  117. :param device: execution device.
  118. """
  119. mode = CollectiveCommMode.BROADCAST
  120. return collective_comm(inp, mode, group, device)
  121. def all_gather(
  122. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  123. ) -> Tensor:
  124. """
  125. Create all_gather operator for collective communication.
  126. :param inp: input tensor.
  127. :param group: communication group.
  128. :param device: execution device.
  129. """
  130. mode = CollectiveCommMode.ALL_GATHER
  131. return collective_comm(inp, mode, group, device)
  132. def reduce_scatter_sum(
  133. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  134. ) -> Tensor:
  135. """
  136. Create reduce_scatter_sum operator for collective communication.
  137. :param inp: input tensor.
  138. :param group: communication group.
  139. :param device: execution device.
  140. """
  141. mode = CollectiveCommMode.REDUCE_SCATTER_SUM
  142. return collective_comm(inp, mode, group, device)
  143. def all_reduce_sum(
  144. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  145. ) -> Tensor:
  146. """
  147. Create all_reduce_sum operator for collective communication.
  148. :param inp: input tensor.
  149. :param group: communication group.
  150. :param device: execution device.
  151. """
  152. mode = CollectiveCommMode.ALL_REDUCE_SUM
  153. return collective_comm(inp, mode, group, device)
  154. def all_reduce_max(
  155. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  156. ) -> Tensor:
  157. """
  158. Create all_reduce_max operator for collective communication.
  159. :param inp: input tensor.
  160. :param group: communication group.
  161. :param device: execution device.
  162. """
  163. mode = CollectiveCommMode.ALL_REDUCE_MAX
  164. return collective_comm(inp, mode, group, device)
  165. def all_reduce_min(
  166. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  167. ) -> Tensor:
  168. """
  169. Create all_reduce_min operator for collective communication.
  170. :param inp: input tensor.
  171. :param group: communication group.
  172. :param device: execution device.
  173. """
  174. mode = CollectiveCommMode.ALL_REDUCE_MIN
  175. return collective_comm(inp, mode, group, device)
  176. def gather(
  177. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  178. ) -> Tensor:
  179. """
  180. Create gather operator for collective communication.
  181. :param inp: input tensor.
  182. :param group: communication group.
  183. :param device: execution device.
  184. """
  185. mode = CollectiveCommMode.GATHER
  186. return collective_comm(inp, mode, group, device)
  187. def scatter(
  188. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  189. ) -> Tensor:
  190. """
  191. Create scatter operator for collective communication.
  192. :param inp: input tensor.
  193. :param group: communication group.
  194. :param device: execution device.
  195. """
  196. mode = CollectiveCommMode.SCATTER
  197. return collective_comm(inp, mode, group, device)
  198. def all_to_all(
  199. inp: Tensor, group: Optional[Group] = WORLD, device: Optional[str] = ""
  200. ) -> Tensor:
  201. """
  202. Create all_to_all operator for collective communication.
  203. :param inp: input tensor.
  204. :param group: communication group.
  205. :param device: execution device.
  206. """
  207. mode = CollectiveCommMode.ALL_TO_ALL
  208. return collective_comm(inp, mode, group, device)
  209. def remote_send(inp: Tensor, dest_rank: int) -> Tensor:
  210. """
  211. Send a Tensor to a remote process.
  212. :param inp: tensor to send.
  213. :param dest_rank: destination process rank.
  214. """
  215. op = RemoteSend()
  216. op.key = "{}->{}".format(get_rank(), dest_rank)
  217. op.addr, op.port = get_mm_server_addr()
  218. op.rank_to = dest_rank
  219. return apply(op, inp)[0]
  220. def remote_recv(
  221. src_rank: int, shape: Tuple[int], dtype: type, device: Optional[str] = None
  222. ) -> Tensor:
  223. """
  224. Receive a Tensor from a remote process.
  225. :param src_rank: source process rank.
  226. :param shape: the shape of the tensor to receive.
  227. :param dtype: the data type of the tensor to receive.
  228. :param device: the device to place the received tensor.
  229. """
  230. key = "{}->{}".format(src_rank, get_rank())
  231. if device is None:
  232. device = get_default_device()
  233. # dummpy input
  234. inp = tensor([0])
  235. tracer_set = get_client().check_remote_tracer(key)
  236. for grad_manager in get_grad_managers():
  237. if grad_manager.name in tracer_set:
  238. grad_manager.wrt(inp)
  239. op = RemoteRecv()
  240. op.key = key
  241. op.cn = device
  242. op.shape = shape
  243. op.dtype = dtype
  244. op.addr, op.port = get_mm_server_addr()
  245. op.rank_from = src_rank
  246. return apply(op, inp)[0]

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