char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
d7a207411
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_nanbeige::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5 6uint32_t n_loops_u = 1 ; 7ml .get_key (LLM_KV_NUM_LOOPS ,n_loops_u , false); 8GGML_ASSERT (n_loops_u >=1 ); 9 10skip_loop_final_norm = false; 11ml .get_key (LLM_KV_SKIP_LOOP_FINAL_NORM ,skip_loop_final_norm , false); 12 13n_layer_phys = (int )hparams .n_layer (); 14 15// Bound-check before casting: signed int mul can overflow and bypass the guard. 16GGML_ASSERT ((size_t )n_layer_phys * (size_t )n_loops_u <= (size_t )LLAMA_MAX_LAYERS ); 17n_loops = (int )n_loops_u ; 18 19// Expand logical layer count before load_tensors() allocates layers / KV. 20if (n_loops > 1 ) { 21for (int j = 1 ;j < n_loops ;++ j ) { 22for (int i = 0 ;i < n_layer_phys ;++ i ) { 23const int dst = i + j * n_layer_phys ; 24hparams .n_head_arr [dst ]= hparams .n_head_arr [i ]; 25hparams .n_head_kv_arr [dst ]= hparams .n_head_kv_arr [i ]; 26hparams .n_ff_arr [dst ]= hparams .n_ff_arr [i ]; 27hparams .is_swa_impl [dst ]= hparams .is_swa_impl [i ]; 28hparams .is_recr_impl [dst ]= hparams .is_recr_impl [i ]; 29 } 30 } 31hparams .n_layer_all = (uint32_t ) ((size_t )n_layer_phys * (size_t )n_loops ); 32 } 33 34type = LLM_TYPE_UNKNOWN ; 35} 36 37void llama_model_nanbeige::load_arch_tensors (llama_model_loader & ) { 38LLAMA_LOAD_LOCALS ; 39 40tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 41 42output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 43output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 44if (output == NULL ) { 45output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 46 } 47 48const int n_phys = n_layer_phys > 0 ?n_layer_phys :n_layer ; 49for (int i = 0 ;i < n_phys ;++ i ) { 50auto & layer = layers [i ]; 51 52layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 53 54create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 55layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 56 57layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 }, 58TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 59 60layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 61layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 62layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 63layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 64 } 65 66// Share physical weights across loops; each slot still has its own KV index. 67if (n_loops > 1 ) { 68for (int j = 1 ;j < n_loops ;++ j ) { 69for (int i = 0 ;i < n_phys ;++ i ) { 70layers [i + j * n_phys ]= layers [i ]; 71 } 72 } 73 } 74} 75 76std::unique_ptr < llm_graph_context > llama_model_nanbeige::build_arch_graph (const llm_graph_params & params )const { 77return std::make_unique < graph > (* this ,params ); 78} 79 80llama_model_nanbeige::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 81llm_graph_context (params ) { 82const auto & nb = static_cast < const llama_model_nanbeige &> (model ); 83 84const int64_t n_embd_head = hparams .n_embd_head_v (); 85GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 86 87const int n_phys = nb .n_layer_phys > 0 ?nb .n_layer_phys : (int )n_layer ; 88const int n_loops = nb .n_loops > 0 ?nb .n_loops :1 ; 89 90ggml_tensor * cur ; 91ggml_tensor * inpL ; 92 93inpL = build_inp_embd (model .tok_embd ); 94 95ggml_tensor * inp_pos = build_inp_pos (); 96 97auto * inp_attn = build_attn_inp_kv (); 98 99const float kq_scale = hparams .f_attention_scale == 0.0f 100 ?1.0f /sqrtf (float (n_embd_head )) 101 :hparams .f_attention_scale ; 102 103ggml_tensor * inp_out_ids = build_inp_out_ids (); 104 105for (int il = 0 ;il < n_layer ;++ il ) { 106res -> t_layer_inp [il ]= inpL ; 107ggml_tensor * inpSA = inpL ; 108 109cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 110cb (cur ,"attn_norm" ,il ); 111 112 { 113ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 114 115auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 116n_embd_head ,n_head ,n_head_kv ,il ); 117 118Qcur = ggml_rope_ext ( 119ctx0 ,Qcur ,inp_pos ,rope_factors , 120n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 121ext_factor ,attn_factor ,beta_fast ,beta_slow ); 122 123Kcur = ggml_rope_ext ( 124ctx0 ,Kcur ,inp_pos ,rope_factors , 125n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 126ext_factor ,attn_factor ,beta_fast ,beta_slow ); 127 128cb (Qcur ,"Qcur" ,il ); 129cb (Kcur ,"Kcur" ,il ); 130cb (Vcur ,"Vcur" ,il ); 131 132cur = build_attn (inp_attn , 133model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 134Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 135cb (cur ,"attn_out" ,il ); 136 } 137 138if (il == n_layer - 1 && inp_out_ids ) { 139cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 140inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 141 } 142 143ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 144cb (ffn_inp ,"ffn_inp" ,il ); 145 146cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 147cb (cur ,"ffn_norm" ,il ); 148 149cur = build_ffn (cur , 150model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,model .layers [il ].ffn_up_s , 151model .layers [il ].ffn_gate ,model .layers [il ].ffn_gate_b ,model .layers [il ].ffn_gate_s , 152model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,model .layers [il ].ffn_down_s , 153NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 154cb (cur ,"ffn_out" ,il ); 155 156cur = ggml_add (ctx0 ,cur ,ffn_inp ); 157cb (cur ,"ffn_out" ,il ); 158 159cur = build_cvec (cur ,il ); 160cb (cur ,"l_out" ,il ); 161 162inpL = cur ; 163 164if (n_loops > 1 && 165 ((il + 1 ) %n_phys )== 0 && 166 (il + 1 )< n_layer && 167 !nb .skip_loop_final_norm ) { 168cur = build_norm (inpL ,model .output_norm ,NULL ,LLM_NORM_RMS ,il ); 169cb (cur ,"loop_norm" ,il ); 170inpL = cur ; 171 } 172 } 173 174cur = inpL ; 175 176cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 177cb (cur ,"result_norm" ,-1 ); 178res -> t_embd = cur ; 179 180cur = build_lora_mm (model .output ,cur ,model .output_s ); 181cb (cur ,"result_output" ,-1 ); 182res -> t_logits = cur ; 183 184ggml_build_forward_expand (gf ,cur ); 185}