Build communication structures for tied modules.
(self)
| 478 | ) |
| 479 | |
| 480 | def _index_tied_modules(self): |
| 481 | ''' Build communication structures for tied modules. ''' |
| 482 | tied_comms = {} |
| 483 | if self._topo.get_dim('pipe') == 1: |
| 484 | return tied_comms |
| 485 | |
| 486 | specs = self._layer_specs |
| 487 | tie_keys = set(s.key for s in specs if isinstance(s, TiedLayerSpec)) |
| 488 | # Since Python 3.7, "Dictionary order is guaranteed to be insertion order." |
| 489 | # Sort tie_keys here so that orders of self.tied_comms.items() are consistent |
| 490 | # among ranks. |
| 491 | for key in sorted(tie_keys): |
| 492 | # Find the layers that the tied module appears in |
| 493 | tied_layers = [] |
| 494 | for idx, layer in enumerate(specs): |
| 495 | if isinstance(layer, TiedLayerSpec) and layer.key == key: |
| 496 | tied_layers.append(idx) |
| 497 | # Find all stages with this tied module |
| 498 | # TODO: Would be nice to remove the nested data/model parallelism loops and |
| 499 | # TODO: instead generalize in some way, since we really just care about the |
| 500 | # TODO: stage that owns the tied layer. Then loop over each (dp, mp, ...) |
| 501 | # TODO: fiber to generate process groups. |
| 502 | tied_stages = set(self.stage_owner(idx) for idx in tied_layers) |
| 503 | for dp in range(self._grid.data_parallel_size): |
| 504 | for mp in range(self._grid.get_slice_parallel_world_size()): |
| 505 | tied_ranks = [] |
| 506 | for s in sorted(tied_stages): |
| 507 | if self._grid.get_slice_parallel_world_size() > 1: |
| 508 | tied_ranks.append(self._grid.stage_to_global(stage_id=s, data=dp, model=mp)) |
| 509 | else: |
| 510 | tied_ranks.append(self._grid.stage_to_global(stage_id=s, data=dp)) |
| 511 | group = dist.new_group(ranks=tied_ranks) |
| 512 | |
| 513 | # Record this tied module if we own a local copy of it. |
| 514 | if self.global_rank in tied_ranks: |
| 515 | assert key in self.tied_modules |
| 516 | if key in self.tied_modules: |
| 517 | tied_comms[key] = { |
| 518 | 'ranks': tied_ranks, |
| 519 | 'group': group, |
| 520 | 'weight_attr': self.tied_weight_attrs[key], |
| 521 | 'module': self.tied_modules[key], |
| 522 | } |
| 523 | # Only count the tied module once in the eyes of the FP16 optimizer |
| 524 | if self.global_rank != tied_ranks[0]: |
| 525 | for p in self.tied_modules[key].parameters(): |
| 526 | p.ds_pipe_replicated = True |
| 527 | |
| 528 | return tied_comms |
| 529 | |
| 530 | def partitions(self): |
| 531 | return self.parts |
no test coverage detected