1#include "models.h"
2
3llm_build_cohere2_iswa::llm_build_cohere2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4 const int64_t n_embd_head = hparams.n_embd_head_v;
5
6 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
7
8 const float f_logit_scale = hparams.f_logit_scale;
9
10 ggml_tensor * cur;
11 ggml_tensor * inpL;
12
13 inpL = build_inp_embd(tok_embd: model.tok_embd);
14
15 // inp_pos - contains the positions
16 ggml_tensor * inp_pos = build_inp_pos();
17
18 auto * inp_attn = build_attn_inp_kv_iswa();
19
20 ggml_tensor * inp_out_ids = build_inp_out_ids();
21
22 for (int il = 0; il < n_layer; ++il) {
23 const bool is_swa = hparams.is_swa(il);
24
25 // norm
26 cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM, il);
27 cb(cur, name: "attn_norm", il);
28 ggml_tensor * ffn_inp = cur;
29
30 // self-attention
31 {
32 // rope freq factors for 128k context
33 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
34
35 // compute Q and K and RoPE them
36 ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur);
37 cb(cur: Qcur, name: "Qcur", il);
38 if (model.layers[il].bq) {
39 Qcur = ggml_add(ctx: ctx0, a: Qcur, b: model.layers[il].bq);
40 cb(cur: Qcur, name: "Qcur", il);
41 }
42
43 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
44 cb(cur: Kcur, name: "Kcur", il);
45 if (model.layers[il].bk) {
46 Kcur = ggml_add(ctx: ctx0, a: Kcur, b: model.layers[il].bk);
47 cb(cur: Kcur, name: "Kcur", il);
48 }
49
50 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
51 cb(cur: Vcur, name: "Vcur", il);
52 if (model.layers[il].bv) {
53 Vcur = ggml_add(ctx: ctx0, a: Vcur, b: model.layers[il].bv);
54 cb(cur: Vcur, name: "Vcur", il);
55 }
56
57 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
58 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
59 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
60
61 if (is_swa) {
62 Qcur = ggml_rope_ext(
63 ctx: ctx0, a: Qcur, b: inp_pos, c: rope_factors,
64 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
65 ext_factor, attn_factor, beta_fast, beta_slow
66 );
67
68 Kcur = ggml_rope_ext(
69 ctx: ctx0, a: Kcur, b: inp_pos, c: rope_factors,
70 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
71 ext_factor, attn_factor, beta_fast, beta_slow
72 );
73 }
74
75 cb(cur: Qcur, name: "Qcur", il);
76 cb(cur: Kcur, name: "Kcur", il);
77 cb(cur: Vcur, name: "Vcur", il);
78
79 cur = build_attn(inp: inp_attn,
80 wo: model.layers[il].wo, wo_b: model.layers[il].bo,
81 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);
82 }
83
84 if (il == n_layer - 1 && inp_out_ids) {
85 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
86 inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids);
87 ffn_inp = ggml_get_rows(ctx: ctx0, a: ffn_inp, b: inp_out_ids);
88 }
89
90 ggml_tensor * attn_out = cur;
91
92 // feed-forward network
93 {
94 cur = build_ffn(cur: ffn_inp,
95 up: model.layers[il].ffn_up, NULL, NULL,
96 gate: model.layers[il].ffn_gate, NULL, NULL,
97 down: model.layers[il].ffn_down, NULL, NULL,
98 NULL, type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
99 cb(cur, name: "ffn_out", il);
100 }
101
102 // add together residual + FFN + self-attention
103 cur = ggml_add(ctx: ctx0, a: cur, b: inpL);
104 cur = ggml_add(ctx: ctx0, a: cur, b: attn_out);
105
106 cur = build_cvec(cur, il);
107 cb(cur, name: "l_out", il);
108
109 // input for next layer
110 inpL = cur;
111 }
112
113 cur = inpL;
114
115 cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM, il: -1);
116
117 cb(cur, name: "result_norm", il: -1);
118 res->t_embd = cur;
119
120 // lm_head
121 cur = build_lora_mm(w: model.output, cur);
122
123 if (f_logit_scale) {
124 cur = ggml_scale(ctx: ctx0, a: cur, s: f_logit_scale);
125 }
126
127 cb(cur, name: "result_output", il: -1);
128 res->t_logits = cur;
129
130 ggml_build_forward_expand(cgraph: gf, tensor: cur);
131}
132