Skip to content

Commit a7653e4

Browse files
compiladearthw
authored andcommitted
convert : identify missing model files (ggml-org#9397)
1 parent 60d53a9 commit a7653e4

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

convert_hf_to_gguf.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,22 @@ def set_vocab(self):
132132
def get_tensors(self) -> Iterator[tuple[str, Tensor]]:
133133
tensor_names_from_parts: set[str] = set()
134134

135-
if len(self.part_names) > 1:
135+
index_name = "model.safetensors" if self.is_safetensors else "pytorch_model.bin"
136+
index_name += ".index.json"
137+
index_file = self.dir_model / index_name
138+
139+
if index_file.is_file():
136140
self.tensor_names = set()
137-
index_name = "model.safetensors" if self.is_safetensors else "pytorch_model.bin"
138-
index_name += ".index.json"
139141
logger.info(f"gguf: loading model weight map from '{index_name}'")
140-
with open(self.dir_model / index_name, "r", encoding="utf-8") as f:
142+
with open(index_file, "r", encoding="utf-8") as f:
141143
index: dict[str, Any] = json.load(f)
142144
weight_map = index.get("weight_map")
143145
if weight_map is None or not isinstance(weight_map, dict):
144146
raise ValueError(f"Can't load 'weight_map' from {index_name!r}")
145147
self.tensor_names.update(weight_map.keys())
146148
else:
147149
self.tensor_names = tensor_names_from_parts
150+
weight_map = {}
148151

149152
for part_name in self.part_names:
150153
logger.info(f"gguf: loading model part '{part_name}'")
@@ -171,9 +174,17 @@ def get_tensors(self) -> Iterator[tuple[str, Tensor]]:
171174
data = LazyTorchTensor.from_eager(data)
172175
yield name, data
173176

174-
# only verify tensor name presence; it doesn't matter if they are not in the right files
175-
if len(sym_diff := tensor_names_from_parts.symmetric_difference(self.tensor_names)) > 0:
176-
raise ValueError(f"Mismatch between weight map and model parts for tensor names: {sym_diff}")
177+
# verify tensor name presence and identify potentially missing files
178+
if len(tensor_names_from_parts.symmetric_difference(self.tensor_names)) > 0:
179+
missing = sorted(self.tensor_names.difference(tensor_names_from_parts))
180+
extra = sorted(tensor_names_from_parts.difference(self.tensor_names))
181+
missing_files = sorted(set(weight_map[n] for n in missing if n in weight_map))
182+
if len(extra) == 0 and len(missing_files) > 0:
183+
raise ValueError(f"Missing or incomplete model files: {missing_files}")
184+
else:
185+
raise ValueError("Mismatch between weight map and model parts for tensor names:\n"
186+
f"Missing tensors: {missing}\n"
187+
f"Extra tensors: {extra}")
177188

178189
def format_tensor_name(self, key: gguf.MODEL_TENSOR, bid: int | None = None, suffix: str = ".weight") -> str:
179190
if key not in gguf.MODEL_TENSORS[self.model_arch]:

0 commit comments

Comments
 (0)