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_jamba::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_SSM_CONV_KERNEL ,hparams .ssm_d_conv ); 5ml .get_key (LLM_KV_SSM_INNER_SIZE ,hparams .ssm_d_inner ); 6ml .get_key (LLM_KV_SSM_STATE_SIZE ,hparams .ssm_d_state ); 7ml .get_key (LLM_KV_SSM_TIME_STEP_RANK ,hparams .ssm_dt_rank ); 8 9ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 10 11for (uint32_t i = 0 ;i < hparams .n_layer ();++ i ) { 12hparams .is_recr_impl [i ]= hparams .n_head_kv (i )== 0 ; 13 } 14 15switch (hparams .n_layer ()) { 16// TODO: Jamba layers are a bit heterogeneous, so naming this is hard. 17case 12 :// 900M 8x???M 18case 32 :// 51B 16x?B 19default :type = LLM_TYPE_UNKNOWN ; 20 } 21} 22 23void llama_model_jamba::load_arch_tensors (llama_model_loader & ) { 24LLAMA_LOAD_LOCALS ; 25 26const int64_t d_conv = hparams .ssm_d_conv ; 27const int64_t d_inner = hparams .ssm_d_inner ; 28const int64_t d_state = hparams .ssm_d_state ; 29const int64_t dt_rank = hparams .ssm_dt_rank ; 30 31// only an expansion factor of 2 is supported for now 32GGML_ASSERT (2 * n_embd == d_inner ); 33 34tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 35 36// output 37 { 38output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 39 40output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 41// if output is NULL, init from the input tok embed, duplicated to allow offloading 42if (output == NULL ) { 43output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 44 } 45 } 46 47for (int i = 0 ;i < n_layer ;++ i ) { 48const int64_t n_head_kv = hparams .n_head_kv (i ); 49const int64_t n_embd_gqa = hparams .n_embd_v_gqa (i ); 50 51auto & layer = layers [i ]; 52 53// norm 54layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 55 56if (n_head_kv == 0 ) { 57// Mamba layer 58layer .ssm_in = create_tensor (tn (LLM_TENSOR_SSM_IN ,"weight" ,i ), {n_embd ,2 * d_inner },0 ); 59 60layer .ssm_conv1d = create_tensor (tn (LLM_TENSOR_SSM_CONV1D ,"weight" ,i ), {d_conv ,d_inner },0 ); 61layer .ssm_conv1d_b = create_tensor (tn (LLM_TENSOR_SSM_CONV1D ,"bias" ,i ), {d_inner },0 ); 62 63layer .ssm_x = create_tensor (tn (LLM_TENSOR_SSM_X ,"weight" ,i ), {d_inner ,dt_rank + 2 * d_state },0 ); 64 65layer .ssm_dt_norm = create_tensor (tn (LLM_TENSOR_SSM_DT_NORM ,"weight" ,i ), {dt_rank },0 ); 66 67layer .ssm_dt = create_tensor (tn (LLM_TENSOR_SSM_DT ,"weight" ,i ), {dt_rank ,d_inner },0 ); 68layer .ssm_dt_b = create_tensor (tn (LLM_TENSOR_SSM_DT ,"bias" ,i ), {d_inner },0 ); 69 70layer .ssm_b_norm = create_tensor (tn (LLM_TENSOR_SSM_B_NORM ,"weight" ,i ), {d_state },0 ); 71layer .ssm_c_norm = create_tensor (tn (LLM_TENSOR_SSM_C_NORM ,"weight" ,i ), {d_state },0 ); 72 73// no "weight" suffix for these 74layer .ssm_a = create_tensor (tn (LLM_TENSOR_SSM_A ,i ), {d_state ,d_inner },0 ); 75layer .ssm_d = create_tensor (tn (LLM_TENSOR_SSM_D ,i ), {d_inner },0 ); 76 77// out_proj 78layer .ssm_out = create_tensor (tn (LLM_TENSOR_SSM_OUT ,"weight" ,i ), {d_inner ,n_embd },0 ); 79 }else { 80// Attention layers 81 82create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 83layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 84 } 85 86layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 87 88layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },TENSOR_NOT_REQUIRED ); 89 90if (layer .ffn_gate_inp ) { 91// MoE 92layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 93layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff ,n_embd ,n_expert },0 ); 94layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 95 }else { 96// FFN (no MoE) 97layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 98layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 99layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 100 } 101 } 102} 103 104std::unique_ptr < llm_graph_context > llama_model_jamba::build_arch_graph (const llm_graph_params & params )const { 105return std::make_unique < graph > (* this ,params ); 106} 107 108llama_model_jamba::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_build_mamba_base (params ) { 109const int64_t n_embd_head = hparams .n_embd_head_v (); 110 111ggml_tensor * cur ; 112ggml_tensor * inpL ; 113 114// {n_embd, n_tokens} 115inpL = build_inp_embd (model .tok_embd ); 116 117auto * inp_hybrid = build_inp_mem_hybrid (); 118 119ggml_tensor * inp_out_ids = build_inp_out_ids (); 120 121for (int il = 0 ;il < n_layer ;++ il ) { 122const int64_t n_head_kv = hparams .n_head_kv (il ); 123 124cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 125cb (cur ,"attn_norm" ,il ); 126 127if (n_head_kv == 0 ) { 128cur = build_mamba_layer (inp_hybrid -> get_recr (),cur ,model ,ubatch ,il ); 129 }else { 130// Attention 131 132auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 133n_embd_head ,n_head ,n_head_kv ,il ); 134 135// No RoPE :) 136cur = build_attn (inp_hybrid -> get_attn (), 137model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 138Qcur ,Kcur ,Vcur ,NULL ,NULL ,NULL ,1.0f /sqrtf (float (n_embd_head )),il ); 139 } 140if (il == n_layer - 1 && inp_out_ids ) { 141cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 142inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 143 } 144// residual 145struct ggml_tensor * ffn_inp = ggml_add (ctx0 ,inpL ,cur ); 146cb (cur ,"ffn_inp" ,il ); 147 148cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 149cb (cur ,"ffn_norm" ,il ); 150 151// feed-forward network 152if (model .layers [il ].ffn_gate_inp == nullptr ) { 153// FFN 154cur = build_ffn (cur , 155model .layers [il ].ffn_up ,NULL ,NULL , 156model .layers [il ].ffn_gate ,NULL ,NULL , 157model .layers [il ].ffn_down ,NULL ,NULL , 158NULL , 159LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 160cb (cur ,"ffn_out" ,il ); 161 }else { 162// MoE branch 163cur = build_moe_ffn (cur , 164model .layers [il ].ffn_gate_inp , 165model .layers [il ].ffn_up_exps , 166model .layers [il ].ffn_gate_exps , 167model .layers [il ].ffn_down_exps , 168nullptr , 169n_expert ,n_expert_used , 170LLM_FFN_SILU , false, 171hparams .expert_weights_scale , 172LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 173il ); 174cb (cur ,"ffn_moe_out" ,il ); 175 } 176// residual 177cur = ggml_add (ctx0 ,ffn_inp ,cur ); 178 179cur = build_cvec (cur ,il ); 180cb (cur ,"l_out" ,il ); 181 182// input for next layer 183inpL = cur ; 184 } 185// final rmsnorm 186cur = build_norm (inpL ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 187 188cb (cur ,"result_norm" ,-1 ); 189res -> t_embd = cur ; 190 191// lm_head 192cur = build_lora_mm (model .output ,cur ,model .output_s ); 193 194cb (cur ,"result_output" ,-1 ); 195res -> t_logits = cur ; 196 197ggml_build_forward_expand (gf ,cur ); 198}