/src/llama.cpp/src/models/refact.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_refact::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 32: type = LLM_TYPE_1B; break; |
8 | 0 | default: type = LLM_TYPE_UNKNOWN; |
9 | 0 | } |
10 | | |
11 | | // TODO: become GGUF KV parameter |
12 | 0 | hparams.f_max_alibi_bias = 8.0f; |
13 | 0 | } |
14 | | |
15 | 0 | void llama_model_refact::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 | | |
20 | | // output |
21 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
22 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
23 | | |
24 | | // if output is NULL, init from the input tok embed |
25 | 0 | if (output == NULL) { |
26 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
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 | |
|
34 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0); |
35 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
36 | | |
37 | | // optional bias tensors |
38 | 0 | layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
39 | |
|
40 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
41 | |
|
42 | 0 | if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) { |
43 | 0 | layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
44 | 0 | layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
45 | 0 | } |
46 | 0 | else { |
47 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
48 | 0 | } |
49 | |
|
50 | 0 | if (n_expert == 0) { |
51 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
52 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
53 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
54 | | |
55 | | // optional MLP bias |
56 | 0 | layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
57 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
58 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
59 | 0 | } else { |
60 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
61 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED); |
62 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0); |
63 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
64 | | |
65 | | // For Granite MoE Shared |
66 | 0 | if (hparams.n_ff_shexp > 0) { |
67 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0); |
68 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0); |
69 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0); |
70 | 0 | } |
71 | 0 | } |
72 | 0 | } |
73 | 0 | } |
74 | | |
75 | 0 | std::unique_ptr<llm_graph_context> llama_model_refact::build_arch_graph(const llm_graph_params & params) const { |
76 | 0 | return std::make_unique<graph>(*this, params); |
77 | 0 | } |
78 | | |
79 | 0 | llama_model_refact::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
80 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
81 | |
|
82 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
83 | |
|
84 | 0 | ggml_tensor * cur; |
85 | 0 | ggml_tensor * inpL; |
86 | |
|
87 | 0 | inpL = build_inp_embd(model.tok_embd); |
88 | |
|
89 | 0 | auto * inp_attn = build_attn_inp_kv(); |
90 | |
|
91 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
92 | |
|
93 | 0 | for (int il = 0; il < n_layer; ++il) { |
94 | 0 | ggml_tensor * inpSA = inpL; |
95 | |
|
96 | 0 | cur = build_norm(inpL, |
97 | 0 | model.layers[il].attn_norm, NULL, |
98 | 0 | LLM_NORM_RMS, il); |
99 | 0 | cb(cur, "attn_norm", il); |
100 | | |
101 | | // self-attention |
102 | 0 | { |
103 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
104 | 0 | n_embd_head, n_head, n_head_kv, il); |
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 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
115 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
116 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
117 | 0 | } |
118 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
119 | 0 | cb(ffn_inp, "ffn_inp", il); |
120 | | |
121 | | // feed-forward network |
122 | 0 | { |
123 | 0 | cur = build_norm(ffn_inp, |
124 | 0 | model.layers[il].ffn_norm, NULL, |
125 | 0 | LLM_NORM_RMS, il); |
126 | 0 | cb(cur, "ffn_norm", il); |
127 | |
|
128 | 0 | cur = build_ffn(cur, |
129 | 0 | model.layers[il].ffn_up, NULL, NULL, |
130 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
131 | 0 | model.layers[il].ffn_down, NULL, NULL, |
132 | 0 | NULL, |
133 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
134 | 0 | cb(cur, "ffn_out", il); |
135 | 0 | } |
136 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
137 | |
|
138 | 0 | cur = build_cvec(cur, il); |
139 | 0 | cb(cur, "l_out", il); |
140 | | |
141 | | // input for next layer |
142 | 0 | inpL = cur; |
143 | 0 | } |
144 | 0 | cur = inpL; |
145 | |
|
146 | 0 | cur = build_norm(cur, |
147 | 0 | model.output_norm, NULL, |
148 | 0 | LLM_NORM_RMS, -1); |
149 | |
|
150 | 0 | cb(cur, "result_norm", -1); |
151 | 0 | res->t_embd = cur; |
152 | | |
153 | | // lm_head |
154 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
155 | |
|
156 | 0 | cb(cur, "result_output", -1); |
157 | 0 | res->t_logits = cur; |
158 | |
|
159 | 0 | ggml_build_forward_expand(gf, cur); |
160 | 0 | } |