/src/llama.cpp/src/models/deci.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | |
4 | | |
5 | 0 | llm_build_deci::llm_build_deci(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 | | // inp_pos - contains the positions |
17 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
18 | |
|
19 | 0 | auto * inp_attn = build_attn_inp_kv(); |
20 | |
|
21 | 0 | const float kq_scale = |
22 | 0 | hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale; |
23 | |
|
24 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
25 | |
|
26 | 0 | for (int il = 0; il < n_layer; ++il) { |
27 | 0 | ggml_tensor * inpSA = inpL; |
28 | 0 | const int64_t n_head_kv = hparams.n_head_kv(il); |
29 | 0 | const int64_t n_head = hparams.n_head(il); |
30 | 0 | const int64_t n_ff = hparams.n_ff(il); |
31 | |
|
32 | 0 | if (n_head == 0) { |
33 | | // attention-free layer of Llama-3_1-Nemotron-51B |
34 | 0 | cur = inpL; |
35 | 0 | } else { |
36 | | // norm |
37 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
38 | 0 | cb(cur, "attn_norm", il); |
39 | 0 | } |
40 | 0 | if (n_head > 0 && n_head_kv == 0) { |
41 | | // "linear attention" of Llama-3_1-Nemotron-51B |
42 | 0 | cur = build_lora_mm(model.layers[il].wo, cur); |
43 | 0 | cb(cur, "wo", il); |
44 | 0 | } else if (n_head > 0) { |
45 | | // self-attention |
46 | | // rope freq factors for llama3; may return nullptr for llama2 and other models |
47 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
48 | | |
49 | | // compute Q and K and RoPE them |
50 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
51 | 0 | cb(Qcur, "Qcur", il); |
52 | 0 | if (model.layers[il].bq) { |
53 | 0 | Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq); |
54 | 0 | cb(Qcur, "Qcur", il); |
55 | 0 | } |
56 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
57 | 0 | cb(Kcur, "Kcur", il); |
58 | 0 | if (model.layers[il].bk) { |
59 | 0 | Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk); |
60 | 0 | cb(Kcur, "Kcur", il); |
61 | 0 | } |
62 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
63 | 0 | cb(Vcur, "Vcur", il); |
64 | 0 | if (model.layers[il].bv) { |
65 | 0 | Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv); |
66 | 0 | cb(Vcur, "Vcur", il); |
67 | 0 | } |
68 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
69 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
70 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
71 | |
|
72 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
73 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
74 | |
|
75 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
76 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
77 | |
|
78 | 0 | cb(Qcur, "Qcur", il); |
79 | 0 | cb(Kcur, "Kcur", il); |
80 | 0 | cb(Vcur, "Vcur", il); |
81 | |
|
82 | 0 | cur = build_attn(inp_attn, |
83 | 0 | model.layers[il].wo, model.layers[il].bo, |
84 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
85 | 0 | } |
86 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
87 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
88 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
89 | 0 | } |
90 | | // FFN-free layer of Llama-3_1-Nemotron-Ultra-253B |
91 | 0 | if (n_ff == 0) { |
92 | 0 | continue; |
93 | 0 | } |
94 | | // modified to support attention-free layer of Llama-3_1-Nemotron-51B |
95 | 0 | ggml_tensor * ffn_inp = cur; |
96 | 0 | if (n_head > 0) { |
97 | 0 | ffn_inp = ggml_add(ctx0, cur, inpSA); |
98 | 0 | cb(ffn_inp, "ffn_inp", il); |
99 | 0 | } |
100 | | // feed-forward network |
101 | 0 | if (model.layers[il].ffn_gate_inp == nullptr) { |
102 | 0 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
103 | 0 | cb(cur, "ffn_norm", il); |
104 | |
|
105 | 0 | cur = build_ffn(cur, |
106 | 0 | model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL, |
107 | 0 | model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL, |
108 | 0 | model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, |
109 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
110 | 0 | cb(cur, "ffn_out", il); |
111 | 0 | } |
112 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
113 | 0 | cb(cur, "ffn_out", il); |
114 | |
|
115 | 0 | cur = build_cvec(cur, il); |
116 | 0 | cb(cur, "l_out", il); |
117 | | |
118 | | // input for next layer |
119 | 0 | inpL = cur; |
120 | 0 | } |
121 | 0 | cur = inpL; |
122 | |
|
123 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
124 | |
|
125 | 0 | cb(cur, "result_norm", -1); |
126 | 0 | res->t_embd = cur; |
127 | | |
128 | | // lm_head |
129 | 0 | cur = build_lora_mm(model.output, cur); |
130 | |
|
131 | 0 | cb(cur, "result_output", -1); |
132 | 0 | res->t_logits = cur; |
133 | |
|
134 | 0 | ggml_build_forward_expand(gf, cur); |
135 | 0 | } |