Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Several types in numpy 2.2+ are defined as typeAliases with no underl…
…ying class (such as np.float32)

looking at microsoft/pyright#9051, they declined to fix it themselves, and suggested instead that the used must add a # pyright: ignore or # type: ignore directive to suppress this error.

Numpy is working to resolve them: numpy/numpy#28076 and has already done so with npfloat64 (which I can verify in our errors) -- see numpy/numpy#27957 .
  • Loading branch information
robbiemu committed May 16, 2025
commit 822c83cc780821f9eea62aef2bf465ed88a3d121
36 changes: 19 additions & 17 deletions gguf-py/gguf/gguf_reader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pyright: reportInvalidTypeForm=false
#
# GGUF file reading/modification support. For API usage information,
# please see the files scripts/ for some fairly simple examples.
Expand All @@ -15,6 +16,7 @@

from .quants import quant_shape_to_byte_shape


if __name__ == "__main__":
from pathlib import Path

Expand Down Expand Up @@ -104,7 +106,7 @@ class ReaderTensor(NamedTuple):
n_elements: int
n_bytes: int
data_offset: int
data: npt.NDArray[Any]
data: np.ndarray
field: ReaderField


Expand Down Expand Up @@ -181,7 +183,7 @@ def __init__(self, path: os.PathLike[str] | str, mode: Literal['r', 'r+', 'c'] =
self.data_offset = offs
self._build_tensors(offs, tensors_fields)

_DT = TypeVar('_DT', bound = npt.DTypeLike)
_DT = TypeVar('_DT', bound = np.dtype[Any])

# Fetch a key/value metadata field by key.
def get_field(self, key: str) -> Union[ReaderField, None]:
Expand All @@ -192,8 +194,8 @@ def get_tensor(self, idx: int) -> ReaderTensor:
return self.tensors[idx]

def _get(
self, offset: int, dtype: npt.DTypeLike, count: int = 1, override_order: None | Literal['I', 'S', '<'] = None,
) -> npt.NDArray[Any]:
self, offset: int, dtype: np.dtype[Any], count: int = 1, override_order: None | Literal['I', 'S', '<'] = None,
) -> np.ndarray:
count = int(count)
itemsize = int(np.empty([], dtype = dtype).itemsize)
end_offs = offset + itemsize * count
Expand All @@ -213,7 +215,7 @@ def _push_field(self, field: ReaderField, skip_sum: bool = False) -> int:

def _get_str(self, offset: int) -> tuple[npt.NDArray[np.uint64], npt.NDArray[np.uint8]]:
slen = self._get(offset, np.uint64)
return slen, self._get(offset + 8, np.uint8, slen[0])
return slen, self._get(offset + 8, np.uint8, slen[0].item())

def _get_field_parts(
self, orig_offs: int, raw_type: int,
Expand All @@ -230,7 +232,7 @@ def _get_field_parts(
# Check if it's a simple scalar type.
nptype = self.gguf_scalar_to_np.get(gtype)
if nptype is not None:
val = self._get(offs, nptype)
val = self._get(offs, np.dtype(nptype))
return int(val.nbytes), [val], [0], types
# Handle arrays.
if gtype == GGUFValueType.ARRAY:
Expand All @@ -242,7 +244,7 @@ def _get_field_parts(
data_idxs: list[int] = []
# FIXME: Handle multi-dimensional arrays properly instead of flattening
for idx in range(alen[0]):
curr_size, curr_parts, curr_idxs, curr_types = self._get_field_parts(offs, raw_itype[0])
curr_size, curr_parts, curr_idxs, curr_types = self._get_field_parts(offs, raw_itype[0].item())
if idx == 0:
types += curr_types
idxs_offs = len(aparts)
Expand All @@ -265,7 +267,7 @@ def _get_tensor_info_field(self, orig_offs: int) -> ReaderField:
offs += int(n_dims.nbytes)

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

# Get Tensor Encoding Scheme Type
Expand All @@ -292,7 +294,7 @@ def _build_fields(self, offs: int, count: int) -> int:
offs += int(raw_kv_type.nbytes)
parts: list[npt.NDArray[Any]] = [kv_klen, kv_kdata, raw_kv_type]
idxs_offs = len(parts)
field_size, field_parts, field_idxs, field_types = self._get_field_parts(offs, raw_kv_type[0])
field_size, field_parts, field_idxs, field_types = self._get_field_parts(offs, raw_kv_type[0].item())
parts += field_parts
self._push_field(ReaderField(
orig_offs,
Expand Down Expand Up @@ -328,28 +330,28 @@ def _build_tensors(self, start_offs: int, fields: list[ReaderField]) -> None:
block_size, type_size = GGML_QUANT_SIZES[ggml_type]
n_bytes = n_elems * type_size // block_size
data_offs = int(start_offs + offset_tensor[0])
item_type: npt.DTypeLike
item_type: np.dtype[Any]
if ggml_type == GGMLQuantizationType.F16:
item_count = n_elems
item_type = np.float16
item_type = np.dtype(np.float16)
elif ggml_type == GGMLQuantizationType.F32:
item_count = n_elems
item_type = np.float32
item_type = np.dtype(np.float32)
elif ggml_type == GGMLQuantizationType.F64:
item_count = n_elems
item_type = np.float64
item_type = np.dtype(np.float64)
elif ggml_type == GGMLQuantizationType.I8:
item_count = n_elems
item_type = np.int8
item_type = np.dtype(np.int8)
elif ggml_type == GGMLQuantizationType.I16:
item_count = n_elems
item_type = np.int16
item_type = np.dtype(np.int16)
elif ggml_type == GGMLQuantizationType.I32:
item_count = n_elems
item_type = np.int32
item_type = np.dtype(np.int32)
elif ggml_type == GGMLQuantizationType.I64:
item_count = n_elems
item_type = np.int64
item_type = np.dtype(np.int64)
else:
item_count = n_bytes
item_type = np.uint8
Expand Down
Loading