1#include "models.h"
2
3llm_build_gemma3_iswa::llm_build_gemma3_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_k;
5
6 ggml_tensor * cur;
7 ggml_tensor * inpL;
8
9 inpL = build_inp_embd(tok_embd: model.tok_embd);
10
11 // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
12 if (ubatch.token) {
13 inpL = ggml_scale(ctx: ctx0, a: inpL, s: sqrtf(x: n_embd));
14 cb(cur: inpL, name: "inp_scaled", il: -1);
15 }
16 // inp_pos - contains the positions
17 ggml_tensor * inp_pos = build_inp_pos();
18
19 // TODO: is causal == true correct? might need some changes
20 auto * inp_attn = build_attn_inp_kv_iswa();
21
22 ggml_tensor * inp_out_ids = build_inp_out_ids();
23
24 for (int il = 0; il < n_layer; ++il) {
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 cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il);
30 cb(cur, name: "attn_norm", il);
31
32 // self-attention
33 {
34 // compute Q and K and RoPE them
35 ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur);
36 cb(cur: Qcur, name: "Qcur", il);
37
38 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
39 cb(cur: Kcur, name: "Kcur", il);
40
41 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
42 cb(cur: Vcur, name: "Vcur", il);
43
44 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
45 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
46 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
47
48 Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL, type: LLM_NORM_RMS, il);
49 cb(cur: Qcur, name: "Qcur_normed", il);
50
51 Qcur = ggml_rope_ext(
52 ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr,
53 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base: freq_base_l, freq_scale: freq_scale_l,
54 ext_factor, attn_factor, beta_fast, beta_slow);
55
56 Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL, type: LLM_NORM_RMS, il);
57 cb(cur: Kcur, name: "Kcur_normed", il);
58
59 Kcur = ggml_rope_ext(
60 ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr,
61 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base: freq_base_l, freq_scale: freq_scale_l,
62 ext_factor, attn_factor, beta_fast, beta_slow);
63
64 cb(cur: Qcur, name: "Qcur", il);
65 cb(cur: Kcur, name: "Kcur", il);
66 cb(cur: Vcur, name: "Vcur", il);
67
68 // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315
69 Qcur = ggml_scale(ctx: ctx0, a: Qcur, s: hparams.f_attention_scale);
70
71 cur = build_attn(inp: inp_attn,
72 wo: model.layers[il].wo, NULL,
73 q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f, il);
74 }
75 if (il == n_layer - 1 && inp_out_ids) {
76 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
77 inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids);
78 }
79 cur = build_norm(cur,
80 mw: model.layers[il].attn_post_norm, NULL,
81 type: LLM_NORM_RMS, il);
82 cb(cur, name: "attn_post_norm", il);
83
84 ggml_tensor * sa_out = ggml_add(ctx: ctx0, a: cur, b: inpL);
85 cb(cur: sa_out, name: "sa_out", il);
86
87 cur = build_norm(cur: sa_out,
88 mw: model.layers[il].ffn_norm, NULL,
89 type: LLM_NORM_RMS, il);
90 cb(cur, name: "ffn_norm", il);
91
92 // feed-forward network
93 {
94 cur = build_ffn(cur,
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,
99 type_op: LLM_FFN_GELU, type_gate: LLM_FFN_PAR, il);
100 cb(cur, name: "ffn_out", il);
101 }
102 cur = build_norm(cur,
103 mw: model.layers[il].ffn_post_norm, NULL,
104 type: LLM_NORM_RMS, il: -1);
105 cb(cur, name: "ffn_post_norm", il: -1);
106
107 cur = ggml_add(ctx: ctx0, a: cur, b: sa_out);
108
109 cur = build_cvec(cur, il);
110 cb(cur, name: "l_out", il);
111
112 // input for next layer
113 inpL = cur;
114 }
115 cur = inpL;
116
117 cur = build_norm(cur,
118 mw: model.output_norm, NULL,
119 type: LLM_NORM_RMS, il: -1);
120
121 cb(cur, name: "result_norm", il: -1);
122 res->t_embd = cur;
123
124 // lm_head
125 cur = build_lora_mm(w: model.output, cur);
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