Skip to content

Commit 63c87b0

Browse files
compiladeNexesenex
authored andcommitted
convert : identify missing model files (ggml-org#9397)
1 parent 1805a54 commit 63c87b0

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
@@ -131,19 +131,22 @@ def set_vocab(self):
131131
def get_tensors(self) -> Iterator[tuple[str, Tensor]]:
132132
tensor_names_from_parts: set[str] = set()
133133

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

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

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

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

0 commit comments

Comments
 (0)