@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
+ llm_build_phi2::llm_build_phi2(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 * attn_norm_output;
12
+ ggml_tensor * ffn_output;
13
+ ggml_tensor * inpL;
14
+
15
+ inpL = build_inp_embd(model.tok_embd);
16
+
17
+ // inp_pos - contains the positions
18
+ ggml_tensor * inp_pos = build_inp_pos();
19
+
20
+ auto * inp_attn = build_attn_inp_kv();
21
+
22
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
23
+
24
+ for (int il = 0; il < n_layer; ++il) {
25
+ attn_norm_output = build_norm(inpL,
26
+ model.layers[il].attn_norm,
27
+ model.layers[il].attn_norm_b,
28
+ LLM_NORM, il);
29
+ cb(attn_norm_output, "attn_norm", il);
30
+
31
+ // self-attention
32
+ {
33
+ ggml_tensor * Qcur = nullptr;
34
+ ggml_tensor * Kcur = nullptr;
35
+ ggml_tensor * Vcur = nullptr;
36
+
37
+ if (model.layers[il].wqkv) {
38
+ cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
39
+ cb(cur, "wqkv", il);
40
+
41
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
42
+ cb(cur, "bqkv", il);
43
+
44
+ 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));
45
+ 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));
46
+ 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));
47
+ } else {
48
+ Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
49
+ Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
50
+ Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
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
+ // with phi2, we scale the Q to avoid precision issues
73
+ // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66
74
+ Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));
75
+
76
+ cur = build_attn(inp_attn,
77
+ model.layers[il].wo, model.layers[il].bo,
78
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
79
+ }
80
+ if (il == n_layer - 1 && inp_out_ids) {
81
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
82
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
83
+ attn_norm_output = ggml_get_rows(ctx0, attn_norm_output, inp_out_ids);
84
+ }
85
+ // FF
86
+ {
87
+ ffn_output = build_ffn(attn_norm_output,
88
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
89
+ NULL, NULL, NULL,
90
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
91
+ NULL,
92
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
93
+ cb(ffn_output, "ffn_out", il);
94
+ }
95
+ cur = ggml_add(ctx0, cur, ffn_output);
96
+ cur = ggml_add(ctx0, cur, inpL);
97
+
98
+ cur = build_cvec(cur, il);
99
+ cb(cur, "l_out", il);
100
+
101
+ // input for next layer
102
+ inpL = cur;
103
+ }
104
+ cur = build_norm(inpL,
105
+ model.output_norm,
106
+ model.output_norm_b,
107
+ LLM_NORM, -1);
108
+
109
+ cb(cur, "result_norm", -1);
110
+ res->t_embd = cur;
111
+
112
+ cur = build_lora_mm(model.output, cur);
113
+ cb(cur, "result_output_no_bias", -1);
114
+
115
+ cur = ggml_add(ctx0, cur, model.output_b);
116
+
117
+ cb(cur, "result_output", -1);
118
+ res->t_logits = cur;
119
+
120
+ ggml_build_forward_expand(gf, cur);
121
+ }
@@ -0,0 +1,152 @@
1
+ #include "models.h"
2
+
3
+ template<bool iswa>
4
+ llm_build_phi3<iswa>::llm_build_phi3(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
+ 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
+ auto * residual = inpL;
30
+
31
+ // self-attention
32
+ {
33
+ // rope freq factors for 128k context
34
+ ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
35
+
36
+ ggml_tensor* attn_norm_output = build_norm(inpL,
37
+ model.layers[il].attn_norm,
38
+ model.layers[il].attn_norm_b,
39
+ LLM_NORM_RMS, il);
40
+ cb(attn_norm_output, "attn_norm", il);
41
+
42
+ ggml_tensor * Qcur = nullptr;
43
+ ggml_tensor * Kcur = nullptr;
44
+ ggml_tensor * Vcur = nullptr;
45
+
46
+ if (model.layers[il].wqkv) {
47
+ cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
48
+ cb(cur, "wqkv", il);
49
+
50
+ 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));
51
+ 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));
52
+ 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));
53
+ }
54
+ else {
55
+ Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
56
+ Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
57
+ Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
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);
62
+ }
63
+ Qcur = ggml_rope_ext(
64
+ ctx0, Qcur, inp_pos, rope_factors,
65
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
66
+ ext_factor, attn_factor, beta_fast, beta_slow
67
+ );
68
+
69
+ Kcur = ggml_rope_ext(
70
+ ctx0, Kcur, inp_pos, rope_factors,
71
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
72
+ ext_factor, attn_factor, beta_fast, beta_slow
73
+ );
74
+
75
+ cb(Qcur, "Qcur", il);
76
+ cb(Kcur, "Kcur", il);
77
+ cb(Vcur, "Vcur", il);
78
+
79
+ Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
80
+ cb(Qcur, "Qcur", il);
81
+
82
+ cur = build_attn(inp_attn,
83
+ model.layers[il].wo, model.layers[il].bo,
84
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
85
+ }
86
+ if (il == n_layer - 1 && inp_out_ids) {
87
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
88
+ residual = ggml_get_rows(ctx0, residual, inp_out_ids);
89
+ }
90
+ cur = ggml_add(ctx0, cur, residual);
91
+ residual = cur;
92
+
93
+ cur = build_norm(cur,
94
+ model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
95
+ LLM_NORM_RMS, il);
96
+ cb(cur, "ffn_norm", il);
97
+
98
+ // feed-forward network
99
+ if (model.layers[il].ffn_gate_inp == nullptr) {
100
+ cur = build_ffn(cur,
101
+ model.layers[il].ffn_up, NULL, NULL,
102
+ NULL, NULL, NULL,
103
+ model.layers[il].ffn_down, NULL, NULL,
104
+ NULL,
105
+ LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
106
+ cb(cur, "ffn_out", il);
107
+ } else {
108
+ // MoE branch
109
+ cur = build_moe_ffn(cur,
110
+ model.layers[il].ffn_gate_inp,
111
+ model.layers[il].ffn_up_exps,
112
+ model.layers[il].ffn_gate_exps,
113
+ model.layers[il].ffn_down_exps,
114
+ nullptr,
115
+ n_expert, n_expert_used,
116
+ LLM_FFN_SILU, true,
117
+ false, 0.0,
118
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
119
+ il);
120
+ cb(cur, "ffn_moe_out", il);
121
+ }
122
+ cur = ggml_add(ctx0, residual, cur);
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 = build_norm(inpL,
131
+ model.output_norm,
132
+ model.output_norm_b,
133
+ LLM_NORM_RMS, -1);
134
+
135
+ cb(cur, "result_norm", -1);
136
+ res->t_embd = cur;
137
+
138
+ cur = build_lora_mm(model.output, cur);
139
+
140
+ if (model.output_b != nullptr) {
141
+ cb(cur, "result_output_no_bias", -1);
142
+ cur = ggml_add(ctx0, cur, model.output_b);
143
+ }
144
+ cb(cur, "result_output", -1);
145
+ res->t_logits = cur;
146
+
147
+ ggml_build_forward_expand(gf, cur);
148
+ }
149
+
150
+ // Explicit template instantiations
151
+ template struct llm_build_phi3<false>;
152
+ template struct llm_build_phi3<true>;
@@ -0,0 +1,110 @@
1
+ #include "models.h"
2
+
3
+ llm_build_plamo::llm_build_plamo(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
+ // norm
23
+ cur = build_norm(inpL,
24
+ model.layers[il].attn_norm, NULL,
25
+ LLM_NORM_RMS, il);
26
+ cb(cur, "attn_norm", il);
27
+
28
+ ggml_tensor * sa_inp = cur;
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
+
36
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
37
+ cb(Kcur, "Kcur", il);
38
+
39
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
40
+ cb(Vcur, "Vcur", il);
41
+
42
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
43
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
44
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
45
+
46
+ Qcur = ggml_rope_ext(
47
+ ctx0, Qcur, inp_pos, nullptr,
48
+ n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
49
+ ext_factor, attn_factor, beta_fast, beta_slow
50
+ );
51
+
52
+ Kcur = ggml_rope_ext(
53
+ ctx0, Kcur, inp_pos, nullptr,
54
+ n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
55
+ ext_factor, attn_factor, beta_fast, beta_slow
56
+ );
57
+
58
+ cb(Qcur, "Qcur", il);
59
+ cb(Kcur, "Kcur", il);
60
+ cb(Vcur, "Vcur", il);
61
+
62
+ cur = build_attn(inp_attn,
63
+ model.layers[il].wo, NULL,
64
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
65
+ }
66
+ if (il == n_layer - 1 && inp_out_ids) {
67
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
68
+ sa_inp = ggml_get_rows(ctx0, sa_inp, inp_out_ids);
69
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
70
+ }
71
+ ggml_tensor * sa_out = cur;
72
+
73
+ cur = sa_inp;
74
+
75
+ // feed-forward network
76
+ {
77
+ cur = build_ffn(cur,
78
+ model.layers[il].ffn_up, NULL, NULL,
79
+ model.layers[il].ffn_gate, NULL, NULL,
80
+ model.layers[il].ffn_down, NULL, NULL,
81
+ NULL,
82
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
83
+ cb(cur, "ffn_out", il);
84
+ }
85
+ cur = ggml_add(ctx0, cur, sa_out);
86
+ cur = ggml_add(ctx0, cur, inpL);
87
+
88
+ cur = build_cvec(cur, il);
89
+ cb(cur, "l_out", il);
90
+
91
+ // input for next layer
92
+ inpL = cur;
93
+ }
94
+ cur = inpL;
95
+
96
+ cur = build_norm(cur,
97
+ model.output_norm, NULL,
98
+ LLM_NORM_RMS, -1);
99
+
100
+ cb(cur, "result_norm", -1);
101
+ res->t_embd = cur;
102
+
103
+ // lm_head
104
+ cur = build_lora_mm(model.output, cur);
105
+
106
+ cb(cur, "result_output", -1);
107
+ res->t_logits = cur;
108
+
109
+ ggml_build_forward_expand(gf, cur);
110
+ }