@@ -2790,85 +2790,18 @@ def set_gguf_parameters(self):
27902790 self .gguf_writer .add_rope_dimension_count (64 )
27912791 self .gguf_writer .add_add_bos_token (False )
27922792
2793- def write_tensors (self ):
2794- block_count = self .hparams ["num_layers" ]
2795- tensors = dict (self .get_tensors ())
2796- tensor_map = gguf .get_tensor_name_map (self .model_arch , block_count )
2797- has_lm_head = True
2798- n_head = self .hparams .get ("n_head" , self .hparams .get ("num_attention_heads" ))
2799- n_embed = self .hparams .get ("hidden_size" , self .hparams .get ("n_embed" ))
2800-
2801- for name , data_torch in tensors .items ():
2802- if name .endswith (".rotary_pos_emb.inv_freq" ):
2803- continue
2804-
2805- if "lm_head.weight" not in tensors .keys () and "output.weight" not in tensors .keys ():
2806- has_lm_head = False
2793+ def modify_tensors (self , data_torch : Tensor , name : str , bid : int | None ) -> Iterable [tuple [str , Tensor ]]:
2794+ if name .endswith (".rotary_pos_emb.inv_freq" ):
2795+ return []
28072796
2808- name = re . sub ( r'transformer\.' , '' , name )
2797+ del bid # unused
28092798
2810- old_dtype = data_torch . dtype
2799+ name = re . sub ( r'transformer\.' , '' , name )
28112800
2812- # convert any unsupported data types to float32
2813- if data_torch .dtype not in (torch .float16 , torch .float32 ):
2814- data_torch = data_torch .to (torch .float32 )
2801+ if name == "word_embeddings.weight" :
2802+ assert self .tensor_names is not None
28152803
2816- data = data_torch .squeeze ().numpy ()
2817-
2818- if re .match (r"h\.\d+\.self_attention\.query_key_value\.weight" , name ):
2819- # Map bloom-style qkv_linear to gpt-style qkv_linear
2820- # bloom: https://github.com/huggingface/transformers/blob/main/src/transformers/models/bloom/modeling_bloom.py#L238-L252 # noqa
2821- # gpt-2: https://github.com/huggingface/transformers/blob/main/src/transformers/models/gpt2/modeling_gpt2.py#L312 # noqa
2822- qkv_weights = data .reshape ((n_head , 3 , n_embed // n_head , n_embed ))
2823- data = np .concatenate (
2824- (
2825- qkv_weights [:, 0 , :, :].reshape ((- 1 , n_embed )),
2826- qkv_weights [:, 1 , :, :].reshape ((- 1 , n_embed )),
2827- qkv_weights [:, 2 , :, :].reshape ((- 1 , n_embed )),
2828- ),
2829- axis = 0 ,
2830- )
2831- print ("re-format attention.linear_qkv.weight" )
2832- elif re .match (r"h\.\d+\.self_attention\.query_key_value\.bias" , name ):
2833- qkv_bias = data .reshape ((n_head , 3 , n_embed // n_head ))
2834- data = np .concatenate (
2835- (
2836- qkv_bias [:, 0 , :].reshape ((n_embed ,)),
2837- qkv_bias [:, 1 , :].reshape ((n_embed ,)),
2838- qkv_bias [:, 2 , :].reshape ((n_embed ,)),
2839- ),
2840- axis = 0 ,
2841- )
2842- print ("re-format attention.linear_qkv.bias" )
2843-
2844- # map tensor names
2845- new_name = tensor_map .get_name (name , try_suffixes = (".weight" , ".bias" ))
2846- if new_name is None :
2847- print (f"Can not map tensor { name !r} " )
2848- sys .exit ()
2849-
2850- n_dims = len (data .shape )
2851- data_dtype = data .dtype
2852-
2853- # if f32 desired, convert any float16 to float32
2854- if self .ftype == 0 and data_dtype == np .float16 :
2855- data = data .astype (np .float32 )
2856-
2857- # TODO: Why cant we use these float16 as-is? There should be not reason to store float16 as float32
2858- if self .ftype == 1 and data_dtype == np .float16 and n_dims == 1 :
2859- data = data .astype (np .float32 )
2860-
2861- # if f16 desired, convert any float32 2-dim weight tensors to float16
2862- if self .ftype == 1 and data_dtype == np .float32 and name .endswith (".weight" ) and n_dims == 2 :
2863- data = data .astype (np .float16 )
2864-
2865- print (f"=> { new_name } , shape = { data .shape } , { old_dtype } --> { data .dtype } " )
2866-
2867- self .gguf_writer .add_tensor (new_name , data )
2868-
2869- if not has_lm_head and name == "word_embeddings.weight" :
2870- self .gguf_writer .add_tensor ("output.weight" , data )
2871- print (name , f"=> output.weight, shape = { data .shape } , { old_dtype } --> { data .dtype } " )
2804+ return [(self .map_tensor_name (name ), data_torch )]
28722805
28732806
28742807###### CONVERSION LOGIC ######
0 commit comments