MCPcopy Create free account
hub / github.com/Alpha-VLLM/LLaMA2-Accessory / load_diff_checkpoint

Function load_diff_checkpoint

accessory/util/tensor_parallel.py:387–422  ·  view source on GitHub ↗

r"""This method loads model from a *_diff format checkpoint. The behavior of loading a diff checkpoint is different from loading a regular checkpoint: In case a key is in the given ``existing_keys``, the new value of the tensor is the value in the state_dict plus the old value. Note

(
    model: nn.Module, state_dict: Dict[str, torch.Tensor],
    existing_keys: Set[str],
)

Source from the content-addressed store, hash-verified

385
386
387def load_diff_checkpoint(
388 model: nn.Module, state_dict: Dict[str, torch.Tensor],
389 existing_keys: Set[str],
390) -> Tuple[List[str], List[str]]:
391 r"""This method loads model from a *_diff format checkpoint. The behavior
392 of loading a diff checkpoint is different from loading a regular
393 checkpoint: In case a key is in the given ``existing_keys``, the new value
394 of the tensor is the value in the state_dict plus the old value.
395
396 Note:
397 The input ``state_dict`` will be changed in-place to save memory.
398
399 Args:
400 model (nn.Module): The model to load the state dict into.
401 state_dict (Dict[str, torch.Tensor]): The state dict to be loaded into
402 the model.
403 existing_keys (Set[str]): A set of keys that have appeared in the
404 previous checkpoints. If a key is in this set, the corresponding
405 value from the state dict will be added to the value in the model;
406 otherwise the value in the model is considered uninitialized and
407 is directly set to the value in the state dict.
408
409 Returns:
410 Tuple[List[str], List[str]]: A pair of lists including missing keys and
411 unexpected keys, following the regular
412 ``torch.nn.Module.load_stat_dict``.
413 """
414 model_state_dict = model.state_dict()
415 for key in list(state_dict.keys()):
416 if key in existing_keys and key in model_state_dict:
417 orig_value = model_state_dict[key]
418 diff_value = state_dict[key]
419 orig_value = orig_value.to(diff_value.device)
420 diff_value = diff_value.to(orig_value.dtype)
421 state_dict[key] = orig_value + diff_value
422 return model.load_state_dict(state_dict, strict=False)
423
424
425def load_tensor_parallel_model_list(

Callers 1

Calls 2

state_dictMethod · 0.80
load_state_dictMethod · 0.45

Tested by

no test coverage detected