1
+ #
1
2
# GGUF file reading/modification support. For API usage information,
2
3
# please see the files scripts/ for some fairly simple examples.
3
4
#
14
15
15
16
from .quants import quant_shape_to_byte_shape
16
17
17
-
18
18
if __name__ == "__main__" :
19
19
from pathlib import Path
20
20
@@ -134,12 +134,12 @@ def __init__(self, path: os.PathLike[str] | str, mode: Literal['r', 'r+', 'c'] =
134
134
offs = 0
135
135
136
136
# 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 :
138
138
raise ValueError ('GGUF magic invalid' )
139
139
offs += 4
140
140
141
141
# Check GGUF version
142
- temp_version = self ._get (offs , np .dtype ( np . uint32 ) )
142
+ temp_version = self ._get (offs , np .uint32 )
143
143
if temp_version [0 ] & 65535 == 0 :
144
144
# If we get 0 here that means it's (probably) a GGUF file created for
145
145
# 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'] =
162
162
offs += self ._push_field (ReaderField (offs , 'GGUF.version' , [temp_version ], [0 ], [GGUFValueType .UINT32 ]))
163
163
164
164
# 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 )
166
166
offs += self ._push_field (ReaderField (offs , 'GGUF.tensor_count' , [temp_counts [:1 ]], [0 ], [GGUFValueType .UINT64 ]))
167
167
offs += self ._push_field (ReaderField (offs , 'GGUF.kv_count' , [temp_counts [1 :]], [0 ], [GGUFValueType .UINT64 ]))
168
168
tensor_count , kv_count = temp_counts
@@ -193,7 +193,7 @@ def get_tensor(self, idx: int) -> ReaderTensor:
193
193
194
194
def _get (
195
195
self , offset : int , dtype : npt .DTypeLike , count : int = 1 , override_order : None | Literal ['I' , 'S' , '<' ] = None ,
196
- ) -> np . ndarray :
196
+ ) -> npt . NDArray [ Any ] :
197
197
count = int (count )
198
198
itemsize = int (np .empty ([], dtype = dtype ).itemsize )
199
199
end_offs = offset + itemsize * count
@@ -212,8 +212,8 @@ def _push_field(self, field: ReaderField, skip_sum: bool = False) -> int:
212
212
return 0 if skip_sum else sum (int (part .nbytes ) for part in field .parts )
213
213
214
214
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 )
8000
td>
216
+ return slen , self ._get (offset + 8 , np .uint8 , slen [0 ])
217
217
218
218
def _get_field_parts (
219
219
self , orig_offs : int , raw_type : int ,
@@ -230,19 +230,19 @@ def _get_field_parts(
230
230
# Check if it's a simple scalar type.
231
231
nptype = self .gguf_scalar_to_np .get (gtype )
232
232
if nptype is not None :
233
- val = self ._get (offs , np . dtype ( nptype ) )
233
+ val = self ._get (offs , nptype )
234
234
return int (val .nbytes ), [val ], [0 ], types
235
235
# Handle arrays.
236
236
if gtype == GGUFValueType .ARRAY :
237
- raw_itype = self ._get (offs , np .dtype ( np . uint32 ) )
237
+ raw_itype = self ._get (offs , np .uint32 )
238
238
offs += int (raw_itype .nbytes )
239
- alen = self ._get (offs , np .dtype ( np . uint64 ) )
239
+ alen = self ._get (offs , np .uint64 )
240
240
offs += int (alen .nbytes )
241
241
aparts : list [npt .NDArray [Any ]] = [raw_itype , alen ]
242
242
data_idxs : list [int ] = []
243
243
# FIXME: Handle multi-dimensional arrays properly instead of flattening
244
244
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 ])
246
246
if idx == 0 :
247
247
types += curr_types
248
248
idxs_offs = len (aparts )
@@ -261,19 +261,19 @@ def _get_tensor_info_field(self, orig_offs: int) -> ReaderField:
261
261
offs += int (name_len .nbytes + name_data .nbytes )
262
262
263
263
# Get Tensor Dimensions Count
264
- n_dims = self ._get (offs , np .dtype ( np . uint32 ) )
264
+ n_dims = self ._get (offs , np .uint32 )
265
265
offs += int (n_dims .nbytes )
266
266
267
267
# 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 ])
269
269
offs += int (dims .nbytes )
270
270
271
271
# Get Tensor Encoding Scheme Type
272
- raw_dtype = self ._get (offs , np .dtype ( np . uint32 ) )
272
+ raw_dtype = self ._get (offs , np .uint32 )
273
273
offs += int (raw_dtype .nbytes )
274
274
275
275
# Get Tensor Offset
276
- offset_tensor = self ._get (offs , np .dtype ( np . uint64 ) )
276
+ offset_tensor = self ._get (offs , np .uint64 )
277
277
offs += int (offset_tensor .nbytes )
278
278
279
279
return ReaderField (
@@ -288,11 +288,11 @@ def _build_fields(self, offs: int, count: int) -> int:
288
288
orig_offs = offs
289
289
kv_klen , kv_kdata = self ._get_str (offs )
290
290
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 )
292
292
offs += int (raw_kv_type .nbytes )
293
293
parts : list [npt .NDArray [Any ]] = [kv_klen , kv_kdata , raw_kv_type ]
294
294
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 ])
296
296
parts += field_parts
297
297
self ._push_field (ReaderField (
298
298
orig_offs ,
@@ -331,28 +331,28 @@ def _build_tensors(self, start_offs: int, fields: list[ReaderField]) -> None:
331
331
item_type : npt .DTypeLike
332
332
if ggml_type == GGMLQuantizationType .F16 :
333
333
item_count = n_elems
334
- item_type = np .dtype ( np . float16 )
334
+ item_type = np .float16
335
335
elif ggml_type == GGMLQuantizationType .F32 :
336
336
item_count = n_elems
337
- item_type = np .dtype ( np . float32 )
337
+ item_type = np .float32
338
338
elif ggml_type == GGMLQuantizationType .F64 :
339
339
item_count = n_elems
340
- item_type = np .dtype ( np . float64 )
340
+ item_type = np .float64
341
341
elif ggml_type == GGMLQuantizationType .I8 :
342
342
item_count = n_elems
343
- item_type = np .dtype ( np . int8 )
343
+ item_type = np .int8
344
344
elif ggml_type == GGMLQuantizationType .I16 :
345
345
item_count = n_elems
346
- item_type = np .dtype ( np . int16 )
346
+ item_type = np .int16
347
347
elif ggml_type == GGMLQuantizationType .I32 :
348
348
item_count = n_elems
349
- item_type = np .dtype ( np . int32 )
349
+ item_type = np .int32
350
350
elif ggml_type == GGMLQuantizationType .I64 :
351
351
item_count = n_elems
352
- item_type = np .dtype ( np . int64 )
352
+ item_type = np .int64
353
353
else :
354
354
item_count = n_bytes
355
- item_type = np .dtype ( np . uint8 )
355
+ item_type = np .uint8
356
356
np_dims = quant_shape_to_byte_shape (np_dims , ggml_type )
357
357
tensors .append (ReaderTensor (
358
358
name = tensor_name ,
0 commit comments