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

Method _get_shared_params

deepspeed/runtime/engine.py:5144–5182  ·  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

5142 return param_group_shapes
5143
5144 def _get_shared_params(self):
5145 """
5146 Returns a dict of shared params, which can later be used to reconstruct the original state dict,
5147 e.g. in `zero_to_fp32`. Each dict entry is a pair of param names, where the key is the name
5148 of the variable that isn't stored and the value is the actual param holding data.
5149 """
5150 shared_index = {}
5151 shared_params_by_full_name = {}
5152
5153 is_zero3_model = (self.zero_optimization_partition_weights()
5154 and any(hasattr(param, "ds_id") for param in self.module.parameters()))
5155
5156 def get_layer_state_dict(module, prefix=""):
5157 # handle params
5158 for name, param in module.named_parameters(recurse=False):
5159 if param is None or (is_zero3_model and not hasattr(param, "ds_id")):
5160 continue
5161 key = prefix + name
5162
5163 # When weights are manged by stage 3, we can't rely on param.data_ptr() as it will be reused
5164 # as weights get gathered and reduced, but param.ds_id is unique across all zero weights
5165 # (and shared params will have the same param.ds_id)
5166 param_id = param.ds_id if is_zero3_model else param.data_ptr()
5167
5168 if param_id in shared_index:
5169 # shared weights
5170 #print(f"`{key}` is shared with `{shared_index[param_id]}`")
5171 shared_params_by_full_name[key] = shared_index[param_id]
5172 else:
5173 shared_index[param_id] = key
5174
5175 for name, child in module.named_children():
5176 if child is not None:
5177 get_layer_state_dict(child, prefix + name + ".")
5178
5179 if dist.get_rank() == 0:
5180 get_layer_state_dict(self.module, prefix="")
5181
5182 return shared_params_by_full_name
5183
5184 def _copy_recovery_script(self, save_path):
5185 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