/src/llama.cpp/src/models/apertus.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | |
4 | | |
5 | 0 | llm_build_apertus::llm_build_apertus(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
6 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v; |
7 | |
|
8 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
9 | 0 | GGML_ASSERT(n_embd_head == hparams.n_rot); |
10 | |
|
11 | 0 | ggml_tensor * cur; |
12 | 0 | ggml_tensor * inpL; |
13 | |
|
14 | 0 | inpL = build_inp_embd(model.tok_embd); |
15 | |
|
16 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
17 | 0 | auto * inp_attn = build_attn_inp_kv(); |
18 | |
|
19 | 0 | const float kq_scale = |
20 | 0 | hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale; |
21 | |
|
22 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
23 | |
|
24 | 0 | for (int il = 0; il < n_layer; ++il) { |
25 | 0 | ggml_tensor * inpSA = inpL; |
26 | |
|
27 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il); |
28 | 0 | cb(cur, "attn_norm", il); |
29 | | |
30 | | // self-attention |
31 | 0 | { |
32 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
33 | | |
34 | | // compute Q and K and RoPE them |
35 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
36 | 0 | cb(Qcur, "Qcur", il); |
37 | |
|
38 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
39 | 0 | cb(Kcur, "Kcur", il); |
40 | |
|
41 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
42 | 0 | cb(Vcur, "Vcur", il); |
43 | |
|
44 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
45 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
46 | 0 | cb(Qcur, "Qcur_normed", il); |
47 | |
|
48 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
49 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
50 | 0 | cb(Kcur, "Kcur_normed", il); |
51 | |
|
52 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
53 | |
|
54 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
55 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
56 | |
|
57 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
58 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
59 | |
|
60 | 0 | cb(Qcur, "Qcur_pos", il); |
61 | 0 | cb(Kcur, "Kcur_pos", il); |
62 | 0 | cb(Vcur, "Vcur_pos", il); |
63 | |
|
64 | 0 | cur = build_attn(inp_attn, |
65 | 0 | model.layers[il].wo, model.layers[il].bo, |
66 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
67 | 0 | cb(cur, "attn_out", il); |
68 | 0 | } |
69 | |
|
70 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
71 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
72 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
73 | 0 | } |
74 | |
|
75 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
76 | 0 | cb(ffn_inp, "ffn_inp", il); |
77 | | |
78 | | // feed-forward network with xIELU activation |
79 | 0 | { |
80 | 0 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, nullptr, LLM_NORM_RMS, il); |
81 | 0 | cb(cur, "ffn_norm", il); |
82 | | |
83 | | // Up projection |
84 | 0 | ggml_tensor * up = build_lora_mm(model.layers[il].ffn_up, cur); |
85 | 0 | cb(up, "ffn_up", il); |
86 | |
|
87 | 0 | float alpha_n_val = hparams.xielu_alpha_n[il]; |
88 | 0 | float alpha_p_val = hparams.xielu_alpha_p[il]; |
89 | 0 | float beta_val = hparams.xielu_beta[il]; |
90 | 0 | float eps_val = hparams.xielu_eps[il]; |
91 | | |
92 | | // Apply xIELU activation |
93 | 0 | ggml_tensor * activated = ggml_xielu(ctx0, up, alpha_n_val, alpha_p_val, beta_val, eps_val); |
94 | 0 | cb(activated, "ffn_xielu", il); |
95 | | |
96 | | // Down projection |
97 | 0 | cur = build_lora_mm(model.layers[il].ffn_down, activated); |
98 | 0 | cb(cur, "ffn_down", il); |
99 | 0 | } |
100 | |
|
101 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
102 | 0 | cb(cur, "ffn_out", il); |
103 | |
|
104 | 0 | cur = build_cvec(cur, il); |
105 | 0 | cb(cur, "l_out", il); |
106 | | |
107 | | // input for next layer |
108 | 0 | inpL = cur; |
109 | 0 | } |
110 | |
|
111 | 0 | cur = inpL; |
112 | |
|
113 | 0 | cur = build_norm(cur, model.output_norm, nullptr, LLM_NORM_RMS, -1); |
114 | |
|
115 | 0 | cb(cur, "result_norm", -1); |
116 | 0 | res->t_embd = cur; |
117 | | |
118 | | // lm_head |
119 | 0 | cur = build_lora_mm(model.output, cur); |
120 | |
|
121 | 0 | cb(cur, "result_output", -1); |
122 | 0 | res->t_logits = cur; |
123 | |
|
124 | 0 | ggml_build_forward_expand(gf, cur); |
125 | 0 | } |