@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.
- package/package.json +14 -14
- package/scripts/llama.cpp.patch +8 -8
- package/src/llama.cpp/common/CMakeLists.txt +2 -0
- package/src/llama.cpp/common/arg.cpp +44 -999
- package/src/llama.cpp/common/arg.h +2 -2
- package/src/llama.cpp/common/chat.cpp +17 -2
- package/src/llama.cpp/common/common.cpp +33 -0
- package/src/llama.cpp/common/common.h +15 -1
- package/src/llama.cpp/common/download.cpp +1054 -0
- package/src/llama.cpp/common/download.h +55 -0
- package/src/llama.cpp/ggml/CMakeLists.txt +1 -1
- package/src/llama.cpp/ggml/include/ggml.h +2 -0
- package/src/llama.cpp/ggml/src/CMakeLists.txt +6 -3
- package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +29 -11
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/arm/quants.c +428 -26
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +4 -5
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +108 -49
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp +50 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +3 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +21 -21
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +172 -75
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +0 -4
- package/src/llama.cpp/ggml/src/ggml-cpu/repack.cpp +82 -21
- package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +25 -25
- package/src/llama.cpp/include/llama.h +7 -3
- package/src/llama.cpp/src/CMakeLists.txt +95 -0
- package/src/llama.cpp/src/llama-arch.cpp +108 -0
- package/src/llama.cpp/src/llama-arch.h +11 -0
- package/src/llama.cpp/src/llama-batch.cpp +63 -31
- package/src/llama.cpp/src/llama-batch.h +12 -1
- package/src/llama.cpp/src/llama-chat.cpp +32 -0
- package/src/llama.cpp/src/llama-chat.h +1 -0
- package/src/llama.cpp/src/llama-context.cpp +36 -13
- package/src/llama.cpp/src/llama-context.h +5 -5
- package/src/llama.cpp/src/llama-cparams.h +1 -0
- package/src/llama.cpp/src/llama-graph.cpp +3 -3
- package/src/llama.cpp/src/llama-hparams.cpp +11 -1
- package/src/llama.cpp/src/llama-hparams.h +6 -0
- package/src/llama.cpp/src/llama-kv-cache-iswa.cpp +3 -1
- package/src/llama.cpp/src/llama-kv-cache.cpp +33 -1
- package/src/llama.cpp/src/llama-kv-cells.h +44 -2
- package/src/llama.cpp/src/llama-memory-recurrent.cpp +4 -3
- package/src/llama.cpp/src/llama-model.cpp +320 -13171
- package/src/llama.cpp/src/llama-model.h +8 -0
- package/src/llama.cpp/src/llama-quant.cpp +1 -1
- package/src/llama.cpp/src/llama-vocab.cpp +5 -0
- package/src/llama.cpp/src/llama-vocab.h +1 -0
- package/src/llama.cpp/src/models/apertus.cpp +125 -0
- package/src/llama.cpp/src/models/arcee.cpp +135 -0
- package/src/llama.cpp/src/models/arctic.cpp +138 -0
- package/src/llama.cpp/src/models/arwkv7.cpp +86 -0
- package/src/llama.cpp/src/models/baichuan.cpp +122 -0
- package/src/llama.cpp/src/models/bailingmoe.cpp +144 -0
- package/src/llama.cpp/src/models/bailingmoe2.cpp +135 -0
- package/src/llama.cpp/src/models/bert.cpp +176 -0
- package/src/llama.cpp/src/models/bitnet.cpp +160 -0
- package/src/llama.cpp/src/models/bloom.cpp +101 -0
- package/src/llama.cpp/src/models/chameleon.cpp +178 -0
- package/src/llama.cpp/src/models/chatglm.cpp +132 -0
- package/src/llama.cpp/src/models/codeshell.cpp +111 -0
- package/src/llama.cpp/src/models/cogvlm.cpp +100 -0
- package/src/llama.cpp/src/models/cohere2-iswa.cpp +131 -0
- package/src/llama.cpp/src/models/command-r.cpp +122 -0
- package/src/llama.cpp/src/models/dbrx.cpp +123 -0
- package/src/llama.cpp/src/models/deci.cpp +135 -0
- package/src/llama.cpp/src/models/deepseek.cpp +144 -0
- package/src/llama.cpp/src/models/deepseek2.cpp +236 -0
- package/src/llama.cpp/src/models/dots1.cpp +134 -0
- package/src/llama.cpp/src/models/dream.cpp +105 -0
- package/src/llama.cpp/src/models/ernie4-5-moe.cpp +150 -0
- package/src/llama.cpp/src/models/ernie4-5.cpp +110 -0
- package/src/llama.cpp/src/models/exaone.cpp +114 -0
- package/src/llama.cpp/src/models/exaone4.cpp +123 -0
- package/src/llama.cpp/src/models/falcon-h1.cpp +113 -0
- package/src/llama.cpp/src/models/falcon.cpp +120 -0
- package/src/llama.cpp/src/models/gemma-embedding.cpp +120 -0
- package/src/llama.cpp/src/models/gemma.cpp +112 -0
- package/src/llama.cpp/src/models/gemma2-iswa.cpp +125 -0
- package/src/llama.cpp/src/models/gemma3-iswa.cpp +131 -0
- package/src/llama.cpp/src/models/gemma3n-iswa.cpp +377 -0
- package/src/llama.cpp/src/models/glm4-moe.cpp +153 -0
- package/src/llama.cpp/src/models/glm4.cpp +127 -0
- package/src/llama.cpp/src/models/gpt2.cpp +105 -0
- package/src/llama.cpp/src/models/gptneox.cpp +144 -0
- package/src/llama.cpp/src/models/granite-hybrid.cpp +196 -0
- package/src/llama.cpp/src/models/granite.cpp +211 -0
- package/src/llama.cpp/src/models/graph-context-mamba.cpp +283 -0
- package/src/llama.cpp/src/models/grok.cpp +159 -0
- package/src/llama.cpp/src/models/grovemoe.cpp +141 -0
- package/src/llama.cpp/src/models/hunyuan-dense.cpp +132 -0
- package/src/llama.cpp/src/models/hunyuan-moe.cpp +154 -0
- package/src/llama.cpp/src/models/internlm2.cpp +120 -0
- package/src/llama.cpp/src/models/jais.cpp +86 -0
- package/src/llama.cpp/src/models/jamba.cpp +106 -0
- package/src/llama.cpp/src/models/lfm2.cpp +173 -0
- package/src/llama.cpp/src/models/llada-moe.cpp +122 -0
- package/src/llama.cpp/src/models/llada.cpp +99 -0
- package/src/llama.cpp/src/models/llama-iswa.cpp +174 -0
- package/src/llama.cpp/src/models/llama.cpp +155 -0
- package/src/llama.cpp/src/models/mamba.cpp +55 -0
- package/src/llama.cpp/src/models/minicpm3.cpp +199 -0
- package/src/llama.cpp/src/models/minimax-m2.cpp +124 -0
- package/src/llama.cpp/src/models/models.h +481 -0
- package/src/llama.cpp/src/models/mpt.cpp +126 -0
- package/src/llama.cpp/src/models/nemotron-h.cpp +121 -0
- package/src/llama.cpp/src/models/nemotron.cpp +122 -0
- package/src/llama.cpp/src/models/neo-bert.cpp +104 -0
- package/src/llama.cpp/src/models/olmo.cpp +121 -0
- package/src/llama.cpp/src/models/olmo2.cpp +150 -0
- package/src/llama.cpp/src/models/olmoe.cpp +124 -0
- package/src/llama.cpp/src/models/openai-moe-iswa.cpp +124 -0
- package/src/llama.cpp/src/models/openelm.cpp +124 -0
- package/src/llama.cpp/src/models/orion.cpp +123 -0
- package/src/llama.cpp/src/models/pangu-embedded.cpp +121 -0
- package/src/llama.cpp/src/models/phi2.cpp +121 -0
- package/src/llama.cpp/src/models/phi3.cpp +152 -0
- package/src/llama.cpp/src/models/plamo.cpp +110 -0
- package/src/llama.cpp/src/models/plamo2.cpp +316 -0
- package/src/llama.cpp/src/models/plm.cpp +168 -0
- package/src/llama.cpp/src/models/qwen.cpp +108 -0
- package/src/llama.cpp/src/models/qwen2.cpp +117 -0
- package/src/llama.cpp/src/models/qwen2moe.cpp +151 -0
- package/src/llama.cpp/src/models/qwen2vl.cpp +117 -0
- package/src/llama.cpp/src/models/qwen3.cpp +117 -0
- package/src/llama.cpp/src/models/qwen3moe.cpp +124 -0
- package/src/llama.cpp/src/models/qwen3vl-moe.cpp +149 -0
- package/src/llama.cpp/src/models/qwen3vl.cpp +141 -0
- package/src/llama.cpp/src/models/refact.cpp +94 -0
- package/src/llama.cpp/src/models/rwkv6-base.cpp +162 -0
- package/src/llama.cpp/src/models/rwkv6.cpp +94 -0
- package/src/llama.cpp/src/models/rwkv6qwen2.cpp +86 -0
- package/src/llama.cpp/src/models/rwkv7-base.cpp +135 -0
- package/src/llama.cpp/src/models/rwkv7.cpp +90 -0
- package/src/llama.cpp/src/models/seed-oss.cpp +124 -0
- package/src/llama.cpp/src/models/smallthinker.cpp +120 -0
- package/src/llama.cpp/src/models/smollm3.cpp +128 -0
- package/src/llama.cpp/src/models/stablelm.cpp +146 -0
- package/src/llama.cpp/src/models/starcoder.cpp +100 -0
- package/src/llama.cpp/src/models/starcoder2.cpp +121 -0
- package/src/llama.cpp/src/models/t5-dec.cpp +166 -0
- package/src/llama.cpp/src/models/t5-enc.cpp +96 -0
- package/src/llama.cpp/src/models/wavtokenizer-dec.cpp +149 -0
- package/src/llama.cpp/src/models/xverse.cpp +108 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
llm_build_cohere2_iswa::llm_build_cohere2_iswa(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
|
+
|
|
8
|
+
const float f_logit_scale = hparams.f_logit_scale;
|
|
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_iswa();
|
|
19
|
+
|
|
20
|
+
ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
21
|
+
|
|
22
|
+
for (int il = 0; il < n_layer; ++il) {
|
|
23
|
+
const bool is_swa = hparams.is_swa(il);
|
|
24
|
+
|
|
25
|
+
// norm
|
|
26
|
+
cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
|
|
27
|
+
cb(cur, "attn_norm", il);
|
|
28
|
+
ggml_tensor * ffn_inp = cur;
|
|
29
|
+
|
|
30
|
+
// self-attention
|
|
31
|
+
{
|
|
32
|
+
// rope freq factors for 128k context
|
|
33
|
+
ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
|
|
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
|
+
if (model.layers[il].bq) {
|
|
39
|
+
Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
40
|
+
cb(Qcur, "Qcur", il);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
44
|
+
cb(Kcur, "Kcur", il);
|
|
45
|
+
if (model.layers[il].bk) {
|
|
46
|
+
Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
47
|
+
cb(Kcur, "Kcur", il);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
51
|
+
cb(Vcur, "Vcur", il);
|
|
52
|
+
if (model.layers[il].bv) {
|
|
53
|
+
Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
54
|
+
cb(Vcur, "Vcur", il);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
58
|
+
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
59
|
+
Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
|
|
60
|
+
|
|
61
|
+
if (is_swa) {
|
|
62
|
+
Qcur = ggml_rope_ext(
|
|
63
|
+
ctx0, Qcur, inp_pos, rope_factors,
|
|
64
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
65
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
Kcur = ggml_rope_ext(
|
|
69
|
+
ctx0, Kcur, inp_pos, rope_factors,
|
|
70
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
71
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
cb(Qcur, "Qcur", il);
|
|
76
|
+
cb(Kcur, "Kcur", il);
|
|
77
|
+
cb(Vcur, "Vcur", il);
|
|
78
|
+
|
|
79
|
+
cur = build_attn(inp_attn,
|
|
80
|
+
model.layers[il].wo, model.layers[il].bo,
|
|
81
|
+
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
85
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
86
|
+
inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
87
|
+
ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
ggml_tensor * attn_out = cur;
|
|
91
|
+
|
|
92
|
+
// feed-forward network
|
|
93
|
+
{
|
|
94
|
+
cur = build_ffn(ffn_inp,
|
|
95
|
+
model.layers[il].ffn_up, NULL, NULL,
|
|
96
|
+
model.layers[il].ffn_gate, NULL, NULL,
|
|
97
|
+
model.layers[il].ffn_down, NULL, NULL,
|
|
98
|
+
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
99
|
+
cb(cur, "ffn_out", il);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// add together residual + FFN + self-attention
|
|
103
|
+
cur = ggml_add(ctx0, cur, inpL);
|
|
104
|
+
cur = ggml_add(ctx0, cur, attn_out);
|
|
105
|
+
|
|
106
|
+
cur = build_cvec(cur, il);
|
|
107
|
+
cb(cur, "l_out", il);
|
|
108
|
+
|
|
109
|
+
// input for next layer
|
|
110
|
+
inpL = cur;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
cur = inpL;
|
|
114
|
+
|
|
115
|
+
cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1);
|
|
116
|
+
|
|
117
|
+
cb(cur, "result_norm", -1);
|
|
118
|
+
res->t_embd = cur;
|
|
119
|
+
|
|
120
|
+
// lm_head
|
|
121
|
+
cur = build_lora_mm(model.output, cur);
|
|
122
|
+
|
|
123
|
+
if (f_logit_scale) {
|
|
124
|
+
cur = ggml_scale(ctx0, cur, f_logit_scale);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
cb(cur, "result_output", -1);
|
|
128
|
+
res->t_logits = cur;
|
|
129
|
+
|
|
130
|
+
ggml_build_forward_expand(gf, cur);
|
|
131
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
llm_build_command_r::llm_build_command_r(const llama_model & model, const llm_graph_params & params) :
|
|
6
|
+
llm_graph_context(params) {
|
|
7
|
+
const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
8
|
+
|
|
9
|
+
GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
10
|
+
|
|
11
|
+
const float f_logit_scale = hparams.f_logit_scale;
|
|
12
|
+
|
|
13
|
+
ggml_tensor * cur;
|
|
14
|
+
ggml_tensor * inpL;
|
|
15
|
+
|
|
16
|
+
inpL = build_inp_embd(model.tok_embd);
|
|
17
|
+
|
|
18
|
+
// inp_pos - contains the positions
|
|
19
|
+
ggml_tensor * inp_pos = build_inp_pos();
|
|
20
|
+
|
|
21
|
+
auto * inp_attn = build_attn_inp_kv();
|
|
22
|
+
|
|
23
|
+
ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
24
|
+
|
|
25
|
+
for (int il = 0; il < n_layer; ++il) {
|
|
26
|
+
// norm
|
|
27
|
+
cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
|
|
28
|
+
cb(cur, "attn_norm", il);
|
|
29
|
+
|
|
30
|
+
ggml_tensor * ffn_inp = cur;
|
|
31
|
+
|
|
32
|
+
// self-attention
|
|
33
|
+
{
|
|
34
|
+
// compute Q and K and RoPE them
|
|
35
|
+
ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
36
|
+
cb(Qcur, "Qcur", il);
|
|
37
|
+
if (model.layers[il].bq) {
|
|
38
|
+
Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
39
|
+
cb(Qcur, "Qcur", il);
|
|
40
|
+
}
|
|
41
|
+
ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
42
|
+
cb(Kcur, "Kcur", il);
|
|
43
|
+
if (model.layers[il].bk) {
|
|
44
|
+
Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
45
|
+
cb(Kcur, "Kcur", il);
|
|
46
|
+
}
|
|
47
|
+
ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
48
|
+
cb(Vcur, "Vcur", il);
|
|
49
|
+
if (model.layers[il].bv) {
|
|
50
|
+
Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
51
|
+
cb(Vcur, "Vcur", 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
|
+
if (model.layers[il].attn_q_norm) {
|
|
58
|
+
Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM, il);
|
|
59
|
+
cb(Qcur, "Qcur", il);
|
|
60
|
+
}
|
|
61
|
+
Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
62
|
+
ext_factor, attn_factor, beta_fast, beta_slow);
|
|
63
|
+
|
|
64
|
+
if (model.layers[il].attn_k_norm) {
|
|
65
|
+
Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM, il);
|
|
66
|
+
cb(Kcur, "Kcur", il);
|
|
67
|
+
}
|
|
68
|
+
Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
69
|
+
ext_factor, attn_factor, beta_fast, beta_slow);
|
|
70
|
+
|
|
71
|
+
cb(Qcur, "Qcur", il);
|
|
72
|
+
cb(Kcur, "Kcur", il);
|
|
73
|
+
cb(Vcur, "Vcur", il);
|
|
74
|
+
|
|
75
|
+
cur = build_attn(inp_attn,
|
|
76
|
+
model.layers[il].wo, model.layers[il].bo,
|
|
77
|
+
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
|
|
78
|
+
}
|
|
79
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
80
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
81
|
+
inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
82
|
+
ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
|
|
83
|
+
}
|
|
84
|
+
ggml_tensor * attn_out = cur;
|
|
85
|
+
|
|
86
|
+
// feed-forward network
|
|
87
|
+
{
|
|
88
|
+
cur = build_ffn(ffn_inp,
|
|
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, LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
93
|
+
cb(cur, "ffn_out", il);
|
|
94
|
+
}
|
|
95
|
+
// add together residual + FFN + self-attention
|
|
96
|
+
cur = ggml_add(ctx0, cur, inpL);
|
|
97
|
+
cur = ggml_add(ctx0, cur, attn_out);
|
|
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, model.output_norm, NULL, LLM_NORM, -1);
|
|
108
|
+
|
|
109
|
+
cb(cur, "result_norm", -1);
|
|
110
|
+
res->t_embd = cur;
|
|
111
|
+
|
|
112
|
+
// lm_head
|
|
113
|
+
cur = build_lora_mm(model.output, cur);
|
|
114
|
+
|
|
115
|
+
if (f_logit_scale) {
|
|
116
|
+
cur = ggml_scale(ctx0, cur, f_logit_scale);
|
|
117
|
+
}
|
|
118
|
+
cb(cur, "result_output", -1);
|
|
119
|
+
res->t_logits = cur;
|
|
120
|
+
|
|
121
|
+
ggml_build_forward_expand(gf, cur);
|
|
122
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
llm_build_dbrx::llm_build_dbrx(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
|
+
GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
10
|
+
|
|
11
|
+
ggml_tensor * cur;
|
|
12
|
+
ggml_tensor * inpL;
|
|
13
|
+
|
|
14
|
+
inpL = build_inp_embd(model.tok_embd);
|
|
15
|
+
|
|
16
|
+
// inp_pos - contains the positions
|
|
17
|
+
ggml_tensor * inp_pos = build_inp_pos();
|
|
18
|
+
|
|
19
|
+
auto * inp_attn = build_attn_inp_kv();
|
|
20
|
+
|
|
21
|
+
ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
22
|
+
|
|
23
|
+
for (int il = 0; il < n_layer; ++il) {
|
|
24
|
+
ggml_tensor * inpSA = inpL;
|
|
25
|
+
|
|
26
|
+
// norm
|
|
27
|
+
cur = build_norm(inpL,
|
|
28
|
+
model.layers[il].attn_norm, NULL,
|
|
29
|
+
LLM_NORM, il);
|
|
30
|
+
cb(cur, "attn_norm", il);
|
|
31
|
+
|
|
32
|
+
// self-attention
|
|
33
|
+
{
|
|
34
|
+
ggml_tensor * Qcur = nullptr;
|
|
35
|
+
ggml_tensor * Kcur = nullptr;
|
|
36
|
+
ggml_tensor * Vcur = nullptr;
|
|
37
|
+
|
|
38
|
+
cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
39
|
+
cb(cur, "wqkv", il);
|
|
40
|
+
|
|
41
|
+
cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
|
|
42
|
+
cb(cur, "wqkv_clamped", 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
|
+
|
|
48
|
+
Qcur = ggml_rope_ext(
|
|
49
|
+
ctx0, Qcur, inp_pos, nullptr,
|
|
50
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
51
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
Kcur = ggml_rope_ext(
|
|
55
|
+
ctx0, Kcur, inp_pos, nullptr,
|
|
56
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
57
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
cb(Qcur, "Qcur", il);
|
|
61
|
+
cb(Kcur, "Kcur", il);
|
|
62
|
+
cb(Vcur, "Vcur", il);
|
|
63
|
+
|
|
64
|
+
cur = build_attn(inp_attn,
|
|
65
|
+
model.layers[il].wo, NULL,
|
|
66
|
+
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
70
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
71
|
+
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
75
|
+
cb(ffn_inp, "ffn_inp", il);
|
|
76
|
+
|
|
77
|
+
// feed-forward network
|
|
78
|
+
// MoE branch
|
|
79
|
+
cur = build_norm(ffn_inp,
|
|
80
|
+
model.layers[il].attn_out_norm, NULL,
|
|
81
|
+
LLM_NORM, il);
|
|
82
|
+
cb(cur, "attn_out_norm", il);
|
|
83
|
+
|
|
84
|
+
cur = build_moe_ffn(cur,
|
|
85
|
+
model.layers[il].ffn_gate_inp,
|
|
86
|
+
model.layers[il].ffn_up_exps,
|
|
87
|
+
model.layers[il].ffn_gate_exps,
|
|
88
|
+
model.layers[il].ffn_down_exps,
|
|
89
|
+
nullptr,
|
|
90
|
+
n_expert, n_expert_used,
|
|
91
|
+
LLM_FFN_SILU, true,
|
|
92
|
+
false, 0.0,
|
|
93
|
+
LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
94
|
+
il);
|
|
95
|
+
cb(cur, "ffn_moe_out", 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
|
+
|
|
107
|
+
cur = inpL;
|
|
108
|
+
|
|
109
|
+
cur = build_norm(cur,
|
|
110
|
+
model.output_norm, NULL,
|
|
111
|
+
LLM_NORM, -1);
|
|
112
|
+
|
|
113
|
+
cb(cur, "result_norm", -1);
|
|
114
|
+
res->t_embd = cur;
|
|
115
|
+
|
|
116
|
+
// lm_head
|
|
117
|
+
cur = build_lora_mm(model.output, cur);
|
|
118
|
+
|
|
119
|
+
cb(cur, "result_output", -1);
|
|
120
|
+
res->t_logits = cur;
|
|
121
|
+
|
|
122
|
+
ggml_build_forward_expand(gf, cur);
|
|
123
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
llm_build_deci::llm_build_deci(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
|
|
6
|
+
const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
7
|
+
|
|
8
|
+
GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
9
|
+
GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
10
|
+
|
|
11
|
+
ggml_tensor * cur;
|
|
12
|
+
ggml_tensor * inpL;
|
|
13
|
+
|
|
14
|
+
inpL = build_inp_embd(model.tok_embd);
|
|
15
|
+
|
|
16
|
+
// inp_pos - contains the positions
|
|
17
|
+
ggml_tensor * inp_pos = build_inp_pos();
|
|
18
|
+
|
|
19
|
+
auto * inp_attn = build_attn_inp_kv();
|
|
20
|
+
|
|
21
|
+
const float kq_scale =
|
|
22
|
+
hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
|
|
23
|
+
|
|
24
|
+
ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
25
|
+
|
|
26
|
+
for (int il = 0; il < n_layer; ++il) {
|
|
27
|
+
ggml_tensor * inpSA = inpL;
|
|
28
|
+
const int64_t n_head_kv = hparams.n_head_kv(il);
|
|
29
|
+
const int64_t n_head = hparams.n_head(il);
|
|
30
|
+
const int64_t n_ff = hparams.n_ff(il);
|
|
31
|
+
|
|
32
|
+
if (n_head == 0) {
|
|
33
|
+
// attention-free layer of Llama-3_1-Nemotron-51B
|
|
34
|
+
cur = inpL;
|
|
35
|
+
} else {
|
|
36
|
+
// norm
|
|
37
|
+
cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
|
|
38
|
+
cb(cur, "attn_norm", il);
|
|
39
|
+
}
|
|
40
|
+
if (n_head > 0 && n_head_kv == 0) {
|
|
41
|
+
// "linear attention" of Llama-3_1-Nemotron-51B
|
|
42
|
+
cur = build_lora_mm(model.layers[il].wo, cur);
|
|
43
|
+
cb(cur, "wo", il);
|
|
44
|
+
} else if (n_head > 0) {
|
|
45
|
+
// self-attention
|
|
46
|
+
// rope freq factors for llama3; may return nullptr for llama2 and other models
|
|
47
|
+
ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
|
|
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
|
+
if (model.layers[il].bq) {
|
|
53
|
+
Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
54
|
+
cb(Qcur, "Qcur", il);
|
|
55
|
+
}
|
|
56
|
+
ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
57
|
+
cb(Kcur, "Kcur", il);
|
|
58
|
+
if (model.layers[il].bk) {
|
|
59
|
+
Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
60
|
+
cb(Kcur, "Kcur", il);
|
|
61
|
+
}
|
|
62
|
+
ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
63
|
+
cb(Vcur, "Vcur", il);
|
|
64
|
+
if (model.layers[il].bv) {
|
|
65
|
+
Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
66
|
+
cb(Vcur, "Vcur", il);
|
|
67
|
+
}
|
|
68
|
+
Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
69
|
+
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
70
|
+
Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
|
|
71
|
+
|
|
72
|
+
Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
73
|
+
ext_factor, attn_factor, beta_fast, beta_slow);
|
|
74
|
+
|
|
75
|
+
Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
76
|
+
ext_factor, attn_factor, beta_fast, beta_slow);
|
|
77
|
+
|
|
78
|
+
cb(Qcur, "Qcur", il);
|
|
79
|
+
cb(Kcur, "Kcur", il);
|
|
80
|
+
cb(Vcur, "Vcur", il);
|
|
81
|
+
|
|
82
|
+
cur = build_attn(inp_attn,
|
|
83
|
+
model.layers[il].wo, model.layers[il].bo,
|
|
84
|
+
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
|
|
85
|
+
}
|
|
86
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
87
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
88
|
+
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
89
|
+
}
|
|
90
|
+
// FFN-free layer of Llama-3_1-Nemotron-Ultra-253B
|
|
91
|
+
if (n_ff == 0) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
// modified to support attention-free layer of Llama-3_1-Nemotron-51B
|
|
95
|
+
ggml_tensor * ffn_inp = cur;
|
|
96
|
+
if (n_head > 0) {
|
|
97
|
+
ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
98
|
+
cb(ffn_inp, "ffn_inp", il);
|
|
99
|
+
}
|
|
100
|
+
// feed-forward network
|
|
101
|
+
if (model.layers[il].ffn_gate_inp == nullptr) {
|
|
102
|
+
cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
|
|
103
|
+
cb(cur, "ffn_norm", il);
|
|
104
|
+
|
|
105
|
+
cur = build_ffn(cur,
|
|
106
|
+
model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
107
|
+
model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
|
|
108
|
+
model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
109
|
+
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
110
|
+
cb(cur, "ffn_out", il);
|
|
111
|
+
}
|
|
112
|
+
cur = ggml_add(ctx0, cur, ffn_inp);
|
|
113
|
+
cb(cur, "ffn_out", il);
|
|
114
|
+
|
|
115
|
+
cur = build_cvec(cur, il);
|
|
116
|
+
cb(cur, "l_out", il);
|
|
117
|
+
|
|
118
|
+
// input for next layer
|
|
119
|
+
inpL = cur;
|
|
120
|
+
}
|
|
121
|
+
cur = inpL;
|
|
122
|
+
|
|
123
|
+
cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
|
|
124
|
+
|
|
125
|
+
cb(cur, "result_norm", -1);
|
|
126
|
+
res->t_embd = cur;
|
|
127
|
+
|
|
128
|
+
// lm_head
|
|
129
|
+
cur = build_lora_mm(model.output, cur);
|
|
130
|
+
|
|
131
|
+
cb(cur, "result_output", -1);
|
|
132
|
+
res->t_logits = cur;
|
|
133
|
+
|
|
134
|
+
ggml_build_forward_expand(gf, cur);
|
|
135
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
llm_build_deepseek::llm_build_deepseek(const llama_model & model, const llm_graph_params & params) :
|
|
6
|
+
llm_graph_context(params) {
|
|
7
|
+
const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
8
|
+
|
|
9
|
+
GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
10
|
+
GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
11
|
+
|
|
12
|
+
ggml_tensor * cur;
|
|
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
|
+
const float kq_scale =
|
|
23
|
+
hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
|
|
24
|
+
|
|
25
|
+
ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
26
|
+
|
|
27
|
+
for (int il = 0; il < n_layer; ++il) {
|
|
28
|
+
ggml_tensor * inpSA = inpL;
|
|
29
|
+
|
|
30
|
+
// norm
|
|
31
|
+
cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
|
|
32
|
+
cb(cur, "attn_norm", il);
|
|
33
|
+
|
|
34
|
+
// self-attention
|
|
35
|
+
{
|
|
36
|
+
// rope freq factors for llama3; may return nullptr for llama2 and other models
|
|
37
|
+
ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
|
|
38
|
+
|
|
39
|
+
// compute Q and K and RoPE them
|
|
40
|
+
ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
41
|
+
cb(Qcur, "Qcur", il);
|
|
42
|
+
if (model.layers[il].bq) {
|
|
43
|
+
Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
44
|
+
cb(Qcur, "Qcur", il);
|
|
45
|
+
}
|
|
46
|
+
ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
47
|
+
cb(Kcur, "Kcur", il);
|
|
48
|
+
if (model.layers[il].bk) {
|
|
49
|
+
Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
50
|
+
cb(Kcur, "Kcur", il);
|
|
51
|
+
}
|
|
52
|
+
ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
53
|
+
cb(Vcur, "Vcur", il);
|
|
54
|
+
if (model.layers[il].bv) {
|
|
55
|
+
Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
56
|
+
cb(Vcur, "Vcur", il);
|
|
57
|
+
}
|
|
58
|
+
Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
59
|
+
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
60
|
+
Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
|
|
61
|
+
|
|
62
|
+
Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
63
|
+
ext_factor, attn_factor, beta_fast, beta_slow);
|
|
64
|
+
|
|
65
|
+
Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
66
|
+
ext_factor, attn_factor, beta_fast, beta_slow);
|
|
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, kq_scale, 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
|
+
cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
|
|
84
|
+
cb(cur, "ffn_norm", il);
|
|
85
|
+
|
|
86
|
+
if ((uint32_t) il < hparams.n_layer_dense_lead) {
|
|
87
|
+
cur = build_ffn(cur,
|
|
88
|
+
model.layers[il].ffn_up, NULL, NULL,
|
|
89
|
+
model.layers[il].ffn_gate, NULL, NULL,
|
|
90
|
+
model.layers[il].ffn_down, NULL, NULL,
|
|
91
|
+
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
92
|
+
cb(cur, "ffn_out", il);
|
|
93
|
+
} else {
|
|
94
|
+
// MoE branch
|
|
95
|
+
ggml_tensor * moe_out = build_moe_ffn(cur,
|
|
96
|
+
model.layers[il].ffn_gate_inp,
|
|
97
|
+
model.layers[il].ffn_up_exps,
|
|
98
|
+
model.layers[il].ffn_gate_exps,
|
|
99
|
+
model.layers[il].ffn_down_exps,
|
|
100
|
+
nullptr,
|
|
101
|
+
n_expert, n_expert_used,
|
|
102
|
+
LLM_FFN_SILU, false,
|
|
103
|
+
false, hparams.expert_weights_scale,
|
|
104
|
+
LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
105
|
+
il);
|
|
106
|
+
cb(moe_out, "ffn_moe_out", il);
|
|
107
|
+
|
|
108
|
+
// FFN shared expert
|
|
109
|
+
{
|
|
110
|
+
ggml_tensor * ffn_shexp =
|
|
111
|
+
build_ffn(cur,
|
|
112
|
+
model.layers[il].ffn_up_shexp, NULL, NULL,
|
|
113
|
+
model.layers[il].ffn_gate_shexp, NULL, NULL,
|
|
114
|
+
model.layers[il].ffn_down_shexp, NULL, NULL,
|
|
115
|
+
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
116
|
+
cb(ffn_shexp, "ffn_shexp", il);
|
|
117
|
+
|
|
118
|
+
cur = ggml_add(ctx0, moe_out, ffn_shexp);
|
|
119
|
+
cb(cur, "ffn_out", il);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
cur = ggml_add(ctx0, cur, ffn_inp);
|
|
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, model.output_norm, NULL, LLM_NORM_RMS, -1);
|
|
133
|
+
|
|
134
|
+
cb(cur, "result_norm", -1);
|
|
135
|
+
res->t_embd = cur;
|
|
136
|
+
|
|
137
|
+
// lm_head
|
|
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
|
+
}
|