diff options
Diffstat (limited to 'llama.cpp/src/models/cohere2-iswa.cpp')
| -rw-r--r-- | llama.cpp/src/models/cohere2-iswa.cpp | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/llama.cpp/src/models/cohere2-iswa.cpp b/llama.cpp/src/models/cohere2-iswa.cpp new file mode 100644 index 0000000..9334b5e --- /dev/null +++ b/llama.cpp/src/models/cohere2-iswa.cpp | |||
| @@ -0,0 +1,134 @@ | |||
| 1 | #include "models.h" | ||
| 2 | |||
| 3 | llm_build_cohere2_iswa::llm_build_cohere2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { | ||
| 4 | const int64_t n_embd_head = hparams.n_embd_head_v; | ||
| 5 | |||
| 6 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); | ||
| 7 | |||
| 8 | const float f_logit_scale = hparams.f_logit_scale; | ||
| 9 | |||
| 10 | ggml_tensor * cur; | ||
| 11 | ggml_tensor * inpL; | ||
| 12 | |||
| 13 | inpL = build_inp_embd(model.tok_embd); | ||
| 14 | |||
| 15 | // inp_pos - contains the positions | ||
| 16 | ggml_tensor * inp_pos = build_inp_pos(); | ||
| 17 | |||
| 18 | auto * inp_attn = build_attn_inp_kv_iswa(); | ||
| 19 | |||
| 20 | ggml_tensor * inp_out_ids = build_inp_out_ids(); | ||
| 21 | |||
| 22 | for (int il = 0; il < n_layer; ++il) { | ||
| 23 | const bool is_swa = hparams.is_swa(il); | ||
| 24 | // UNUSED: | ||
| 25 | // const float freq_base_l = model.get_rope_freq_base (cparams, il); | ||
| 26 | // const float freq_scale_l = model.get_rope_freq_scale(cparams, il); | ||
| 27 | |||
| 28 | // norm | ||
| 29 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il); | ||
| 30 | cb(cur, "attn_norm", il); | ||
| 31 | ggml_tensor * ffn_inp = cur; | ||
| 32 | |||
| 33 | // self-attention | ||
| 34 | { | ||
| 35 | // rope freq factors for 128k context | ||
| 36 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); | ||
| 37 | |||
| 38 | // compute Q and K and RoPE them | ||
| 39 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); | ||
| 40 | cb(Qcur, "Qcur", il); | ||
| 41 | if (model.layers[il].bq) { | ||
| 42 | Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq); | ||
| 43 | cb(Qcur, "Qcur", il); | ||
| 44 | } | ||
| 45 | |||
| 46 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); | ||
| 47 | cb(Kcur, "Kcur", il); | ||
| 48 | if (model.layers[il].bk) { | ||
| 49 | Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk); | ||
| 50 | cb(Kcur, "Kcur", il); | ||
| 51 | } | ||
| 52 | |||
| 53 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); | ||
| 54 | cb(Vcur, "Vcur", il); | ||
| 55 | if (model.layers[il].bv) { | ||
| 56 | Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv); | ||
| 57 | cb(Vcur, "Vcur", il); | ||
| 58 | } | ||
| 59 | |||
| 60 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); | ||
| 61 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); | ||
| 62 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); | ||
| 63 | |||
| 64 | if (is_swa) { | ||
| 65 | Qcur = ggml_rope_ext( | ||
| 66 | ctx0, Qcur, inp_pos, rope_factors, | ||
| 67 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, | ||
| 68 | ext_factor, attn_factor, beta_fast, beta_slow | ||
| 69 | ); | ||
| 70 | |||
| 71 | Kcur = ggml_rope_ext( | ||
| 72 | ctx0, Kcur, inp_pos, rope_factors, | ||
| 73 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, | ||
| 74 | ext_factor, attn_factor, beta_fast, beta_slow | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | |||
| 78 | cb(Qcur, "Qcur", il); | ||
| 79 | cb(Kcur, "Kcur", il); | ||
| 80 | cb(Vcur, "Vcur", il); | ||
| 81 | |||
| 82 | cur = build_attn(inp_attn, | ||
| 83 | model.layers[il].wo, model.layers[il].bo, | ||
| 84 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); | ||
| 85 | } | ||
| 86 | |||
| 87 | if (il == n_layer - 1 && inp_out_ids) { | ||
| 88 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); | ||
| 89 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); | ||
| 90 | ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids); | ||
| 91 | } | ||
| 92 | |||
| 93 | ggml_tensor * attn_out = cur; | ||
| 94 | |||
| 95 | // feed-forward network | ||
| 96 | { | ||
| 97 | cur = build_ffn(ffn_inp, | ||
| 98 | model.layers[il].ffn_up, NULL, NULL, | ||
| 99 | model.layers[il].ffn_gate, NULL, NULL, | ||
| 100 | model.layers[il].ffn_down, NULL, NULL, | ||
| 101 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); | ||
| 102 | cb(cur, "ffn_out", il); | ||
| 103 | } | ||
| 104 | |||
| 105 | // add together residual + FFN + self-attention | ||
| 106 | cur = ggml_add(ctx0, cur, inpL); | ||
| 107 | cur = ggml_add(ctx0, cur, attn_out); | ||
| 108 | |||
| 109 | cur = build_cvec(cur, il); | ||
| 110 | cb(cur, "l_out", il); | ||
| 111 | |||
| 112 | // input for next layer | ||
| 113 | inpL = cur; | ||
| 114 | } | ||
| 115 | |||
| 116 | cur = inpL; | ||
| 117 | |||
| 118 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1); | ||
| 119 | |||
| 120 | cb(cur, "result_norm", -1); | ||
| 121 | res->t_embd = cur; | ||
| 122 | |||
| 123 | // lm_head | ||
| 124 | cur = build_lora_mm(model.output, cur); | ||
| 125 | |||
| 126 | if (f_logit_scale) { | ||
| 127 | cur = ggml_scale(ctx0, cur, f_logit_scale); | ||
| 128 | } | ||
| 129 | |||
| 130 | cb(cur, "result_output", -1); | ||
| 131 | res->t_logits = cur; | ||
| 132 | |||
| 133 | ggml_build_forward_expand(gf, cur); | ||
| 134 | } | ||
