Skip to content

Commit ed9f252

Browse files
authored
gguf-py : decouple adding metadata from writing in GGUFWriter (#7827)
Main changes of this PR is to consolidate GGUFWriter.add_key and GGUFWriter.add_val into GGUFWriter.add_key_value. In addition use_temp_file is now opt-in instead of opt-out defaulting to False. Also GGUFWriter now does not require output file name until when actually writing to it. And GGUFWriter doesn't really need to eagerly prepare the data layout of the metadata
1 parent fe1e391 commit ed9f252

File tree

3 files changed

+161
-125
lines changed

3 files changed

+161
-125
lines changed

convert-hf-to-gguf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Model:
4747
_model_classes: dict[str, type[Model]] = {}
4848

4949
dir_model: Path
50-
ftype: int
50+
ftype: gguf.LlamaFileType
5151
is_big_endian: bool
5252
endianess: gguf.GGUFEndian
5353
use_temp_file: bool
@@ -94,7 +94,7 @@ def __init__(self, dir_model: Path, ftype: gguf.LlamaFileType, fname_out: Path,
9494
ftype_lw: str = ftype_up.lower()
9595
# allow templating the file name with the output ftype, useful with the "auto" ftype
9696
self.fname_out = fname_out.parent / fname_out.name.format(ftype_lw, outtype=ftype_lw, ftype=ftype_lw, OUTTYPE=ftype_up, FTYPE=ftype_up)
97-
self.gguf_writer = gguf.GGUFWriter(self.fname_out, gguf.MODEL_ARCH_NAMES[self.model_arch], endianess=self.endianess, use_temp_file=self.use_temp_file)
97+
self.gguf_writer = gguf.GGUFWriter(path=None, arch=gguf.MODEL_ARCH_NAMES[self.model_arch], endianess=self.endianess, use_temp_file=self.use_temp_file)
9898

9999
@classmethod
100100
def __init_subclass__(cls):
@@ -324,13 +324,13 @@ def write_tensors(self):
324324

325325
def write(self):
326326
self.write_tensors()
327-
self.gguf_writer.write_header_to_file()
327+
self.gguf_writer.write_header_to_file(self.fname_out)
328328
self.gguf_writer.write_kv_data_to_file()
329329
self.gguf_writer.write_tensors_to_file(progress=True)
330330
self.gguf_writer.close()
331331

332332
def write_vocab(self):
333-
self.gguf_writer.write_header_to_file()
333+
self.gguf_writer.write_header_to_file(self.fname_out)
334334
self.gguf_writer.write_kv_data_to_file()
335335
self.gguf_writer.close()
336336

0 commit comments

Comments
 (0)