Skip to content

Commit d49b004

Browse files
committed
Uses np.dtype for NumPy type hints in GGUF reader
Replaces uses of bare NumPy type hints (e.g., `np.uint32`) with `np.dtype(np.uint32)` in the GGUF reader. This aligns with changes in NumPy 2.0+ and ensures forward compatibility. Also applies this change to the quantize/dequantize functions in the quants module.
1 parent 6ee8253 commit d49b004

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

gguf-py/gguf/gguf_reader.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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.uint32, override_order = '<')[0] != GGUF_MAGIC:
137+
if self._get(offs, np.dtype(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.uint32)
142+
temp_version = self._get(offs, np.dtype(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.uint64, 2)
165+
temp_counts = self._get(offs, np.dtype(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
@@ -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.uint64)
216-
return slen, self._get(offset + 8, np.uint8, slen[0].item())
215+
slen = self._get(offset, np.dtype(np.uint64))
216+
return slen, self._get(offset + 8, np.dtype(np.uint8), slen[0].item())
217217

218218
def _get_field_parts(
219219
self, orig_offs: int, raw_type: int,
@@ -234,9 +234,9 @@ def _get_field_parts(
234234
return int(val.nbytes), [val], [0], types
235235
# Handle arrays.
236236
if gtype == GGUFValueType.ARRAY:
237-
raw_itype = self._get(offs, np.uint32)
237+
raw_itype = self._get(offs, np.dtype(np.uint32))
238238
offs += int(raw_itype.nbytes)
239-
alen = self._get(offs, np.uint64)
239+
alen = self._get(offs, np.dtype(np.uint64))
240240
offs += int(alen.nbytes)
241241
aparts: list[npt.NDArray[Any]] = [raw_itype, alen]
242242
data_idxs: list[int] = []
@@ -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.uint32)
264+
n_dims = self._get(offs, np.dtype(np.uint32))
265265
offs += int(n_dims.nbytes)
266266

267267
# Get Tensor Dimension Array
268-
dims = self._get(offs, np.uint64, n_dims[0].item())
268+
dims = self._get(offs, np.dtype(np.uint64), n_dims[0].item())
269269
offs += int(dims.nbytes)
270270

271271
# Get Tensor Encoding Scheme Type
272-
raw_dtype = self._get(offs, np.uint32)
272+
raw_dtype = self._get(offs, np.dtype(np.uint32))
273273
offs += int(raw_dtype.nbytes)
274274

275275
# Get Tensor Offset
276-
offset_tensor = self._get(offs, np.uint64)
276+
offset_tensor = self._get(offs, np.dtype(np.uint64))
277277
offs += int(offset_tensor.nbytes)
278278

279279
return ReaderField(
@@ -288,7 +288,7 @@ 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.uint32)
291+
raw_kv_type = self._get(offs, np.dtype(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)
@@ -352,7 +352,7 @@ def _build_tensors(self, start_offs: int, fields: list[ReaderField]) -> None:
352352
item_type = np.dtype(np.int64)
353353
else:
354354
item_count = n_bytes
355-
item_type = np.uint8
355+
item_type = np.dtype(np.uint8)
356356
np_dims = quant_shape_to_byte_shape(np_dims, ggml_type)
357357
tensors.append(ReaderTensor(
358358
name = tensor_name,

gguf-py/gguf/quants.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ def __init_subclass__(cls, qtype: GGMLQuantizationType) -> None:
9191
cls.block_size, cls.type_size = GGML_QUANT_SIZES[qtype]
9292
cls.__quantize_lazy = LazyNumpyTensor._wrap_fn(
9393
cls.__quantize_array,
94-
meta_noop=(np.uint8, cls.__shape_to_bytes)
94+
meta_noop=(np.dtype(np.uint8), cls.__shape_to_bytes)
9595
)
9696
cls.__dequantize_lazy = LazyNumpyTensor._wrap_fn(
9797
cls.__dequantize_array,
98-
meta_noop=(np.float32, cls.__shape_from_bytes)
98+
meta_noop=(np.dtype(np.float32), cls.__shape_from_bytes)
9999
)
100100
assert qtype not in _type_traits
101101
_type_traits[qtype] = cls
@@ -163,12 +163,12 @@ def __shape_from_bytes(cls, shape: Sequence[int]):
163163

164164
@classmethod
165165
def __quantize_array(cls, array: np.ndarray) -> np.ndarray:
166-
return _apply_over_grouped_rows(cls.quantize_rows, arr=array, otype=np.uint8, oshape=cls.__shape_to_bytes(array.shape))
166+
return _apply_over_grouped_rows(cls.quantize_rows, arr=array, otype=np.dtype(np.uint8), oshape=cls.__shape_to_bytes(array.shape))
167167

168168
@classmethod
169169
def __dequantize_array(cls, array: np.ndarray) -> np.ndarray:
170170
cls.init_grid()
171-
return _apply_over_grouped_rows(cls.dequantize_rows, arr=array, otype=np.float32, oshape=cls.__shape_from_bytes(array.shape))
171+
return _apply_over_grouped_rows(cls.dequantize_rows, arr=array, otype=np.dtype(np.float32), oshape=cls.__shape_from_bytes(array.shape))
172172

173173
@classmethod
174174
def __quantize_lazy(cls, lazy_tensor: LazyNumpyTensor, /) -> Any:

pyrightconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extraPaths": ["gguf-py"],
33
"pythonVersion": "3.10",
44
"pythonPlatform": "All",
5-
"reportInvalidTypeForm": false, // TODO: remove once numpy 2.2.x resolves the transition to their new type system
5+
"reportInvalidTypeForm": false,
66
"reportUnusedImport": "warning",
77
"reportDuplicateImport": "error",
88
"reportDeprecated": "warning",

0 commit comments

Comments
 (0)