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_mamba::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 ); 8ml .get_key (LLM_KV_SSM_DT_B_C_RMS ,hparams .ssm_dt_b_c_rms , false); 9 10ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 11 12switch (hparams .n_layer ()) { 13case 24 : 14switch (hparams .n_embd ) { 15case 768 :type = LLM_TYPE_SMALL ;break ; 16default :type = LLM_TYPE_UNKNOWN ; 17 }break ; 18case 48 : 19switch (hparams .n_embd ) { 20case 1024 :type = LLM_TYPE_MEDIUM ;break ; 21case 1536 :type = LLM_TYPE_LARGE ;break ; 22case 2048 :type = LLM_TYPE_XL ;break ; 23default :type = LLM_TYPE_UNKNOWN ; 24 }break ; 25case 64 : 26switch (hparams .n_embd ) { 27case 2560 :type = LLM_TYPE_3B ;break ; 28default :type = LLM_TYPE_UNKNOWN ; 29 }break ; 30default :type = LLM_TYPE_UNKNOWN ; 31 } 32} 33 34void llama_model_mamba::load_arch_tensors (llama_model_loader & ) { 35LLAMA_LOAD_LOCALS ; 36 37const int64_t d_conv = hparams .ssm_d_conv ; 38const int64_t d_inner = hparams .ssm_d_inner ; 39const int64_t d_state = hparams .ssm_d_state ; 40const int64_t dt_rank = hparams .ssm_dt_rank ; 41 42// only an expansion factor of 2 is supported for now 43if (2 * n_embd != d_inner ) { 44throw std::runtime_error ("only an expansion factor of 2 is supported for now" ); 45 } 46 47tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 48 49// output 50output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 51 52output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 53// if output is NULL, init from the input tok embed, duplicated to allow offloading 54if (output == NULL ) { 55output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 56 } 57 58for (int i = 0 ;i < n_layer ;++ i ) { 59auto & layer = layers [i ]; 60 61// norm 62layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 63 64layer .ssm_in = create_tensor (tn (LLM_TENSOR_SSM_IN ,"weight" ,i ), {n_embd ,2 * d_inner },0 ); 65 66layer .ssm_conv1d = create_tensor (tn (LLM_TENSOR_SSM_CONV1D ,"weight" ,i ), {d_conv ,d_inner },0 ); 67layer .ssm_conv1d_b = create_tensor (tn (LLM_TENSOR_SSM_CONV1D ,"bias" ,i ), {d_inner },0 ); 68 69layer .ssm_x = create_tensor (tn (LLM_TENSOR_SSM_X ,"weight" ,i ), {d_inner ,dt_rank + 2 * d_state },0 ); 70 71layer .ssm_dt = create_tensor (tn (LLM_TENSOR_SSM_DT ,"weight" ,i ), {dt_rank ,d_inner },0 ); 72layer .ssm_dt_b = create_tensor (tn (LLM_TENSOR_SSM_DT ,"bias" ,i ), {d_inner },0 ); 73 74// no "weight" suffix for these 75layer .ssm_a = create_tensor (tn (LLM_TENSOR_SSM_A ,i ), {d_state ,d_inner },0 ); 76layer .ssm_d = create_tensor (tn (LLM_TENSOR_SSM_D ,i ), {d_inner },0 ); 77 78// out_proj 79layer .ssm_out = create_tensor (tn (LLM_TENSOR_SSM_OUT ,"weight" ,i ), {d_inner ,n_embd },0 ); 80 } 81} 82 83std::unique_ptr < llm_graph_context > llama_model_mamba::build_arch_graph (const llm_graph_params & params )const { 84return std::make_unique < graph > (* this ,params ); 85} 86 87llama_model_mamba::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_build_mamba_base (params ) { 88ggml_tensor * cur ; 89ggml_tensor * inpL ; 90 91// {n_embd, n_tokens} 92inpL = build_inp_embd (model .tok_embd ); 93 94auto * rs_inp = build_rs_inp (); 95 96ggml_tensor * inp_out_ids = build_inp_out_ids (); 97 98for (int il = 0 ;il < n_layer ;++ il ) { 99// norm 100cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 101cb (cur ,"attn_norm" ,il ); 102 103if (model .arch == LLM_ARCH_MAMBA2 ) { 104cur = build_mamba2_layer (rs_inp ,cur ,model ,ubatch ,il ); 105 }else { 106cur = build_mamba_layer (rs_inp ,cur ,model ,ubatch ,il ); 107 } 108 109if (il == n_layer - 1 && inp_out_ids ) { 110cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 111inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 112 } 113 114// residual 115cur = ggml_add (ctx0 ,cur ,inpL ); 116 117cur = build_cvec (cur ,il ); 118cb (cur ,"l_out" ,il ); 119 120// input for next layer 121inpL = cur ; 122 } 123 124// final rmsnorm 125cur = build_norm (inpL ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 126 127cb (cur ,"result_norm" ,-1 ); 128res -> t_embd = cur ; 129 130// lm_head 131cur = build_lora_mm (model .output ,cur ,model .output_s ); 132 133cb (cur ,"result_output" ,-1 ); 134res -> t_logits = cur ; 135 136ggml_build_forward_expand (gf ,cur ); 137}