Build communication structures for tied modules.
(self)
| 425 | ) |
| 426 | |
| 427 | def _index_tied_modules(self): |
| 428 | ''' Build communication structures for tied modules. ''' |
| 429 | tied_comms = {} |
| 430 | if self._topo.get_dim('pipe') == 1: |
| 431 | return tied_comms |
| 432 | |
| 433 | specs = self._layer_specs |
| 434 | tie_keys = set(s.key for s in specs if isinstance(s, TiedLayerSpec)) |
| 435 | for key in tie_keys: |
| 436 | # Find the layers that the tied module appears in |
| 437 | tied_layers = [] |
| 438 | for idx, layer in enumerate(specs): |
| 439 | if isinstance(layer, TiedLayerSpec) and layer.key == key: |
| 440 | tied_layers.append(idx) |
| 441 | # Find all stages with this tied module |
| 442 | # TODO: Would be nice to remove the nested data/model parallelism loops and |
| 443 | # TODO: instead generalize in some way, since we really just care about the |
| 444 | # TODO: stage that owns the tied layer. Then loop over each (dp, mp, ...) |
| 445 | # TODO: fiber to generate process groups. |
| 446 | tied_stages = set(self.stage_owner(idx) for idx in tied_layers) |
| 447 | for dp in range(self._grid.data_parallel_size): |
| 448 | for mp in range(self._grid.get_slice_parallel_world_size()): |
| 449 | tied_ranks = [] |
| 450 | for s in sorted(tied_stages): |
| 451 | if self._grid.get_slice_parallel_world_size() > 1: |
| 452 | tied_ranks.append(self._grid.stage_to_global(stage_id=s, data=dp, model=mp)) |
| 453 | else: |
| 454 | tied_ranks.append(self._grid.stage_to_global(stage_id=s, data=dp)) |
| 455 | group = dist.new_group(ranks=tied_ranks) |
| 456 | |
| 457 | # Record this tied module if we own a local copy of it. |
| 458 | if self.global_rank in tied_ranks: |
| 459 | assert key in self.tied_modules |
| 460 | if key in self.tied_modules: |
| 461 | tied_comms[key] = { |
| 462 | 'ranks': tied_ranks, |
| 463 | 'group': group, |
| 464 | 'weight_attr': self.tied_weight_attrs[key], |
| 465 | 'module': self.tied_modules[key], |
| 466 | } |
| 467 | # Only count the tied module once in the eyes of the FP16 optimizer |
| 468 | if self.global_rank != tied_ranks[0]: |
| 469 | for p in self.tied_modules[key].parameters(): |
| 470 | p.ds_pipe_replicated = True |
| 471 | ''' |
| 472 | if len(tied_comms) > 0: |
| 473 | print(f'RANK={self.global_rank} tied_comms={tied_comms}') |
| 474 | ''' |
| 475 | |
| 476 | return tied_comms |
| 477 | |
| 478 | def partitions(self): |
| 479 | return self.parts |
no test coverage detected