Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_mamba::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);5 ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);6 ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);7 ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);8 ml.get_key(LLM_KV_SSM_DT_B_C_RMS, hparams.ssm_dt_b_c_rms, false);9 10 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);11 12 switch (hparams.n_layer()) {13 case 24:14 switch (hparams.n_embd) {15 case 768: type = LLM_TYPE_SMALL; break;16 default: type = LLM_TYPE_UNKNOWN;17 } break;18 case 48:19 switch (hparams.n_embd) {20 case 1024: type = LLM_TYPE_MEDIUM; break;21 case 1536: type = LLM_TYPE_LARGE; break;22 case 2048: type = LLM_TYPE_XL; break;23 default: type = LLM_TYPE_UNKNOWN;24 } break;25 case 64:26 switch (hparams.n_embd) {27 case 2560: type = LLM_TYPE_3B; break;28 default: type = LLM_TYPE_UNKNOWN;29 } break;30 default: type = LLM_TYPE_UNKNOWN;31 }32}33 34void llama_model_mamba::load_arch_tensors(llama_model_loader &) {35 LLAMA_LOAD_LOCALS;36 37 const int64_t d_conv = hparams.ssm_d_conv;38 const int64_t d_inner = hparams.ssm_d_inner;39 const int64_t d_state = hparams.ssm_d_state;40 const int64_t dt_rank = hparams.ssm_dt_rank;41 42 // only an expansion factor of 2 is supported for now43 if (2 * n_embd != d_inner) {44 throw std::runtime_error("only an expansion factor of 2 is supported for now");45 }46 47 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);48 49 // output50 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);51 52 output = 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 offloading54 if (output == NULL) {55 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);56 }57 58 for (int i = 0; i < n_layer; ++i) {59 auto & layer = layers[i];60 61 // norm62 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);63 64 layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);65 66 layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);67 layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);68 69 layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);70 71 layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);72 layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);73 74 // no "weight" suffix for these75 layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);76 layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);77 78 // out_proj79 layer.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 {84 return 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) {88 ggml_tensor * cur;89 ggml_tensor * inpL;90 91 // {n_embd, n_tokens}92 inpL = build_inp_embd(model.tok_embd);93 94 auto * rs_inp = build_rs_inp();95 96 ggml_tensor * inp_out_ids = build_inp_out_ids();97 98 for (int il = 0; il < n_layer; ++il) {99 // norm100 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);101 cb(cur, "attn_norm", il);102 103 if (model.arch == LLM_ARCH_MAMBA2) {104 cur = build_mamba2_layer(rs_inp, cur, model, ubatch, il);105 } else {106 cur = build_mamba_layer(rs_inp, cur, model, ubatch, il);107 }108 109 if (il == n_layer - 1 && inp_out_ids) {110 cur = ggml_get_rows(ctx0, cur, inp_out_ids);111 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);112 }113 114 // residual115 cur = ggml_add(ctx0, cur, inpL);116 117 cur = build_cvec(cur, il);118 cb(cur, "l_out", il);119 120 // input for next layer121 inpL = cur;122 }123 124 // final rmsnorm125 cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);126 127 cb(cur, "result_norm", -1);128 res->t_embd = cur;129 130 // lm_head131 cur = build_lora_mm(model.output, cur, model.output_s);132 133 cb(cur, "result_output", -1);134 res->t_logits = cur;135 136 ggml_build_forward_expand(gf, cur);137}138 