/src/llama.cpp/src/models/dbrx.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_dbrx::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); |
6 | |
|
7 | 0 | switch (hparams.n_layer()) { |
8 | 0 | case 40: type = LLM_TYPE_16x12B; break; |
9 | 0 | default: type = LLM_TYPE_UNKNOWN; |
10 | 0 | } |
11 | 0 | } |
12 | | |
13 | 0 | void llama_model_dbrx::load_arch_tensors(llama_model_loader &) { |
14 | 0 | LLAMA_LOAD_LOCALS; |
15 | |
|
16 | 0 | if (n_expert == 0) { |
17 | 0 | throw std::runtime_error("DBRX model cannot have zero experts"); |
18 | 0 | } |
19 | | |
20 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
21 | | |
22 | | // output |
23 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
24 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
25 | |
|
26 | 0 | for (int i = 0; i < n_layer; ++i) { |
27 | 0 | auto & layer = layers[i]; |
28 | |
|
29 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
30 | |
|
31 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
32 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
33 | |
|
34 | 0 | layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); |
35 | |
|
36 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
37 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
38 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0); |
39 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
40 | 0 | } |
41 | 0 | } |
42 | | |
43 | 0 | std::unique_ptr<llm_graph_context> llama_model_dbrx::build_arch_graph(const llm_graph_params & params) const { |
44 | 0 | return std::make_unique<graph>(*this, params); |
45 | 0 | } |
46 | | |
47 | 0 | llama_model_dbrx::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
48 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
49 | |
|
50 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
51 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
52 | |
|
53 | 0 | ggml_tensor * cur; |
54 | 0 | ggml_tensor * inpL; |
55 | |
|
56 | 0 | inpL = build_inp_embd(model.tok_embd); |
57 | | |
58 | | // inp_pos - contains the positions |
59 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
60 | |
|
61 | 0 | auto * inp_attn = build_attn_inp_kv(); |
62 | |
|
63 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
64 | |
|
65 | 0 | for (int il = 0; il < n_layer; ++il) { |
66 | 0 | ggml_tensor * inpSA = inpL; |
67 | | |
68 | | // norm |
69 | 0 | cur = build_norm(inpL, |
70 | 0 | model.layers[il].attn_norm, NULL, |
71 | 0 | LLM_NORM, il); |
72 | 0 | cb(cur, "attn_norm", il); |
73 | | |
74 | | // self-attention |
75 | 0 | { |
76 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
77 | 0 | n_embd_head, n_head, n_head_kv, il); |
78 | |
|
79 | 0 | Qcur = ggml_rope_ext( |
80 | 0 | ctx0, Qcur, inp_pos, nullptr, |
81 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
82 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
83 | 0 | ); |
84 | |
|
85 | 0 | Kcur = ggml_rope_ext( |
86 | 0 | ctx0, Kcur, inp_pos, nullptr, |
87 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
88 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
89 | 0 | ); |
90 | |
|
91 | 0 | cb(Qcur, "Qcur", il); |
92 | 0 | cb(Kcur, "Kcur", il); |
93 | 0 | cb(Vcur, "Vcur", il); |
94 | |
|
95 | 0 | cur = build_attn(inp_attn, |
96 | 0 | model.layers[il].wo, NULL, model.layers[il].wo_s, |
97 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
98 | 0 | } |
99 | |
|
100 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
101 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
102 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
103 | 0 | } |
104 | |
|
105 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
106 | 0 | cb(ffn_inp, "ffn_inp", il); |
107 | | |
108 | | // feed-forward network |
109 | | // MoE branch |
110 | 0 | cur = build_norm(ffn_inp, |
111 | 0 | model.layers[il].attn_out_norm, NULL, |
112 | 0 | LLM_NORM, il); |
113 | 0 | cb(cur, "attn_out_norm", il); |
114 | |
|
115 | 0 | cur = build_moe_ffn(cur, |
116 | 0 | model.layers[il].ffn_gate_inp, |
117 | 0 | model.layers[il].ffn_up_exps, |
118 | 0 | model.layers[il].ffn_gate_exps, |
119 | 0 | model.layers[il].ffn_down_exps, |
120 | 0 | nullptr, |
121 | 0 | n_expert, n_expert_used, |
122 | 0 | LLM_FFN_SILU, true, |
123 | 0 | hparams.expert_weights_scale, |
124 | 0 | LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
125 | 0 | il); |
126 | 0 | cb(cur, "ffn_moe_out", il); |
127 | |
|
128 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
129 | 0 | cb(cur, "ffn_out", il); |
130 | |
|
131 | 0 | cur = build_cvec(cur, il); |
132 | 0 | cb(cur, "l_out", il); |
133 | | |
134 | | // input for next layer |
135 | 0 | inpL = cur; |
136 | 0 | } |
137 | |
|
138 | 0 | cur = inpL; |
139 | |
|
140 | 0 | cur = build_norm(cur, |
141 | 0 | model.output_norm, NULL, |
142 | 0 | LLM_NORM, -1); |
143 | |
|
144 | 0 | cb(cur, "result_norm", -1); |
145 | 0 | res->t_embd = cur; |
146 | | |
147 | | // lm_head |
148 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
149 | |
|
150 | 0 | cb(cur, "result_output", -1); |
151 | 0 | res->t_logits = cur; |
152 | |
|
153 | 0 | ggml_build_forward_expand(gf, cur); |
154 | 0 | } |