/src/llama.cpp/src/models/deepseek.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_deepseek::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
5 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
6 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
7 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
8 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
9 | |
|
10 | 0 | switch (hparams.n_ff_exp) { |
11 | 0 | case 1408: type = LLM_TYPE_16B; break; |
12 | 0 | case 1792: type = LLM_TYPE_20B; break; |
13 | 0 | default: type = LLM_TYPE_UNKNOWN; |
14 | 0 | } |
15 | 0 | } |
16 | | |
17 | 0 | void llama_model_deepseek::load_arch_tensors(llama_model_loader &) { |
18 | 0 | LLAMA_LOAD_LOCALS; |
19 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
20 | | |
21 | |
|
22 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
23 | |
|
24 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
25 | | |
26 | | // output |
27 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
28 | | // try to load output.weight, if not found, use token_embd (tied embeddings) |
29 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
30 | 0 | if (!output) { |
31 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
32 | 0 | } |
33 | |
|
34 | 0 | for (int i = 0; i < n_layer; ++i) { |
35 | 0 | auto & layer = layers[i]; |
36 | |
|
37 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
38 | |
|
39 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0); |
40 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
41 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
42 | |
|
43 | 0 | if (i < (int) hparams.n_layer_dense_lead) { |
44 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
45 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
46 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
47 | 0 | } else { |
48 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
49 | |
|
50 | 0 | if (n_expert == 0) { |
51 | 0 | throw std::runtime_error("n_expert must be > 0"); |
52 | 0 | } |
53 | 0 | if (n_expert_used == 0) { |
54 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
55 | 0 | } |
56 | | |
57 | | // MoE branch |
58 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
59 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
60 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
61 | | |
62 | | // Shared expert branch |
63 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
64 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0); |
65 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
66 | 0 | } |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | 0 | std::unique_ptr<llm_graph_context> llama_model_deepseek::build_arch_graph(const llm_graph_params & params) const { |
71 | 0 | return std::make_unique<graph>(*this, params); |
72 | 0 | } |
73 | | |
74 | | llama_model_deepseek::graph::graph(const llama_model & model, const llm_graph_params & params) : |
75 | 0 | llm_graph_context(params) { |
76 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
77 | |
|
78 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
79 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
80 | |
|
81 | 0 | ggml_tensor * cur; |
82 | 0 | ggml_tensor * inpL; |
83 | |
|
84 | 0 | inpL = build_inp_embd(model.tok_embd); |
85 | | |
86 | | // inp_pos - contains the positions |
87 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
88 | |
|
89 | 0 | auto * inp_attn = build_attn_inp_kv(); |
90 | |
|
91 | 0 | const float kq_scale = |
92 | 0 | hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale; |
93 | |
|
94 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
95 | |
|
96 | 0 | for (int il = 0; il < n_layer; ++il) { |
97 | 0 | ggml_tensor * inpSA = inpL; |
98 | | |
99 | | // norm |
100 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
101 | 0 | cb(cur, "attn_norm", il); |
102 | | |
103 | | // self-attention |
104 | 0 | { |
105 | | // rope freq factors for llama3; may return nullptr for llama2 and other models |
106 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
107 | | |
108 | | // compute Q and K and RoPE them |
109 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
110 | 0 | n_embd_head, n_head, n_head_kv, il); |
111 | |
|
112 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
113 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
114 | |
|
115 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
116 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
117 | |
|
118 | 0 | cb(Qcur, "Qcur", il); |
119 | 0 | cb(Kcur, "Kcur", il); |
120 | 0 | cb(Vcur, "Vcur", il); |
121 | |
|
122 | 0 | cur = build_attn(inp_attn, |
123 | 0 | model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
124 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
125 | 0 | } |
126 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
127 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
128 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
129 | 0 | } |
130 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
131 | 0 | cb(ffn_inp, "ffn_inp", il); |
132 | |
|
133 | 0 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
134 | 0 | cb(cur, "ffn_norm", il); |
135 | |
|
136 | 0 | if ((uint32_t) il < hparams.n_layer_dense_lead) { |
137 | 0 | cur = build_ffn(cur, |
138 | 0 | model.layers[il].ffn_up, NULL, NULL, |
139 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
140 | 0 | model.layers[il].ffn_down, NULL, NULL, |
141 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
142 | 0 | cb(cur, "ffn_out", il); |
143 | 0 | } else { |
144 | | // MoE branch |
145 | 0 | ggml_tensor * moe_out = build_moe_ffn(cur, |
146 | 0 | model.layers[il].ffn_gate_inp, |
147 | 0 | model.layers[il].ffn_up_exps, |
148 | 0 | model.layers[il].ffn_gate_exps, |
149 | 0 | model.layers[il].ffn_down_exps, |
150 | 0 | nullptr, |
151 | 0 | n_expert, n_expert_used, |
152 | 0 | LLM_FFN_SILU, false, |
153 | 0 | hparams.expert_weights_scale, |
154 | 0 | LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
155 | 0 | il); |
156 | 0 | cb(moe_out, "ffn_moe_out", il); |
157 | | |
158 | | // FFN shared expert |
159 | 0 | { |
160 | 0 | ggml_tensor * ffn_shexp = |
161 | 0 | build_ffn(cur, |
162 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
163 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
164 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
165 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
166 | 0 | cb(ffn_shexp, "ffn_shexp", il); |
167 | |
|
168 | 0 | cur = ggml_add(ctx0, moe_out, ffn_shexp); |
169 | 0 | cb(cur, "ffn_out", il); |
170 | 0 | } |
171 | 0 | } |
172 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
173 | |
|
174 | 0 | cur = build_cvec(cur, il); |
175 | 0 | cb(cur, "l_out", il); |
176 | | |
177 | | // input for next layer |
178 | 0 | inpL = cur; |
179 | 0 | } |
180 | 0 | cur = inpL; |
181 | |
|
182 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
183 | |
|
184 | 0 | cb(cur, "result_norm", -1); |
185 | 0 | res->t_embd = cur; |
186 | | |
187 | | // lm_head |
188 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
189 | |
|
190 | 0 | cb(cur, "result_output", -1); |
191 | 0 | res->t_logits = cur; |
192 | |
|
193 | 0 | ggml_build_forward_expand(gf, cur); |
194 | 0 | } |