mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-25 11:44:03 +00:00
Refactored docstring to google style
This commit is contained in:
@@ -12,21 +12,22 @@ from colossalai.utils import get_current_device
|
||||
|
||||
|
||||
def all_gather(tensor: Tensor, dim: int, parallel_mode: ParallelMode, async_op: bool = False) -> Tensor:
|
||||
"""Gathers all tensors from the parallel group and concatenates them in a
|
||||
r"""Gathers all tensors from the parallel group and concatenates them in a
|
||||
specific dimension.
|
||||
|
||||
:param tensor: Tensor to be gathered
|
||||
:param dim: The dimension concatenating in
|
||||
:param parallel_mode: Parallel group mode used in this communication
|
||||
:param async_op: Whether operations are asynchronous
|
||||
|
||||
:type tensor: :class:`torch.Tensor`
|
||||
:type dim: int
|
||||
:type parallel_mode: :class:`colossalai.context.ParallelMode`
|
||||
:type async_op: bool, optional
|
||||
Note:
|
||||
The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found
|
||||
in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_.
|
||||
|
||||
:return: The tensor generated by all-gather
|
||||
:rtype: :class:`torch.Tensor`
|
||||
Args:
|
||||
tensor (:class:`torch.Tensor`): Tensor to be gathered.
|
||||
dim (int): The dimension concatenating in.
|
||||
parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication.
|
||||
async_op (bool, optional): Whether operations are asynchronous.
|
||||
|
||||
Returns:
|
||||
Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The result of all-together only,
|
||||
if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True.
|
||||
"""
|
||||
depth = gpc.get_world_size(parallel_mode)
|
||||
if depth == 1:
|
||||
@@ -54,23 +55,26 @@ def reduce_scatter(tensor: Tensor,
|
||||
parallel_mode: ParallelMode,
|
||||
op: ReduceOp = ReduceOp.SUM,
|
||||
async_op: bool = False) -> Tensor:
|
||||
"""Reduces all tensors then scatters it in a specific dimension to all
|
||||
r"""Reduces all tensors then scatters it in a specific dimension to all
|
||||
members in the parallel group.
|
||||
|
||||
:param tensor: Tensor to be reduced and scattered
|
||||
:param dim: The dimension scattering in
|
||||
:param parallel_mode: Parallel group mode used in this communication
|
||||
:param op: The type of reduce operation
|
||||
:param async_op: Whether operations are asynchronous
|
||||
|
||||
:type tensor: :class:`torch.Tensor`
|
||||
:type dim: int
|
||||
:type parallel_mode: :class:`colossalai.context.ParallelMode`
|
||||
:type op: ReduceOp, optional
|
||||
:type async_op: bool, optional
|
||||
Note:
|
||||
The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found
|
||||
in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_.
|
||||
|
||||
:return: The tensor generated by reduce-scatter
|
||||
:rtype: :class:`Tensor`
|
||||
Args:
|
||||
tensor (:class:`torch.Tensor`): Tensor to be reduce_scattered.
|
||||
dim (int): The dimension concatenating in.
|
||||
parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication.
|
||||
op (torch.distributed.ReduceOp, optional): The type of reduce operation,
|
||||
should be included in [SUM, AVG, PRODUCT, MIN, MAX, BAND, BOR, BXOR].
|
||||
More details about ReduceOp please refer to
|
||||
`ReduceOp <https://pytorch.org/docs/stable/distributed.html#torch.distributed.ReduceOp>`_.
|
||||
async_op (bool, optional): Whether operations are asynchronous.
|
||||
|
||||
Returns:
|
||||
Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The result of reduce_scatter only,
|
||||
if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True.
|
||||
"""
|
||||
depth = gpc.get_world_size(parallel_mode)
|
||||
if depth == 1:
|
||||
@@ -94,6 +98,25 @@ def all_reduce(tensor: Tensor,
|
||||
parallel_mode: ParallelMode,
|
||||
op: ReduceOp = ReduceOp.SUM,
|
||||
async_op: bool = False) -> Tensor:
|
||||
r"""Reduces the tensor data across whole parallel group in such a way that all get the final result.
|
||||
|
||||
Note:
|
||||
The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found
|
||||
in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_.
|
||||
|
||||
Args:
|
||||
tensor (:class:`torch.Tensor`): Tensor to be all-reduced.
|
||||
parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication.
|
||||
op (torch.distributed.ReduceOp, optional): The type of reduce operation,
|
||||
should be included in [SUM, AVG, PRODUCT, MIN, MAX, BAND, BOR, BXOR].
|
||||
More details about ReduceOp please refer to
|
||||
`ReduceOp <https://pytorch.org/docs/stable/distributed.html#torch.distributed.ReduceOp>`_.
|
||||
async_op (bool, optional): Whether operations are asynchronous.
|
||||
|
||||
Returns:
|
||||
Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The result of all-gather only,
|
||||
if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True.
|
||||
"""
|
||||
depth = gpc.get_world_size(parallel_mode)
|
||||
if depth == 1:
|
||||
out = tensor
|
||||
@@ -108,6 +131,23 @@ def all_reduce(tensor: Tensor,
|
||||
|
||||
|
||||
def broadcast(tensor: Tensor, src: int, parallel_mode: ParallelMode, async_op: bool = False):
|
||||
r"""Broadcast tensors to whole parallel group. Tensor must have the same
|
||||
number of elements in all processes participating in the collective.
|
||||
|
||||
Note:
|
||||
The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found
|
||||
in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_.
|
||||
|
||||
Args:
|
||||
tensor (:class:`torch.Tensor`): Tensor to be broadcast.
|
||||
src (int): Source rank.
|
||||
parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication.
|
||||
async_op (bool, optional): Whether operations are asynchronous.
|
||||
|
||||
Returns:
|
||||
Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The tensor need to be broadcast only,
|
||||
if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True.
|
||||
"""
|
||||
depth = gpc.get_world_size(parallel_mode)
|
||||
if depth == 1:
|
||||
out = tensor
|
||||
@@ -122,6 +162,23 @@ def broadcast(tensor: Tensor, src: int, parallel_mode: ParallelMode, async_op: b
|
||||
|
||||
|
||||
def reduce(tensor: Tensor, dst: int, parallel_mode: ParallelMode, op: ReduceOp = ReduceOp.SUM, async_op: bool = False):
|
||||
r"""Reduce tensors across whole parallel group. Only the process with
|
||||
rank ``dst`` is going to receive the final result.
|
||||
|
||||
Note:
|
||||
The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found
|
||||
in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_.
|
||||
|
||||
Args:
|
||||
tensor (:class:`torch.Tensor`): Tensor to be reduced.
|
||||
dst (int): Destination rank.
|
||||
parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication.
|
||||
async_op (bool, optional): Whether operations are asynchronous.
|
||||
|
||||
Returns:
|
||||
Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The result of reduce only,
|
||||
if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True.
|
||||
"""
|
||||
depth = gpc.get_world_size(parallel_mode)
|
||||
if depth == 1:
|
||||
out = tensor
|
||||
|
Reference in New Issue
Block a user