/src/llama.cpp/src/models/mpt.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_mpt::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
5 | 0 | ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false); |
6 | 0 | ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias, false); |
7 | |
|
8 | 0 | switch (hparams.n_layer()) { |
9 | 0 | case 32: type = LLM_TYPE_7B; break; |
10 | 0 | case 48: type = LLM_TYPE_30B; break; |
11 | 0 | default: type = LLM_TYPE_UNKNOWN; |
12 | 0 | } |
13 | 0 | } |
14 | | |
15 | 0 | void llama_model_mpt::load_arch_tensors(llama_model_loader &) { |
16 | 0 | LLAMA_LOAD_LOCALS; |
17 | |
|
18 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
19 | 0 | pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, TENSOR_NOT_REQUIRED); |
20 | | |
21 | | // output |
22 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
23 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED); |
24 | |
|
25 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
26 | 0 | if (!output) { |
27 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU |
28 | 0 | } |
29 | |
|
30 | 0 | for (int i = 0; i < n_layer; ++i) { |
31 | 0 | auto & layer = layers[i]; |
32 | |
|
33 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
34 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
35 | |
|
36 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
37 | 0 | layer.wqkv_b = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED); |
38 | |
|
39 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
40 | 0 | layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
41 | |
|
42 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
43 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
44 | |
|
45 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
46 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
47 | |
|
48 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
49 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
50 | | |
51 | | // FIXME test-llama-archs crashes if q_norm is created |
52 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED | TENSOR_SKIP_IF_VIRTUAL); |
53 | 0 | layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED | TENSOR_SKIP_IF_VIRTUAL); |
54 | |
|
55 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
56 | 0 | layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
57 | | |
58 | | // AWQ ScaleActivation layer |
59 | 0 | layer.ffn_act = create_tensor(tn(LLM_TENSOR_FFN_ACT, "scales", i), {n_ff}, TENSOR_NOT_REQUIRED); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | 0 | std::unique_ptr<llm_graph_context> llama_model_mpt::build_arch_graph(const llm_graph_params & params) const { |
64 | 0 | return std::make_unique<graph>(*this, params); |
65 | 0 | } |
66 | | |
67 | 0 | llama_model_mpt::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
68 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
69 | |
|
70 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
71 | |
|
72 | 0 | ggml_tensor * cur; |
73 | 0 | ggml_tensor * pos; |
74 | 0 | ggml_tensor * inpL; |
75 | |
|
76 | 0 | inpL = build_inp_embd(model.tok_embd); |
77 | |
|
78 | 0 | auto * inp_attn = build_attn_inp_kv(); |
79 | |
|
80 | 0 | if (model.pos_embd) { |
81 | | // inp_pos - contains the positions |
82 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
83 | 0 | pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos); |
84 | 0 | cb(pos, "pos_embd", -1); |
85 | |
|
86 | 0 | inpL = ggml_add(ctx0, inpL, pos); |
87 | 0 | cb(inpL, "inpL", -1); |
88 | 0 | } |
89 | |
|
90 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
91 | |
|
92 | 0 | for (int il = 0; il < n_layer; ++il) { |
93 | 0 | ggml_tensor * attn_norm; |
94 | |
|
95 | 0 | attn_norm = build_norm(inpL, model.layers[il].attn_norm, model.layers[il].attn_norm_b, LLM_NORM, il); |
96 | 0 | cb(attn_norm, "attn_norm", il); |
97 | | |
98 | | // self-attention |
99 | 0 | { |
100 | 0 | cur = attn_norm; |
101 | |
|
102 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
103 | 0 | n_embd_head, n_head, n_head_kv, il); |
104 | | |
105 | | // Q/K Layernorm |
106 | 0 | if (model.layers[il].attn_q_norm) { |
107 | 0 | Qcur = ggml_reshape_2d(ctx0, Qcur, n_embd_head * n_head, n_tokens); |
108 | 0 | Kcur = ggml_reshape_2d(ctx0, Kcur, n_embd_head * n_head_kv, n_tokens); |
109 | |
|
110 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, model.layers[il].attn_q_norm_b, LLM_NORM, il); |
111 | |
|
112 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, model.layers[il].attn_k_norm_b, LLM_NORM, il); |
113 | |
|
114 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
115 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
116 | 0 | } |
117 | |
|
118 | 0 | cb(Qcur, "Qcur", il); |
119 | 0 | cb(Kcur, "Kcur", il); |
120 | 0 | cb(Vcur, "Vcur", il); |
121 | |
|
122 | 0 | cur = build_attn(inp_attn, |
123 | 0 | model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
124 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il); |
125 | 0 | } |
126 | |
|
127 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
128 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
129 | 0 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); |
130 | 0 | } |
131 | | |
132 | | // Add the input |
133 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL); |
134 | 0 | cb(ffn_inp, "ffn_inp", il); |
135 | | |
136 | | // feed forward |
137 | 0 | { |
138 | 0 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, model.layers[il].ffn_norm_b, LLM_NORM, il); |
139 | 0 | cb(cur, "ffn_norm", il); |
140 | 0 | cur = build_ffn(cur, |
141 | 0 | model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL, |
142 | 0 | NULL, NULL, NULL, |
143 | 0 | model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, |
144 | 0 | model.layers[il].ffn_act, LLM_FFN_GELU, LLM_FFN_SEQ, il); |
145 | 0 | cb(cur, "ffn_out", il); |
146 | 0 | } |
147 | |
|
148 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
149 | |
|
150 | 0 | cur = build_cvec(cur, il); |
151 | 0 | cb(cur, "l_out", il); |
152 | | |
153 | | // input for next layer |
154 | 0 | inpL = cur; |
155 | 0 | } |
156 | |
|
157 | 0 | cur = inpL; |
158 | |
|
159 | 0 | cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1); |
160 | |
|
161 | 0 | cb(cur, "result_norm", -1); |
162 | 0 | res->t_embd = cur; |
163 | |
|
164 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
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 | } |