/src/llama.cpp/src/models/gemma2-iswa.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | llm_build_gemma2_iswa::llm_build_gemma2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
4 | 0 | const int64_t n_embd_head = hparams.n_embd_head_k; |
5 | |
|
6 | 0 | ggml_tensor * cur; |
7 | 0 | ggml_tensor * inpL; |
8 | |
|
9 | 0 | inpL = build_inp_embd(model.tok_embd); |
10 | |
|
11 | 0 | inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd)); |
12 | 0 | cb(inpL, "inp_scaled", -1); |
13 | | |
14 | | // inp_pos - contains the positions |
15 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
16 | |
|
17 | 0 | auto * inp_attn = build_attn_inp_kv_iswa(); |
18 | |
|
19 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
20 | |
|
21 | 0 | for (int il = 0; il < n_layer; ++il) { |
22 | 0 | const float freq_base_l = model.get_rope_freq_base (cparams, il); |
23 | 0 | const float freq_scale_l = model.get_rope_freq_scale(cparams, il); |
24 | | |
25 | | // norm |
26 | 0 | cur = build_norm(inpL, |
27 | 0 | model.layers[il].attn_norm, NULL, |
28 | 0 | LLM_NORM_RMS, il); |
29 | 0 | cb(cur, "attn_norm", il); |
30 | | |
31 | | // self-attention |
32 | 0 | { |
33 | | // compute Q and K and RoPE them |
34 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
35 | 0 | cb(Qcur, "Qcur", il); |
36 | |
|
37 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
38 | 0 | cb(Kcur, "Kcur", il); |
39 | |
|
40 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
41 | 0 | cb(Vcur, "Vcur", il); |
42 | |
|
43 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
44 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
45 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
46 | |
|
47 | 0 | Qcur = ggml_rope_ext( |
48 | 0 | ctx0, Qcur, inp_pos, nullptr, |
49 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
50 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
51 | |
|
52 | 0 | Kcur = ggml_rope_ext( |
53 | 0 | ctx0, Kcur, inp_pos, nullptr, |
54 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
55 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
56 | |
|
57 | 0 | cb(Qcur, "Qcur", il); |
58 | 0 | cb(Kcur, "Kcur", il); |
59 | 0 | cb(Vcur, "Vcur", il); |
60 | |
|
61 | 0 | Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale); |
62 | |
|
63 | 0 | cur = build_attn(inp_attn, |
64 | 0 | model.layers[il].wo, NULL, |
65 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il); |
66 | 0 | } |
67 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
68 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
69 | 0 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); |
70 | 0 | } |
71 | 0 | cur = build_norm(cur, |
72 | 0 | model.layers[il].attn_post_norm, NULL, |
73 | 0 | LLM_NORM_RMS, il); |
74 | 0 | cb(cur, "attn_post_norm", il); |
75 | |
|
76 | 0 | ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL); |
77 | 0 | cb(sa_out, "sa_out", il); |
78 | |
|
79 | 0 | cur = build_norm(sa_out, |
80 | 0 | model.layers[il].ffn_norm, NULL, |
81 | 0 | LLM_NORM_RMS, il); |
82 | 0 | cb(cur, "ffn_norm", il); |
83 | | |
84 | | // feed-forward network |
85 | 0 | { |
86 | 0 | cur = build_ffn(cur, |
87 | 0 | model.layers[il].ffn_up, NULL, NULL, |
88 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
89 | 0 | model.layers[il].ffn_down, NULL, NULL, |
90 | 0 | NULL, |
91 | 0 | LLM_FFN_GELU, LLM_FFN_PAR, il); |
92 | 0 | cb(cur, "ffn_out", il); |
93 | 0 | } |
94 | 0 | cur = build_norm(cur, |
95 | 0 | model.layers[il].ffn_post_norm, NULL, |
96 | 0 | LLM_NORM_RMS, -1); |
97 | 0 | cb(cur, "ffn_post_norm", -1); |
98 | |
|
99 | 0 | cur = ggml_add(ctx0, cur, sa_out); |
100 | |
|
101 | 0 | cur = build_cvec(cur, il); |
102 | 0 | cb(cur, "l_out", il); |
103 | | |
104 | | // input for next layer |
105 | 0 | inpL = cur; |
106 | 0 | } |
107 | 0 | cur = inpL; |
108 | |
|
109 | 0 | cur = build_norm(cur, |
110 | 0 | model.output_norm, NULL, |
111 | 0 | LLM_NORM_RMS, -1); |
112 | |
|
113 | 0 | cb(cur, "result_norm", -1); |
114 | 0 | res->t_embd = cur; |
115 | | |
116 | | // lm_head |
117 | 0 | cur = build_lora_mm(model.output, cur); |
118 | | |
119 | | // final logit soft-capping |
120 | 0 | cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping); |
121 | 0 | cur = ggml_tanh(ctx0, cur); |
122 | 0 | cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping); |
123 | |
|
124 | 0 | cb(cur, "result_output", -1); |
125 | 0 | res->t_logits = cur; |
126 | |
|
127 | 0 | ggml_build_forward_expand(gf, cur); |
128 | 0 | } |