char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
fa88ae936
char/aliceai-t5-moe
1from __future__import annotations 2 3import json 4 5from typing import Iterable ,TYPE_CHECKING 6 7import torch 8 9if TYPE_CHECKING : 10from torch import Tensor 11 12from .base import ModelBase ,TextModel ,gguf 13 14 15@ ModelBase . register ( "PlamoForCausalLM" ) 16@ ModelBase . example ( "pfnet/plamo-13b" ) 17class PlamoModel (TextModel ): 18model_arch = gguf .MODEL_ARCH .PLAMO 19 20def set_vocab (self ): 21self ._set_vocab_sentencepiece () 22 23def set_gguf_parameters (self ): 24hparams = self .hparams 25 26self .gguf_writer .add_context_length (4096 )# not in config.json 27self .gguf_writer .add_embedding_length (hparams ["hidden_size" ]) 28self .gguf_writer .add_feed_forward_length (hparams ["intermediate_size" ]) 29self .gguf_writer .add_block_count (self .block_count ) 30self .gguf_writer .add_head_count (hparams ["num_attention_heads" ]) 31self .gguf_writer .add_head_count_kv (5 )# hparams["num_key_value_heads"]) is wrong 32self .gguf_writer .add_layer_norm_rms_eps (hparams ["rms_norm_eps" ]) 33self .gguf_writer .add_file_type (self .ftype ) 34 35def shuffle_attn_q_weight (self ,data_torch ): 36assert data_torch .size ()== (5120 ,5120 ) 37data_torch = data_torch .reshape (8 ,5 ,128 ,5120 ) 38data_torch = torch .permute (data_torch , (1 ,0 ,2 ,3 )) 39data_torch = torch .reshape (data_torch , (5120 ,5120 )) 40return data_torch 41 42def shuffle_attn_output_weight (self ,data_torch ): 43assert data_torch .size ()== (5120 ,5120 ) 44data_torch = data_torch .reshape (5120 ,8 ,5 ,128 ) 45data_torch = torch .permute (data_torch , (0 ,2 ,1 ,3 )) 46data_torch = torch .reshape (data_torch , (5120 ,5120 )) 47return data_torch 48 49def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 50new_name = self .map_tensor_name (name ) 51 52# shuffle for broadcasting of gqa in ggml_mul_mat 53if new_name .endswith ("attn_q.weight" ): 54data_torch = self .shuffle_attn_q_weight (data_torch ) 55elif new_name .endswith ("attn_output.weight" ): 56data_torch = self .shuffle_attn_output_weight (data_torch ) 57 58yield from super ().modify_tensors (data_torch ,name ,bid ) 59 60 61@ ModelBase . register ( "Plamo2ForCausalLM" , "PLaMo2ForCausalLM" ) 62@ ModelBase . example ( "pfnet/plamo-2-1b" ) 63class Plamo2Model (TextModel ): 64model_arch = gguf .MODEL_ARCH .PLAMO2 65 66def set_vocab (self ): 67self ._set_vocab_plamo () 68 69def set_gguf_parameters (self ): 70hparams = self .hparams 71self .gguf_writer .add_vocab_size (self .hparams ["vocab_size" ]) 72 73# Which layers are Mamba layers 74# PLaMo 2 uses mamba_step to indicate the pattern (e.g., 2 means every other layer) 75# This logic matches modeling_plamo.py's is_mamba function 76mamba_step = hparams .get ("mamba_step" ,2 ) 77mamba_enabled = hparams .get ("mamba_enabled" ,True ) 78num_key_value_heads = [] 79num_attention_heads = [] 80 81if mamba_enabled : 82for i in range (self .block_count ): 83if self .block_count <= (mamba_step // 2 ): 84# use attention in last layer 85is_mamba = (i != self .block_count - 1 ) 86else : 87is_mamba = (i % mamba_step )!= (mamba_step // 2 ) 88if is_mamba : 89num_key_value_heads .append (0 ) 90num_attention_heads .append (0 ) 91else : 92num_key_value_heads .append (hparams .get ("num_key_value_heads" ,4 )) 93num_attention_heads .append (hparams .get ("num_attention_heads" ,32 )) 94 95if num_key_value_heads and num_attention_heads : 96self .gguf_writer .add_head_count_kv (num_key_value_heads ) 97self .gguf_writer .add_head_count (num_attention_heads ) 98 99self .gguf_writer .add_context_length (hparams .get ("max_position_embeddings" ,2048 )) 100self .gguf_writer .add_embedding_length (hparams .get ("hidden_size" ,4096 )) 101self .gguf_writer .add_key_length (hparams .get ("hidden_size_per_head" ,128 )) 102self .gguf_writer .add_value_length (hparams .get ("hidden_size_per_head" ,128 )) 103self .gguf_writer .add_block_count (self .block_count ) 104self .gguf_writer .add_layer_norm_rms_eps (hparams .get ("rms_norm_eps" ,1e-06 )) 105self .gguf_writer .add_rope_freq_base (self .rope_parameters .get ("rope_theta" ,10000 )) 106 107# Mamba parameters 108self .gguf_writer .add_ssm_state_size (hparams .get ("mamba_d_state" ,64 )) 109self .gguf_writer .add_ssm_conv_kernel (hparams .get ("mamba_d_conv" ,4 )) 110self .gguf_writer .add_ssm_time_step_rank (hparams .get ("mamba_num_heads" ,64 )) 111intermediate_size = hparams .get ("mamba_num_heads" ,64 )* hparams .get ("hidden_size_per_head" ,128 ) 112self .gguf_writer .add_ssm_inner_size (intermediate_size ) 113self .gguf_writer .add_ssm_group_count (0 ) 114 115# MLP feed forward parameters (for attention layers) 116self .gguf_writer .add_feed_forward_length (hparams .get ("intermediate_size" ,13312 )) 117self .gguf_writer .add_file_type (self .ftype ) 118 119def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 120if name .endswith (".A_log" ): 121data_torch = - torch .exp (data_torch ) 122elif name .endswith (".dt_bias" ): 123name = name .rpartition (".dt_bias" )[0 ]+ ".dt_proj.bias" 124elif name .endswith (".dt_norm_weight" ): 125name = name .rpartition (".dt_norm_weight" )[0 ]+ ".dt_norm.weight" 126elif name .endswith (".B_norm_weight" ): 127name = name .rpartition (".B_norm_weight" )[0 ]+ ".B_norm.weight" 128elif name .endswith (".C_norm_weight" ): 129name = name .rpartition (".C_norm_weight" )[0 ]+ ".C_norm.weight" 130elif name .endswith (".k_weight" ): 131name = name .rpartition (".k_weight" )[0 ]+ ".k.weight" 132elif name .endswith (".q_weight" ): 133name = name .rpartition (".q_weight" )[0 ]+ ".q.weight" 134elif name .endswith (".conv1d.weight" ): 135data_torch = torch .squeeze (data_torch )# remove (, 1, ) 136assert data_torch .ndim == 2 137elif name .endswith (".pre_mixer_norm.weight" ): 138data_torch += 1.0 139elif name .endswith (".post_mixer_norm.weight" ): 140data_torch += 1.0 / 5 141elif name .endswith (".pre_mlp_norm.weight" ): 142data_torch += 1.0 143elif name .endswith (".post_mlp_norm.weight" ): 144data_torch += 1.0 / (5 ** 1.5 ) 145elif name .endswith (".norm.weight" ): 146data_torch += 1.0 147 148yield from super ().modify_tensors (data_torch ,name ,bid ) 149 150 151@ ModelBase . register ( "Plamo3ForCausalLM" , "PLaMo3ForCausalLM" ) 152# [TAG_HF_EXAMPLE_GATED] pfnet/plamo-3-nict-2b-base is gated 153@ ModelBase . example ( "midorin-Linux/plamo-3-12b-self-merged-base" ) 154class Plamo3Model (TextModel ): 155model_arch = gguf .MODEL_ARCH .PLAMO3 156 157def set_vocab (self ): 158self ._set_vocab_plamo () 159 160tokenizer_config_path = self .dir_model / "tokenizer_config.json" 161tokenizer_config = {} 162 163if tokenizer_config_path .is_file (): 164with open (tokenizer_config_path ,encoding = "utf-8" )as f : 165tokenizer_config = json .load (f ) 166 167chat_template = tokenizer_config .get ("chat_template" ) 168chat_template_jinja = self .dir_model / "chat_template.jinja" 169 170if chat_template_jinja .is_file (): 171with open (chat_template_jinja ,encoding = "utf-8" )as f : 172chat_template = f .read () 173 174if chat_template : 175self .gguf_writer .add_chat_template (chat_template ) 176 177def set_gguf_parameters (self ): 178super ().set_gguf_parameters () 179self .gguf_writer .add_vocab_size (self .hparams ["vocab_size" ]) 180if (sliding_window := self .find_hparam (["window_size" ,"sliding_window" ],optional = True ))is not None : 181self .gguf_writer .add_sliding_window (sliding_window ) 182self .gguf_writer .add_sliding_window_pattern (self .hparams ["sliding_window_pattern" ]) 183 184def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 185 186if name .endswith (".pre_mixer_norm.weight" ): 187data_torch = data_torch + 1.0 188elif name .endswith (".post_mixer_norm.weight" ): 189data_torch = data_torch + 1.0 / 5 190elif name .endswith (".pre_mlp_norm.weight" ): 191data_torch = data_torch + 1.0 192elif name .endswith (".post_mlp_norm.weight" ): 193data_torch = data_torch + 1.0 / (5 ** 1.5 ) 194elif name .endswith ((".mixer.q_norm.weight" ,".mixer.k_norm.weight" )): 195data_torch = data_torch + 1.0 196elif name .endswith (".norm.weight" ): 197data_torch = data_torch + 1.0 198 199yield from super ().modify_tensors (data_torch ,name ,bid )