/src/llama.cpp/src/models/gemma3-iswa.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | llm_build_gemma3_iswa::llm_build_gemma3_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_k; |
5 | |
|
6 | 0 | ggml_tensor * cur; |
7 | 0 | ggml_tensor * inpL; |
8 | |
|
9 | 0 | inpL = build_inp_embd(model.tok_embd); |
10 | | |
11 | | // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings) |
12 | 0 | if (ubatch.token) { |
13 | 0 | inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd)); |
14 | 0 | cb(inpL, "inp_scaled", -1); |
15 | 0 | } |
16 | | // inp_pos - contains the positions |
17 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
18 | | |
19 | | // TODO: is causal == true correct? might need some changes |
20 | 0 | auto * inp_attn = build_attn_inp_kv_iswa(); |
21 | |
|
22 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
23 | |
|
24 | 0 | for (int il = 0; il < n_layer; ++il) { |
25 | 0 | const float freq_base_l = model.get_rope_freq_base (cparams, il); |
26 | 0 | 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_RMS, il); |
30 | 0 | cb(cur, "attn_norm", il); |
31 | | |
32 | | // self-attention |
33 | 0 | { |
34 | | // compute Q and K and RoPE them |
35 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
36 | 0 | cb(Qcur, "Qcur", il); |
37 | |
|
38 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
39 | 0 | cb(Kcur, "Kcur", il); |
40 | |
|
41 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
42 | 0 | cb(Vcur, "Vcur", il); |
43 | |
|
44 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
45 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
46 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
47 | |
|
48 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
49 | 0 | cb(Qcur, "Qcur_normed", il); |
50 | |
|
51 | 0 | Qcur = ggml_rope_ext( |
52 | 0 | ctx0, Qcur, inp_pos, nullptr, |
53 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
54 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
55 | |
|
56 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
57 | 0 | cb(Kcur, "Kcur_normed", il); |
58 | |
|
59 | 0 | Kcur = ggml_rope_ext( |
60 | 0 | ctx0, Kcur, inp_pos, nullptr, |
61 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
62 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
63 | |
|
64 | 0 | cb(Qcur, "Qcur", il); |
65 | 0 | cb(Kcur, "Kcur", il); |
66 | 0 | cb(Vcur, "Vcur", il); |
67 | | |
68 | | // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315 |
69 | 0 | Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale); |
70 | |
|
71 | 0 | cur = build_attn(inp_attn, |
72 | 0 | model.layers[il].wo, NULL, |
73 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il); |
74 | 0 | } |
75 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
76 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
77 | 0 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); |
78 | 0 | } |
79 | 0 | cur = build_norm(cur, |
80 | 0 | model.layers[il].attn_post_norm, NULL, |
81 | 0 | LLM_NORM_RMS, il); |
82 | 0 | cb(cur, "attn_post_norm", il); |
83 | |
|
84 | 0 | ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL); |
85 | 0 | cb(sa_out, "sa_out", il); |
86 | |
|
87 | 0 | cur = build_norm(sa_out, |
88 | 0 | model.layers[il].ffn_norm, NULL, |
89 | 0 | LLM_NORM_RMS, il); |
90 | 0 | cb(cur, "ffn_norm", il); |
91 | | |
92 | | // feed-forward network |
93 | 0 | { |
94 | 0 | cur = build_ffn(cur, |
95 | 0 | model.layers[il].ffn_up, NULL, NULL, |
96 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
97 | 0 | model.layers[il].ffn_down, NULL, NULL, |
98 | 0 | NULL, |
99 | 0 | LLM_FFN_GELU, LLM_FFN_PAR, il); |
100 | 0 | cb(cur, "ffn_out", il); |
101 | 0 | } |
102 | 0 | cur = build_norm(cur, |
103 | 0 | model.layers[il].ffn_post_norm, NULL, |
104 | 0 | LLM_NORM_RMS, -1); |
105 | 0 | cb(cur, "ffn_post_norm", -1); |
106 | |
|
107 | 0 | cur = ggml_add(ctx0, cur, sa_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 | 0 | cur = inpL; |
116 | |
|
117 | 0 | cur = build_norm(cur, |
118 | 0 | model.output_norm, NULL, |
119 | 0 | LLM_NORM_RMS, -1); |
120 | |
|
121 | 0 | cb(cur, "result_norm", -1); |
122 | 0 | res->t_embd = cur; |
123 | | |
124 | | // lm_head |
125 | 0 | cur = build_lora_mm(model.output, cur); |
126 | |
|
127 | 0 | cb(cur, "result_output", -1); |
128 | 0 | res->t_logits = cur; |
129 | |
|
130 | 0 | ggml_build_forward_expand(gf, cur); |
131 | 0 | } |