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