Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_gptneox::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);5 ml.get_key(LLM_KV_USE_PARALLEL_RESIDUAL, hparams.use_par_res);6 7 switch (hparams.n_layer()) {8 case 6:9 switch (hparams.n_ff()) {10 case 512: type = LLM_TYPE_14M; break;11 case 2048: type = LLM_TYPE_70M; break;12 default: type = LLM_TYPE_UNKNOWN;13 } break;14 case 12:15 switch (hparams.n_ff()) {16 case 3072: type = LLM_TYPE_160M; break;17 default: type = LLM_TYPE_UNKNOWN;18 } break;19 case 16:20 switch (hparams.n_ff()) {21 case 8192: type = LLM_TYPE_1B; break;22 default: type = LLM_TYPE_UNKNOWN;23 } break;24 case 24:25 switch (hparams.n_ff()) {26 case 4096: type = LLM_TYPE_410M; break;27 case 8192: type = LLM_TYPE_1_4B; break;28 default: type = LLM_TYPE_UNKNOWN;29 } break;30 case 32:31 switch (hparams.n_ff()) {32 case 10240: type = LLM_TYPE_2_8B; break;33 case 16384: type = LLM_TYPE_6_9B; break;34 default: type = LLM_TYPE_UNKNOWN;35 } break;36 case 36:37 switch (hparams.n_ff()) {38 case 20480: type = LLM_TYPE_12B; break;39 default: type = LLM_TYPE_UNKNOWN;40 } break;41 case 44:42 switch (hparams.n_ff()) {43 case 24576: type = LLM_TYPE_20B; break;44 default: type = LLM_TYPE_UNKNOWN;45 } break;46 default: type = LLM_TYPE_UNKNOWN;47 }48}49 50void llama_model_gptneox::load_arch_tensors(llama_model_loader &) {51 LLAMA_LOAD_LOCALS;52 53 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);54 55 // output56 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);57 output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);58 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);59 60 for (int i = 0; i < n_layer; ++i) {61 auto & layer = layers[i];62 63 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);64 layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);65 66 layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);67 layer.wqkv_b = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);68 69 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);70 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);71 72 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);73 layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);74 75 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);76 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);77 78 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);79 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);80 }81}82 83std::unique_ptr<llm_graph_context> llama_model_gptneox::build_arch_graph(const llm_graph_params & params) const {84 return std::make_unique<graph>(*this, params);85}86 87llama_model_gptneox::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {88 const int64_t n_embd_head = hparams.n_embd_head_v();89 90 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());91 92 ggml_tensor * cur;93 ggml_tensor * inpL;94 95 inpL = build_inp_embd(model.tok_embd);96 97 // inp_pos - contains the positions98 ggml_tensor * inp_pos = build_inp_pos();99 100 auto * inp_attn = build_attn_inp_kv();101 102 ggml_tensor * inp_out_ids = build_inp_out_ids();103 104 for (int il = 0; il < n_layer; ++il) {105 cur = build_norm(inpL,106 model.layers[il].attn_norm,107 model.layers[il].attn_norm_b,108 LLM_NORM, il);109 cb(cur, "attn_norm", il);110 111 // self-attention112 {113 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,114 n_embd_head, n_head, n_head_kv, il);115 116 Qcur = ggml_rope_ext(117 ctx0, Qcur, inp_pos, nullptr,118 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,119 ext_factor, attn_factor, beta_fast, beta_slow120 );121 122 Kcur = ggml_rope_ext(123 ctx0, Kcur, inp_pos, nullptr,124 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,125 ext_factor, attn_factor, beta_fast, beta_slow126 );127 128 cb(Qcur, "Qcur", il);129 cb(Kcur, "Kcur", il);130 cb(Vcur, "Vcur", il);131 132 cur = build_attn(inp_attn,133 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,134 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);135 }136 137 if (il == n_layer - 1 && inp_out_ids) {138 cur = ggml_get_rows(ctx0, cur, inp_out_ids);139 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);140 }141 142 // ffn143 if (hparams.use_par_res) {144 // attention and ffn are computed in parallel145 // x = x + attn(ln1(x)) + ffn(ln2(x))146 147 ggml_tensor * attn_out = cur;148 149 cur = build_norm(inpL,150 model.layers[il].ffn_norm,151 model.layers[il].ffn_norm_b,152 LLM_NORM, il);153 cb(cur, "ffn_norm", il);154 155 cur = build_ffn(cur,156 model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,157 NULL, NULL, NULL,158 model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,159 NULL,160 LLM_FFN_GELU, LLM_FFN_SEQ, il);161 cb(cur, "ffn_out", il);162 163 cur = ggml_add(ctx0, cur, inpL);164 cb(cur, "ffn_out", il);165 166 cur = ggml_add(ctx0, cur, attn_out);167 168 cur = build_cvec(cur, il);169 cb(cur, "l_out", il);170 171 // input for next layer172 inpL = cur;173 } else {174 // attention and ffn are computed sequentially175 // x = x + attn(ln1(x))176 // x = x + ffn(ln2(x))177 178 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);179 cb(ffn_inp, "ffn_inp", il);180 181 cur = build_norm(ffn_inp,182 model.layers[il].ffn_norm,183 model.layers[il].ffn_norm_b,184 LLM_NORM, il);185 cb(cur, "ffn_norm", il);186 187 cur = build_ffn(cur,188 model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,189 NULL, NULL, NULL,190 model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,191 NULL,192 LLM_FFN_GELU, LLM_FFN_SEQ, il);193 cb(cur, "ffn_out", il);194 195 cur = ggml_add(ctx0, cur, ffn_inp);196 197 cur = build_cvec(cur, il);198 cb(cur, "l_out", il);199 200 // input for next layer201 inpL = cur;202 }203 }204 205 cur = build_norm(inpL,206 model.output_norm,207 model.output_norm_b,208 LLM_NORM, -1);209 210 cb(cur, "result_norm", -1);211 res->t_embd = cur;212 213 cur = build_lora_mm(model.output, cur, model.output_s);214 215 cb(cur, "result_output", -1);216 res->t_logits = cur;217 218 ggml_build_forward_expand(gf, cur);219}220 