@@ -239,10 +239,7 @@ def write_tensors(self):
239
239
data : np .ndarray = data # type hint
240
240
n_dims = len (data .shape )
241
241
data_dtype = data .dtype
242
-
243
- # if f32 desired, convert any float16 to float32
244
- if self .ftype == 0 and data_dtype == np .float16 :
245
- data = data .astype (np .float32 )
242
+ data_qtype : gguf .GGMLQuantizationType | None = None
246
243
247
244
# when both are True, f32 should win
248
245
extra_f32 = self .extra_f32_tensors (name , new_name , bid , n_dims )
@@ -254,20 +251,27 @@ def write_tensors(self):
254
251
# if f16 desired, convert any float32 2-dim weight tensors to float16
255
252
extra_f16 = extra_f16 or (name .endswith (".weight" ) and n_dims >= 2 )
256
253
257
- # when both extra_f32 and extra_f16 are False, convert to float32 by default
258
- if self .ftype == 1 and data_dtype == np .float16 and (extra_f32 or not extra_f16 ):
259
- data = data .astype (np .float32 )
254
+ if self .ftype != gguf .GGMLFileType .ALL_F32 and extra_f16 and not extra_f32 :
255
+ if self .ftype == gguf .GGMLFileType .MOSTLY_F16 :
256
+ if data_dtype != np .float16 :
257
+ data = data .astype (np .float16 )
258
+ data_qtype = gguf .GGMLQuantizationType .F16
259
+ # TODO: add more types (like BF16) here
260
+
261
+ else : # by default, convert to float32
262
+ if data_dtype != np .float32 :
263
+ data = data .astype (np .float32 )
264
+ data_qtype = gguf .GGMLQuantizationType .F32
260
265
261
- if self .ftype == 1 and data_dtype == np .float32 and extra_f16 and not extra_f32 :
262
- data = data .astype (np .float16 )
266
+ assert data_qtype is not None
263
267
264
268
# reverse shape to make it similar to the internal ggml dimension order
265
269
shape_str = f"{{{ ', ' .join (str (n ) for n in reversed (data .shape ))} }}"
266
270
267
271
# n_dims is implicit in the shape
268
- logger .info (f"{ f'%-{ max_name_len } s' % f'{ new_name } ,' } { old_dtype } --> { data . dtype } , shape = { shape_str } " )
272
+ logger .info (f"{ f'%-{ max_name_len } s' % f'{ new_name } ,' } { old_dtype } --> { data_qtype . name } , shape = { shape_str } " )
269
273
270
- self .gguf_writer .add_tensor (new_name , data )
274
+ self .gguf_writer .add_tensor (new_name , data , raw_dtype = data_qtype )
271
275
272
276
def write (self ):
273
277
self .write_tensors ()
@@ -2472,9 +2476,9 @@ def main() -> None:
2472
2476
logger .error (f'Error: { args .model } is not a directory' )
2473
2477
sys .exit (1 )
2474
2478
2475
- ftype_map = {
2476
- "f32" : gguf .GGMLQuantizationType . F32 ,
2477
- "f16" : gguf .GGMLQuantizationType . F16 ,
2479
+ ftype_map : dict [ str , gguf . GGMLFileType ] = {
2480
+ "f32" : gguf .GGMLFileType . ALL_F32 ,
2481
+ "f16" : gguf .GGMLFileType . MOSTLY_F16 ,
2478
2482
}
2479
2483
2480
2484
if args .outfile is not None :
0 commit comments