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)
| 3197 | return param_group_shapes |
| 3198 | |
| 3199 | def _get_shared_params(self): |
| 3200 | """ |
| 3201 | Returns a dict of shared params, which can later be used to reconstruct the original state dict, |
| 3202 | e.g. in `zero_to_fp32`. Each dict entry is a pair of param names, where the key is the name |
| 3203 | of the variable that isn't stored and the value is the actual param holding data. |
| 3204 | """ |
| 3205 | shared_ds_ids = {} |
| 3206 | shared_params_by_full_name = {} |
| 3207 | |
| 3208 | def get_layer_state_dict(module, prefix=""): |
| 3209 | # handle params |
| 3210 | for name, param in module.named_parameters(recurse=False): |
| 3211 | if param is None or not hasattr(param, "ds_id"): |
| 3212 | continue |
| 3213 | key = prefix + name |
| 3214 | # can't rely on param.data_ptr() as it will be reused as weights gets |
| 3215 | # gathered and reduced, but param.ds_id is unique across all zero weights |
| 3216 | # (and shared params will have the same param.ds_id) |
| 3217 | if param.ds_id in shared_ds_ids: |
| 3218 | # shared weights |
| 3219 | #print(f"`{key}` is shared with `{shared_ds_ids[param.ds_id]}`") |
| 3220 | shared_params_by_full_name[key] = shared_ds_ids[param.ds_id] |
| 3221 | else: |
| 3222 | shared_ds_ids[param.ds_id] = key |
| 3223 | |
| 3224 | for name, child in module.named_children(): |
| 3225 | if child is not None: |
| 3226 | get_layer_state_dict(child, prefix + name + ".") |
| 3227 | |
| 3228 | if dist.get_rank() == 0: |
| 3229 | get_layer_state_dict(self.module, prefix="") |
| 3230 | |
| 3231 | return shared_params_by_full_name |
| 3232 | |
| 3233 | def _copy_recovery_script(self, save_path): |
| 3234 | base_dir = os.path.dirname(os.path.dirname(__file__)) |
no test coverage detected