/src/llama.cpp/src/models/llama-iswa.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | llm_build_llama_iswa::llm_build_llama_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_v; |
5 | |
|
6 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
7 | 0 | GGML_ASSERT(n_embd_head == hparams.n_rot); |
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 | | // temperature tuning |
18 | 0 | ggml_tensor * inp_attn_scale = nullptr; |
19 | 0 | inp_attn_scale = build_inp_attn_scale(); |
20 | |
|
21 | 0 | auto * inp_attn = build_attn_inp_kv_iswa(); |
22 | |
|
23 | 0 | const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale; |
24 | |
|
25 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
26 | |
|
27 | 0 | for (int il = 0; il < n_layer; ++il) { |
28 | 0 | ggml_tensor * inpSA = inpL; |
29 | |
|
30 | 0 | const bool use_rope = hparams.n_no_rope_layer_step > 0 && |
31 | 0 | (il + 1) % hparams.n_no_rope_layer_step != 0; |
32 | | |
33 | | // norm |
34 | 0 | cur = build_norm(inpL, |
35 | 0 | model.layers[il].attn_norm, NULL, |
36 | 0 | LLM_NORM_RMS, il); |
37 | 0 | cb(cur, "attn_norm", il); |
38 | | |
39 | | // self-attention |
40 | 0 | { |
41 | | // rope freq factors for llama3; may return nullptr for llama2 and other models |
42 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
43 | | |
44 | | // compute Q and K and RoPE them |
45 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
46 | 0 | cb(Qcur, "Qcur", il); |
47 | 0 | if (model.layers[il].bq) { |
48 | 0 | Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq); |
49 | 0 | cb(Qcur, "Qcur", il); |
50 | 0 | } |
51 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
52 | 0 | cb(Kcur, "Kcur", il); |
53 | 0 | if (model.layers[il].bk) { |
54 | 0 | Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk); |
55 | 0 | cb(Kcur, "Kcur", il); |
56 | 0 | } |
57 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
58 | 0 | cb(Vcur, "Vcur", il); |
59 | 0 | if (model.layers[il].bv) { |
60 | 0 | Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv); |
61 | 0 | cb(Vcur, "Vcur", il); |
62 | 0 | } |
63 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
64 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
65 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
66 | |
|
67 | 0 | if (use_rope) { |
68 | 0 | Qcur = ggml_rope_ext( |
69 | 0 | ctx0, Qcur, inp_pos, rope_factors, |
70 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
71 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
72 | 0 | ); |
73 | |
|
74 | 0 | Kcur = ggml_rope_ext( |
75 | 0 | ctx0, Kcur, inp_pos, rope_factors, |
76 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
77 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
78 | 0 | ); |
79 | 0 | } else if (inp_attn_scale) { |
80 | 0 | Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale); |
81 | 0 | } |
82 | 0 | cb(Qcur, "Qcur", il); |
83 | 0 | cb(Kcur, "Kcur", il); |
84 | 0 | cb(Vcur, "Vcur", il); |
85 | |
|
86 | 0 | if (use_rope && hparams.use_kq_norm) { |
87 | | // Llama4TextL2Norm |
88 | 0 | Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps); |
89 | 0 | Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps); |
90 | 0 | cb(Qcur, "Qcur_normed", il); |
91 | 0 | cb(Kcur, "Kcur_normed", il); |
92 | 0 | } |
93 | 0 | cur = build_attn(inp_attn, |
94 | 0 | model.layers[il].wo, model.layers[il].bo, |
95 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
96 | 0 | cb(cur, "attn_out", il); |
97 | 0 | } |
98 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
99 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
100 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
101 | 0 | } |
102 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
103 | 0 | cb(ffn_inp, "ffn_inp", il); |
104 | | |
105 | | // feed-forward network (non-MoE) |
106 | 0 | if (model.layers[il].ffn_gate_inp == nullptr) { |
107 | 0 | cur = build_norm(ffn_inp, |
108 | 0 | model.layers[il].ffn_norm, NULL, |
109 | 0 | LLM_NORM_RMS, il); |
110 | 0 | cb(cur, "ffn_norm", il); |
111 | |
|
112 | 0 | cur = build_ffn(cur, |
113 | 0 | model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL, |
114 | 0 | model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL, |
115 | 0 | model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, |
116 | 0 | NULL, |
117 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
118 | 0 | cb(cur, "ffn_out", il); |
119 | 0 | } else { |
120 | 0 | ggml_tensor * ffn_inp_normed = build_norm(ffn_inp, |
121 | 0 | model.layers[il].ffn_norm, NULL, |
122 | 0 | LLM_NORM_RMS, il); |
123 | 0 | cb(cur, "ffn_norm", il); |
124 | |
|
125 | 0 | ggml_tensor * moe_out = build_moe_ffn(ffn_inp_normed, |
126 | 0 | model.layers[il].ffn_gate_inp, |
127 | 0 | model.layers[il].ffn_up_exps, |
128 | 0 | model.layers[il].ffn_gate_exps, |
129 | 0 | model.layers[il].ffn_down_exps, |
130 | 0 | nullptr, |
131 | 0 | n_expert, n_expert_used, |
132 | 0 | LLM_FFN_SILU, false, |
133 | 0 | false, 0.0, |
134 | 0 | LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID, |
135 | 0 | il); |
136 | | |
137 | | // Shared experts |
138 | 0 | ggml_tensor * shexp_out = build_ffn(ffn_inp_normed, |
139 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
140 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
141 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
142 | 0 | NULL, |
143 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
144 | 0 | cb(shexp_out, "ffn_moe_shexp", il); |
145 | |
|
146 | 0 | cur = ggml_add(ctx0, moe_out, shexp_out); |
147 | 0 | cb(cur, "ffn_moe_out_merged", il); |
148 | 0 | } |
149 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
150 | 0 | cb(cur, "ffn_out", il); |
151 | |
|
152 | 0 | cur = build_cvec(cur, il); |
153 | 0 | cb(cur, "l_out", il); |
154 | | |
155 | | // input for next layer |
156 | 0 | inpL = cur; |
157 | 0 | } |
158 | 0 | cur = inpL; |
159 | |
|
160 | 0 | cur = build_norm(cur, |
161 | 0 | model.output_norm, NULL, |
162 | 0 | LLM_NORM_RMS, -1); |
163 | |
|
164 | 0 | cb(cur, "result_norm", -1); |
165 | 0 | res->t_embd = cur; |
166 | | |
167 | | // lm_head |
168 | 0 | cur = build_lora_mm(model.output, cur); |
169 | |
|
170 | 0 | cb(cur, "result_output", -1); |
171 | 0 | res->t_logits = cur; |
172 | |
|
173 | 0 | ggml_build_forward_expand(gf, cur); |
174 | 0 | } |