1#include "models.h"
2
3
4llm_build_qwen::llm_build_qwen(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
5 const int64_t n_embd_head = hparams.n_embd_head_v;
6
7 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8
9 ggml_tensor * cur;
10 ggml_tensor * inpL;
11
12 inpL = build_inp_embd(tok_embd: model.tok_embd);
13
14 // inp_pos - contains the positions
15 ggml_tensor * inp_pos = build_inp_pos();
16
17 auto * inp_attn = build_attn_inp_kv();
18
19 ggml_tensor * inp_out_ids = build_inp_out_ids();
20
21 for (int il = 0; il < n_layer; ++il) {
22 ggml_tensor * inpSA = inpL;
23
24 cur = build_norm(cur: inpL,
25 mw: model.layers[il].attn_norm, NULL,
26 type: LLM_NORM_RMS, il);
27 cb(cur, name: "attn_norm", il);
28
29 // self-attention
30 {
31 cur = build_lora_mm(w: model.layers[il].wqkv, cur);
32 cb(cur, name: "wqkv", il);
33
34 cur = ggml_add(ctx: ctx0, a: cur, b: model.layers[il].bqkv);
35 cb(cur, name: "bqkv", il);
36
37 ggml_tensor * Qcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens, nb1: n_embd_head*sizeof(float), nb2: cur->nb[1], offset: 0*sizeof(float)*(n_embd));
38 ggml_tensor * Kcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head*sizeof(float), nb2: cur->nb[1], offset: 1*sizeof(float)*(n_embd));
39 ggml_tensor * Vcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head*sizeof(float), nb2: cur->nb[1], offset: 2*sizeof(float)*(n_embd));
40
41 // using mode = 2 for neox mode
42 Qcur = ggml_rope_ext(
43 ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr,
44 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
45 ext_factor, attn_factor, beta_fast, beta_slow
46 );
47
48 Kcur = ggml_rope_ext(
49 ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr,
50 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
51 ext_factor, attn_factor, beta_fast, beta_slow
52 );
53
54 cb(cur: Qcur, name: "Qcur", il);
55 cb(cur: Kcur, name: "Kcur", il);
56 cb(cur: Vcur, name: "Vcur", il);
57
58 cur = build_attn(inp: inp_attn,
59 wo: model.layers[il].wo, NULL,
60 q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f/sqrtf(x: float(n_embd_head)), il);
61 }
62 if (il == n_layer - 1 && inp_out_ids) {
63 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
64 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
65 }
66 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA);
67 cb(cur: ffn_inp, name: "ffn_inp", il);
68
69 // feed-forward forward
70 {
71 cur = build_norm(cur: ffn_inp,
72 mw: model.layers[il].ffn_norm, NULL,
73 type: LLM_NORM_RMS, il);
74 cb(cur, name: "ffn_norm", il);
75
76 cur = build_ffn(cur,
77 up: model.layers[il].ffn_up, NULL, NULL,
78 gate: model.layers[il].ffn_gate, NULL, NULL,
79 down: model.layers[il].ffn_down, NULL, NULL,
80 NULL,
81 type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
82 cb(cur, name: "ffn_out", il);
83 }
84 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
85
86 cur = build_cvec(cur, il);
87 cb(cur, name: "l_out", il);
88
89 // input for next layer
90 inpL = cur;
91 }
92 cur = inpL;
93
94 cur = build_norm(cur,
95 mw: model.output_norm, NULL,
96 type: LLM_NORM_RMS, il: -1);
97
98 cb(cur, name: "result_norm", il: -1);
99 res->t_embd = cur;
100
101 // lm_head
102 cur = build_lora_mm(w: model.output, cur);
103
104 cb(cur, name: "result_output", il: -1);
105 res->t_logits = cur;
106
107 ggml_build_forward_expand(cgraph: gf, tensor: cur);
108}
109