@@ -107,18 +107,24 @@ class GGUFInfoParser:
107
107
def is_model_gguf (model_path : str ) -> bool :
108
108
try :
109
109
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 )
111
111
return magic_number == GGUFModelInfo .MAGIC_NUMBER
112
112
except Exception as ex :
113
113
console .warning (f"Failed to read model '{ model_path } ': { ex } " )
114
114
return False
115
115
116
116
@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 :
118
120
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" )
122
128
123
129
@staticmethod
124
130
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
151
157
elif value_type == GGUFValueType .BOOL :
152
158
value = GGUFInfoParser .read_bool (model , model_endianness )
153
159
elif value_type == GGUFValueType .STRING :
154
- value = GGUFInfoParser .read_string (model )
160
+ value = GGUFInfoParser .read_string (model , model_endianness )
155
161
elif value_type == GGUFValueType .ARRAY :
156
162
array_type = GGUFInfoParser .read_value_type (model , model_endianness )
157
163
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
167
173
model_endianness = GGUFEndian .LITTLE
168
174
169
175
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 )
171
177
if magic_number != GGUFModelInfo .MAGIC_NUMBER :
172
178
raise ParseError (f"Invalid GGUF magic number '{ magic_number } '" )
173
179
@@ -185,13 +191,13 @@ def parse(model_name: str, model_registry: str, model_path: str) -> GGUFModelInf
185
191
186
192
metadata = {}
187
193
for _ in range (metadata_kv_count ):
188
- key = GGUFInfoParser .read_string (model )
194
+ key = GGUFInfoParser .read_string (model , model_endianness )
189
195
value_type = GGUFInfoParser .read_value_type (model , model_endianness )
190
196
metadata [key ] = GGUFInfoParser .read_value (model , value_type , model_endianness )
191
197
192
198
tensors : list [Tensor ] = []
193
199
for _ in range (tensor_count ):
194
- name = GGUFInfoParser .read_string (model )
200
+ name = GGUFInfoParser .read_string (model , model_endianness )
195
201
n_dimensions = GGUFInfoParser .read_number (model , GGUFValueType .UINT32 , model_endianness )
196
202
dimensions : list [int ] = []
197
203
for _ in range (n_dimensions ):
0 commit comments