@fugood/llama.node 1.3.0-rc.6 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/CMakeLists.txt +12 -2
  2. package/package.json +14 -14
  3. package/scripts/llama.cpp.patch +8 -9
  4. package/src/llama.cpp/common/CMakeLists.txt +2 -0
  5. package/src/llama.cpp/common/arg.cpp +39 -1001
  6. package/src/llama.cpp/common/arg.h +2 -2
  7. package/src/llama.cpp/common/chat.cpp +216 -2
  8. package/src/llama.cpp/common/chat.h +1 -0
  9. package/src/llama.cpp/common/common.cpp +33 -0
  10. package/src/llama.cpp/common/common.h +13 -0
  11. package/src/llama.cpp/common/download.cpp +1054 -0
  12. package/src/llama.cpp/common/download.h +55 -0
  13. package/src/llama.cpp/common/json-schema-to-grammar.cpp +19 -3
  14. package/src/llama.cpp/ggml/CMakeLists.txt +3 -1
  15. package/src/llama.cpp/ggml/include/ggml-hexagon.h +19 -0
  16. package/src/llama.cpp/ggml/include/ggml.h +2 -0
  17. package/src/llama.cpp/ggml/src/CMakeLists.txt +7 -3
  18. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +10 -3
  19. package/src/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +4 -5
  20. package/src/llama.cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +108 -49
  21. package/src/llama.cpp/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp +50 -0
  22. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +3 -1
  23. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +0 -5
  24. package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +172 -35
  25. package/src/llama.cpp/ggml/src/ggml-cpu/repack.cpp +82 -21
  26. package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +25 -25
  27. package/src/llama.cpp/include/llama.h +7 -3
  28. package/src/llama.cpp/src/CMakeLists.txt +95 -0
  29. package/src/llama.cpp/src/llama-arch.cpp +108 -0
  30. package/src/llama.cpp/src/llama-arch.h +11 -0
  31. package/src/llama.cpp/src/llama-batch.cpp +63 -31
  32. package/src/llama.cpp/src/llama-batch.h +12 -1
  33. package/src/llama.cpp/src/llama-chat.cpp +32 -0
  34. package/src/llama.cpp/src/llama-chat.h +1 -0
  35. package/src/llama.cpp/src/llama-context.cpp +44 -16
  36. package/src/llama.cpp/src/llama-context.h +5 -5
  37. package/src/llama.cpp/src/llama-cparams.h +1 -0
  38. package/src/llama.cpp/src/llama-graph.cpp +12 -7
  39. package/src/llama.cpp/src/llama-hparams.cpp +11 -1
  40. package/src/llama.cpp/src/llama-hparams.h +6 -0
  41. package/src/llama.cpp/src/llama-kv-cache-iswa.cpp +3 -1
  42. package/src/llama.cpp/src/llama-kv-cache.cpp +56 -21
  43. package/src/llama.cpp/src/llama-kv-cache.h +2 -4
  44. package/src/llama.cpp/src/llama-kv-cells.h +44 -2
  45. package/src/llama.cpp/src/llama-memory-recurrent.cpp +18 -14
  46. package/src/llama.cpp/src/llama-memory-recurrent.h +2 -2
  47. package/src/llama.cpp/src/llama-model.cpp +350 -13194
  48. package/src/llama.cpp/src/llama-model.h +9 -2
  49. package/src/llama.cpp/src/llama-quant.cpp +1 -1
  50. package/src/llama.cpp/src/llama-vocab.cpp +5 -0
  51. package/src/llama.cpp/src/llama-vocab.h +1 -0
  52. package/src/llama.cpp/src/models/apertus.cpp +125 -0
  53. package/src/llama.cpp/src/models/arcee.cpp +135 -0
  54. package/src/llama.cpp/src/models/arctic.cpp +138 -0
  55. package/src/llama.cpp/src/models/arwkv7.cpp +86 -0
  56. package/src/llama.cpp/src/models/baichuan.cpp +122 -0
  57. package/src/llama.cpp/src/models/bailingmoe.cpp +144 -0
  58. package/src/llama.cpp/src/models/bailingmoe2.cpp +135 -0
  59. package/src/llama.cpp/src/models/bert.cpp +176 -0
  60. package/src/llama.cpp/src/models/bitnet.cpp +160 -0
  61. package/src/llama.cpp/src/models/bloom.cpp +101 -0
  62. package/src/llama.cpp/src/models/chameleon.cpp +178 -0
  63. package/src/llama.cpp/src/models/chatglm.cpp +132 -0
  64. package/src/llama.cpp/src/models/codeshell.cpp +111 -0
  65. package/src/llama.cpp/src/models/cogvlm.cpp +100 -0
  66. package/src/llama.cpp/src/models/cohere2-iswa.cpp +131 -0
  67. package/src/llama.cpp/src/models/command-r.cpp +122 -0
  68. package/src/llama.cpp/src/models/dbrx.cpp +123 -0
  69. package/src/llama.cpp/src/models/deci.cpp +135 -0
  70. package/src/llama.cpp/src/models/deepseek.cpp +144 -0
  71. package/src/llama.cpp/src/models/deepseek2.cpp +236 -0
  72. package/src/llama.cpp/src/models/dots1.cpp +134 -0
  73. package/src/llama.cpp/src/models/dream.cpp +105 -0
  74. package/src/llama.cpp/src/models/ernie4-5-moe.cpp +150 -0
  75. package/src/llama.cpp/src/models/ernie4-5.cpp +111 -0
  76. package/src/llama.cpp/src/models/exaone.cpp +114 -0
  77. package/src/llama.cpp/src/models/exaone4.cpp +123 -0
  78. package/src/llama.cpp/src/models/falcon-h1.cpp +113 -0
  79. package/src/llama.cpp/src/models/falcon.cpp +120 -0
  80. package/src/llama.cpp/src/models/gemma-embedding.cpp +120 -0
  81. package/src/llama.cpp/src/models/gemma.cpp +112 -0
  82. package/src/llama.cpp/src/models/gemma2-iswa.cpp +125 -0
  83. package/src/llama.cpp/src/models/gemma3-iswa.cpp +131 -0
  84. package/src/llama.cpp/src/models/gemma3n-iswa.cpp +377 -0
  85. package/src/llama.cpp/src/models/glm4-moe.cpp +153 -0
  86. package/src/llama.cpp/src/models/glm4.cpp +127 -0
  87. package/src/llama.cpp/src/models/gpt2.cpp +105 -0
  88. package/src/llama.cpp/src/models/gptneox.cpp +144 -0
  89. package/src/llama.cpp/src/models/granite-hybrid.cpp +196 -0
  90. package/src/llama.cpp/src/models/granite.cpp +211 -0
  91. package/src/llama.cpp/src/models/graph-context-mamba.cpp +283 -0
  92. package/src/llama.cpp/src/models/grok.cpp +159 -0
  93. package/src/llama.cpp/src/models/grovemoe.cpp +141 -0
  94. package/src/llama.cpp/src/models/hunyuan-dense.cpp +132 -0
  95. package/src/llama.cpp/src/models/hunyuan-moe.cpp +154 -0
  96. package/src/llama.cpp/src/models/internlm2.cpp +120 -0
  97. package/src/llama.cpp/src/models/jais.cpp +86 -0
  98. package/src/llama.cpp/src/models/jamba.cpp +106 -0
  99. package/src/llama.cpp/src/models/lfm2.cpp +173 -0
  100. package/src/llama.cpp/src/models/llada-moe.cpp +122 -0
  101. package/src/llama.cpp/src/models/llada.cpp +99 -0
  102. package/src/llama.cpp/src/models/llama-iswa.cpp +174 -0
  103. package/src/llama.cpp/src/models/llama.cpp +155 -0
  104. package/src/llama.cpp/src/models/mamba.cpp +55 -0
  105. package/src/llama.cpp/src/models/minicpm3.cpp +199 -0
  106. package/src/llama.cpp/src/models/minimax-m2.cpp +124 -0
  107. package/src/llama.cpp/src/models/models.h +481 -0
  108. package/src/llama.cpp/src/models/mpt.cpp +126 -0
  109. package/src/llama.cpp/src/models/nemotron-h.cpp +121 -0
  110. package/src/llama.cpp/src/models/nemotron.cpp +122 -0
  111. package/src/llama.cpp/src/models/neo-bert.cpp +104 -0
  112. package/src/llama.cpp/src/models/olmo.cpp +121 -0
  113. package/src/llama.cpp/src/models/olmo2.cpp +150 -0
  114. package/src/llama.cpp/src/models/olmoe.cpp +124 -0
  115. package/src/llama.cpp/src/models/openai-moe-iswa.cpp +123 -0
  116. package/src/llama.cpp/src/models/openelm.cpp +124 -0
  117. package/src/llama.cpp/src/models/orion.cpp +123 -0
  118. package/src/llama.cpp/src/models/pangu-embedded.cpp +121 -0
  119. package/src/llama.cpp/src/models/phi2.cpp +121 -0
  120. package/src/llama.cpp/src/models/phi3.cpp +152 -0
  121. package/src/llama.cpp/src/models/plamo.cpp +110 -0
  122. package/src/llama.cpp/src/models/plamo2.cpp +316 -0
  123. package/src/llama.cpp/src/models/plm.cpp +168 -0
  124. package/src/llama.cpp/src/models/qwen.cpp +108 -0
  125. package/src/llama.cpp/src/models/qwen2.cpp +117 -0
  126. package/src/llama.cpp/src/models/qwen2moe.cpp +151 -0
  127. package/src/llama.cpp/src/models/qwen2vl.cpp +117 -0
  128. package/src/llama.cpp/src/models/qwen3.cpp +117 -0
  129. package/src/llama.cpp/src/models/qwen3moe.cpp +124 -0
  130. package/src/llama.cpp/src/models/qwen3vl-moe.cpp +149 -0
  131. package/src/llama.cpp/src/models/qwen3vl.cpp +141 -0
  132. package/src/llama.cpp/src/models/refact.cpp +94 -0
  133. package/src/llama.cpp/src/models/rwkv6-base.cpp +162 -0
  134. package/src/llama.cpp/src/models/rwkv6.cpp +94 -0
  135. package/src/llama.cpp/src/models/rwkv6qwen2.cpp +86 -0
  136. package/src/llama.cpp/src/models/rwkv7-base.cpp +135 -0
  137. package/src/llama.cpp/src/models/rwkv7.cpp +90 -0
  138. package/src/llama.cpp/src/models/seed-oss.cpp +124 -0
  139. package/src/llama.cpp/src/models/smallthinker.cpp +120 -0
  140. package/src/llama.cpp/src/models/smollm3.cpp +128 -0
  141. package/src/llama.cpp/src/models/stablelm.cpp +146 -0
  142. package/src/llama.cpp/src/models/starcoder.cpp +100 -0
  143. package/src/llama.cpp/src/models/starcoder2.cpp +121 -0
  144. package/src/llama.cpp/src/models/t5-dec.cpp +166 -0
  145. package/src/llama.cpp/src/models/t5-enc.cpp +96 -0
  146. package/src/llama.cpp/src/models/wavtokenizer-dec.cpp +149 -0
  147. package/src/llama.cpp/src/models/xverse.cpp +108 -0
@@ -0,0 +1,121 @@
1
+ #include "models.h"
2
+
3
+
4
+
5
+ llm_build_nemotron_h::llm_build_nemotron_h(const llama_model & model, const llm_graph_params & params) :
6
+ llm_graph_context_mamba(params) {
7
+ const int64_t n_embd_head = hparams.n_embd_head_v;
8
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
9
+
10
+ ggml_tensor * cur;
11
+ ggml_tensor * inpL;
12
+
13
+ inpL = build_inp_embd(model.tok_embd);
14
+ ggml_build_forward_expand(gf, inpL);
15
+
16
+ auto * inp = build_inp_mem_hybrid();
17
+
18
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
19
+
20
+ for (int il = 0; il < n_layer; ++il) {
21
+ struct ggml_tensor * inpSA = inpL;
22
+
23
+ // norm
24
+ cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
25
+ cb(cur, "attn_norm", il);
26
+
27
+ if (hparams.is_recurrent(il)) {
28
+ // ssm layer //
29
+ cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
30
+ } else if (hparams.n_ff(il) == 0) {
31
+ // attention layer //
32
+ cur = build_attention_layer(cur, inp->get_attn(), model, n_embd_head, il);
33
+ } else {
34
+ cur = build_ffn_layer(cur, model, il);
35
+ }
36
+
37
+ if (il == n_layer - 1 && inp_out_ids) {
38
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
39
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
40
+ }
41
+
42
+ // add residual
43
+ cur = ggml_add(ctx0, cur, inpSA);
44
+ cb(cur, "nemotron_h_block_out", il);
45
+
46
+ // input for next layer
47
+ inpL = cur;
48
+ }
49
+
50
+ cur = inpL;
51
+
52
+ cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
53
+
54
+ cb(cur, "result_norm", -1);
55
+ res->t_embd = cur;
56
+
57
+ // lm_head
58
+ cur = build_lora_mm(model.output, cur);
59
+ cb(cur, "result_output", -1);
60
+ res->t_logits = cur;
61
+
62
+ ggml_build_forward_expand(gf, cur);
63
+ }
64
+
65
+ ggml_tensor * llm_build_nemotron_h::build_attention_layer(ggml_tensor * cur,
66
+ llm_graph_input_attn_kv * inp_attn,
67
+ const llama_model & model,
68
+ const int64_t n_embd_head,
69
+ const int il) {
70
+ // compute Q and K and (optionally) RoPE them
71
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
72
+ cb(Qcur, "Qcur", il);
73
+ if (model.layers[il].bq) {
74
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
75
+ cb(Qcur, "Qcur", il);
76
+ }
77
+
78
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
79
+ cb(Kcur, "Kcur", il);
80
+ if (model.layers[il].bk) {
81
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
82
+ cb(Kcur, "Kcur", il);
83
+ }
84
+
85
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
86
+ cb(Vcur, "Vcur", il);
87
+ if (model.layers[il].bv) {
88
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
89
+ cb(Vcur, "Vcur", il);
90
+ }
91
+
92
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
93
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
94
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
95
+
96
+ cb(Qcur, "Qcur", il);
97
+ cb(Kcur, "Kcur", il);
98
+ cb(Vcur, "Vcur", il);
99
+
100
+ const float kq_scale =
101
+ hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
102
+ cur = build_attn(inp_attn,
103
+ model.layers[il].wo, model.layers[il].bo,
104
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
105
+ cb(cur, "attn_out", il);
106
+ return cur;
107
+ }
108
+
109
+ ggml_tensor * llm_build_nemotron_h::build_ffn_layer(ggml_tensor * cur, const llama_model & model, const int il) {
110
+ cur = build_ffn(cur,
111
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
112
+ NULL, NULL, NULL,
113
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
114
+ NULL, LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);
115
+ cb(cur, "ffn_out", il);
116
+
117
+ cur = build_cvec(cur, il);
118
+ cb(cur, "l_out", il);
119
+
120
+ return cur;
121
+ }
@@ -0,0 +1,122 @@
1
+ #include "models.h"
2
+
3
+ llm_build_nemotron::llm_build_nemotron(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
+ const int64_t n_embd_head = hparams.n_embd_head_v;
5
+
6
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
7
+ //GGML_ASSERT(n_embd_head == hparams.n_rot);
8
+
9
+ ggml_tensor * cur;
10
+ ggml_tensor * inpL;
11
+
12
+ inpL = build_inp_embd(model.tok_embd);
13
+
14
+ // inp_pos - contains the positions
15
+ ggml_tensor * inp_pos = build_inp_pos();
16
+
17
+ auto * inp_attn = build_attn_inp_kv();
18
+
19
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
20
+
21
+ for (int il = 0; il < n_layer; ++il) {
22
+ ggml_tensor * inpSA = inpL;
23
+
24
+ // norm
25
+ cur = build_norm(inpL,
26
+ model.layers[il].attn_norm,
27
+ model.layers[il].attn_norm_b,
28
+ LLM_NORM, il);
29
+ cb(cur, "attn_norm", il);
30
+
31
+ // self-attention
32
+ {
33
+ // compute Q and K and RoPE them
34
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
35
+ cb(Qcur, "Qcur", il);
36
+ if (model.layers[il].bq) {
37
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
38
+ cb(Qcur, "Qcur", il);
39
+ }
40
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
41
+ cb(Kcur, "Kcur", il);
42
+ if (model.layers[il].bk) {
43
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
44
+ cb(Kcur, "Kcur", il);
45
+ }
46
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
47
+ cb(Vcur, "Vcur", il);
48
+ if (model.layers[il].bv) {
49
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
50
+ cb(Vcur, "Vcur", il);
51
+ }
52
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
53
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
54
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
55
+
56
+ Qcur = ggml_rope_ext(
57
+ ctx0, Qcur, inp_pos, nullptr,
58
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
59
+ ext_factor, attn_factor, beta_fast, beta_slow
60
+ );
61
+
62
+ Kcur = ggml_rope_ext(
63
+ ctx0, Kcur, inp_pos, nullptr,
64
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
65
+ ext_factor, attn_factor, beta_fast, beta_slow
66
+ );
67
+
68
+ cb(Qcur, "Qcur", il);
69
+ cb(Kcur, "Kcur", il);
70
+ cb(Vcur, "Vcur", il);
71
+
72
+ cur = build_attn(inp_attn,
73
+ model.layers[il].wo, model.layers[il].bo,
74
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
75
+ }
76
+ if (il == n_layer - 1 && inp_out_ids) {
77
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
78
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
79
+ }
80
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
81
+ cb(ffn_inp, "ffn_inp", il);
82
+
83
+ // feed-forward network
84
+ cur = build_norm(ffn_inp,
85
+ model.layers[il].ffn_norm,
86
+ model.layers[il].ffn_norm_b,
87
+ LLM_NORM, il);
88
+ cb(cur, "ffn_norm", il);
89
+
90
+ cur = build_ffn(cur,
91
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
92
+ NULL, NULL, NULL,
93
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
94
+ NULL,
95
+ LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
96
+
97
+ cur = ggml_add(ctx0, cur, ffn_inp);
98
+ cb(cur, "ffn_out", il);
99
+
100
+ cur = build_cvec(cur, il);
101
+ cb(cur, "l_out", il);
102
+
103
+ // input for next layer
104
+ inpL = cur;
105
+ }
106
+ cur = inpL;
107
+
108
+ cur = build_norm(cur,
109
+ model.output_norm, model.output_norm_b,
110
+ LLM_NORM, -1);
111
+
112
+ cb(cur, "result_norm", -1);
113
+ res->t_embd = cur;
114
+
115
+ // lm_head
116
+ cur = build_lora_mm(model.output, cur);
117
+
118
+ cb(cur, "result_output", -1);
119
+ res->t_logits = cur;
120
+
121
+ ggml_build_forward_expand(gf, cur);
122
+ }
@@ -0,0 +1,104 @@
1
+ #include "models.h"
2
+
3
+ llm_build_neo_bert::llm_build_neo_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
+ const int64_t n_embd_head = hparams.n_embd_head_v;
5
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
6
+
7
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8
+
9
+ ggml_tensor * cur;
10
+ ggml_tensor * inpL;
11
+ ggml_tensor * inp_pos = build_inp_pos();
12
+
13
+ // construct input embeddings (token, type, position)
14
+ inpL = build_inp_embd(model.tok_embd);
15
+ cb(inpL, "inp_embd", -1);
16
+
17
+ auto * inp_attn = build_attn_inp_no_cache();
18
+
19
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
20
+
21
+ for (int il = 0; il < n_layer; ++il) {
22
+ ggml_tensor * cur = inpL;
23
+
24
+ // pre-norm
25
+ cur = build_norm(inpL,
26
+ model.layers[il].attn_norm, NULL,
27
+ LLM_NORM_RMS, il);
28
+
29
+ {
30
+ ggml_tensor * Qcur;
31
+ ggml_tensor * Kcur;
32
+ ggml_tensor * Vcur;
33
+
34
+ // self-attention
35
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
36
+ cb(cur, "wqkv", il);
37
+
38
+ Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
39
+ Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
40
+ Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
41
+
42
+ // RoPE
43
+ Qcur = ggml_rope_ext(
44
+ ctx0, Qcur, inp_pos, nullptr,
45
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
46
+ ext_factor, attn_factor, beta_fast, beta_slow
47
+ );
48
+
49
+ Kcur = ggml_rope_ext(
50
+ ctx0, Kcur, inp_pos, nullptr,
51
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
52
+ ext_factor, attn_factor, beta_fast, beta_slow
53
+ );
54
+
55
+ cb(Qcur, "Qcur", il);
56
+ cb(Kcur, "Kcur", il);
57
+ cb(Vcur, "Vcur", il);
58
+
59
+ cur = build_attn(inp_attn,
60
+ model.layers[il].wo, nullptr,
61
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
62
+ cb(cur, "kqv_out", il);
63
+ }
64
+ if (il == n_layer - 1 && inp_out_ids) {
65
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
66
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
67
+ }
68
+ // re-add the layer input
69
+ cur = ggml_add(ctx0, cur, inpL);
70
+
71
+ ggml_tensor * ffn_inp = cur;
72
+ cb(ffn_inp, "ffn_inp", il);
73
+
74
+ // pre-norm
75
+ cur = build_norm(ffn_inp,
76
+ model.layers[il].ffn_norm, NULL,
77
+ LLM_NORM_RMS, il);
78
+ cb(cur, "ffn_norm", il);
79
+
80
+ // feed-forward network
81
+ cur = build_ffn(cur,
82
+ model.layers[il].ffn_up,
83
+ NULL, NULL, NULL, NULL, NULL,
84
+ model.layers[il].ffn_down,
85
+ NULL, NULL, NULL,
86
+ LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
87
+
88
+ // attentions bypass the intermediate layer
89
+ cur = ggml_add(ctx0, cur, ffn_inp);
90
+
91
+ // input for next layer
92
+ inpL = cur;
93
+ }
94
+ cur = inpL;
95
+
96
+ cur = build_norm(cur,
97
+ model.output_norm_enc, NULL,
98
+ LLM_NORM_RMS, -1);
99
+
100
+ cb(cur, "result_embd", -1);
101
+ res->t_embd = cur;
102
+
103
+ ggml_build_forward_expand(gf, cur);
104
+ }
@@ -0,0 +1,121 @@
1
+ #include "models.h"
2
+
3
+ llm_build_olmo::llm_build_olmo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
+ const int64_t n_embd_head = hparams.n_embd_head_v;
5
+
6
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
7
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
8
+
9
+ ggml_tensor * cur;
10
+ ggml_tensor * inpL;
11
+
12
+ inpL = build_inp_embd(model.tok_embd);
13
+
14
+ // inp_pos - contains the positions
15
+ ggml_tensor * inp_pos = build_inp_pos();
16
+
17
+ auto * inp_attn = build_attn_inp_kv();
18
+
19
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
20
+
21
+ for (int il = 0; il < n_layer; ++il) {
22
+ ggml_tensor * inpSA = inpL;
23
+
24
+ // norm
25
+ cur = build_norm(inpL,
26
+ NULL, NULL,
27
+ LLM_NORM, il);
28
+ cb(cur, "attn_norm", il);
29
+
30
+ // self-attention
31
+ {
32
+ // compute Q and K and RoPE them
33
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
34
+ cb(Qcur, "Qcur", il);
35
+ if (hparams.f_clamp_kqv > 0.0f) {
36
+ Qcur = ggml_clamp(ctx0, Qcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
37
+ cb(Qcur, "Qcur", il);
38
+ }
39
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
40
+ cb(Kcur, "Kcur", il);
41
+ if (hparams.f_clamp_kqv > 0.0f) {
42
+ Kcur = ggml_clamp(ctx0, Kcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
43
+ cb(Kcur, "Kcur", il);
44
+ }
45
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
46
+ cb(Vcur, "Vcur", il);
47
+ if (hparams.f_clamp_kqv > 0.0f) {
48
+ Vcur = ggml_clamp(ctx0, Vcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
49
+ cb(Vcur, "Vcur", il);
50
+ }
51
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
52
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
53
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
54
+
55
+ Qcur = ggml_rope_ext(
56
+ ctx0, Qcur, inp_pos, nullptr,
57
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
58
+ ext_factor, attn_factor, beta_fast, beta_slow
59
+ );
60
+
61
+ Kcur = ggml_rope_ext(
62
+ ctx0, Kcur, inp_pos, nullptr,
63
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
64
+ ext_factor, attn_factor, beta_fast, beta_slow
65
+ );
66
+
67
+ cb(Qcur, "Qcur", il);
68
+ cb(Kcur, "Kcur", il);
69
+ cb(Vcur, "Vcur", il);
70
+
71
+ cur = build_attn(inp_attn,
72
+ model.layers[il].wo, nullptr,
73
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
74
+ }
75
+ if (il == n_layer - 1 && inp_out_ids) {
76
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
77
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
78
+ }
79
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
80
+ cb(ffn_inp, "ffn_inp", il);
81
+
82
+ // feed-forward network
83
+ cur = build_norm(ffn_inp,
84
+ NULL, NULL,
85
+ LLM_NORM, il);
86
+ cb(cur, "ffn_norm", il);
87
+
88
+ cur = build_ffn(cur,
89
+ model.layers[il].ffn_up, NULL, NULL,
90
+ model.layers[il].ffn_gate, NULL, NULL,
91
+ model.layers[il].ffn_down, NULL, NULL,
92
+ NULL,
93
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
94
+ cb(cur, "ffn_out", il);
95
+
96
+ cur = ggml_add(ctx0, cur, ffn_inp);
97
+ cb(cur, "ffn_out", il);
98
+
99
+ cur = build_cvec(cur, il);
100
+ cb(cur, "l_out", il);
101
+
102
+ // input for next layer
103
+ inpL = cur;
104
+ }
105
+ cur = inpL;
106
+
107
+ cur = build_norm(cur,
108
+ NULL, NULL,
109
+ LLM_NORM, -1);
110
+
111
+ cb(cur, "result_norm", -1);
112
+ res->t_embd = cur;
113
+
114
+ // lm_head
115
+ cur = build_lora_mm(model.output, cur);
116
+
117
+ cb(cur, "result_output", -1);
118
+ res->t_logits = cur;
119
+
120
+ ggml_build_forward_expand(gf, cur);
121
+ }
@@ -0,0 +1,150 @@
1
+ #include "models.h"
2
+
3
+ template <bool iswa>
4
+ llm_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.
62
+ // We also set ext_factor to 0 to avoid a few unnecessary computations.
63
+ Qcur = ggml_rope_ext(
64
+ ctx0, Qcur, inp_pos, nullptr,
65
+ n_rot, rope_type, n_ctx_orig, freq_base, 1.0,
66
+ 0.0, 1.0, beta_fast, beta_slow
67
+ );
68
+
69
+ Kcur = ggml_rope_ext(
70
+ ctx0, Kcur, inp_pos, nullptr,
71
+ n_rot, rope_type, n_ctx_orig, freq_base, 1.0,
72
+ 0.0, 1.0, beta_fast, beta_slow
73
+ );
74
+ } else {
75
+ Qcur = ggml_rope_ext(
76
+ ctx0, Qcur, inp_pos, nullptr,
77
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
78
+ ext_factor, attn_factor, beta_fast, beta_slow
79
+ );
80
+
81
+ Kcur = ggml_rope_ext(
82
+ ctx0, Kcur, inp_pos, nullptr,
83
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
84
+ ext_factor, attn_factor, beta_fast, beta_slow
85
+ );
86
+ }
87
+ cb(Qcur, "Qcur", il);
88
+ cb(Kcur, "Kcur", il);
89
+ cb(Vcur, "Vcur", il);
90
+
91
+ cur = build_attn(inp_attn,
92
+ model.layers[il].wo, NULL,
93
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
94
+ }
95
+ if (il == n_layer - 1 && inp_out_ids) {
96
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
97
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
98
+ }
99
+ cur = build_norm(cur,
100
+ model.layers[il].attn_post_norm, NULL,
101
+ LLM_NORM_RMS, il);
102
+ cb(cur, "attn_post_norm", il);
103
+
104
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
105
+ cb(ffn_inp, "ffn_inp", il);
106
+
107
+ // feed-forward network
108
+ cur = build_ffn(ffn_inp,
109
+ model.layers[il].ffn_up, NULL, NULL,
110
+ model.layers[il].ffn_gate, NULL, NULL,
111
+ model.layers[il].ffn_down, NULL, NULL,
112
+ NULL,
113
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
114
+ cb(cur, "ffn_out", il);
115
+
116
+ cur = build_norm(cur,
117
+ model.layers[il].ffn_post_norm, NULL,
118
+ LLM_NORM_RMS, -1);
119
+ cb(cur, "ffn_post_norm", -1);
120
+
121
+ cur = ggml_add(ctx0, cur, ffn_inp);
122
+ cb(cur, "ffn_out", il);
123
+
124
+ cur = build_cvec(cur, il);
125
+ cb(cur, "l_out", il);
126
+
127
+ // input for next layer
128
+ inpL = cur;
129
+ }
130
+ cur = inpL;
131
+
132
+ cur = build_norm(cur,
133
+ model.output_norm, NULL,
134
+ LLM_NORM_RMS, -1);
135
+
136
+ cb(cur, "result_norm", -1);
137
+ res->t_embd = cur;
138
+
139
+ // lm_head
140
+ cur = build_lora_mm(model.output, cur);
141
+
142
+ cb(cur, "result_output", -1);
143
+ res->t_logits = cur;
144
+
145
+ ggml_build_forward_expand(gf, cur);
146
+ }
147
+
148
+ // Explicit template instantiations
149
+ template struct llm_build_olmo2<false>;
150
+ template struct llm_build_olmo2<true>;