/src/llama.cpp/src/models/falcon.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_falcon::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
5 | |
|
6 | 0 | switch (hparams.n_layer()) { |
7 | 0 | case 32: type = LLM_TYPE_7B; break; |
8 | 0 | case 60: type = LLM_TYPE_40B; break; |
9 | 0 | default: type = LLM_TYPE_UNKNOWN; |
10 | 0 | } |
11 | 0 | } |
12 | | |
13 | 0 | void llama_model_falcon::load_arch_tensors(llama_model_loader &) { |
14 | 0 | LLAMA_LOAD_LOCALS; |
15 | |
|
16 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
17 | | |
18 | | // output |
19 | 0 | { |
20 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
21 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
22 | |
|
23 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
24 | 0 | if (!output) { |
25 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU |
26 | 0 | } |
27 | 0 | } |
28 | |
|
29 | 0 | for (int i = 0; i < n_layer; ++i) { |
30 | 0 | auto & layer = layers[i]; |
31 | |
|
32 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
33 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
34 | |
|
35 | 0 | layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
36 | 0 | layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
37 | |
|
38 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
39 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
40 | |
|
41 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
42 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | 0 | std::unique_ptr<llm_graph_context> llama_model_falcon::build_arch_graph(const llm_graph_params & params) const { |
47 | 0 | return std::make_unique<graph>(*this, params); |
48 | 0 | } |
49 | | |
50 | 0 | llama_model_falcon::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
51 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
52 | |
|
53 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
54 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
55 | |
|
56 | 0 | ggml_tensor * cur; |
57 | 0 | ggml_tensor * inpL; |
58 | |
|
59 | 0 | inpL = build_inp_embd(model.tok_embd); |
60 | | |
61 | | // inp_pos - contains the positions |
62 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
63 | |
|
64 | 0 | auto * inp_attn = build_attn_inp_kv(); |
65 | |
|
66 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
67 | |
|
68 | 0 | for (int il = 0; il < n_layer; ++il) { |
69 | 0 | ggml_tensor * attn_norm; |
70 | |
|
71 | 0 | attn_norm = build_norm(inpL, |
72 | 0 | model.layers[il].attn_norm, |
73 | 0 | model.layers[il].attn_norm_b, |
74 | 0 | LLM_NORM, il); |
75 | 0 | cb(attn_norm, "attn_norm", il); |
76 | | |
77 | | // self-attention |
78 | 0 | { |
79 | 0 | if (model.layers[il].attn_norm_2) { |
80 | | // Falcon-40B |
81 | 0 | cur = build_norm(inpL, |
82 | 0 | model.layers[il].attn_norm_2, |
83 | 0 | model.layers[il].attn_norm_2_b, |
84 | 0 | LLM_NORM, il); |
85 | 0 | cb(cur, "attn_norm_2", il); |
86 | 0 | } else { |
87 | 0 | cur = attn_norm; |
88 | 0 | } |
89 | |
|
90 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
91 | 0 | n_embd_head, n_head, n_head_kv, il); |
92 | | |
93 | | // using mode = 2 for neox mode |
94 | 0 | Qcur = ggml_rope_ext( |
95 | 0 | ctx0, Qcur, inp_pos, nullptr, |
96 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
97 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
98 | 0 | ); |
99 | |
|
100 | 0 | Kcur = ggml_rope_ext( |
101 | 0 | ctx0, Kcur, inp_pos, nullptr, |
102 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
103 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
104 | 0 | ); |
105 | |
|
106 | 0 | cb(Qcur, "Qcur", il); |
107 | 0 | cb(Kcur, "Kcur", il); |
108 | 0 | cb(Vcur, "Vcur", il); |
109 | |
|
110 | 0 | cur = build_attn(inp_attn, |
111 | 0 | model.layers[il].wo, NULL, model.layers[il].wo_s, |
112 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
113 | 0 | } |
114 | |
|
115 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
116 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
117 | 0 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); |
118 | 0 | attn_norm = ggml_get_rows(ctx0, attn_norm, inp_out_ids); |
119 | 0 | } |
120 | |
|
121 | 0 | ggml_tensor * ffn_inp = cur; |
122 | | |
123 | | // feed forward |
124 | 0 | { |
125 | 0 | cur = build_ffn(attn_norm, // !! use the attn norm, not the result |
126 | 0 | model.layers[il].ffn_up, NULL, NULL, |
127 | 0 | NULL, NULL, NULL, |
128 | 0 | model.layers[il].ffn_down, NULL, NULL, |
129 | 0 | NULL, |
130 | 0 | LLM_FFN_GELU, LLM_FFN_SEQ, il); |
131 | 0 | cb(cur, "ffn_out", il); |
132 | 0 | } |
133 | |
|
134 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
135 | 0 | cur = ggml_add(ctx0, cur, inpL); |
136 | |
|
137 | 0 | cur = build_cvec(cur, il); |
138 | 0 | cb(cur, "l_out", il); |
139 | | |
140 | | // input for next layer |
141 | 0 | inpL = cur; |
142 | 0 | } |
143 | |
|
144 | 0 | cur = inpL; |
145 | | |
146 | | // norm |
147 | 0 | cur = build_norm(cur, |
148 | 0 | model.output_norm, |
149 | 0 | model.output_norm_b, |
150 | 0 | LLM_NORM, -1); |
151 | |
|
152 | 0 | cb(cur, "result_norm", -1); |
153 | 0 | res->t_embd = cur; |
154 | |
|
155 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
156 | |
|
157 | 0 | cb(cur, "result_output", -1); |
158 | 0 | res->t_logits = cur; |
159 | |
|
160 | 0 | ggml_build_forward_expand(gf, cur); |
161 | 0 | } |