Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_bitnet::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 6 switch (hparams.n_layer()) {7 case 26: type = LLM_TYPE_3B; break;8 default: type = LLM_TYPE_UNKNOWN;9 }10}11 12void llama_model_bitnet::load_arch_tensors(llama_model_loader &) {13 LLAMA_LOAD_LOCALS;14 15 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);16 17 // output18 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);19 20 for (int i = 0; i < n_layer; ++i) {21 auto & layer = layers[i];22 23 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);24 layer.attn_sub_norm = create_tensor(tn(LLM_TENSOR_ATTN_SUB_NORM, "weight", i), {n_embd}, 0);25 26 layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);27 layer.wq_s = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED);28 layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);29 layer.wk_s = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED);30 layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);31 layer.wv_s = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED);32 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);33 layer.wo_s = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED);34 35 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);36 layer.ffn_sub_norm = create_tensor(tn(LLM_TENSOR_FFN_SUB_NORM, "weight", i), {n_ff}, 0);37 38 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);39 layer.ffn_gate_s = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED);40 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);41 layer.ffn_down_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED);42 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);43 layer.ffn_up_s = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED);44 }45}46 47std::unique_ptr<llm_graph_context> llama_model_bitnet::build_arch_graph(const llm_graph_params & params) const {48 return std::make_unique<graph>(*this, params);49}50 51llama_model_bitnet::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {52 const int64_t n_embd_head = hparams.n_embd_head_v();53 54 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());55 56 ggml_tensor * cur;57 ggml_tensor * inpL;58 59 inpL = build_inp_embd(model.tok_embd);60 61 // inp_pos - contains the positions62 ggml_tensor * inp_pos = build_inp_pos();63 64 auto * inp_attn = build_attn_inp_kv();65 66 ggml_tensor * inp_out_ids = build_inp_out_ids();67 68 for (int il = 0; il < n_layer; ++il) {69 ggml_tensor * inpSA = inpL;70 71 cur = build_norm(inpL,72 model.layers[il].attn_norm, NULL,73 LLM_NORM_RMS, il);74 cb(cur, "attn_norm", il);75 76 // self-attention77 {78 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,79 n_embd_head, n_head, n_head_kv, il);80 81 Qcur = ggml_rope_ext(82 ctx0, Qcur, inp_pos, nullptr,83 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,84 ext_factor, attn_factor, beta_fast, beta_slow85 );86 87 Kcur = ggml_rope_ext(88 ctx0, Kcur, inp_pos, nullptr,89 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,90 ext_factor, attn_factor, beta_fast, beta_slow91 );92 93 cb(Qcur, "Qcur", il);94 cb(Kcur, "Kcur", il);95 cb(Vcur, "Vcur", il);96 97 cur = build_attn(inp_attn,98 NULL, NULL, NULL,99 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);100 101 cur = build_norm(cur,102 model.layers[il].attn_sub_norm, NULL,103 LLM_NORM_RMS, il);104 cb(cur, "attn_sub_norm", il);105 106 cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);107 if (model.layers[il].wo_b) {108 cur = ggml_add(ctx0, cur, model.layers[il].wo_b);109 }110 cb(cur, "attn_out", il);111 }112 113 if (il == n_layer - 1 && inp_out_ids) {114 cur = ggml_get_rows(ctx0, cur, inp_out_ids);115 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);116 }117 118 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);119 cb(ffn_inp, "ffn_inp", il);120 121 // feed-forward forward122 cur = build_norm(ffn_inp,123 model.layers[il].ffn_norm, NULL,124 LLM_NORM_RMS, il);125 cb(cur, "ffn_norm", il);126 127 cur = build_ffn(cur,128 model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,129 model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,130 NULL, NULL, NULL,131 NULL,132 LLM_FFN_SILU, LLM_FFN_PAR, il);133 cb(cur, "ffn_sub_out", il);134 135 cur = build_norm(cur,136 model.layers[il].ffn_sub_norm, NULL,137 LLM_NORM_RMS, il);138 cb(cur, "ffn_sub_norm", il);139 140 cur = build_lora_mm(model.layers[il].ffn_down, cur, model.layers[il].ffn_down_s);141 cb(cur, "ffn_down", il);142 143 cur = ggml_add(ctx0, cur, ffn_inp);144 cb(cur, "l_out", il);145 146 cur = build_cvec(cur, il);147 cb(cur, "l_out", il);148 149 // input for next layer150 inpL = cur;151 }152 153 cur = inpL;154 155 cur = build_norm(cur,156 model.output_norm, NULL,157 LLM_NORM_RMS, -1);158 159 cb(cur, "result_norm", -1);160 res->t_embd = cur;161 162 // lm_head163 // FIXME: do not use model.tok_embd directly, duplicate as model.output164 cur = build_lora_mm(model.tok_embd, cur);165 166 cb(cur, "result_output", -1);167 res->t_logits = cur;168 169 ggml_build_forward_expand(gf, cur);170}171 