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