mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2024-12-28 12:24:35 +00:00
gguf : write tensors one by one and code reuse
This commit is contained in:
parent
aa99562d70
commit
93f7f7aef7
8
gguf.py
8
gguf.py
@ -61,7 +61,6 @@ class GGUFWriter:
|
|||||||
def __init__(self, fout: IO):
|
def __init__(self, fout: IO):
|
||||||
self.fout = fout
|
self.fout = fout
|
||||||
self.offset_tensor = 0
|
self.offset_tensor = 0
|
||||||
self.tensors: List[np.ndarray] = []
|
|
||||||
|
|
||||||
def write_header(self, tensor_count: int, metadata_kv_count: int):
|
def write_header(self, tensor_count: int, metadata_kv_count: int):
|
||||||
self.fout.write(struct.pack("<I", constants.GGUF_MAGIC))
|
self.fout.write(struct.pack("<I", constants.GGUF_MAGIC))
|
||||||
@ -75,9 +74,7 @@ class GGUFWriter:
|
|||||||
return cls(f)
|
return cls(f)
|
||||||
|
|
||||||
def write_key(self, key: str):
|
def write_key(self, key: str):
|
||||||
encoded_key = key.encode("utf8")
|
self.write_val(key, GGUFValueType.STRING, write_vtype=False)
|
||||||
self.fout.write(struct.pack("<I", len(encoded_key)))
|
|
||||||
self.fout.write(encoded_key)
|
|
||||||
|
|
||||||
def write_uint8(self, key: str, val: int):
|
def write_uint8(self, key: str, val: int):
|
||||||
self.write_key(key)
|
self.write_key(key)
|
||||||
@ -179,12 +176,11 @@ class GGUFWriter:
|
|||||||
|
|
||||||
self.tensors.append(tensor)
|
self.tensors.append(tensor)
|
||||||
|
|
||||||
def write_tensors(self):
|
def write_tensor(self, tensor: np.ndarray):
|
||||||
pad = GGUFWriter.ggml_pad(self.fout.tell(), constants.GGUF_DEFAULT_ALIGNMENT) - self.fout.tell()
|
pad = GGUFWriter.ggml_pad(self.fout.tell(), constants.GGUF_DEFAULT_ALIGNMENT) - self.fout.tell()
|
||||||
if pad != 0:
|
if pad != 0:
|
||||||
self.fout.write(bytes([0] * pad))
|
self.fout.write(bytes([0] * pad))
|
||||||
|
|
||||||
for tensor in self.tensors:
|
|
||||||
tensor.tofile(self.fout)
|
tensor.tofile(self.fout)
|
||||||
pad = GGUFWriter.ggml_pad(tensor.nbytes, constants.GGUF_DEFAULT_ALIGNMENT) - tensor.nbytes
|
pad = GGUFWriter.ggml_pad(tensor.nbytes, constants.GGUF_DEFAULT_ALIGNMENT) - tensor.nbytes
|
||||||
if pad != 0:
|
if pad != 0:
|
||||||
|
Loading…
Reference in New Issue
Block a user