mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-10-31 08:51:55 +00:00 
			
		
		
		
	gguf : write tensors one by one and code reuse
This commit is contained in:
		
							
								
								
									
										16
									
								
								gguf.py
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								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,16 +176,15 @@ 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: |             self.fout.write(bytes([0] * pad)) | ||||||
|                 self.fout.write(bytes([0] * pad)) |  | ||||||
|  |  | ||||||
|     def flush(self): |     def flush(self): | ||||||
|         self.fout.flush() |         self.fout.flush() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 M. Yusuf Sarıgöz
					M. Yusuf Sarıgöz