(files: list[str])
| 129 | |
| 130 | |
| 131 | def _load_checkpoint(files: list[str]) -> dict[str, torch.Tensor]: |
| 132 | class TPMeta(BaseModel): |
| 133 | concat_dim: int |
| 134 | size: int |
| 135 | |
| 136 | parameters: dict[str, torch.Tensor] = {} |
| 137 | parameter_metas: dict[str, ParameterMeta] = {} |
| 138 | tp_metas: dict[str, TPMeta] = {} |
| 139 | parameters_with_tp: dict[str, dict[int, torch.Tensor]] = {} |
| 140 | for file in files: |
| 141 | tp_rank, ret = _load_checkpoint_file(file) |
| 142 | for parameter_name, (meta, weight) in ret.items(): |
| 143 | if parameter_name not in parameters_with_tp: |
| 144 | parameters_with_tp[parameter_name] = {} |
| 145 | parameters_with_tp[parameter_name][tp_rank] = weight |
| 146 | if parameter_name not in tp_metas: |
| 147 | tp_metas[parameter_name] = TPMeta( |
| 148 | concat_dim=meta["tp_concat_dim"], |
| 149 | size=1, |
| 150 | ) |
| 151 | if parameter_name not in parameter_metas: |
| 152 | assert isinstance(meta["dtype"], torch.dtype), ( |
| 153 | f"meta {meta} dtype should be torch.dtype" |
| 154 | ) |
| 155 | assert isinstance(meta["shape"], torch.Size), ( |
| 156 | f"meta {meta} shape should be torch.Size" |
| 157 | ) |
| 158 | parameter_metas[parameter_name] = ParameterMeta( |
| 159 | name=parameter_name, |
| 160 | shape=meta["shape"], |
| 161 | dtype=meta["dtype"], |
| 162 | aligned_size=_align_size(meta["dtype"], meta["shape"]), |
| 163 | ) |
| 164 | tp_meta = tp_metas[parameter_name] |
| 165 | if tp_meta.concat_dim != -1: |
| 166 | tp_meta.size = max(tp_meta.size, tp_rank + 1) |
| 167 | for name, tp_meta in tp_metas.items(): |
| 168 | if tp_meta.concat_dim != -1: |
| 169 | shape = list(parameter_metas[name].shape) |
| 170 | shape[tp_meta.concat_dim] = shape[tp_meta.concat_dim] * tp_meta.size |
| 171 | parameter_metas[name] = ParameterMeta( |
| 172 | name=name, |
| 173 | shape=torch.Size(shape), |
| 174 | dtype=parameter_metas[name].dtype, |
| 175 | aligned_size=_align_size(parameter_metas[name].dtype, torch.Size(shape)), |
| 176 | ) |
| 177 | weights_in_cpu = [parameters_with_tp[name][key] for key in sorted(parameters_with_tp[name])] |
| 178 | # TODO: here concat is serial, which may be slow |
| 179 | # but since tp storage is not used in the future |
| 180 | # we ignore this performance issue for now |
| 181 | parameters[name] = _concat_tp_weights(weights_in_cpu, tp_meta.concat_dim, tp_meta.size) |
| 182 | for name, parameter in parameters.items(): |
| 183 | assert name in parameter_metas, f"parameter {name} not found in parameter_metas" |
| 184 | assert parameter_metas[name].shape == parameter.shape, ( |
| 185 | f"parameter {name} shape mismatch, {parameter_metas[name].shape} != {parameter.shape}" |
| 186 | ) |
| 187 | assert parameter_metas[name].dtype == parameter.dtype, ( |
| 188 | f"parameter {name} dtype mismatch, {parameter_metas[name].dtype} != {parameter.dtype}" |
no test coverage detected