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