(model: nn.Module, checkpoint_dir: Union[str, bytes,
os.PathLike],
num_partitions: int)
| 151 | |
| 152 | |
| 153 | def _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 | |
| 176 | def _save_converted_checkpoint( |
no test coverage detected
searching dependent graphs…