MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / _get_shared_params

Method _get_shared_params

deepspeed/runtime/engine.py:5685–5722  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

5683 return param_group_shapes
5684
5685 def _get_shared_params(self):
5686 """
5687 Returns a dict of shared params, which can later be used to reconstruct the original state dict,
5688 e.g. in `zero_to_fp32`. Each dict entry is a pair of param names, where the key is the name
5689 of the variable that isn't stored and the value is the actual param holding data.
5690 """
5691 shared_index = {}
5692 shared_params_by_full_name = {}
5693
5694 is_zero3_model = (self.zero_optimization_partition_weights()
5695 and any(hasattr(param, "ds_id") for param in self.module.parameters()))
5696
5697 def get_layer_state_dict(module, prefix=""):
5698 # handle params
5699 for name, param in module.named_parameters(recurse=False):
5700 if param is None or (is_zero3_model and not hasattr(param, "ds_id")):
5701 continue
5702 key = prefix + name
5703
5704 # When weights are manged by stage 3, we can't rely on param.data_ptr() as it will be reused
5705 # as weights get gathered and reduced, but param.ds_id is unique across all zero weights
5706 # (and shared params will have the same param.ds_id)
5707 param_id = param.ds_id if is_zero3_model else param.data_ptr()
5708
5709 if param_id in shared_index:
5710 # shared weights
5711 shared_params_by_full_name[key] = shared_index[param_id]
5712 else:
5713 shared_index[param_id] = key
5714
5715 for name, child in module.named_children():
5716 if child is not None:
5717 get_layer_state_dict(child, prefix + name + ".")
5718
5719 if dist.get_rank() == 0:
5720 get_layer_state_dict(self.module, prefix="")
5721
5722 return shared_params_by_full_name
5723
5724 def _copy_recovery_script(self, save_path):
5725 base_dir = os.path.dirname(os.path.dirname(__file__))

Callers 1

Calls 3

parametersMethod · 0.45
get_rankMethod · 0.45

Tested by

no test coverage detected