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

Method llm_build_olmo2

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

Source from the content-addressed store, hash-verified

2
3template <bool iswa>
4llm_build_olmo2<iswa>::llm_build_olmo2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
5 const int64_t n_embd_head = hparams.n_embd_head_v;
6
7 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8 GGML_ASSERT(n_embd_head == hparams.n_rot);
9
10 ggml_tensor * cur;
11 ggml_tensor * inpL;
12
13 inpL = build_inp_embd(model.tok_embd);
14
15 // inp_pos - contains the positions
16 ggml_tensor * inp_pos = build_inp_pos();
17
18 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
19 inp_attn_type * inp_attn = nullptr;
20
21 if constexpr (iswa) {
22 inp_attn = build_attn_inp_kv_iswa();
23 } else {
24 inp_attn = build_attn_inp_kv();
25 }
26 ggml_tensor * inp_out_ids = build_inp_out_ids();
27
28 for (int il = 0; il < n_layer; ++il) {
29 ggml_tensor * inpSA = inpL;
30
31 cur = inpL;
32
33 // self_attention
34 {
35 // compute Q and K and RoPE them
36 ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
37 cb(Qcur, "Qcur", il);
38
39 ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
40 cb(Kcur, "Kcur", il);
41
42 ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
43 cb(Vcur, "Vcur", il);
44
45 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
46 LLM_NORM_RMS, il);
47 cb(Qcur, "Qcur_normed", il);
48
49 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
50 LLM_NORM_RMS, il);
51 cb(Kcur, "Kcur_normed", il);
52
53 Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
54 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
55 Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
56
57 const bool is_swa = hparams.is_swa(il);
58
59 if (is_swa) {
60 // For sliding window layers, Olmo3 use regular rope with no yarn rope scaling.
61 // This is achieved here by setting freq_scale and attn_factor to 1.

Callers

nothing calls this directly

Calls 6

ggml_reshape_3dFunction · 0.85
ggml_rope_extFunction · 0.85
ggml_get_rowsFunction · 0.85
ggml_addFunction · 0.85
is_swaMethod · 0.80

Tested by

no test coverage detected