char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
6e62ba538
char/aliceai-t5-moe
1#include "models.h" 2 3// backbone of the pocket-tts CALM pipeline: the "text" side of a flow language model. 4// it has no lm_head, the audio latents are produced by the flow net inside the mmproj 5 6void llama_model_pockettts::load_arch_hparams (llama_model_loader & ml ) { 7ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 8 9switch (hparams .n_layer ()) { 10case 6 :type = LLM_TYPE_109M ;break ; 11case 24 :type = LLM_TYPE_335M ;break ; 12default :type = LLM_TYPE_UNKNOWN ; 13 } 14} 15 16void llama_model_pockettts::load_arch_tensors (llama_model_loader & ) { 17LLAMA_LOAD_LOCALS ; 18 19tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 20 21output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 22output_norm_b = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"bias" ), {n_embd },0 ); 23// no output head, the logits are unused; reuse the embedding table so a sampler can still run 24output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 25 26for (int i = 0 ;i < n_layer ;++ i ) { 27auto & layer = layers [i ]; 28 29layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 30layer .attn_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"bias" ,i ), {n_embd },0 ); 31 32create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,TENSOR_NOT_REQUIRED ); 33 34layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 35 36layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 37layer .ffn_norm_b = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"bias" ,i ), {n_embd },0 ); 38 39layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 40layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 41 } 42} 43 44std::unique_ptr < llm_graph_context > llama_model_pockettts::build_arch_graph (const llm_graph_params & params )const { 45return std::make_unique < graph > (* this ,params ); 46} 47 48llama_model_pockettts::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 49const int64_t n_embd_head = hparams .n_embd_head_v (); 50 51GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 52GGML_ASSERT (n_embd_head == n_rot ); 53 54ggml_tensor * cur ; 55ggml_tensor * inpL ; 56 57inpL = build_inp_embd (model .tok_embd ); 58 59ggml_tensor * inp_pos = build_inp_pos (); 60 61auto * inp_attn = build_attn_inp_kv (); 62 63ggml_tensor * inp_out_ids = build_inp_out_ids (); 64 65for (int il = 0 ;il < n_layer ;++ il ) { 66cur = build_norm (inpL , 67model .layers [il ].attn_norm , 68model .layers [il ].attn_norm_b , 69LLM_NORM ,il ); 70cb (cur ,"attn_norm" ,il ); 71 72// self-attention 73 { 74auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 75n_embd_head ,n_head ,n_head_kv ,il ); 76 77Qcur = ggml_rope_ext ( 78ctx0 ,Qcur ,inp_pos ,nullptr , 79n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 80ext_factor ,attn_factor ,beta_fast ,beta_slow 81 ); 82 83Kcur = ggml_rope_ext ( 84ctx0 ,Kcur ,inp_pos ,nullptr , 85n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 86ext_factor ,attn_factor ,beta_fast ,beta_slow 87 ); 88 89cb (Qcur ,"Qcur" ,il ); 90cb (Kcur ,"Kcur" ,il ); 91cb (Vcur ,"Vcur" ,il ); 92 93cur = build_attn (inp_attn , 94model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 95Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 96 } 97 98if (il == n_layer - 1 && inp_out_ids ) { 99cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 100inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 101 } 102 103ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpL ); 104cb (ffn_inp ,"ffn_inp" ,il ); 105 106// FF 107 { 108cur = build_norm (ffn_inp , 109model .layers [il ].ffn_norm , 110model .layers [il ].ffn_norm_b , 111LLM_NORM ,il ); 112cb (cur ,"ffn_norm" ,il ); 113 114cur = build_ffn (cur , 115model .layers [il ].ffn_up ,NULL ,NULL , 116NULL ,NULL ,NULL , 117model .layers [il ].ffn_down ,NULL ,NULL , 118NULL , 119LLM_FFN_GELU ,LLM_FFN_SEQ ,il ); 120cb (cur ,"ffn_out" ,il ); 121 } 122 123cur = ggml_add (ctx0 ,cur ,ffn_inp ); 124 125cur = build_cvec (cur ,il ); 126cb (cur ,"l_out" ,il ); 127 128// input for next layer 129inpL = cur ; 130 } 131 132cur = build_norm (inpL , 133model .output_norm , 134model .output_norm_b , 135LLM_NORM ,-1 ); 136 137cb (cur ,"result_norm" ,-1 ); 138res -> t_embd = cur ; 139 140cur = build_lora_mm (model .output ,cur ,model .output_s ); 141 142cb (cur ,"result_output" ,-1 ); 143res -> t_logits = cur ; 144 145ggml_build_forward_expand (gf ,cur ); 146}