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

Method load_module_state_dict

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

4430 moe_layer_id += 1
4431
4432 def load_module_state_dict(self,
4433 checkpoint,
4434 strict=True,
4435 custom_load_fn=None,
4436 fetch_z3_params=False,
4437 z3_params_to_fetch=None,
4438 allowed_missing_keys=None):
4439 if z3_params_to_fetch is not None:
4440 params_to_fetch = [
4441 p for p in z3_params_to_fetch if hasattr(p, 'ds_id') and p.ds_status == ZeroParamStatus.NOT_AVAILABLE
4442 ]
4443 elif fetch_z3_params:
4444 params_to_fetch = [
4445 p for p in self.module.parameters()
4446 if hasattr(p, 'ds_id') and p.ds_status == ZeroParamStatus.NOT_AVAILABLE
4447 ]
4448 else:
4449 params_to_fetch = []
4450
4451 with deepspeed.zero.GatheredParameters(params_to_fetch, modifier_rank=0):
4452 module_state_dict = checkpoint['module']
4453 if custom_load_fn:
4454 custom_load_fn(src=module_state_dict, dst=self.module)
4455 else:
4456 load_result = self.module.load_state_dict(
4457 module_state_dict, # TODO
4458 strict=strict and allowed_missing_keys is None)
4459 # The expert-key allowance only tightens strict loads; a caller
4460 # passing strict=False keeps the usual non-strict semantics.
4461 if strict and allowed_missing_keys is not None:
4462 missing_keys = set(load_result.missing_keys)
4463 unexpected_keys = set(load_result.unexpected_keys)
4464 unexpected_missing = missing_keys - set(allowed_missing_keys)
4465 if unexpected_missing or unexpected_keys:
4466 raise RuntimeError("Checkpoint module state did not match the model outside AutoEP expert "
4467 f"parameters: missing={sorted(unexpected_missing)}, "
4468 f"unexpected={sorted(unexpected_keys)}")
4469
4470 if checkpoint.get(FROZEN_PARAM_FRAGMENTS, None) is not None:
4471 saved_frozen_params = checkpoint[FROZEN_PARAM_FRAGMENTS]
4472 for param in self.module.parameters():
4473 if is_optimized_parameter(param):
4474 continue
4475 if param not in self.param_names:
4476 raise ValueError(f"failed to find frozen {param} in named params")
4477 name = self.param_names[param]
4478 if hasattr(param, 'ds_id'):
4479 param.ds_tensor.data.copy_(saved_frozen_params[name].data)
4480 else:
4481 param.data.copy_(saved_frozen_params[name].data)
4482
4483 def _get_zero_ckpt_prefix(self, dp_rank, bf16_mode):
4484 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