@fugood/llama.node 1.3.0 → 1.3.2

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 (143) hide show
  1. package/package.json +14 -14
  2. package/scripts/llama.cpp.patch +8 -8
  3. package/src/llama.cpp/common/CMakeLists.txt +2 -0
  4. package/src/llama.cpp/common/arg.cpp +44 -999
  5. package/src/llama.cpp/common/arg.h +2 -2
  6. package/src/llama.cpp/common/chat.cpp +17 -2
  7. package/src/llama.cpp/common/common.cpp +33 -0
  8. package/src/llama.cpp/common/common.h +15 -1
  9. package/src/llama.cpp/common/download.cpp +1054 -0
  10. package/src/llama.cpp/common/download.h +55 -0
  11. package/src/llama.cpp/ggml/CMakeLists.txt +1 -1
  12. package/src/llama.cpp/ggml/include/ggml.h +2 -0
  13. package/src/llama.cpp/ggml/src/CMakeLists.txt +6 -3
  14. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +29 -11
  15. package/src/llama.cpp/ggml/src/ggml-cpu/arch/arm/quants.c +428 -26
  16. package/src/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +4 -5
  17. package/src/llama.cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +108 -49
  18. package/src/llama.cpp/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp +50 -0
  19. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +3 -1
  20. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +21 -21
  21. package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +172 -75
  22. package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +0 -4
  23. package/src/llama.cpp/ggml/src/ggml-cpu/repack.cpp +82 -21
  24. package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +25 -25
  25. package/src/llama.cpp/include/llama.h +7 -3
  26. package/src/llama.cpp/src/CMakeLists.txt +95 -0
  27. package/src/llama.cpp/src/llama-arch.cpp +108 -0
  28. package/src/llama.cpp/src/llama-arch.h +11 -0
  29. package/src/llama.cpp/src/llama-batch.cpp +63 -31
  30. package/src/llama.cpp/src/llama-batch.h +12 -1
  31. package/src/llama.cpp/src/llama-chat.cpp +32 -0
  32. package/src/llama.cpp/src/llama-chat.h +1 -0
  33. package/src/llama.cpp/src/llama-context.cpp +36 -13
  34. package/src/llama.cpp/src/llama-context.h +5 -5
  35. package/src/llama.cpp/src/llama-cparams.h +1 -0
  36. package/src/llama.cpp/src/llama-graph.cpp +3 -3
  37. package/src/llama.cpp/src/llama-hparams.cpp +11 -1
  38. package/src/llama.cpp/src/llama-hparams.h +6 -0
  39. package/src/llama.cpp/src/llama-kv-cache-iswa.cpp +3 -1
  40. package/src/llama.cpp/src/llama-kv-cache.cpp +33 -1
  41. package/src/llama.cpp/src/llama-kv-cells.h +44 -2
  42. package/src/llama.cpp/src/llama-memory-recurrent.cpp +4 -3
  43. package/src/llama.cpp/src/llama-model.cpp +320 -13171
  44. package/src/llama.cpp/src/llama-model.h +8 -0
  45. package/src/llama.cpp/src/llama-quant.cpp +1 -1
  46. package/src/llama.cpp/src/llama-vocab.cpp +5 -0
  47. package/src/llama.cpp/src/llama-vocab.h +1 -0
  48. package/src/llama.cpp/src/models/apertus.cpp +125 -0
  49. package/src/llama.cpp/src/models/arcee.cpp +135 -0
  50. package/src/llama.cpp/src/models/arctic.cpp +138 -0
  51. package/src/llama.cpp/src/models/arwkv7.cpp +86 -0
  52. package/src/llama.cpp/src/models/baichuan.cpp +122 -0
  53. package/src/llama.cpp/src/models/bailingmoe.cpp +144 -0
  54. package/src/llama.cpp/src/models/bailingmoe2.cpp +135 -0
  55. package/src/llama.cpp/src/models/bert.cpp +176 -0
  56. package/src/llama.cpp/src/models/bitnet.cpp +160 -0
  57. package/src/llama.cpp/src/models/bloom.cpp +101 -0
  58. package/src/llama.cpp/src/models/chameleon.cpp +178 -0
  59. package/src/llama.cpp/src/models/chatglm.cpp +132 -0
  60. package/src/llama.cpp/src/models/codeshell.cpp +111 -0
  61. package/src/llama.cpp/src/models/cogvlm.cpp +100 -0
  62. package/src/llama.cpp/src/models/cohere2-iswa.cpp +131 -0
  63. package/src/llama.cpp/src/models/command-r.cpp +122 -0
  64. package/src/llama.cpp/src/models/dbrx.cpp +123 -0
  65. package/src/llama.cpp/src/models/deci.cpp +135 -0
  66. package/src/llama.cpp/src/models/deepseek.cpp +144 -0
  67. package/src/llama.cpp/src/models/deepseek2.cpp +236 -0
  68. package/src/llama.cpp/src/models/dots1.cpp +134 -0
  69. package/src/llama.cpp/src/models/dream.cpp +105 -0
  70. package/src/llama.cpp/src/models/ernie4-5-moe.cpp +150 -0
  71. package/src/llama.cpp/src/models/ernie4-5.cpp +110 -0
  72. package/src/llama.cpp/src/models/exaone.cpp +114 -0
  73. package/src/llama.cpp/src/models/exaone4.cpp +123 -0
  74. package/src/llama.cpp/src/models/falcon-h1.cpp +113 -0
  75. package/src/llama.cpp/src/models/falcon.cpp +120 -0
  76. package/src/llama.cpp/src/models/gemma-embedding.cpp +120 -0
  77. package/src/llama.cpp/src/models/gemma.cpp +112 -0
  78. package/src/llama.cpp/src/models/gemma2-iswa.cpp +125 -0
  79. package/src/llama.cpp/src/models/gemma3-iswa.cpp +131 -0
  80. package/src/llama.cpp/src/models/gemma3n-iswa.cpp +377 -0
  81. package/src/llama.cpp/src/models/glm4-moe.cpp +153 -0
  82. package/src/llama.cpp/src/models/glm4.cpp +127 -0
  83. package/src/llama.cpp/src/models/gpt2.cpp +105 -0
  84. package/src/llama.cpp/src/models/gptneox.cpp +144 -0
  85. package/src/llama.cpp/src/models/granite-hybrid.cpp +196 -0
  86. package/src/llama.cpp/src/models/granite.cpp +211 -0
  87. package/src/llama.cpp/src/models/graph-context-mamba.cpp +283 -0
  88. package/src/llama.cpp/src/models/grok.cpp +159 -0
  89. package/src/llama.cpp/src/models/grovemoe.cpp +141 -0
  90. package/src/llama.cpp/src/models/hunyuan-dense.cpp +132 -0
  91. package/src/llama.cpp/src/models/hunyuan-moe.cpp +154 -0
  92. package/src/llama.cpp/src/models/internlm2.cpp +120 -0
  93. package/src/llama.cpp/src/models/jais.cpp +86 -0
  94. package/src/llama.cpp/src/models/jamba.cpp +106 -0
  95. package/src/llama.cpp/src/models/lfm2.cpp +173 -0
  96. package/src/llama.cpp/src/models/llada-moe.cpp +122 -0
  97. package/src/llama.cpp/src/models/llada.cpp +99 -0
  98. package/src/llama.cpp/src/models/llama-iswa.cpp +174 -0
  99. package/src/llama.cpp/src/models/llama.cpp +155 -0
  100. package/src/llama.cpp/src/models/mamba.cpp +55 -0
  101. package/src/llama.cpp/src/models/minicpm3.cpp +199 -0
  102. package/src/llama.cpp/src/models/minimax-m2.cpp +124 -0
  103. package/src/llama.cpp/src/models/models.h +481 -0
  104. package/src/llama.cpp/src/models/mpt.cpp +126 -0
  105. package/src/llama.cpp/src/models/nemotron-h.cpp +121 -0
  106. package/src/llama.cpp/src/models/nemotron.cpp +122 -0
  107. package/src/llama.cpp/src/models/neo-bert.cpp +104 -0
  108. package/src/llama.cpp/src/models/olmo.cpp +121 -0
  109. package/src/llama.cpp/src/models/olmo2.cpp +150 -0
  110. package/src/llama.cpp/src/models/olmoe.cpp +124 -0
  111. package/src/llama.cpp/src/models/openai-moe-iswa.cpp +124 -0
  112. package/src/llama.cpp/src/models/openelm.cpp +124 -0
  113. package/src/llama.cpp/src/models/orion.cpp +123 -0
  114. package/src/llama.cpp/src/models/pangu-embedded.cpp +121 -0
  115. package/src/llama.cpp/src/models/phi2.cpp +121 -0
  116. package/src/llama.cpp/src/models/phi3.cpp +152 -0
  117. package/src/llama.cpp/src/models/plamo.cpp +110 -0
  118. package/src/llama.cpp/src/models/plamo2.cpp +316 -0
  119. package/src/llama.cpp/src/models/plm.cpp +168 -0
  120. package/src/llama.cpp/src/models/qwen.cpp +108 -0
  121. package/src/llama.cpp/src/models/qwen2.cpp +117 -0
  122. package/src/llama.cpp/src/models/qwen2moe.cpp +151 -0
  123. package/src/llama.cpp/src/models/qwen2vl.cpp +117 -0
  124. package/src/llama.cpp/src/models/qwen3.cpp +117 -0
  125. package/src/llama.cpp/src/models/qwen3moe.cpp +124 -0
  126. package/src/llama.cpp/src/models/qwen3vl-moe.cpp +149 -0
  127. package/src/llama.cpp/src/models/qwen3vl.cpp +141 -0
  128. package/src/llama.cpp/src/models/refact.cpp +94 -0
  129. package/src/llama.cpp/src/models/rwkv6-base.cpp +162 -0
  130. package/src/llama.cpp/src/models/rwkv6.cpp +94 -0
  131. package/src/llama.cpp/src/models/rwkv6qwen2.cpp +86 -0
  132. package/src/llama.cpp/src/models/rwkv7-base.cpp +135 -0
  133. package/src/llama.cpp/src/models/rwkv7.cpp +90 -0
  134. package/src/llama.cpp/src/models/seed-oss.cpp +124 -0
  135. package/src/llama.cpp/src/models/smallthinker.cpp +120 -0
  136. package/src/llama.cpp/src/models/smollm3.cpp +128 -0
  137. package/src/llama.cpp/src/models/stablelm.cpp +146 -0
  138. package/src/llama.cpp/src/models/starcoder.cpp +100 -0
  139. package/src/llama.cpp/src/models/starcoder2.cpp +121 -0
  140. package/src/llama.cpp/src/models/t5-dec.cpp +166 -0
  141. package/src/llama.cpp/src/models/t5-enc.cpp +96 -0
  142. package/src/llama.cpp/src/models/wavtokenizer-dec.cpp +149 -0
  143. package/src/llama.cpp/src/models/xverse.cpp +108 -0
@@ -0,0 +1,105 @@
1
+ #include "models.h"
2
+
3
+ llm_build_gpt2::llm_build_gpt2(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 * pos;
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
+ auto * inp_attn = build_attn_inp_kv();
19
+
20
+ pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
21
+ cb(pos, "pos_embd", -1);
22
+
23
+ inpL = ggml_add(ctx0, inpL, pos);
24
+ cb(inpL, "inpL", -1);
25
+
26
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
27
+
28
+ for (int il = 0; il < n_layer; ++il) {
29
+ cur = build_norm(inpL,
30
+ model.layers[il].attn_norm,
31
+ model.layers[il].attn_norm_b,
32
+ LLM_NORM, il);
33
+ cb(cur, "attn_norm", il);
34
+
35
+ // self-attention
36
+ {
37
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
38
+ cb(cur, "wqkv", il);
39
+
40
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
41
+ cb(cur, "bqkv", il);
42
+
43
+ ggml_tensor * 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));
44
+ ggml_tensor * 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));
45
+ ggml_tensor * 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));
46
+
47
+ cb(Qcur, "Qcur", il);
48
+ cb(Kcur, "Kcur", il);
49
+ cb(Vcur, "Vcur", il);
50
+
51
+ cur = build_attn(inp_attn,
52
+ model.layers[il].wo, model.layers[il].bo,
53
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
54
+ }
55
+
56
+ if (il == n_layer - 1 && inp_out_ids) {
57
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
58
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
59
+ }
60
+
61
+ // add the input
62
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
63
+ cb(ffn_inp, "ffn_inp", il);
64
+
65
+ // FF
66
+ {
67
+ cur = build_norm(ffn_inp,
68
+ model.layers[il].ffn_norm,
69
+ model.layers[il].ffn_norm_b,
70
+ LLM_NORM, il);
71
+ cb(cur, "ffn_norm", il);
72
+
73
+ cur = build_ffn(cur,
74
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
75
+ NULL, NULL, NULL,
76
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
77
+ NULL,
78
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
79
+ cb(cur, "ffn_out", il);
80
+ }
81
+
82
+ cur = ggml_add(ctx0, cur, ffn_inp);
83
+
84
+ cur = build_cvec(cur, il);
85
+ cb(cur, "l_out", il);
86
+
87
+ // input for next layer
88
+ inpL = cur;
89
+ }
90
+
91
+ cur = build_norm(inpL,
92
+ model.output_norm,
93
+ model.output_norm_b,
94
+ LLM_NORM, -1);
95
+
96
+ cb(cur, "result_norm", -1);
97
+ res->t_embd = cur;
98
+
99
+ cur = build_lora_mm(model.output, cur);
100
+
101
+ cb(cur, "result_output", -1);
102
+ res->t_logits = cur;
103
+
104
+ ggml_build_forward_expand(gf, cur);
105
+ }
@@ -0,0 +1,144 @@
1
+ #include "models.h"
2
+
3
+
4
+ llm_build_gptneox::llm_build_gptneox(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
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
7
+
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
+
15
+ // inp_pos - contains the positions
16
+ ggml_tensor * inp_pos = build_inp_pos();
17
+
18
+ auto * inp_attn = build_attn_inp_kv();
19
+
20
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
21
+
22
+ for (int il = 0; il < n_layer; ++il) {
23
+ cur = build_norm(inpL,
24
+ model.layers[il].attn_norm,
25
+ model.layers[il].attn_norm_b,
26
+ LLM_NORM, il);
27
+ cb(cur, "attn_norm", il);
28
+
29
+ // self-attention
30
+ {
31
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
32
+ cb(cur, "wqkv", il);
33
+
34
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
35
+ cb(cur, "bqkv", il);
36
+
37
+ ggml_tensor * 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));
38
+ ggml_tensor * 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));
39
+ ggml_tensor * 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));
40
+
41
+ Qcur = ggml_rope_ext(
42
+ ctx0, Qcur, inp_pos, nullptr,
43
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
44
+ ext_factor, attn_factor, beta_fast, beta_slow
45
+ );
46
+
47
+ Kcur = ggml_rope_ext(
48
+ ctx0, Kcur, inp_pos, nullptr,
49
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
50
+ ext_factor, attn_factor, beta_fast, beta_slow
51
+ );
52
+
53
+ cb(Qcur, "Qcur", il);
54
+ cb(Kcur, "Kcur", il);
55
+ cb(Vcur, "Vcur", il);
56
+
57
+ cur = build_attn(inp_attn,
58
+ model.layers[il].wo, model.layers[il].bo,
59
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
60
+ }
61
+
62
+ if (il == n_layer - 1 && inp_out_ids) {
63
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
64
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
65
+ }
66
+
67
+ // ffn
68
+ if (hparams.use_par_res) {
69
+ // attention and ffn are computed in parallel
70
+ // x = x + attn(ln1(x)) + ffn(ln2(x))
71
+
72
+ ggml_tensor * attn_out = cur;
73
+
74
+ cur = build_norm(inpL,
75
+ model.layers[il].ffn_norm,
76
+ model.layers[il].ffn_norm_b,
77
+ LLM_NORM, il);
78
+ cb(cur, "ffn_norm", il);
79
+
80
+ cur = build_ffn(cur,
81
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
82
+ NULL, NULL, NULL,
83
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
84
+ NULL,
85
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
86
+ cb(cur, "ffn_out", il);
87
+
88
+ cur = ggml_add(ctx0, cur, inpL);
89
+ cb(cur, "ffn_out", il);
90
+
91
+ cur = ggml_add(ctx0, cur, attn_out);
92
+
93
+ cur = build_cvec(cur, il);
94
+ cb(cur, "l_out", il);
95
+
96
+ // input for next layer
97
+ inpL = cur;
98
+ } else {
99
+ // attention and ffn are computed sequentially
100
+ // x = x + attn(ln1(x))
101
+ // x = x + ffn(ln2(x))
102
+
103
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
104
+ cb(ffn_inp, "ffn_inp", il);
105
+
106
+ cur = build_norm(ffn_inp,
107
+ model.layers[il].ffn_norm,
108
+ model.layers[il].ffn_norm_b,
109
+ LLM_NORM, il);
110
+ cb(cur, "ffn_norm", il);
111
+
112
+ cur = build_ffn(cur,
113
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
114
+ NULL, NULL, NULL,
115
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
116
+ NULL,
117
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
118
+ cb(cur, "ffn_out", il);
119
+
120
+ cur = ggml_add(ctx0, cur, ffn_inp);
121
+
122
+ cur = build_cvec(cur, il);
123
+ cb(cur, "l_out", il);
124
+
125
+ // input for next layer
126
+ inpL = cur;
127
+ }
128
+ }
129
+
130
+ cur = build_norm(inpL,
131
+ model.output_norm,
132
+ model.output_norm_b,
133
+ LLM_NORM, -1);
134
+
135
+ cb(cur, "result_norm", -1);
136
+ res->t_embd = cur;
137
+
138
+ cur = build_lora_mm(model.output, cur);
139
+
140
+ cb(cur, "result_output", -1);
141
+ res->t_logits = cur;
142
+
143
+ ggml_build_forward_expand(gf, cur);
144
+ }
@@ -0,0 +1,196 @@
1
+ #include "models.h"
2
+
3
+
4
+ llm_build_granite_hybrid::llm_build_granite_hybrid(const llama_model & model, const llm_graph_params & params) :
5
+ llm_graph_context_mamba(params) {
6
+ const int64_t n_embd_head = hparams.n_embd_head_v;
7
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8
+
9
+ ggml_tensor * cur;
10
+ ggml_tensor * inpL;
11
+
12
+ inpL = build_inp_embd(model.tok_embd);
13
+
14
+ auto * inp = build_inp_mem_hybrid();
15
+
16
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
17
+
18
+ // Positional embeddings populated if rope enabled
19
+ ggml_tensor * inp_pos = nullptr;
20
+ if (hparams.rope_finetuned) {
21
+ inp_pos = build_inp_pos();
22
+ }
23
+
24
+ for (int il = 0; il < n_layer; ++il) {
25
+ struct ggml_tensor * inpSA = inpL;
26
+
27
+ // norm
28
+ cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
29
+ cb(cur, "attn_norm", il);
30
+
31
+ if (hparams.is_recurrent(il)) {
32
+ // ssm layer //
33
+ cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
34
+ } else {
35
+ // attention layer //
36
+ cur = build_attention_layer(cur, inp_pos, inp->get_attn(), model, n_embd_head, il);
37
+ }
38
+
39
+ if (il == n_layer - 1 && inp_out_ids) {
40
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
41
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
42
+ }
43
+
44
+ // ffn
45
+ cur = build_layer_ffn(cur, inpSA, model, il);
46
+
47
+ // input for next layer
48
+ inpL = cur;
49
+ }
50
+
51
+ cur = inpL;
52
+
53
+ cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
54
+
55
+ cb(cur, "result_norm", -1);
56
+ res->t_embd = cur;
57
+
58
+ // lm_head
59
+ cur = build_lora_mm(model.output, cur);
60
+
61
+ // For Granite architectures - scale logits
62
+ if (hparams.f_logit_scale) {
63
+ cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
64
+ }
65
+ cb(cur, "result_output", -1);
66
+ res->t_logits = cur;
67
+
68
+ ggml_build_forward_expand(gf, cur);
69
+ }
70
+
71
+ ggml_tensor * llm_build_granite_hybrid::build_attention_layer(ggml_tensor * cur,
72
+ ggml_tensor * inp_pos,
73
+ llm_graph_input_attn_kv * inp_attn,
74
+ const llama_model & model,
75
+ const int64_t n_embd_head,
76
+ const int il) {
77
+ // compute Q and K and (optionally) RoPE them
78
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
79
+ cb(Qcur, "Qcur", il);
80
+ if (model.layers[il].bq) {
81
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
82
+ cb(Qcur, "Qcur", il);
83
+ }
84
+
85
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
86
+ cb(Kcur, "Kcur", il);
87
+ if (model.layers[il].bk) {
88
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
89
+ cb(Kcur, "Kcur", il);
90
+ }
91
+
92
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
93
+ cb(Vcur, "Vcur", il);
94
+ if (model.layers[il].bv) {
95
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
96
+ cb(Vcur, "Vcur", il);
97
+ }
98
+
99
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
100
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
101
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
102
+
103
+ const bool use_rope = hparams.rope_finetuned;
104
+ if (use_rope) {
105
+ ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
106
+ Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
107
+ ext_factor, attn_factor, beta_fast, beta_slow);
108
+
109
+ Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
110
+ ext_factor, attn_factor, beta_fast, beta_slow);
111
+ }
112
+
113
+ cb(Qcur, "Qcur", il);
114
+ cb(Kcur, "Kcur", il);
115
+ cb(Vcur, "Vcur", il);
116
+
117
+ const float kq_scale =
118
+ hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
119
+ cur = build_attn(inp_attn,
120
+ model.layers[il].wo, model.layers[il].bo,
121
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
122
+ cb(cur, "attn_out", il);
123
+ return cur;
124
+ }
125
+
126
+ ggml_tensor * llm_build_granite_hybrid::build_layer_ffn(ggml_tensor * cur,
127
+ ggml_tensor * inpSA,
128
+ const llama_model & model,
129
+ const int il) {
130
+ // For Granite architectures - scale residual
131
+ if (hparams.f_residual_scale) {
132
+ cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
133
+ }
134
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
135
+ cb(ffn_inp, "ffn_inp", il);
136
+
137
+ // feed-forward network (non-MoE)
138
+ if (model.layers[il].ffn_gate_inp == nullptr) {
139
+ cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
140
+ cb(cur, "ffn_norm", il);
141
+
142
+ cur = build_ffn(cur,
143
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
144
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
145
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
146
+ NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
147
+ cb(cur, "ffn_out", il);
148
+
149
+ } else {
150
+ // MoE branch
151
+ cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
152
+ cb(cur, "ffn_norm", il);
153
+
154
+ ggml_tensor * moe_out =
155
+ build_moe_ffn(cur,
156
+ model.layers[il].ffn_gate_inp,
157
+ model.layers[il].ffn_up_exps,
158
+ model.layers[il].ffn_gate_exps,
159
+ model.layers[il].ffn_down_exps,
160
+ nullptr,
161
+ n_expert, n_expert_used,
162
+ LLM_FFN_SILU, true,
163
+ false, 0.0,
164
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
165
+ il);
166
+ cb(moe_out, "ffn_moe_out", il);
167
+
168
+ // For Granite MoE Shared
169
+ if (hparams.n_ff_shexp > 0) {
170
+ ggml_tensor * ffn_shexp =
171
+ build_ffn(cur,
172
+ model.layers[il].ffn_up_shexp, NULL, NULL,
173
+ model.layers[il].ffn_gate_shexp, NULL, NULL,
174
+ model.layers[il].ffn_down_shexp, NULL, NULL,
175
+ NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
176
+ cb(ffn_shexp, "ffn_shexp", il);
177
+
178
+ cur = ggml_add(ctx0, moe_out, ffn_shexp);
179
+ cb(cur, "ffn_out", il);
180
+ } else {
181
+ cur = moe_out;
182
+ }
183
+ }
184
+
185
+ // For Granite architectures - scale residual
186
+ if (hparams.f_residual_scale) {
187
+ cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
188
+ }
189
+ cur = ggml_add(ctx0, cur, ffn_inp);
190
+ cb(cur, "ffn_out", il);
191
+
192
+ cur = build_cvec(cur, il);
193
+ cb(cur, "l_out", il);
194
+
195
+ return cur;
196
+ }
@@ -0,0 +1,211 @@
1
+ #include "models.h"
2
+
3
+
4
+ llm_build_granite::llm_build_granite(
5
+ const llama_model & model,
6
+ const llm_graph_params & params)
7
+ : llm_graph_context(params) {
8
+
9
+ const int64_t n_embd_head = hparams.n_embd_head_v;
10
+
11
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
12
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
13
+
14
+ ggml_tensor * cur;
15
+ ggml_tensor * inpL;
16
+
17
+ inpL = build_inp_embd(model.tok_embd);
18
+
19
+ // inp_pos - built only if rope enabled
20
+ ggml_tensor * inp_pos = nullptr;
21
+ if (hparams.rope_finetuned) {
22
+ inp_pos = build_inp_pos();
23
+ }
24
+ auto * 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
+ // norm
32
+ cur = build_norm(inpL,
33
+ model.layers[il].attn_norm, NULL,
34
+ LLM_NORM_RMS, il);
35
+ cb(cur, "attn_norm", il);
36
+
37
+ // self-attention
38
+ cur = build_attention_layer(
39
+ cur, inp_pos, inp_attn,
40
+ model, n_embd_head, il);
41
+
42
+ if (il == n_layer - 1 && inp_out_ids) {
43
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
44
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
45
+ }
46
+ // ffn
47
+ cur = build_layer_ffn(cur, inpSA, model, il);
48
+
49
+ // input for next layer
50
+ inpL = cur;
51
+ }
52
+ cur = inpL;
53
+
54
+ cur = build_norm(cur,
55
+ model.output_norm, NULL,
56
+ LLM_NORM_RMS, -1);
57
+
58
+ cb(cur, "result_norm", -1);
59
+ res->t_embd = cur;
60
+
61
+ // lm_head
62
+ cur = build_lora_mm(model.output, cur);
63
+
64
+ // For Granite architectures - scale logits
65
+ cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
66
+ cb(cur, "result_output", -1);
67
+ res->t_logits = cur;
68
+
69
+ ggml_build_forward_expand(gf, cur);
70
+ }
71
+
72
+ ggml_tensor * llm_build_granite::build_attention_layer(
73
+ ggml_tensor * cur,
74
+ ggml_tensor * inp_pos,
75
+ llm_graph_input_attn_kv * inp_attn,
76
+ const llama_model & model,
77
+ const int64_t n_embd_head,
78
+ const int il) {
79
+
80
+ // compute Q and K and (optionally) RoPE them
81
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
82
+ cb(Qcur, "Qcur", il);
83
+ if (model.layers[il].bq) {
84
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
85
+ cb(Qcur, "Qcur", il);
86
+ }
87
+
88
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
89
+ cb(Kcur, "Kcur", il);
90
+ if (model.layers[il].bk) {
91
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
92
+ cb(Kcur, "Kcur", il);
93
+ }
94
+
95
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
96
+ cb(Vcur, "Vcur", il);
97
+ if (model.layers[il].bv) {
98
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
99
+ cb(Vcur, "Vcur", il);
100
+ }
101
+
102
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
103
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
104
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
105
+
106
+ const bool use_rope = hparams.rope_finetuned;
107
+ if (use_rope) {
108
+ ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
109
+ Qcur = ggml_rope_ext(
110
+ ctx0, Qcur, inp_pos, rope_factors,
111
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
112
+ ext_factor, attn_factor, beta_fast, beta_slow
113
+ );
114
+
115
+ Kcur = ggml_rope_ext(
116
+ ctx0, Kcur, inp_pos, rope_factors,
117
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
118
+ ext_factor, attn_factor, beta_fast, beta_slow
119
+ );
120
+ }
121
+
122
+ cb(Qcur, "Qcur", il);
123
+ cb(Kcur, "Kcur", il);
124
+ cb(Vcur, "Vcur", il);
125
+
126
+ const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
127
+ cur = build_attn(inp_attn,
128
+ model.layers[il].wo, model.layers[il].bo,
129
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
130
+ cb(cur, "attn_out", il);
131
+ return cur;
132
+ }
133
+
134
+ ggml_tensor * llm_build_granite::build_layer_ffn(
135
+ ggml_tensor * cur,
136
+ ggml_tensor * inpSA,
137
+ const llama_model & model,
138
+ const int il) {
139
+
140
+ // For Granite architectures - scale residual
141
+ if (hparams.f_residual_scale) {
142
+ cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
143
+ }
144
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
145
+ cb(ffn_inp, "ffn_inp", il);
146
+
147
+ // feed-forward network (non-MoE)
148
+ if (model.layers[il].ffn_gate_inp == nullptr) {
149
+
150
+ cur = build_norm(ffn_inp,
151
+ model.layers[il].ffn_norm, NULL,
152
+ LLM_NORM_RMS, il);
153
+ cb(cur, "ffn_norm", il);
154
+
155
+ cur = build_ffn(cur,
156
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
157
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
158
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
159
+ NULL,
160
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
161
+ cb(cur, "ffn_out", il);
162
+
163
+ } else {
164
+ // MoE branch
165
+ cur = build_norm(ffn_inp,
166
+ model.layers[il].ffn_norm, NULL,
167
+ LLM_NORM_RMS, il);
168
+ cb(cur, "ffn_norm", il);
169
+
170
+ ggml_tensor * moe_out = build_moe_ffn(cur,
171
+ model.layers[il].ffn_gate_inp,
172
+ model.layers[il].ffn_up_exps,
173
+ model.layers[il].ffn_gate_exps,
174
+ model.layers[il].ffn_down_exps,
175
+ nullptr,
176
+ n_expert, n_expert_used,
177
+ LLM_FFN_SILU, true,
178
+ false, 0.0,
179
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
180
+ il);
181
+ cb(moe_out, "ffn_moe_out", il);
182
+
183
+ // For Granite MoE Shared
184
+ if (hparams.n_ff_shexp > 0) {
185
+ ggml_tensor * ffn_shexp = build_ffn(cur,
186
+ model.layers[il].ffn_up_shexp, NULL, NULL,
187
+ model.layers[il].ffn_gate_shexp, NULL, NULL,
188
+ model.layers[il].ffn_down_shexp, NULL, NULL,
189
+ NULL,
190
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
191
+ cb(ffn_shexp, "ffn_shexp", il);
192
+
193
+ cur = ggml_add(ctx0, moe_out, ffn_shexp);
194
+ cb(cur, "ffn_out", il);
195
+ } else {
196
+ cur = moe_out;
197
+ }
198
+ }
199
+
200
+ // For Granite architectures - scale residual
201
+ if (hparams.f_residual_scale) {
202
+ cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
203
+ }
204
+ cur = ggml_add(ctx0, cur, ffn_inp);
205
+ cb(cur, "ffn_out", il);
206
+
207
+ cur = build_cvec(cur, il);
208
+ cb(cur, "l_out", il);
209
+
210
+ return cur;
211
+ }