r""" This class defines the generic join context manager, which allows custom hooks to be called after a process joins. These hooks should shadow the collective communications of non-joined processes to prevent hanging and erroring and to ensure algorithmic correctness. Refer to :cl
| 104 | |
| 105 | |
| 106 | class Join: |
| 107 | r""" |
| 108 | This class defines the generic join context manager, which allows custom hooks to be called after a process joins. |
| 109 | |
| 110 | These hooks should shadow the |
| 111 | collective communications of non-joined processes to prevent hanging and |
| 112 | erroring and to ensure algorithmic correctness. Refer to :class:`JoinHook` |
| 113 | for details about the hook definition. |
| 114 | |
| 115 | .. warning:: |
| 116 | The context manager requires each participating :class:`Joinable` to |
| 117 | call the method :meth:`notify_join_context()` before its own per- |
| 118 | iteration collective communications to ensure correctness. |
| 119 | |
| 120 | .. warning:: |
| 121 | The context manager requires that all ``process_group`` attributes in |
| 122 | the :class:`JoinHook` objects are the same. If there are multiple |
| 123 | :class:`JoinHook` objects, then the ``device`` of the first is used. |
| 124 | The process group and device information is used for checking for non- |
| 125 | joined processes and for notifying processes to throw an exception if |
| 126 | ``throw_on_early_termination`` is enabled, both of which using an all- |
| 127 | reduce. |
| 128 | |
| 129 | Arguments: |
| 130 | joinables (List[Joinable]): a list of the participating |
| 131 | :class:`Joinable` s; their hooks are iterated over in the given |
| 132 | order. |
| 133 | |
| 134 | enable (bool): a flag enabling uneven input detection; setting to |
| 135 | ``False`` disables the context manager's functionality and should |
| 136 | only be set when the user knows the inputs will not be uneven |
| 137 | (default: ``True``). |
| 138 | |
| 139 | throw_on_early_termination (bool): a flag controlling whether to throw an |
| 140 | exception upon detecting uneven inputs (default: ``False``). |
| 141 | |
| 142 | Example:: |
| 143 | |
| 144 | >>> import os |
| 145 | >>> import torch |
| 146 | >>> import torch.distributed as dist |
| 147 | >>> import torch.multiprocessing as mp |
| 148 | >>> # xdoctest: +SKIP |
| 149 | >>> import torch.nn.parallel.DistributedDataParallel as DDP |
| 150 | >>> import torch.distributed.optim.ZeroRedundancyOptimizer as ZeRO |
| 151 | >>> from torch.distributed.algorithms.join import Join |
| 152 | >>> |
| 153 | >>> # On each spawned worker |
| 154 | >>> def worker(rank): |
| 155 | >>> dist.init_process_group("nccl", rank=rank, world_size=2) |
| 156 | >>> model = DDP(torch.nn.Linear(1, 1).to(rank), device_ids=[rank]) |
| 157 | >>> optim = ZeRO(model.parameters(), torch.optim.Adam, lr=0.01) |
| 158 | >>> # Rank 1 gets one more input than rank 0 |
| 159 | >>> inputs = [torch.tensor([1.]).to(rank) for _ in range(10 + rank)] |
| 160 | >>> with Join([model, optim]): |
| 161 | >>> for input in inputs: |
| 162 | >>> loss = model(input).sum() |
| 163 | >>> loss.backward() |
no outgoing calls
searching dependent graphs…