char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
42928bc14
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_qwen2vl::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key_or_arr (LLM_KV_ROPE_DIMENSION_SECTIONS ,hparams .rope_sections ,4 , true); 5} 6// fall through 7 8void llama_model_qwen2vl::load_arch_tensors (llama_model_loader & ) { 9LLAMA_LOAD_LOCALS ; 10 11tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 12 13// output 14output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 15output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 16output_b = create_tensor (tn (LLM_TENSOR_OUTPUT ,"bias" ), {n_vocab },TENSOR_NOT_REQUIRED ); 17// if output is NULL, init from the input tok embed 18if (output == NULL ) { 19output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 20 } 21 22for (int i = 0 ;i < n_layer ;++ i ) { 23auto & layer = layers [i ]; 24 25layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 26 27create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 28layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 29 30layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 31 32layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 33layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 34layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 35 } 36} 37 38std::unique_ptr < llm_graph_context > llama_model_qwen2vl::build_arch_graph (const llm_graph_params & params )const { 39return std::make_unique < graph > (* this ,params ); 40} 41 42llama_model_qwen2vl::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 43const int64_t n_embd_head = hparams .n_embd_head_v (); 44 45GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 46GGML_ASSERT (n_embd_head == n_rot ); 47 48ggml_tensor * cur ; 49ggml_tensor * inpL ; 50 51inpL = build_inp_embd (model .tok_embd ); 52 53// inp_pos - contains the positions 54ggml_tensor * inp_pos = build_inp_pos (); 55 56auto * inp_attn = build_attn_inp_kv (); 57 58int sections [4 ]; 59 std::copy (std::begin (hparams .rope_sections ), std::begin (hparams .rope_sections )+ 4 ,sections ); 60 61ggml_tensor * inp_out_ids = build_inp_out_ids (); 62 63for (int il = 0 ;il < n_layer ;++ il ) { 64ggml_tensor * inpSA = inpL ; 65 66// norm 67cur = build_norm (inpL , 68model .layers [il ].attn_norm ,NULL , 69LLM_NORM_RMS ,il ); 70cb (cur ,"attn_norm" ,il ); 71 72// self-attention 73 { 74// compute Q and K and RoPE them 75auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 76n_embd_head ,n_head ,n_head_kv ,il ); 77 78Qcur = ggml_rope_multi ( 79ctx0 ,Qcur ,inp_pos ,nullptr , 80n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 81ext_factor ,attn_factor ,beta_fast ,beta_slow 82 ); 83 84Kcur = ggml_rope_multi ( 85ctx0 ,Kcur ,inp_pos ,nullptr , 86n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 87ext_factor ,attn_factor ,beta_fast ,beta_slow 88 ); 89 90cb (Qcur ,"Qcur" ,il ); 91cb (Kcur ,"Kcur" ,il ); 92cb (Vcur ,"Vcur" ,il ); 93 94cur = build_attn (inp_attn , 95model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 96Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 97 } 98if (il == n_layer - 1 && inp_out_ids ) { 99cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 100inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 101 } 102ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 103cb (ffn_inp ,"ffn_inp" ,il ); 104 105// feed-forward network 106cur = build_norm (ffn_inp , 107model .layers [il ].ffn_norm ,NULL , 108LLM_NORM_RMS ,il ); 109cb (cur ,"ffn_norm" ,il ); 110 111cur = build_ffn (cur , 112model .layers [il ].ffn_up ,NULL ,NULL , 113model .layers [il ].ffn_gate ,NULL ,NULL , 114model .layers [il ].ffn_down ,NULL ,NULL , 115NULL , 116LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 117cb (cur ,"ffn_out" ,il ); 118 119cur = ggml_add (ctx0 ,cur ,ffn_inp ); 120 121cur = build_cvec (cur ,il ); 122cb (cur ,"l_out" ,il ); 123 124// input for next layer 125inpL = cur ; 126 } 127cur = inpL ; 128 129cur = build_norm (cur , 130model .output_norm ,NULL , 131LLM_NORM_RMS ,-1 ); 132 133cb (cur ,"result_norm" ,-1 ); 134res -> t_embd = cur ; 135 136// lm_head 137cur = build_lora_mm (model .output ,cur ,model .output_s ); 138 139cb (cur ,"result_output" ,-1 ); 140res -> t_logits = cur ; 141 142ggml_build_forward_expand (gf ,cur ); 143}