MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / llm_build_gemma3

Method llm_build_gemma3

subprojects/llama.cpp/src/models/gemma3.cpp:4–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2
3template <bool iswa>
4llm_build_gemma3<iswa>::llm_build_gemma3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
5 const int64_t n_embd_head = hparams.n_embd_head_k;
6
7 ggml_tensor * cur;
8 ggml_tensor * inpL;
9
10 inpL = build_inp_embd(model.tok_embd);
11
12 // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
13 inpL = ggml_scale(ctx0, inpL, ubatch.token ? sqrtf(n_embd) : 1.0f);
14 cb(inpL, "inp_scaled", -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 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
21 inp_attn_type * inp_attn = nullptr;
22
23 if constexpr (iswa) {
24 inp_attn = build_attn_inp_kv_iswa();
25 } else {
26 inp_attn = build_attn_inp_kv();
27 }
28
29 ggml_tensor * inp_out_ids = build_inp_out_ids();
30
31 for (int il = 0; il < n_layer; ++il) {
32 float freq_base_l = 0.0f;
33 float freq_scale_l = 0.0f;
34
35 if constexpr (iswa) {
36 freq_base_l = model.get_rope_freq_base (cparams, il);
37 freq_scale_l = model.get_rope_freq_scale(cparams, il);
38 } else {
39 freq_base_l = freq_base;
40 freq_scale_l = freq_scale;
41 }
42
43 // norm
44 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
45 cb(cur, "attn_norm", il);
46
47 // self-attention
48 {
49 // compute Q and K and RoPE them
50 ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
51 cb(Qcur, "Qcur", il);
52
53 ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
54 cb(Kcur, "Kcur", il);
55
56 ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
57 cb(Vcur, "Vcur", il);
58
59 Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
60 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
61 Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);

Callers

nothing calls this directly

Calls 9

ggml_scaleFunction · 0.85
ggml_reshape_3dFunction · 0.85
ggml_rope_extFunction · 0.85
ggml_get_rowsFunction · 0.85
ggml_addFunction · 0.85
ggml_tanhFunction · 0.85
get_rope_freq_baseMethod · 0.80
get_rope_freq_scaleMethod · 0.80

Tested by

no test coverage detected