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