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

Method load_module_state_dict

deepspeed/runtime/engine.py:4350–4399  ·  view source on GitHub ↗
(self,
                               checkpoint,
                               strict=True,
                               custom_load_fn=None,
                               fetch_z3_params=False,
                               z3_params_to_fetch=None,
                               allowed_missing_keys=None)

Source from the content-addressed store, hash-verified

4348 moe_layer_id += 1
4349
4350 def load_module_state_dict(self,
4351 checkpoint,
4352 strict=True,
4353 custom_load_fn=None,
4354 fetch_z3_params=False,
4355 z3_params_to_fetch=None,
4356 allowed_missing_keys=None):
4357 if z3_params_to_fetch is not None:
4358 params_to_fetch = [
4359 p for p in z3_params_to_fetch if hasattr(p, 'ds_id') and p.ds_status == ZeroParamStatus.NOT_AVAILABLE
4360 ]
4361 elif fetch_z3_params:
4362 params_to_fetch = [
4363 p for p in self.module.parameters()
4364 if hasattr(p, 'ds_id') and p.ds_status == ZeroParamStatus.NOT_AVAILABLE
4365 ]
4366 else:
4367 params_to_fetch = []
4368
4369 with deepspeed.zero.GatheredParameters(params_to_fetch, modifier_rank=0):
4370 module_state_dict = checkpoint['module']
4371 if custom_load_fn:
4372 custom_load_fn(src=module_state_dict, dst=self.module)
4373 else:
4374 load_result = self.module.load_state_dict(
4375 module_state_dict, # TODO
4376 strict=strict and allowed_missing_keys is None)
4377 # The expert-key allowance only tightens strict loads; a caller
4378 # passing strict=False keeps the usual non-strict semantics.
4379 if strict and allowed_missing_keys is not None:
4380 missing_keys = set(load_result.missing_keys)
4381 unexpected_keys = set(load_result.unexpected_keys)
4382 unexpected_missing = missing_keys - set(allowed_missing_keys)
4383 if unexpected_missing or unexpected_keys:
4384 raise RuntimeError("Checkpoint module state did not match the model outside AutoEP expert "
4385 f"parameters: missing={sorted(unexpected_missing)}, "
4386 f"unexpected={sorted(unexpected_keys)}")
4387
4388 if checkpoint.get(FROZEN_PARAM_FRAGMENTS, None) is not None:
4389 saved_frozen_params = checkpoint[FROZEN_PARAM_FRAGMENTS]
4390 for param in self.module.parameters():
4391 if is_optimized_parameter(param):
4392 continue
4393 if param not in self.param_names:
4394 raise ValueError(f"failed to find frozen {param} in named params")
4395 name = self.param_names[param]
4396 if hasattr(param, 'ds_id'):
4397 param.ds_tensor.data.copy_(saved_frozen_params[name].data)
4398 else:
4399 param.data.copy_(saved_frozen_params[name].data)
4400
4401 def _get_zero_ckpt_prefix(self, dp_rank, bf16_mode):
4402 return f'{"bf16_" if bf16_mode else ""}zero_pp_rank_{dp_rank}'

Calls 5

is_optimized_parameterFunction · 0.90
parametersMethod · 0.45
load_state_dictMethod · 0.45
getMethod · 0.45
copy_Method · 0.45