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