Skip to content

Commit 1c15138

Browse files
committed
feat(py): Allow gguf duplicate keys if they match by value and type
This is helpful for hybrid models that want to do gguf param setting by calling multiple parent classes without needing to make those parent classes try/except on every attempt to set a gguf value. Branch: GraniteFour Signed-off-by: Gabe Goodhart <[email protected]>
1 parent 8f40c57 commit 1c15138

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

gguf-py/gguf/gguf_writer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,14 @@ def write_ti_data_to_file(self) -> None:
270270
self.state = WriterState.TI_DATA
271271

272272
def add_key_value(self, key: str, val: Any, vtype: GGUFValueType, sub_type: GGUFValueType | None = None) -> None:
273-
if any(key in kv_data for kv_data in self.kv_data):
273+
# Disallow duplicate keys if they differ by value or type
274+
if any(
275+
(
276+
key in kv_data and
277+
(kv_data[key].value != val or kv_data[key].type != vtype)
278+
)
279+
for kv_data in self.kv_data
280+
):
274281
raise ValueError(f'Duplicated key name {key!r}')
275282

276283
self.kv_data[0][key] = GGUFValue(value=val, type=vtype, sub_type=sub_type)

0 commit comments

Comments
 (0)