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