broadcast.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import paddle
  15. import paddle.distributed as dist
  16. from paddle import framework
  17. from paddle.distributed.communication import stream
  18. from .serialization_utils import (
  19. convert_object_to_tensor,
  20. convert_tensor_to_object,
  21. )
  22. def broadcast(tensor, src, group=None, sync_op=True):
  23. """
  24. Broadcast a tensor from the source to all others.
  25. As shown below, one process is started with a GPU and GPU0 owns data 0. Through broadcast operator,
  26. data 0 will be sent to all GPUs from GPU0.
  27. .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/api/paddle/distributed/img/broadcast.png
  28. :width: 800
  29. :alt: broadcast
  30. :align: center
  31. Args:
  32. tensor (Tensor): The tensor to send if current rank is the source, or the tensor to receive otherwise. Its data type
  33. should be float16, float32, float64, int32, int64, int8, uint8, bool, bfloat16, complex64 or complex128.
  34. src (int): The source rank in global view.
  35. group (Group, optional): The group instance return by new_group or None for global default group.
  36. sync_op (bool, optional): Whether this op is a sync op. The default value is True.
  37. Returns:
  38. Return a task object.
  39. Examples:
  40. .. code-block:: python
  41. >>> # doctest: +REQUIRES(env: DISTRIBUTED)
  42. >>> import paddle
  43. >>> import paddle.distributed as dist
  44. >>> dist.init_parallel_env()
  45. >>> if dist.get_rank() == 0:
  46. ... data = paddle.to_tensor([[4, 5, 6], [4, 5, 6]])
  47. >>> else:
  48. ... data = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
  49. >>> dist.broadcast(data, src=1)
  50. >>> print(data)
  51. >>> # [[1, 2, 3], [1, 2, 3]] (2 GPUs)
  52. """
  53. return stream.broadcast(
  54. tensor,
  55. src,
  56. group=group,
  57. sync_op=sync_op,
  58. use_calc_stream=False,
  59. )
  60. def broadcast_object_list(object_list, src, group=None):
  61. """
  62. Broadcast picklable objects from the source to all others. Similiar to broadcast(), but python object can be passed in.
  63. Args:
  64. object_list (list): The list of objects to send if current rank is the source, or the list of objects to receive otherwise.
  65. src (int): The source rank in global view.
  66. group (Group): The group instance return by new_group or None for global default group.
  67. Returns:
  68. None.
  69. Warning:
  70. This API only supports the dygraph mode.
  71. Examples:
  72. .. code-block:: python
  73. >>> # doctest: +REQUIRES(env: DISTRIBUTED)
  74. >>> import paddle.distributed as dist
  75. >>> dist.init_parallel_env()
  76. >>> if dist.get_rank() == 0:
  77. ... object_list = [{"foo": [1, 2, 3]}]
  78. >>> else:
  79. ... object_list = [{"bar": [4, 5, 6]}]
  80. >>> dist.broadcast_object_list(object_list, src=1)
  81. >>> print(object_list)
  82. >>> # [{"bar": [4, 5, 6]}] (2 GPUs)
  83. """
  84. assert (
  85. framework.in_dynamic_mode()
  86. ), "broadcast_object_list doesn't support static graph mode."
  87. rank = dist.get_rank()
  88. obj_tensors = []
  89. obj_nums = len(object_list)
  90. if rank == src:
  91. obj_sizes = []
  92. for obj in object_list:
  93. obj_tensor, obj_size = convert_object_to_tensor(obj)
  94. obj_tensors.append(obj_tensor)
  95. obj_sizes.append(obj_size)
  96. obj_size_tensor = paddle.stack(obj_sizes)
  97. else:
  98. obj_size_tensor = paddle.empty([obj_nums], dtype="int64")
  99. broadcast(obj_size_tensor, src, group)
  100. if rank == src:
  101. # cast to uint8 to keep the same dtype
  102. obj_data_tensor = paddle.concat(obj_tensors).cast("uint8")
  103. else:
  104. data_len = paddle.sum(obj_size_tensor).item()
  105. obj_data_tensor = paddle.empty([data_len], dtype="uint8")
  106. broadcast(obj_data_tensor, src, group)
  107. offset = 0
  108. for i in range(obj_nums):
  109. data_len = obj_size_tensor[i]
  110. object_list[i] = convert_tensor_to_object(
  111. obj_data_tensor[offset : offset + data_len], data_len
  112. )
  113. offset += data_len