char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
7acb4e8cd
char/aliceai-t5-moe
1#include "models.h" 2#include <float.h> 3 4void llama_model_chameleon::load_arch_hparams (llama_model_loader & ml ) { 5ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 6hparams .f_norm_eps = 1e-5 ;// eps for qk-norm, torch default 7ml .get_key (LLM_KV_SWIN_NORM ,hparams .swin_norm , false); 8 9switch (hparams .n_layer ()) { 10case 32 :type = LLM_TYPE_7B ;break ; 11case 48 :type = LLM_TYPE_34B ;break ; 12default :type = LLM_TYPE_UNKNOWN ; 13 } 14} 15 16void llama_model_chameleon::load_arch_tensors (llama_model_loader & ) { 17LLAMA_LOAD_LOCALS ; 18 19tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 20 21// output 22output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 23output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 24// if output is NULL, init from the input tok embed 25if (output == NULL ) { 26output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 27 } 28 29for (int i = 0 ;i < n_layer ;++ i ) { 30auto & layer = layers [i ]; 31 32layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 33layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k ,n_head },0 ); 34layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k ,n_head_kv },0 ); 35layer .attn_q_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"bias" ,i ), {n_embd_head_k ,n_head },TENSOR_NOT_REQUIRED ); 36layer .attn_k_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"bias" ,i ), {n_embd_head_k ,n_head_kv },TENSOR_NOT_REQUIRED ); 37 38create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 39layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 40 41layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 42 43layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 44layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 45layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 46 } 47} 48 49std::unique_ptr < llm_graph_context > llama_model_chameleon::build_arch_graph (const llm_graph_params & params )const { 50return std::make_unique < graph > (* this ,params ); 51} 52 53llama_model_chameleon::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 54const int64_t n_embd_head = hparams .n_embd_head_v (); 55 56GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 57GGML_ASSERT (n_embd_head == n_rot ); 58 59ggml_tensor * cur ; 60ggml_tensor * inpL ; 61 62inpL = build_inp_embd (model .tok_embd ); 63 64// inp_pos - contains the positions 65ggml_tensor * inp_pos = build_inp_pos (); 66 67auto * inp_attn = build_attn_inp_kv (); 68 69ggml_tensor * inp_out_ids = build_inp_out_ids (); 70 71for (int il = 0 ;il < n_layer ;++ il ) { 72ggml_tensor * inpSA = inpL ; 73 74// norm 75if (hparams .swin_norm ) { 76cur = inpL ; 77 }else { 78cur = build_norm (inpL , 79model .layers [il ].attn_norm ,NULL , 80LLM_NORM_RMS ,il ); 81cb (cur ,"attn_norm" ,il ); 82 } 83 84// self-attention 85 { 86// compute Q and K and RoPE them 87auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 88n_embd_head ,n_head ,n_head_kv ,il ); 89 90if (model .layers [il ].attn_q_norm ) { 91Qcur = build_norm (Qcur , 92model .layers [il ].attn_q_norm , 93model .layers [il ].attn_q_norm_b , 94LLM_NORM ,il ); 95cb (Qcur ,"Qcur" ,il ); 96 } 97 98if (model .layers [il ].attn_k_norm ) { 99Kcur = build_norm (Kcur , 100model .layers [il ].attn_k_norm , 101model .layers [il ].attn_k_norm_b , 102LLM_NORM ,il ); 103cb (Kcur ,"Kcur" ,il ); 104 } 105 106Qcur = ggml_rope_ext ( 107ctx0 ,Qcur ,inp_pos ,nullptr , 108n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 109ext_factor ,attn_factor ,beta_fast ,beta_slow 110 ); 111 112Kcur = ggml_rope_ext ( 113ctx0 ,Kcur ,inp_pos ,nullptr , 114n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 115ext_factor ,attn_factor ,beta_fast ,beta_slow 116 ); 117 118cb (Qcur ,"Qcur" ,il ); 119cb (Kcur ,"Kcur" ,il ); 120cb (Vcur ,"Vcur" ,il ); 121 122cur = build_attn (inp_attn , 123model .layers [il ].wo ,nullptr ,model .layers [il ].wo_s , 124Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 125 } 126 127if (il == n_layer - 1 && inp_out_ids ) { 128cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 129inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 130 } 131 132if (hparams .swin_norm ) { 133cur = build_norm (cur , 134model .layers [il ].attn_norm ,NULL , 135LLM_NORM_RMS ,il ); 136 } 137 138ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 139cb (ffn_inp ,"ffn_inp" ,il ); 140 141// feed-forward network 142if (!hparams .swin_norm ) { 143cur = build_norm (ffn_inp , 144model .layers [il ].ffn_norm ,NULL , 145LLM_NORM_RMS ,il ); 146cb (cur ,"ffn_norm" ,il ); 147 } 148 149cur = build_ffn (cur , 150model .layers [il ].ffn_up ,NULL ,NULL , 151model .layers [il ].ffn_gate ,NULL ,NULL , 152model .layers [il ].ffn_down ,NULL ,NULL , 153NULL , 154LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 155cb (cur ,"ffn_out" ,il ); 156 157if (hparams .swin_norm ) { 158cur = build_norm (cur , 159model .layers [il ].ffn_norm ,NULL , 160LLM_NORM_RMS ,il ); 161cb (cur ,"ffn_norm" ,il ); 162 } 163 164cur = ggml_add (ctx0 ,cur ,ffn_inp ); 165cb (cur ,"ffn_out" ,il ); 166 167cur = build_cvec (cur ,il ); 168cb (cur ,"l_out" ,il ); 169 170// input for next layer 171inpL = cur ; 172 } 173 174cur = inpL ; 175 176cur = build_norm (cur , 177model .output_norm ,NULL , 178LLM_NORM_RMS ,-1 ); 179 180cb (cur ,"result_norm" ,-1 ); 181res -> t_embd = cur ; 182 183// lm_head 184cur = build_lora_mm (model .output ,cur ,model .output_s ); 185cb (cur ,"result_output_with_img_logits" ,-1 ); 186 187// TODO: this suppresses the output of image tokens, which is required to enable text-only outputs. 188// Needs to be removed once image outputs are supported. 189int img_token_end_idx = 8196 ; 190int img_token_start_idx = 4 ; 191int num_img_tokens = img_token_end_idx - img_token_start_idx ; 192// creates 1d tensor of size num_img_tokens and values -FLT_MAX, 193// which ensures that text token values are always at least larger than image token values 194ggml_tensor * img_logits = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_F32 ,num_img_tokens ); 195img_logits = ggml_clamp (ctx0 ,img_logits ,- FLT_MAX ,- FLT_MAX ); 196cb (img_logits ,"img_logits" ,-1 ); 197 198cur = ggml_set_1d (ctx0 ,cur ,img_logits ,ggml_element_size (cur )* img_token_start_idx ); 199 200cb (cur ,"result_output" ,-1 ); 201res -> t_logits = cur ; 202 203ggml_build_forward_expand (gf ,cur ); 204}