Returns a dict of shared params, which can later be used to reconstruct the original state dict, e.g. in `zero_to_fp32`. Each dict entry is a pair of param names, where the key is the name of the variable that isn't stored and the value is the actual param holding data.
(self)
| 5587 | return param_group_shapes |
| 5588 | |
| 5589 | def _get_shared_params(self): |
| 5590 | """ |
| 5591 | Returns a dict of shared params, which can later be used to reconstruct the original state dict, |
| 5592 | e.g. in `zero_to_fp32`. Each dict entry is a pair of param names, where the key is the name |
| 5593 | of the variable that isn't stored and the value is the actual param holding data. |
| 5594 | """ |
| 5595 | shared_index = {} |
| 5596 | shared_params_by_full_name = {} |
| 5597 | |
| 5598 | is_zero3_model = (self.zero_optimization_partition_weights() |
| 5599 | and any(hasattr(param, "ds_id") for param in self.module.parameters())) |
| 5600 | |
| 5601 | def get_layer_state_dict(module, prefix=""): |
| 5602 | # handle params |
| 5603 | for name, param in module.named_parameters(recurse=False): |
| 5604 | if param is None or (is_zero3_model and not hasattr(param, "ds_id")): |
| 5605 | continue |
| 5606 | key = prefix + name |
| 5607 | |
| 5608 | # When weights are manged by stage 3, we can't rely on param.data_ptr() as it will be reused |
| 5609 | # as weights get gathered and reduced, but param.ds_id is unique across all zero weights |
| 5610 | # (and shared params will have the same param.ds_id) |
| 5611 | param_id = param.ds_id if is_zero3_model else param.data_ptr() |
| 5612 | |
| 5613 | if param_id in shared_index: |
| 5614 | # shared weights |
| 5615 | shared_params_by_full_name[key] = shared_index[param_id] |
| 5616 | else: |
| 5617 | shared_index[param_id] = key |
| 5618 | |
| 5619 | for name, child in module.named_children(): |
| 5620 | if child is not None: |
| 5621 | get_layer_state_dict(child, prefix + name + ".") |
| 5622 | |
| 5623 | if dist.get_rank() == 0: |
| 5624 | get_layer_state_dict(self.module, prefix="") |
| 5625 | |
| 5626 | return shared_params_by_full_name |
| 5627 | |
| 5628 | def _copy_recovery_script(self, save_path): |
| 5629 | base_dir = os.path.dirname(os.path.dirname(__file__)) |
no test coverage detected