char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
7acb4e8cd
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_plamo3::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5const bool found_swa = ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 6if (found_swa && hparams .n_swa > 0 ) { 7hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 8ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 9uint32_t swa_period = 8 ; 10ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,swa_period , false); 11hparams .set_swa_pattern (swa_period ); 12 }else { 13hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 14 } 15 16switch (hparams .n_layer ()) { 17case 24 :type = LLM_TYPE_2B ;break ; 18default :type = LLM_TYPE_UNKNOWN ; 19 } 20} 21 22void llama_model_plamo3::load_arch_tensors (llama_model_loader & ) { 23LLAMA_LOAD_LOCALS ; 24 25const int64_t head_dim_q = hparams .n_embd_head_k (); 26const int64_t head_dim_v = hparams .n_embd_head_v (); 27 28tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 29 30output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 31output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 32if (output == NULL ) { 33output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 34 } 35 36for (int i = 0 ;i < n_layer ;++ i ) { 37auto & layer = layers [i ]; 38 39const int64_t num_attention_heads = hparams .n_head (i ); 40const int64_t num_key_value_heads = hparams .n_head_kv (i ); 41const int64_t q_proj_dim = num_attention_heads * head_dim_q ; 42const int64_t k_proj_dim = num_key_value_heads * head_dim_q ; 43const int64_t v_proj_dim = num_key_value_heads * head_dim_v ; 44const int64_t n_ff_cur = hparams .n_ff (i ); 45 46layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 47layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), 48 {n_embd ,q_proj_dim + k_proj_dim + v_proj_dim },0 ); 49layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {head_dim_q },0 ); 50layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {head_dim_q },0 ); 51layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {num_attention_heads * head_dim_v ,n_embd },0 ); 52layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,i ), {n_embd },0 ); 53 54layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 55layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,i ), {n_embd },0 ); 56 57layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff_cur * 2 },0 ); 58layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff_cur ,n_embd },0 ); 59 } 60} 61 62std::unique_ptr < llm_graph_context > llama_model_plamo3::build_arch_graph (const llm_graph_params & params )const { 63if (hparams .swa_type != LLAMA_SWA_TYPE_NONE ) { 64return std::make_unique < graph < true>> (* this ,params ); 65 }else { 66return std::make_unique < graph < false>> (* this ,params ); 67 } 68} 69 70template < bool iswa > 71llama_model_plamo3::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) : 72llm_graph_context (params ) { 73const int64_t head_dim_q = hparams .n_embd_head_k (); 74const int64_t head_dim_v = hparams .n_embd_head_v (); 75 76ggml_tensor * cur ; 77ggml_tensor * inpL = build_inp_embd (model .tok_embd ); 78ggml_tensor * inp_pos = build_inp_pos (); 79 80using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 81inp_attn_type * inp_attn = nullptr ; 82 83if constexpr (iswa ) { 84inp_attn = build_attn_inp_kv_iswa (); 85 }else { 86inp_attn = build_attn_inp_kv (); 87 } 88 89ggml_tensor * inp_out_ids = build_inp_out_ids (); 90 91for (int il = 0 ;il < n_layer ;++ il ) { 92ggml_tensor * residual = inpL ; 93 94float freq_base_l = 0.0f ; 95float freq_scale_l = 0.0f ; 96if constexpr (iswa ) { 97freq_base_l = model .get_rope_freq_base (cparams ,il ); 98freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 99 }else { 100freq_base_l = freq_base ; 101freq_scale_l = freq_scale ; 102 } 103 104cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 105cb (cur ,"attn_norm" ,il ); 106 107ggml_tensor * qkv = build_lora_mm (model .layers [il ].wqkv ,cur ); 108cb (cur ,"wqkv" ,il ); 109 110const int32_t n_head = hparams .n_head (il ); 111const int32_t n_head_kv = hparams .n_head_kv (il ); 112 113const int64_t q_offset = 0 ; 114const int64_t k_offset = head_dim_q * n_head ; 115const int64_t v_offset = k_offset + head_dim_q * n_head_kv ; 116 117ggml_tensor * Qcur = ggml_view_3d (ctx0 ,qkv ,head_dim_q ,n_head ,n_tokens , 118head_dim_q * sizeof (float ),qkv -> nb [1 ],q_offset * ggml_element_size (qkv )); 119ggml_tensor * Kcur = ggml_view_3d (ctx0 ,qkv ,head_dim_q ,n_head_kv ,n_tokens , 120head_dim_q * sizeof (float ),qkv -> nb [1 ],k_offset * ggml_element_size (qkv )); 121ggml_tensor * Vcur = ggml_view_3d (ctx0 ,qkv ,head_dim_v ,n_head_kv ,n_tokens , 122head_dim_v * sizeof (float ),qkv -> nb [1 ],v_offset * ggml_element_size (qkv )); 123 124cb (Qcur ,"Qcur" ,il ); 125cb (Kcur ,"Kcur" ,il ); 126cb (Vcur ,"Vcur" ,il ); 127 128Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 129cb (Qcur ,"attn_q_norm" ,il ); 130Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 131cb (Kcur ,"attn_k_norm" ,il ); 132 133Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr , 134n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 135ext_factor ,attn_factor ,beta_fast ,beta_slow ); 136Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr , 137n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 138ext_factor ,attn_factor ,beta_fast ,beta_slow ); 139 140const float attn_scale = 1.0f /sqrtf (float (head_dim_q )); 141 142cur = build_attn (inp_attn , 143model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 144Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,attn_scale ,il ); 145cb (cur ,"attn_out" ,il ); 146 147if (il == n_layer - 1 && inp_out_ids ) { 148cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 149residual = ggml_get_rows (ctx0 ,residual ,inp_out_ids ); 150 } 151 152cur = build_norm (cur ,model .layers [il ].attn_post_norm ,NULL ,LLM_NORM_RMS ,il ); 153cb (cur ,"attn_post_norm" ,il ); 154 155cur = ggml_add (ctx0 ,cur ,residual ); 156cb (cur ,"attn_residual" ,il ); 157 158residual = cur ; 159 160cur = build_norm (cur ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 161cb (cur ,"ffn_norm" ,il ); 162 163cur = build_ffn (cur , 164model .layers [il ].ffn_up ,NULL ,NULL , 165NULL ,NULL ,NULL , 166model .layers [il ].ffn_down ,NULL ,NULL , 167NULL , 168LLM_FFN_SWIGLU ,LLM_FFN_SEQ ,il ); 169cb (cur ,"ffn_out" ,il ); 170 171cur = build_norm (cur ,model .layers [il ].ffn_post_norm ,NULL ,LLM_NORM_RMS ,il ); 172cb (cur ,"ffn_post_norm" ,il ); 173 174cur = ggml_add (ctx0 ,cur ,residual ); 175cb (cur ,"ffn_residual" ,il ); 176 177cur = build_cvec (cur ,il ); 178cb (cur ,"l_out" ,il ); 179 180// input for next layer 181inpL = cur ; 182 } 183 184cur = inpL ; 185 186cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 187res -> t_embd = cur ; 188 189cur = build_lora_mm (model .output ,cur ,model .output_s ); 190res -> t_logits = cur ; 191 192ggml_build_forward_expand (gf ,cur ); 193} 194 195// Explicit template instantiations 196template struct llama_model_plamo3::graph < false> ; 197template struct llama_model_plamo3:: graph < true > ;