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

Function _merge_checkpoint

modelscope/utils/megatron_utils.py:153–173  ·  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

151
152
153def _merge_checkpoint(model: nn.Module, checkpoint_dir: Union[str, bytes,
154 os.PathLike],
155 num_partitions: int) -> Dict[str, torch.Tensor]:
156 target_rank = int(os.getenv('RANK'))
157 origin_rank_list = [
158 target_rank * num_partitions + i for i in range(num_partitions)
159 ]
160 state_dict_list = [
161 _load_by_rank(checkpoint_dir, i) for i in origin_rank_list
162 ]
163
164 target_state_dict = {}
165 for name, parameter in model.named_parameters():
166 dim = _get_diff_dim(parameter, state_dict_list[0][name])
167 if dim == -1:
168 target_state_dict[name] = state_dict_list[0][name]
169 continue
170 target_state_dict[name] = torch.cat(
171 [state_dict[name] for state_dict in state_dict_list],
172 dim=dim).clone()
173 return target_state_dict
174
175
176def _save_converted_checkpoint(

Callers 1

Calls 5

_load_by_rankFunction · 0.85
_get_diff_dimFunction · 0.85
named_parametersMethod · 0.45
cloneMethod · 0.45
catMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…