char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
465e49b9c
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_jais2::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 5 6switch (hparams .n_layer ()) { 7case 32 :type = LLM_TYPE_8B ;break ; 8case 68 :type = LLM_TYPE_70B ;break ; 9default :type = LLM_TYPE_UNKNOWN ; 10 } 11} 12 13void llama_model_jais2::load_arch_tensors (llama_model_loader & ) { 14LLAMA_LOAD_LOCALS ; 15 16tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 17 18// output 19output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 20output_norm_b = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"bias" ), {n_embd },0 ); 21output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 22if (!output ) { 23output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 24 } 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_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 33layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 34 35layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },0 ); 36 37layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 38layer .ffn_norm_b = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"bias" ,i ), {n_embd },0 ); 39 40// Jais-2 uses simple MLP (no gate) with biases 41layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 42layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },0 ); 43layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 44layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },0 ); 45 } 46} 47 48std::unique_ptr < llm_graph_context > llama_model_jais2::build_arch_graph (const llm_graph_params & params )const { 49return std::make_unique < graph > (* this ,params ); 50} 51 52// JAIS-2 model graph builder 53// Uses: LayerNorm (not RMSNorm), relu2 activation, separate Q/K/V, RoPE embeddings 54llama_model_jais2::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 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 68// KV input for attention 69auto * inp_attn = build_attn_inp_kv (); 70 71ggml_tensor * inp_out_ids = build_inp_out_ids (); 72 73for (int il = 0 ;il < n_layer ;++ il ) { 74// Pre-attention LayerNorm 75cur = build_norm (inpL , 76model .layers [il ].attn_norm , 77model .layers [il ].attn_norm_b , 78LLM_NORM ,il ); 79cb (cur ,"attn_norm" ,il ); 80 81// Self-attention with separate Q, K, V projections 82 { 83auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 84n_embd_head ,n_head ,n_head_kv ,il ); 85 86// Apply RoPE 87Qcur = ggml_rope_ext ( 88ctx0 ,Qcur ,inp_pos ,nullptr , 89n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 90ext_factor ,attn_factor ,beta_fast ,beta_slow 91 ); 92 93Kcur = ggml_rope_ext ( 94ctx0 ,Kcur ,inp_pos ,nullptr , 95n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 96ext_factor ,attn_factor ,beta_fast ,beta_slow 97 ); 98 99cb (Qcur ,"Qcur_rope" ,il ); 100cb (Kcur ,"Kcur_rope" ,il ); 101 102cur = build_attn (inp_attn , 103model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 104Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 105 } 106 107if (il == n_layer - 1 && inp_out_ids ) { 108cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 109inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 110 } 111 112// Residual connection 113ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpL ); 114cb (ffn_inp ,"ffn_inp" ,il ); 115 116// Pre-FFN LayerNorm 117cur = build_norm (ffn_inp , 118model .layers [il ].ffn_norm , 119model .layers [il ].ffn_norm_b , 120LLM_NORM ,il ); 121cb (cur ,"ffn_norm" ,il ); 122 123// FFN with relu2 activation (ReLU squared) - no gate projection 124// up -> relu2 -> down 125cur = build_ffn (cur , 126model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 127NULL ,NULL ,NULL ,// no gate 128model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 129NULL , 130LLM_FFN_RELU_SQR ,LLM_FFN_SEQ ,il ); 131cb (cur ,"ffn_out" ,il ); 132 133// Residual connection 134inpL = ggml_add (ctx0 ,cur ,ffn_inp ); 135inpL = build_cvec (inpL ,il ); 136cb (inpL ,"l_out" ,il ); 137 } 138 139// Final LayerNorm 140cur = build_norm (inpL , 141model .output_norm , 142model .output_norm_b , 143LLM_NORM ,-1 ); 144cb (cur ,"result_norm" ,-1 ); 145 146res -> t_embd = cur ; 147 148// Output projection 149cur = build_lora_mm (model .output ,cur ,model .output_s ); 150cb (cur ,"result_output" ,-1 ); 151 152res -> t_logits = cur ; 153 154ggml_build_forward_expand (gf ,cur ); 155}