/src/llama.cpp/src/models/bitnet.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_bitnet::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
5 | |
|
6 | 0 | switch (hparams.n_layer()) { |
7 | 0 | case 26: type = LLM_TYPE_3B; break; |
8 | 0 | default: type = LLM_TYPE_UNKNOWN; |
9 | 0 | } |
10 | 0 | } |
11 | | |
12 | 0 | void llama_model_bitnet::load_arch_tensors(llama_model_loader &) { |
13 | 0 | LLAMA_LOAD_LOCALS; |
14 | |
|
15 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
16 | | |
17 | | // output |
18 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
19 | |
|
20 | 0 | for (int i = 0; i < n_layer; ++i) { |
21 | 0 | auto & layer = layers[i]; |
22 | |
|
23 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
24 | 0 | layer.attn_sub_norm = create_tensor(tn(LLM_TENSOR_ATTN_SUB_NORM, "weight", i), {n_embd}, 0); |
25 | |
|
26 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
27 | 0 | layer.wq_s = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
28 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
29 | 0 | layer.wk_s = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
30 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
31 | 0 | layer.wv_s = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
32 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
33 | 0 | layer.wo_s = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
34 | |
|
35 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
36 | 0 | layer.ffn_sub_norm = create_tensor(tn(LLM_TENSOR_FFN_SUB_NORM, "weight", i), {n_ff}, 0); |
37 | |
|
38 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
39 | 0 | layer.ffn_gate_s = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
40 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
41 | 0 | layer.ffn_down_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
42 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
43 | 0 | layer.ffn_up_s = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | 0 | std::unique_ptr<llm_graph_context> llama_model_bitnet::build_arch_graph(const llm_graph_params & params) const { |
48 | 0 | return std::make_unique<graph>(*this, params); |
49 | 0 | } |
50 | | |
51 | 0 | llama_model_bitnet::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
52 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
53 | |
|
54 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
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 * inpSA = inpL; |
70 | |
|
71 | 0 | cur = build_norm(inpL, |
72 | 0 | model.layers[il].attn_norm, NULL, |
73 | 0 | LLM_NORM_RMS, il); |
74 | 0 | cb(cur, "attn_norm", il); |
75 | | |
76 | | // self-attention |
77 | 0 | { |
78 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
79 | 0 | n_embd_head, n_head, n_head_kv, il); |
80 | |
|
81 | 0 | Qcur = ggml_rope_ext( |
82 | 0 | ctx0, Qcur, inp_pos, nullptr, |
83 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
84 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
85 | 0 | ); |
86 | |
|
87 | 0 | Kcur = ggml_rope_ext( |
88 | 0 | ctx0, Kcur, inp_pos, nullptr, |
89 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
90 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
91 | 0 | ); |
92 | |
|
93 | 0 | cb(Qcur, "Qcur", il); |
94 | 0 | cb(Kcur, "Kcur", il); |
95 | 0 | cb(Vcur, "Vcur", il); |
96 | |
|
97 | 0 | cur = build_attn(inp_attn, |
98 | 0 | NULL, NULL, NULL, |
99 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
100 | |
|
101 | 0 | cur = build_norm(cur, |
102 | 0 | model.layers[il].attn_sub_norm, NULL, |
103 | 0 | LLM_NORM_RMS, il); |
104 | 0 | cb(cur, "attn_sub_norm", il); |
105 | |
|
106 | 0 | cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s); |
107 | 0 | if (model.layers[il].wo_b) { |
108 | 0 | cur = ggml_add(ctx0, cur, model.layers[il].wo_b); |
109 | 0 | } |
110 | 0 | cb(cur, "attn_out", il); |
111 | 0 | } |
112 | |
|
113 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
114 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
115 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
116 | 0 | } |
117 | |
|
118 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
119 | 0 | cb(ffn_inp, "ffn_inp", il); |
120 | | |
121 | | // feed-forward forward |
122 | 0 | cur = build_norm(ffn_inp, |
123 | 0 | model.layers[il].ffn_norm, NULL, |
124 | 0 | LLM_NORM_RMS, il); |
125 | 0 | cb(cur, "ffn_norm", il); |
126 | |
|
127 | 0 | cur = build_ffn(cur, |
128 | 0 | model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s, |
129 | 0 | model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s, |
130 | 0 | NULL, NULL, NULL, |
131 | 0 | NULL, |
132 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
133 | 0 | cb(cur, "ffn_sub_out", il); |
134 | |
|
135 | 0 | cur = build_norm(cur, |
136 | 0 | model.layers[il].ffn_sub_norm, NULL, |
137 | 0 | LLM_NORM_RMS, il); |
138 | 0 | cb(cur, "ffn_sub_norm", il); |
139 | |
|
140 | 0 | cur = build_lora_mm(model.layers[il].ffn_down, cur, model.layers[il].ffn_down_s); |
141 | 0 | cb(cur, "ffn_down", il); |
142 | |
|
143 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
144 | 0 | cb(cur, "l_out", il); |
145 | |
|
146 | 0 | cur = build_cvec(cur, il); |
147 | 0 | cb(cur, "l_out", il); |
148 | | |
149 | | // input for next layer |
150 | 0 | inpL = cur; |
151 | 0 | } |
152 | |
|
153 | 0 | cur = inpL; |
154 | |
|
155 | 0 | cur = build_norm(cur, |
156 | 0 | model.output_norm, NULL, |
157 | 0 | LLM_NORM_RMS, -1); |
158 | |
|
159 | 0 | cb(cur, "result_norm", -1); |
160 | 0 | res->t_embd = cur; |
161 | | |
162 | | // lm_head |
163 | | // FIXME: do not use model.tok_embd directly, duplicate as model.output |
164 | 0 | cur = build_lora_mm(model.tok_embd, cur); |
165 | |
|
166 | 0 | cb(cur, "result_output", -1); |
167 | 0 | res->t_logits = cur; |
168 | |
|
169 | 0 | ggml_build_forward_expand(gf, cur); |
170 | 0 | } |