Skip to content

Commit b6cc604

Browse files
committed
fix(gguf_parser): fix memoryerror exception when loading non-native
Signed-off-by: Aaron Teo <[email protected]> fix(gguf_parser): missed some calls Signed-off-by: Aaron Teo <[email protected]> fix(gguf_parser): typo `return` vs `raise` Signed-off-by: Aaron Teo <[email protected]>
1 parent 8a604e3 commit b6cc604

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

ramalama/gguf_parser.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,24 @@ class GGUFInfoParser:
107107
def is_model_gguf(model_path: str) -> bool:
108108
try:
109109
with open(model_path, "rb") as model_file:
110-
magic_number = GGUFInfoParser.read_string(model_file, 4)
110+
magic_number = GGUFInfoParser.read_string(model_file, GGUFEndian.LITTLE, 4)
111111
return magic_number == GGUFModelInfo.MAGIC_NUMBER
112112
except Exception as ex:
113113
console.warning(f"Failed to read model '{model_path}': {ex}")
114114
return False
115115

116116
@staticmethod
117-
def read_string(model: io.BufferedReader, length: int = -1) -> str:
117+
def read_string(
118+
model: io.BufferedReader, model_endianness: GGUFEndian = GGUFEndian.LITTLE, length: int = -1
119+
) -> str:
118120
if length == -1:
119-
type_string = GGUF_VALUE_TYPE_FORMAT[GGUFValueType.UINT64]
120-
length = struct.unpack(type_string, model.read(struct.calcsize(type_string)))[0]
121-
return model.read(length).decode("utf-8")
121+
length = GGUFInfoParser.read_number(model, GGUFValueType.UINT64, model_endianness)
122+
123+
raw = model.read(length)
124+
if len(raw) < length:
125+
raise ParseError(f"Unexpected EOF: wanted {length} bytes, got {len(raw)}")
126+
127+
return raw.decode("utf-8")
122128

123129
@staticmethod
124130
def read_number(model: io.BufferedReader, value_type: GGUFValueType, model_endianness: GGUFEndian) -> float:
@@ -151,7 +157,7 @@ def read_value(model: io.BufferedReader, value_type: GGUFValueType, model_endian
151157
elif value_type == GGUFValueType.BOOL:
152158
value = GGUFInfoParser.read_bool(model, model_endianness)
153159
elif value_type == GGUFValueType.STRING:
154-
value = GGUFInfoParser.read_string(model)
160+
value = GGUFInfoParser.read_string(model, model_endianness)
155161
elif value_type == GGUFValueType.ARRAY:
156162
array_type = GGUFInfoParser.read_value_type(model, model_endianness)
157163
array_length = GGUFInfoParser.read_number(model, GGUFValueType.UINT64, model_endianness)
@@ -167,7 +173,7 @@ def parse(model_name: str, model_registry: str, model_path: str) -> GGUFModelInf
167173
model_endianness = GGUFEndian.LITTLE
168174

169175
with open(model_path, "rb") as model:
170-
magic_number = GGUFInfoParser.read_string(model, 4)
176+
magic_number = GGUFInfoParser.read_string(model, model_endianness, 4)
171177
if magic_number != GGUFModelInfo.MAGIC_NUMBER:
172178
raise ParseError(f"Invalid GGUF magic number '{magic_number}'")
173179

@@ -185,13 +191,13 @@ def parse(model_name: str, model_registry: str, model_path: str) -> GGUFModelInf
185191

186192
metadata = {}
187193
for _ in range(metadata_kv_count):
188-
key = GGUFInfoParser.read_string(model)
194+
key = GGUFInfoParser.read_string(model, model_endianness)
189195
value_type = GGUFInfoParser.read_value_type(model, model_endianness)
190196
metadata[key] = GGUFInfoParser.read_value(model, value_type, model_endianness)
191197

192198
tensors: list[Tensor] = []
193199
for _ in range(tensor_count):
194-
name = GGUFInfoParser.read_string(model)
200+
name = GGUFInfoParser.read_string(model, model_endianness)
195201
n_dimensions = GGUFInfoParser.read_number(model, GGUFValueType.UINT32, model_endianness)
196202
dimensions: list[int] = []
197203
for _ in range(n_dimensions):

0 commit comments

Comments
 (0)