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