| 4474 | moe_layer_id += 1 |
| 4475 | |
| 4476 | def load_module_state_dict(self, |
| 4477 | checkpoint, |
| 4478 | strict=True, |
| 4479 | custom_load_fn=None, |
| 4480 | fetch_z3_params=False, |
| 4481 | z3_params_to_fetch=None, |
| 4482 | allowed_missing_keys=None): |
| 4483 | if z3_params_to_fetch is not None: |
| 4484 | params_to_fetch = [ |
| 4485 | p for p in z3_params_to_fetch if hasattr(p, 'ds_id') and p.ds_status == ZeroParamStatus.NOT_AVAILABLE |
| 4486 | ] |
| 4487 | elif fetch_z3_params: |
| 4488 | params_to_fetch = [ |
| 4489 | p for p in self.module.parameters() |
| 4490 | if hasattr(p, 'ds_id') and p.ds_status == ZeroParamStatus.NOT_AVAILABLE |
| 4491 | ] |
| 4492 | else: |
| 4493 | params_to_fetch = [] |
| 4494 | |
| 4495 | with deepspeed.zero.GatheredParameters(params_to_fetch, modifier_rank=0): |
| 4496 | module_state_dict = checkpoint['module'] |
| 4497 | if custom_load_fn: |
| 4498 | custom_load_fn(src=module_state_dict, dst=self.module) |
| 4499 | else: |
| 4500 | load_result = self.module.load_state_dict( |
| 4501 | module_state_dict, # TODO |
| 4502 | strict=strict and allowed_missing_keys is None) |
| 4503 | # The expert-key allowance only tightens strict loads; a caller |
| 4504 | # passing strict=False keeps the usual non-strict semantics. |
| 4505 | if strict and allowed_missing_keys is not None: |
| 4506 | missing_keys = set(load_result.missing_keys) |
| 4507 | unexpected_keys = set(load_result.unexpected_keys) |
| 4508 | unexpected_missing = missing_keys - set(allowed_missing_keys) |
| 4509 | if unexpected_missing or unexpected_keys: |
| 4510 | raise RuntimeError("Checkpoint module state did not match the model outside AutoEP expert " |
| 4511 | f"parameters: missing={sorted(unexpected_missing)}, " |
| 4512 | f"unexpected={sorted(unexpected_keys)}") |
| 4513 | |
| 4514 | if checkpoint.get(FROZEN_PARAM_FRAGMENTS, None) is not None: |
| 4515 | saved_frozen_params = checkpoint[FROZEN_PARAM_FRAGMENTS] |
| 4516 | for param in self.module.parameters(): |
| 4517 | if is_optimized_parameter(param): |
| 4518 | continue |
| 4519 | if param not in self.param_names: |
| 4520 | raise ValueError(f"failed to find frozen {param} in named params") |
| 4521 | name = self.param_names[param] |
| 4522 | if hasattr(param, 'ds_id'): |
| 4523 | param.ds_tensor.data.copy_(saved_frozen_params[name].data) |
| 4524 | else: |
| 4525 | param.data.copy_(saved_frozen_params[name].data) |
| 4526 | |
| 4527 | def _get_zero_ckpt_prefix(self, dp_rank, bf16_mode): |
| 4528 | return f'{"bf16_" if bf16_mode else ""}zero_pp_rank_{dp_rank}' |