@@ -49,6 +49,7 @@ class TensorInfo:
4949class GGUFValue :
5050 value : Any
5151 type : GGUFValueType
52+ sub_type : GGUFValueType | None = None
5253
5354
5455class WriterState (Enum ):
@@ -238,7 +239,7 @@ def write_kv_data_to_file(self) -> None:
238239
239240 for key , val in kv_data .items ():
240241 kv_bytes += self ._pack_val (key , GGUFValueType .STRING , add_vtype = False )
241- kv_bytes += self ._pack_val (val .value , val .type , add_vtype = True )
242+ kv_bytes += self ._pack_val (val .value , val .type , add_vtype = True , sub_type = val . sub_type )
242243
243244 fout .write (kv_bytes )
244245
@@ -268,11 +269,11 @@ def write_ti_data_to_file(self) -> None:
268269 fout .flush ()
269270 self .state = WriterState .TI_DATA
270271
271- def add_key_value (self , key : str , val : Any , vtype : GGUFValueType ) -> None :
272+ def add_key_value (self , key : str , val : Any , vtype : GGUFValueType , sub_type : GGUFValueType | None = None ) -> None :
272273 if any (key in kv_data for kv_data in self .kv_data ):
273274 raise ValueError (f'Duplicated key name { key !r} ' )
274275
275- self .kv_data [0 ][key ] = GGUFValue (value = val , type = vtype )
276+ self .kv_data [0 ][key ] = GGUFValue (value = val , type = vtype , sub_type = sub_type )
276277
277278 def add_uint8 (self , key : str , val : int ) -> None :
278279 self .add_key_value (key ,val , GGUFValueType .UINT8 )
@@ -1022,7 +1023,7 @@ def _pack(self, fmt: str, value: Any, skip_pack_prefix: bool = False) -> bytes:
10221023 pack_prefix = '<' if self .endianess == GGUFEndian .LITTLE else '>'
10231024 return struct .pack (f'{ pack_prefix } { fmt } ' , value )
10241025
1025- def _pack_val (self , val : Any , vtype : GGUFValueType , add_vtype : bool ) -> bytes :
1026+ def _pack_val (self , val : Any , vtype : GGUFValueType , add_vtype : bool , sub_type : GGUFValueType | None = None ) -> bytes :
10261027 kv_data = bytearray ()
10271028
10281029 if add_vtype :
@@ -1043,7 +1044,9 @@ def _pack_val(self, val: Any, vtype: GGUFValueType, add_vtype: bool) -> bytes:
10431044 if len (val ) == 0 :
10441045 raise ValueError ("Invalid GGUF metadata array. Empty array" )
10451046
1046- if isinstance (val , bytes ):
1047+ if sub_type is not None :
1048+ ltype = sub_type
1049+ elif isinstance (val , bytes ):
10471050 ltype = GGUFValueType .UINT8
10481051 else :
10491052 ltype = GGUFValueType .get_type (val [0 ])
0 commit comments