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

Method _get_shared_params

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

5267 return param_group_shapes
5268
5269 def _get_shared_params(self):
5270 """
5271 Returns a dict of shared params, which can later be used to reconstruct the original state dict,
5272 e.g. in `zero_to_fp32`. Each dict entry is a pair of param names, where the key is the name
5273 of the variable that isn't stored and the value is the actual param holding data.
5274 """
5275 shared_index = {}
5276 shared_params_by_full_name = {}
5277
5278 is_zero3_model = (self.zero_optimization_partition_weights()
5279 and any(hasattr(param, "ds_id") for param in self.module.parameters()))
5280
5281 def get_layer_state_dict(module, prefix=""):
5282 # handle params
5283 for name, param in module.named_parameters(recurse=False):
5284 if param is None or (is_zero3_model and not hasattr(param, "ds_id")):
5285 continue
5286 key = prefix + name
5287
5288 # When weights are manged by stage 3, we can't rely on param.data_ptr() as it will be reused
5289 # as weights get gathered and reduced, but param.ds_id is unique across all zero weights
5290 # (and shared params will have the same param.ds_id)
5291 param_id = param.ds_id if is_zero3_model else param.data_ptr()
5292
5293 if param_id in shared_index:
5294 # shared weights
5295 #print(f"`{key}` is shared with `{shared_index[param_id]}`")
5296 shared_params_by_full_name[key] = shared_index[param_id]
5297 else:
5298 shared_index[param_id] = key
5299
5300 for name, child in module.named_children():
5301 if child is not None:
5302 get_layer_state_dict(child, prefix + name + ".")
5303
5304 if dist.get_rank() == 0:
5305 get_layer_state_dict(self.module, prefix="")
5306
5307 return shared_params_by_full_name
5308
5309 def _copy_recovery_script(self, save_path):
5310 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