/src/llama.cpp/src/models/mimo2-iswa.cpp
Line | Count | Source |
1 | | |
2 | | #include "models.h" |
3 | | |
4 | 0 | llm_build_mimo2_iswa::llm_build_mimo2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
5 | 0 | ggml_tensor * cur; |
6 | 0 | ggml_tensor * inpL; |
7 | |
|
8 | 0 | inpL = build_inp_embd(model.tok_embd); |
9 | |
|
10 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
11 | 0 | auto * inp_attn = build_attn_inp_kv_iswa(); |
12 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
13 | |
|
14 | 0 | for (int il = 0; il < n_layer; ++il) { |
15 | 0 | ggml_tensor * inpSA = inpL; |
16 | |
|
17 | 0 | uint32_t n_head_l = hparams.n_head(il); |
18 | 0 | uint32_t n_head_kv_l = hparams.n_head_kv(il); |
19 | 0 | const float freq_base_l = model.get_rope_freq_base(cparams, il); |
20 | 0 | const float freq_scale_l = model.get_rope_freq_scale(cparams, il); |
21 | |
|
22 | 0 | cur = inpL; |
23 | | |
24 | | // self_attention |
25 | 0 | { |
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 | | // compute Q and K and RoPE them |
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_k, n_head_l, n_tokens); |
40 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head_k, n_head_kv_l, n_tokens); |
41 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head_v, n_head_kv_l, n_tokens); |
42 | |
|
43 | 0 | Qcur = ggml_rope_ext( |
44 | 0 | ctx0, Qcur, inp_pos, nullptr, |
45 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
46 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
47 | 0 | ); |
48 | |
|
49 | 0 | Kcur = ggml_rope_ext( |
50 | 0 | ctx0, Kcur, inp_pos, nullptr, |
51 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
52 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
53 | 0 | ); |
54 | |
|
55 | 0 | cb(Qcur, "Qcur", il); |
56 | 0 | cb(Kcur, "Kcur", il); |
57 | 0 | cb(Vcur, "Vcur", il); |
58 | |
|
59 | 0 | ggml_tensor * sinks = model.layers[il].attn_sinks; |
60 | |
|
61 | 0 | cur = build_attn(inp_attn, |
62 | 0 | model.layers[il].wo, NULL, |
63 | 0 | Qcur, Kcur, Vcur, nullptr, sinks, nullptr, 1.0f/sqrtf(float(n_embd_head_k)), il); |
64 | 0 | } |
65 | |
|
66 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
67 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
68 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
69 | 0 | } |
70 | |
|
71 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
72 | 0 | cb(ffn_inp, "ffn_inp", il); |
73 | |
|
74 | 0 | cur = build_norm(ffn_inp, |
75 | 0 | model.layers[il].ffn_norm, NULL, |
76 | 0 | LLM_NORM_RMS, il); |
77 | 0 | cb(cur, "ffn_norm", il); |
78 | | |
79 | | // feed-forward network |
80 | 0 | if (model.layers[il].ffn_gate_inp == nullptr) { |
81 | | // dense branch |
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, |
87 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
88 | 0 | cb(cur, "ffn_out", il); |
89 | 0 | } else { |
90 | | // MoE branch |
91 | 0 | cur = build_moe_ffn(cur, model.layers[il].ffn_gate_inp, model.layers[il].ffn_up_exps, |
92 | 0 | model.layers[il].ffn_gate_exps, model.layers[il].ffn_down_exps, |
93 | 0 | model.layers[il].ffn_exp_probs_b, n_expert, n_expert_used, LLM_FFN_SILU, true, false, |
94 | 0 | 0.0, LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID, il); |
95 | 0 | cb(cur, "ffn_moe_out", il); |
96 | 0 | } |
97 | |
|
98 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
99 | |
|
100 | 0 | cur = build_cvec(cur, il); |
101 | 0 | cb(cur, "l_out", il); |
102 | | |
103 | | // input for next layer |
104 | 0 | inpL = cur; |
105 | 0 | } |
106 | |
|
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 | 0 | cb(cur, "result_output", -1); |
120 | 0 | res->t_logits = cur; |
121 | |
|
122 | 0 | ggml_build_forward_expand(gf, cur); |
123 | 0 | } |