Skip to content

gguf-py: Improve GGUFReader read-only mode performance #10159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Changes from 1 commit
Commits
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
code format
  • Loading branch information
Isotr0py committed Nov 5, 2024
commit 6a13722ca5c07c0b812bdc872c14949d10544234
5 changes: 2 additions & 3 deletions gguf-py/gguf/gguf_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import logging
import os
import mmap
import struct
from collections import OrderedDict
from typing import Any, Literal, NamedTuple, TypeVar, Union
Expand Down Expand Up @@ -133,7 +132,7 @@ def __init__(self, path: os.PathLike[str] | str, mode: Literal['r', 'r+', 'c'] =
offs += self.alignment - padding
self.data_offset = offs
self._build_tensors(offs, tensors_fields)

def __del__(self) -> None:
self.data.close()

Expand Down Expand Up @@ -174,7 +173,7 @@ def _push_field(self, field: ReaderField, skip_sum: bool = False) -> int:
def _get_str(self, offset: int, return_size=False) -> tuple[npt.NDArray[np.uint64], npt.NDArray[np.uint8]]:
self.data.seek(offset)
slen = struct.unpack('<Q', self.data.read(8))
sdata = struct.unpack('<'+'B'*slen[0], self.data.read(1*slen[0]))
sdata = struct.unpack('<' + 'B' * slen[0], self.data.read(slen[0]))
output = (slen, sdata)
if return_size:
output += (8 + slen[0],)
Expand Down