char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
7acb4e8cd
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_dream::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5 6// Dream models are primarily 7B with 28 layers 7switch (hparams .n_layer ()) { 8case 28 : 9type = LLM_TYPE_7B ; 10break ; 11default : 12type = LLM_TYPE_UNKNOWN ; 13 } 14// Set non-causal attention for diffusion models 15hparams .causal_attn = false; 16} 17 18void llama_model_dream::load_arch_tensors (llama_model_loader & ) { 19LLAMA_LOAD_LOCALS ; 20 21tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 22 23// output 24output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 25output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 26output_b = create_tensor (tn (LLM_TENSOR_OUTPUT ,"bias" ), {n_vocab },TENSOR_NOT_REQUIRED ); 27// if output is NULL, init from the input tok embed 28if (output == NULL ) { 29output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 30 } 31 32for (int i = 0 ;i < n_layer ;++ i ) { 33auto & layer = layers [i ]; 34 35layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 36 37create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 38layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 39 40layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 41 42layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 43layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 44layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 45 } 46} 47 48std::unique_ptr < llm_graph_context > llama_model_dream::build_arch_graph (const llm_graph_params & params )const { 49return std::make_unique < graph > (* this ,params ); 50} 51 52llama_model_dream::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 53llm_graph_context (params ) { 54//copied from qwen2 55const int64_t n_embd_head = hparams .n_embd_head_v (); 56 57GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 58GGML_ASSERT (n_embd_head == n_rot ); 59 60ggml_tensor * cur ; 61ggml_tensor * inpL ; 62 63inpL = build_inp_embd (model .tok_embd ); 64 65// inp_pos - contains the positions 66ggml_tensor * inp_pos = build_inp_pos (); 67 68auto * inp_attn = build_attn_inp_no_cache (); 69 70ggml_tensor * inp_out_ids = build_inp_out_ids (); 71 72for (int il = 0 ;il < n_layer ;++ il ) { 73ggml_tensor * inpSA = inpL ; 74 75// norm 76cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 77cb (cur ,"attn_norm" ,il ); 78 79// self-attention 80 { 81auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 82n_embd_head ,n_head ,n_head_kv ,il ); 83 84Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 85ext_factor ,attn_factor ,beta_fast ,beta_slow ); 86 87Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 88ext_factor ,attn_factor ,beta_fast ,beta_slow ); 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 ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 107cb (cur ,"ffn_norm" ,il ); 108 109cur = build_ffn (cur , 110model .layers [il ].ffn_up ,NULL ,NULL , 111model .layers [il ].ffn_gate ,NULL ,NULL , 112model .layers [il ].ffn_down ,NULL ,NULL , 113NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 114cb (cur ,"ffn_out" ,il ); 115 116cur = ggml_add (ctx0 ,cur ,ffn_inp ); 117 118cur = build_cvec (cur ,il ); 119cb (cur ,"l_out" ,il ); 120 121// input for next layer 122inpL = cur ; 123 } 124cur = inpL ; 125 126cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 127 128cb (cur ,"result_norm" ,-1 ); 129res -> t_embd = cur ; 130 131// lm_head 132cur = build_lora_mm (model .output ,cur ,model .output_s ); 133 134cb (cur ,"result_output" ,-1 ); 135res -> t_logits = cur ; 136 137ggml_build_forward_expand (gf ,cur ); 138}