char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
42928bc14
char/aliceai-t5-moe
1#include "models.h" 2 3std::unique_ptr < llm_graph_context > llama_model_paddleocr::build_arch_graph (const llm_graph_params & params )const { 4return std::make_unique < graph > (* this ,params ); 5} 6 7llama_model_paddleocr::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 8llm_graph_context (params ) { 9 10// NOTE: same with qwen2vl.cpp, but bias tensors are optional 11 12const int64_t n_embd_head = hparams .n_embd_head_v (); 13 14GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 15GGML_ASSERT (n_embd_head == n_rot ); 16 17ggml_tensor * cur ; 18ggml_tensor * inpL ; 19 20inpL = build_inp_embd (model .tok_embd ); 21 22int sections [4 ]; 23 std::copy (std::begin (hparams .rope_sections ), std::begin (hparams .rope_sections )+ 4 ,sections ); 24 25// inp_pos - contains the positions 26ggml_tensor * inp_pos = build_inp_pos (); 27 28auto * inp_attn = build_attn_inp_kv (); 29 30ggml_tensor * inp_out_ids = build_inp_out_ids (); 31 32for (int il = 0 ;il < n_layer ;++ il ) { 33ggml_tensor * inpSA = inpL ; 34 35// norm 36 { 37cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 38cb (cur ,"attn_norm" ,il ); 39 } 40// self-attention 41 { 42auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 43n_embd_head ,n_head ,n_head_kv ,il ); 44 45Qcur = ggml_rope_multi ( 46ctx0 ,Qcur ,inp_pos ,nullptr , 47n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 48ext_factor ,attn_factor ,beta_fast ,beta_slow 49 ); 50 51Kcur = ggml_rope_multi ( 52ctx0 ,Kcur ,inp_pos ,nullptr , 53n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 54ext_factor ,attn_factor ,beta_fast ,beta_slow 55 ); 56 57cb (Qcur ,"Qcur" ,il ); 58cb (Kcur ,"Kcur" ,il ); 59cb (Vcur ,"Vcur" ,il ); 60 61cur = build_attn (inp_attn , 62model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 63Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 64 } 65if (il == n_layer - 1 ) { 66// skip computing output for unused tokens 67cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 68inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 69 } 70ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 71cb (ffn_inp ,"ffn_inp" ,il ); 72 73// feed-forward network 74 { 75cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 76cb (cur ,"ffn_norm" ,il ); 77 78cur = build_ffn (cur , 79model .layers [il ].ffn_up ,NULL ,NULL , 80model .layers [il ].ffn_gate ,NULL ,NULL , 81model .layers [il ].ffn_down ,NULL ,NULL , 82NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 83cb (cur ,"ffn_out" ,il ); 84 } 85cur = ggml_add (ctx0 ,cur ,ffn_inp ); 86 87cur = build_cvec (cur ,il ); 88cb (cur ,"l_out" ,il ); 89 90// input for next layer 91inpL = cur ; 92 } 93cur = inpL ; 94 95cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 96 97cb (cur ,"result_norm" ,-1 ); 98res -> t_embd = cur ; 99 100// lm_head 101cur = build_lora_mm (model .output ,cur ,model .output_s ); 102 103cb (cur ,"result_output" ,-1 ); 104res -> t_logits = cur ; 105 106ggml_build_forward_expand (gf ,cur ); 107}