MCPcopy Create free account
hub / github.com/modelscope/modelscope / _split_checkpoint

Function _split_checkpoint

modelscope/utils/megatron_utils.py:134–150  ·  view source on GitHub ↗
(model: nn.Module, checkpoint_dir: Union[str, bytes,
                                                              os.PathLike],
                      num_partitions: int)

Source from the content-addressed store, hash-verified

132
133
134def _split_checkpoint(model: nn.Module, checkpoint_dir: Union[str, bytes,
135 os.PathLike],
136 num_partitions: int) -> Dict[str, torch.Tensor]:
137 target_rank = int(os.getenv('RANK'))
138 origin_rank = target_rank // num_partitions
139 state_dict = _load_by_rank(checkpoint_dir, origin_rank)
140
141 target_state_dict = {}
142 for name, parameter in model.named_parameters():
143 dim = _get_diff_dim(parameter, state_dict[name])
144 if dim == -1:
145 target_state_dict[name] = state_dict[name]
146 continue
147 partitions_list = _split_tensor(state_dict[name], num_partitions, dim)
148 target_state_dict[name] = partitions_list[target_rank
149 % num_partitions].clone()
150 return target_state_dict
151
152
153def _merge_checkpoint(model: nn.Module, checkpoint_dir: Union[str, bytes,

Callers 1

Calls 5

_load_by_rankFunction · 0.85
_get_diff_dimFunction · 0.85
_split_tensorFunction · 0.85
named_parametersMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…