(reduce_after=True, no_scale=False, fp32_allreduce=False)
| 49 | dist.broadcast(p, src_rank, group=self.data_parallel_group) |
| 50 | |
| 51 | def allreduce_params(reduce_after=True, no_scale=False, fp32_allreduce=False): |
| 52 | if (self.needs_reduction): |
| 53 | self.needs_reduction = False |
| 54 | buckets = {} |
| 55 | for name, param in self.module.named_parameters(): |
| 56 | if param.requires_grad and param.grad is not None: |
| 57 | tp = (param.data.type()) |
| 58 | if tp not in buckets: |
| 59 | buckets[tp] = [] |
| 60 | buckets[tp].append(param) |
| 61 | if self.warn_on_half: |
| 62 | if torch.cuda.HalfTensor in buckets: |
| 63 | print("WARNING: gloo dist backend for half parameters may be extremely slow." + |
| 64 | " It is recommended to use the NCCL backend in this case.") |
| 65 | self.warn_on_half = False |
| 66 | for tp in buckets: |
| 67 | bucket = buckets[tp] |
| 68 | grads = [param.grad.data for param in bucket] |
| 69 | coalesced = _flatten_dense_tensors(grads) |
| 70 | if fp32_allreduce: |
| 71 | coalesced = coalesced.float() |
| 72 | if not no_scale and not reduce_after: |
| 73 | coalesced /= dist.get_world_size(group=self.data_parallel_group) |
| 74 | dist.all_reduce(coalesced, group=self.data_parallel_group) |
| 75 | torch.cuda.synchronize() |
| 76 | if not no_scale and reduce_after: |
| 77 | coalesced /= dist.get_world_size(group=self.data_parallel_group) |
| 78 | for buf, synced in zip(grads, _unflatten_dense_tensors(coalesced, grads)): |
| 79 | buf.copy_(synced) |
| 80 | |
| 81 | self.hook_handles = [] |
| 82 | self.hooks = [] |
no test coverage detected