1+ #
12# GGUF file reading/modification support. For API usage information,
23# please see the files scripts/ for some fairly simple examples.
34#
1415
1516from .quants import quant_shape_to_byte_shape
1617
17-
1818if __name__ == "__main__" :
1919 from pathlib import Path
2020
@@ -134,12 +134,12 @@ def __init__(self, path: os.PathLike[str] | str, mode: Literal['r', 'r+', 'c'] =
134134 offs = 0
135135
136136 # Check for GGUF magic
137- if self ._get (offs , np .dtype ( np . uint32 ) , override_order = '<' )[0 ] != GGUF_MAGIC :
137+ if self ._get (offs , np .uint32 , override_order = '<' )[0 ] != GGUF_MAGIC :
138138 raise ValueError ('GGUF magic invalid' )
139139 offs += 4
140140
141141 # Check GGUF version
142- temp_version = self ._get (offs , np .dtype ( np . uint32 ) )
142+ temp_version = self ._get (offs , np .uint32 )
143143 if temp_version [0 ] & 65535 == 0 :
144144 # If we get 0 here that means it's (probably) a GGUF file created for
145145 # the opposite byte order of the machine this script is running on.
@@ -162,7 +162,7 @@ def __init__(self, path: os.PathLike[str] | str, mode: Literal['r', 'r+', 'c'] =
162162 offs += self ._push_field (ReaderField (offs , 'GGUF.version' , [temp_version ], [0 ], [GGUFValueType .UINT32 ]))
163163
164164 # Check tensor count and kv count
165- temp_counts = self ._get (offs , np .dtype ( np . uint64 ) , 2 )
165+ temp_counts = self ._get (offs , np .uint64 , 2 )
166166 offs += self ._push_field (ReaderField (offs , 'GGUF.tensor_count' , [temp_counts [:1 ]], [0 ], [GGUFValueType .UINT64 ]))
167167 offs += self ._push_field (ReaderField (offs , 'GGUF.kv_count' , [temp_counts [1 :]], [0 ], [GGUFValueType .UINT64 ]))
168168 tensor_count , kv_count = temp_counts
@@ -193,7 +193,7 @@ def get_tensor(self, idx: int) -> ReaderTensor:
193193
194194 def _get (
195195 self , offset : int , dtype : npt .DTypeLike , count : int = 1 , override_order : None | Literal ['I' , 'S' , '<' ] = None ,
196- ) -> np . ndarray :
196+ ) -> npt . NDArray [ Any ] :
197197 count = int (count )
198198 itemsize = int (np .empty ([], dtype = dtype ).itemsize )
199199 end_offs = offset + itemsize * count
@@ -212,8 +212,8 @@ def _push_field(self, field: ReaderField, skip_sum: bool = False) -> int:
212212 return 0 if skip_sum else sum (int (part .nbytes ) for part in field .parts )
213213
214214 def _get_str (self , offset : int ) -> tuple [npt .NDArray [np .uint64 ], npt .NDArray [np .uint8 ]]:
215- slen = self ._get (offset , np .dtype ( np . uint64 ) )
216- return slen , self ._get (offset + 8 , np .dtype ( np . uint8 ) , slen [0 ]. item () )
215+ slen = self ._get (offset , np .uint64 )
216+ return slen , self ._get (offset + 8 , np .uint8 , slen [0 ])
217217
218218 def _get_field_parts (
219219 self , orig_offs : int , raw_type : int ,
@@ -230,19 +230,19 @@ def _get_field_parts(
230230 # Check if it's a simple scalar type.
231231 nptype = self .gguf_scalar_to_np .get (gtype )
232232 if nptype is not None :
233- val = self ._get (offs , np . dtype ( nptype ) )
233+ val = self ._get (offs , nptype )
234234 return int (val .nbytes ), [val ], [0 ], types
235235 # Handle arrays.
236236 if gtype == GGUFValueType .ARRAY :
237- raw_itype = self ._get (offs , np .dtype ( np . uint32 ) )
237+ raw_itype = self ._get (offs , np .uint32 )
238238 offs += int (raw_itype .nbytes )
239- alen = self ._get (offs , np .dtype ( np . uint64 ) )
239+ alen = self ._get (offs , np .uint64 )
240240 offs += int (alen .nbytes )
241241 aparts : list [npt .NDArray [Any ]] = [raw_itype , alen ]
242242 data_idxs : list [int ] = []
243243 # FIXME: Handle multi-dimensional arrays properly instead of flattening
244244 for idx in range (alen [0 ]):
245- curr_size , curr_parts , curr_idxs , curr_types = self ._get_field_parts (offs , raw_itype [0 ]. item () )
245+ curr_size , curr_parts , curr_idxs , curr_types = self ._get_field_parts (offs , raw_itype [0 ])
246246 if idx == 0 :
247247 types += curr_types
248248 idxs_offs = len (aparts )
@@ -261,19 +261,19 @@ def _get_tensor_info_field(self, orig_offs: int) -> ReaderField:
261261 offs += int (name_len .nbytes + name_data .nbytes )
262262
263263 # Get Tensor Dimensions Count
264- n_dims = self ._get (offs , np .dtype ( np . uint32 ) )
264+ n_dims = self ._get (offs , np .uint32 )
265265 offs += int (n_dims .nbytes )
266266
267267 # Get Tensor Dimension Array
268- dims = self ._get (offs , np .dtype ( np . uint64 ) , n_dims [0 ]. item () )
268+ dims = self ._get (offs , np .uint64 , n_dims [0 ])
269269 offs += int (dims .nbytes )
270270
271271 # Get Tensor Encoding Scheme Type
272- raw_dtype = self ._get (offs , np .dtype ( np . uint32 ) )
272+ raw_dtype = self ._get (offs , np .uint32 )
273273 offs += int (raw_dtype .nbytes )
274274
275275 # Get Tensor Offset
276- offset_tensor = self ._get (offs , np .dtype ( np . uint64 ) )
276+ offset_tensor = self ._get (offs , np .uint64 )
277277 offs += int (offset_tensor .nbytes )
278278
279279 return ReaderField (
@@ -288,11 +288,11 @@ def _build_fields(self, offs: int, count: int) -> int:
288288 orig_offs = offs
289289 kv_klen , kv_kdata = self ._get_str (offs )
290290 offs += int (kv_klen .nbytes + kv_kdata .nbytes )
291- raw_kv_type = self ._get (offs , np .dtype ( np . uint32 ) )
291+ raw_kv_type = self ._get (offs , np .uint32 )
292292 offs += int (raw_kv_type .nbytes )
293293 parts : list [npt .NDArray [Any ]] = [kv_klen , kv_kdata , raw_kv_type ]
294294 idxs_offs = len (parts )
295- field_size , field_parts , field_idxs , field_types = self ._get_field_parts (offs , raw_kv_type [0 ]. item () )
295+ field_size , field_parts , field_idxs , field_types = self ._get_field_parts (offs , raw_kv_type [0 ])
296296 parts += field_parts
297297 self ._push_field (ReaderField (
298298 orig_offs ,
@@ -331,28 +331,28 @@ def _build_tensors(self, start_offs: int, fields: list[ReaderField]) -> None:
331331 item_type : npt .DTypeLike
332332 if ggml_type == GGMLQuantizationType .F16 :
333333 item_count = n_elems
334- item_type = np .dtype ( np . float16 )
334+ item_type = np .float16
335335 elif ggml_type == GGMLQuantizationType .F32 :
336336 item_count = n_elems
337- item_type = np .dtype ( np . float32 )
337+ item_type = np .float32
338338 elif ggml_type == GGMLQuantizationType .F64 :
339339 item_count = n_elems
340- item_type = np .dtype ( np . float64 )
340+ item_type = np .float64
341341 elif ggml_type == GGMLQuantizationType .I8 :
342342 item_count = n_elems
343- item_type = np .dtype ( np . int8 )
343+ item_type = np .int8
344344 elif ggml_type == GGMLQuantizationType .I16 :
345345 item_count = n_elems
346- item_type = np .dtype ( np . int16 )
346+ item_type = np .int16
347347 elif ggml_type == GGMLQuantizationType .I32 :
348348 item_count = n_elems
349- item_type = np .dtype ( np . int32 )
349+ item_type = np .int32
350350 elif ggml_type == GGMLQuantizationType .I64 :
351351 item_count = n_elems
352- item_type = np .dtype ( np . int64 )
352+ item_type = np .int64
353353 else :
354354 item_count = n_bytes
355- item_type = np .dtype ( np . uint8 )
355+ item_type = np .uint8
356356 np_dims = quant_shape_to_byte_shape (np_dims , ggml_type )
357357 tensors .append (ReaderTensor (
358358 name = tensor_name ,
0 commit comments