diff options
Diffstat (limited to 'llama.cpp/src/models/falcon-h1.cpp')
| -rw-r--r-- | llama.cpp/src/models/falcon-h1.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/llama.cpp/src/models/falcon-h1.cpp b/llama.cpp/src/models/falcon-h1.cpp new file mode 100644 index 0000000..b641a09 --- /dev/null +++ b/llama.cpp/src/models/falcon-h1.cpp | |||
| @@ -0,0 +1,113 @@ | |||
| 1 | #include "models.h" | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | llm_build_falcon_h1::llm_build_falcon_h1(const llama_model & model, const llm_graph_params & params) : | ||
| 6 | llm_graph_context_mamba(params) { | ||
| 7 | const int64_t n_embd_head = hparams.n_embd_head_v; | ||
| 8 | |||
| 9 | ggml_tensor * cur; | ||
| 10 | ggml_tensor * inpL; | ||
| 11 | |||
| 12 | inpL = build_inp_embd(model.tok_embd); | ||
| 13 | |||
| 14 | // inp_pos - contains the positions | ||
| 15 | ggml_tensor * inp_pos = build_inp_pos(); | ||
| 16 | |||
| 17 | // Build the inputs in the recurrent & kv cache | ||
| 18 | auto * inp = build_inp_mem_hybrid(); | ||
| 19 | |||
| 20 | const float kq_scale = | ||
| 21 | hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale; | ||
| 22 | |||
| 23 | ggml_tensor * inp_out_ids = build_inp_out_ids(); | ||
| 24 | |||
| 25 | for (int il = 0; il < n_layer; ++il) { | ||
| 26 | ggml_tensor * inpSA = inpL; | ||
| 27 | |||
| 28 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); | ||
| 29 | cb(cur, "attn_norm", il); | ||
| 30 | |||
| 31 | // self-attention | ||
| 32 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); | ||
| 33 | cb(Qcur, "Qcur", il); | ||
| 34 | |||
| 35 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); | ||
| 36 | cb(Kcur, "Kcur", il); | ||
| 37 | |||
| 38 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); | ||
| 39 | cb(Vcur, "Vcur", il); | ||
| 40 | |||
| 41 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); | ||
| 42 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); | ||
| 43 | |||
| 44 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); | ||
| 45 | |||
| 46 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale, | ||
| 47 | ext_factor, attn_factor, beta_fast, beta_slow); | ||
| 48 | |||
| 49 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale, | ||
| 50 | ext_factor, attn_factor, beta_fast, beta_slow); | ||
| 51 | |||
| 52 | cb(Qcur, "Qcur-post-rope", il); | ||
| 53 | cb(Kcur, "Kcur-post-rope", il); | ||
| 54 | cb(Vcur, "Vcur-post-rope", il); | ||
| 55 | |||
| 56 | ggml_tensor * attn_out = build_attn(inp->get_attn(), | ||
| 57 | model.layers[il].wo, NULL, | ||
| 58 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); | ||
| 59 | cb(attn_out, "attn_out", il); | ||
| 60 | |||
| 61 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); | ||
| 62 | // Mamba2 layer | ||
| 63 | cb(cur, "ssm_in", il); | ||
| 64 | |||
| 65 | ggml_tensor * ssm_out = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il); | ||
| 66 | cb(ssm_out, "ssm_out", il); | ||
| 67 | |||
| 68 | // // Aggregation | ||
| 69 | cur = ggml_add(ctx0, attn_out, ssm_out); | ||
| 70 | inpSA = ggml_add(ctx0, cur, inpSA); | ||
| 71 | cb(cur, "layer_out", il); | ||
| 72 | |||
| 73 | if (il == n_layer - 1 && inp_out_ids) { | ||
| 74 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); | ||
| 75 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); | ||
| 76 | } | ||
| 77 | ggml_tensor * ffn_inp = inpSA; | ||
| 78 | cb(ffn_inp, "ffn_inp", il); | ||
| 79 | |||
| 80 | // feed-forward network | ||
| 81 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); | ||
| 82 | cb(cur, "ffn_norm", il); | ||
| 83 | |||
| 84 | cur = build_ffn(cur, | ||
| 85 | model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL, | ||
| 86 | model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL, | ||
| 87 | model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, | ||
| 88 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); | ||
| 89 | cb(cur, "ffn_out", il); | ||
| 90 | |||
| 91 | cur = ggml_add(ctx0, cur, inpSA); | ||
| 92 | |||
| 93 | cur = build_cvec(cur, il); | ||
| 94 | cb(cur, "l_out", il); | ||
| 95 | |||
| 96 | // input for next layer | ||
| 97 | inpL = cur; | ||
| 98 | } | ||
| 99 | cur = inpL; | ||
| 100 | |||
| 101 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); | ||
| 102 | |||
| 103 | cb(cur, "result_norm", -1); | ||
| 104 | res->t_embd = cur; | ||
| 105 | |||
| 106 | // lm_head | ||
| 107 | cur = build_lora_mm(model.output, cur); | ||
| 108 | |||
| 109 | cb(cur, "result_output", -1); | ||
| 110 | res->t_logits = cur; | ||
| 111 | |||
| 112 | ggml_build_forward_expand(gf, cur); | ||
| 113 | } | ||
