char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
a2878d30d
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_qwen3vl::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_NUM_DEEPSTACK_LAYERS ,hparams .n_deepstack_layers , false); 5ml .get_key_or_arr (LLM_KV_ROPE_DIMENSION_SECTIONS ,hparams .rope_sections ,4 , true); 6ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 7 8switch (hparams .n_layer ()) { 9case 28 :type = LLM_TYPE_1_7B ;break ; 10case 36 :type = hparams .n_embd == 2560 ?LLM_TYPE_4B :LLM_TYPE_8B ;break ; 11case 64 :type = LLM_TYPE_32B ;break ; 12default :type = LLM_TYPE_UNKNOWN ; 13 } 14} 15 16void llama_model_qwen3vl::load_arch_tensors (llama_model_loader & ) { 17LLAMA_LOAD_LOCALS ; 18 19int64_t n_vocab_out = n_vocab ; 20if (arch == LLM_ARCH_QWEN3TTS ) { 21// [TAG_LLAMA_N_VOCAB_OUT] 22n_vocab_out = 3072 ; 23 } 24 25tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 26 27// output 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_out },TENSOR_NOT_REQUIRED ); 30// if output is NULL, init from the input tok embed 31if (output == NULL ) { 32output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 33 } 34 35// output rerank head 36cls_out = create_tensor (tn (LLM_TENSOR_CLS_OUT ,"weight" ), {n_embd ,hparams .n_cls_out },TENSOR_NOT_REQUIRED ); 37 38for (int i = 0 ;i < n_layer ;++ i ) { 39auto & layer = layers [i ]; 40 41layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 42 43create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_gqa ,n_embd_gqa ,0 ); 44layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 45 46layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 47layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 48 49layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 50layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 51layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 52layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 53 } 54} 55 56std::unique_ptr < llm_graph_context > llama_model_qwen3vl::build_arch_graph (const llm_graph_params & params )const { 57return std::make_unique < graph > (* this ,params ); 58} 59 60llama_model_qwen3vl::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 61const size_t n_deepstack_layers = hparams .n_deepstack_layers ; 62 63const int64_t n_embd = hparams .n_embd ; 64const int64_t n_embd_head = hparams .n_embd_head_v (); 65 66GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 67GGML_ASSERT (n_embd_head == n_rot ); 68 69ggml_tensor * cur ; 70ggml_tensor * inpL ; 71 72inpL = build_inp_embd (model .tok_embd ); 73 74int sections [4 ]; 75 std::copy (std::begin (hparams .rope_sections ), std::begin (hparams .rope_sections )+ 4 ,sections ); 76 77// inp_pos - contains the positions 78ggml_tensor * inp_pos = build_inp_pos (); 79 80auto * inp_attn = build_attn_inp_kv (); 81 82ggml_tensor * inp_out_ids = build_inp_out_ids (); 83 84for (int il = 0 ;il < n_layer ;++ il ) { 85ggml_tensor * inpSA = inpL ; 86 87// norm 88cur = build_norm (inpL , 89model .layers [il ].attn_norm ,NULL , 90LLM_NORM_RMS ,il ); 91cb (cur ,"attn_norm" ,il ); 92 93// self-attention 94 { 95// compute Q and K and RoPE them 96auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 97n_embd_head ,n_head ,n_head_kv ,il ); 98 99Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 100cb (Qcur ,"Qcur_normed" ,il ); 101 102Qcur = ggml_rope_multi ( 103ctx0 ,Qcur ,inp_pos ,nullptr , 104n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 105ext_factor ,attn_factor ,beta_fast ,beta_slow 106 ); 107 108Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 109cb (Kcur ,"Kcur_normed" ,il ); 110 111Kcur = ggml_rope_multi ( 112ctx0 ,Kcur ,inp_pos ,nullptr , 113n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 114ext_factor ,attn_factor ,beta_fast ,beta_slow 115 ); 116 117cb (Qcur ,"Qcur" ,il ); 118cb (Kcur ,"Kcur" ,il ); 119cb (Vcur ,"Vcur" ,il ); 120 121cur = build_attn (inp_attn , 122model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 123Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 124 } 125 126if (il == n_layer - 1 && inp_out_ids ) { 127cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 128inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 129 } 130 131ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 132cb (ffn_inp ,"ffn_inp" ,il ); 133 134// feed-forward network 135cur = build_norm (ffn_inp , 136model .layers [il ].ffn_norm ,NULL , 137LLM_NORM_RMS ,il ); 138cb (cur ,"ffn_norm" ,il ); 139 140cur = build_ffn (cur , 141model .layers [il ].ffn_up ,NULL ,NULL , 142model .layers [il ].ffn_gate ,NULL ,NULL , 143model .layers [il ].ffn_down ,NULL ,NULL , 144NULL , 145LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 146cb (cur ,"ffn_out" ,il ); 147 148cur = ggml_add (ctx0 ,cur ,ffn_inp ); 149 150cur = build_cvec (cur ,il ); 151cb (cur ,"l_out" ,il ); 152 153if (il < (int )n_deepstack_layers ) { 154ggml_tensor * ds = ggml_view_2d (ctx0 ,res -> t_inp_embd ,n_embd ,n_tokens ,res -> t_inp_embd -> nb [1 ], (il + 1 )* n_embd * sizeof (float )); 155cur = ggml_add (ctx0 ,cur ,ds ); 156cb (cur ,"deepstack_out" ,il ); 157 } 158 159// input for next layer 160inpL = cur ; 161 } 162 163cur = inpL ; 164 165cur = build_norm (cur , 166model .output_norm ,NULL , 167LLM_NORM_RMS ,-1 ); 168 169cb (cur ,"result_norm" ,-1 ); 170res -> t_embd = cur ; 171 172// lm_head 173cur = build_lora_mm (model .output ,cur ,model .output_s ); 174 175int64_t n_vocab_in = model .tok_embd -> ne [1 ]; 176int64_t n_vocab_out = model .output -> ne [1 ]; 177if (n_vocab_in > n_vocab_out ) { 178// case: Qwen3TTS model with codec_head as output 179GGML_ASSERT (model .output_norm ); 180int64_t pad = n_vocab_in - n_vocab_out ; 181 182// using this trick to get a scalar -inf tensor to pad the output 183ggml_tensor * neg_inf = ggml_scale_bias (ctx0 , 184ggml_view_1d (ctx0 ,model .output_norm ,1 ,0 ), 1850.0f ,- INFINITY ); 186neg_inf = ggml_repeat_4d (ctx0 ,neg_inf ,pad ,cur -> ne [1 ],1 ,1 ); 187cur = ggml_concat (ctx0 ,neg_inf ,cur ,0 );// [padded .. n_vocab_out, n_stream] 188 189 }else if (n_vocab_in < n_vocab_out ) { 190GGML_ABORT ("invalid case" ); 191 } 192 193cb (cur ,"result_output" ,-1 ); 194res -> t_logits = cur ; 195 196ggml_build_forward_expand (gf ,cur ); 197}