@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.
- package/CMakeLists.txt +12 -2
- package/package.json +14 -14
- package/scripts/llama.cpp.patch +8 -9
- package/src/llama.cpp/common/CMakeLists.txt +2 -0
- package/src/llama.cpp/common/arg.cpp +39 -1001
- package/src/llama.cpp/common/arg.h +2 -2
- package/src/llama.cpp/common/chat.cpp +216 -2
- package/src/llama.cpp/common/chat.h +1 -0
- package/src/llama.cpp/common/common.cpp +33 -0
- package/src/llama.cpp/common/common.h +13 -0
- package/src/llama.cpp/common/download.cpp +1054 -0
- package/src/llama.cpp/common/download.h +55 -0
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +19 -3
- package/src/llama.cpp/ggml/CMakeLists.txt +3 -1
- package/src/llama.cpp/ggml/include/ggml-hexagon.h +19 -0
- package/src/llama.cpp/ggml/include/ggml.h +2 -0
- package/src/llama.cpp/ggml/src/CMakeLists.txt +7 -3
- package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +10 -3
- 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 +0 -5
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +172 -35
- 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 +44 -16
- 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 +12 -7
- 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 +56 -21
- package/src/llama.cpp/src/llama-kv-cache.h +2 -4
- package/src/llama.cpp/src/llama-kv-cells.h +44 -2
- package/src/llama.cpp/src/llama-memory-recurrent.cpp +18 -14
- package/src/llama.cpp/src/llama-memory-recurrent.h +2 -2
- package/src/llama.cpp/src/llama-model.cpp +350 -13194
- package/src/llama.cpp/src/llama-model.h +9 -2
- 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 +111 -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 +123 -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,316 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
llm_build_plamo2::llm_build_plamo2(const llama_model & model, const llm_graph_params & params) :
|
|
4
|
+
llm_graph_context_mamba(params) {
|
|
5
|
+
ggml_tensor * cur;
|
|
6
|
+
ggml_tensor * inpL;
|
|
7
|
+
|
|
8
|
+
// {n_embd, n_tokens}
|
|
9
|
+
inpL = build_inp_embd(model.tok_embd);
|
|
10
|
+
cb(inpL, "embedding_output", -1);
|
|
11
|
+
|
|
12
|
+
ggml_tensor * inp_pos = build_inp_pos();
|
|
13
|
+
|
|
14
|
+
auto * inp_hybrid = build_inp_mem_hybrid();
|
|
15
|
+
|
|
16
|
+
ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
17
|
+
|
|
18
|
+
for (int il = 0; il < n_layer; ++il) {
|
|
19
|
+
ggml_tensor * residual = inpL;
|
|
20
|
+
|
|
21
|
+
// ggml_graph_add_node(gf, model.layers[il].attn_norm);
|
|
22
|
+
// cb(model.layers[il].attn_norm, "attn_norm", il);
|
|
23
|
+
|
|
24
|
+
// pre_mixer_norm
|
|
25
|
+
cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
|
|
26
|
+
|
|
27
|
+
// check if this layer is Mamba or Attention
|
|
28
|
+
bool is_mamba_layer = hparams.is_recurrent(il);
|
|
29
|
+
|
|
30
|
+
if (is_mamba_layer) {
|
|
31
|
+
// PLaMo-2 Mamba layer
|
|
32
|
+
cur = build_plamo2_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
|
|
33
|
+
} else {
|
|
34
|
+
// PLaMo-2 Attention layer
|
|
35
|
+
cur = build_plamo2_attn_layer(inp_hybrid->get_attn(), inp_pos, cur, model, il);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// post_mixer_norm
|
|
39
|
+
cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
|
|
40
|
+
cb(cur, "attn_post_norm", il);
|
|
41
|
+
|
|
42
|
+
// residual connection
|
|
43
|
+
cur = ggml_add(ctx0, cur, residual);
|
|
44
|
+
cb(cur, "attn_residual", il);
|
|
45
|
+
residual = cur;
|
|
46
|
+
|
|
47
|
+
// pre-ffn norm
|
|
48
|
+
cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
|
|
49
|
+
cb(cur, "ffn_pre_norm", il);
|
|
50
|
+
|
|
51
|
+
// feed-forward network
|
|
52
|
+
cur = build_ffn(cur,
|
|
53
|
+
model.layers[il].ffn_up, NULL, NULL,
|
|
54
|
+
NULL, NULL, NULL,
|
|
55
|
+
model.layers[il].ffn_down, NULL, NULL,
|
|
56
|
+
NULL, LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
|
|
57
|
+
cb(cur, "ffn_out", il);
|
|
58
|
+
|
|
59
|
+
// post ffn norm
|
|
60
|
+
cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);
|
|
61
|
+
cb(cur, "ffn_post_norm", il);
|
|
62
|
+
|
|
63
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
64
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
65
|
+
residual = ggml_get_rows(ctx0, residual, inp_out_ids);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// residual connection
|
|
69
|
+
cur = ggml_add(ctx0, cur, residual);
|
|
70
|
+
cb(cur, "ffn_residual", il);
|
|
71
|
+
|
|
72
|
+
inpL = cur;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
cur = inpL;
|
|
76
|
+
|
|
77
|
+
// final norm
|
|
78
|
+
cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
|
|
79
|
+
cb(cur, "result_norm", -1);
|
|
80
|
+
|
|
81
|
+
res->t_embd = cur;
|
|
82
|
+
|
|
83
|
+
// lm_head
|
|
84
|
+
cur = build_lora_mm(model.output, cur);
|
|
85
|
+
cb(cur, "result_output", -1);
|
|
86
|
+
|
|
87
|
+
// Explicitly mark as output tensor to ensure proper backend assignment
|
|
88
|
+
ggml_set_output(cur);
|
|
89
|
+
|
|
90
|
+
res->t_logits = cur;
|
|
91
|
+
|
|
92
|
+
ggml_build_forward_expand(gf, cur);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
ggml_tensor * llm_build_plamo2::build_plamo2_attn_layer(llm_graph_input_attn_kv * inp,
|
|
96
|
+
ggml_tensor * inp_pos,
|
|
97
|
+
ggml_tensor * cur,
|
|
98
|
+
const llama_model & model,
|
|
99
|
+
int il) {
|
|
100
|
+
// self-attention
|
|
101
|
+
{
|
|
102
|
+
// PLaMo-2 uses combined QKV tensor
|
|
103
|
+
ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur);
|
|
104
|
+
cb(qkv, "wqkv", il);
|
|
105
|
+
|
|
106
|
+
// split QKV tensor into Q, K, V
|
|
107
|
+
const int64_t n_embd_head_q = hparams.n_embd_head_k;
|
|
108
|
+
const int64_t n_embd_head_k = hparams.n_embd_head_k;
|
|
109
|
+
const int64_t n_embd_head_v = hparams.n_embd_head_v;
|
|
110
|
+
int32_t n_head = hparams.n_head(il);
|
|
111
|
+
int32_t n_head_kv = hparams.n_head_kv(il);
|
|
112
|
+
|
|
113
|
+
const int64_t q_offset = 0;
|
|
114
|
+
const int64_t k_offset = n_embd_head_q * n_head;
|
|
115
|
+
const int64_t v_offset = k_offset + n_embd_head_k * n_head_kv;
|
|
116
|
+
|
|
117
|
+
ggml_tensor * Qcur = ggml_view_3d(ctx0, qkv, n_embd_head_q, n_head, n_tokens, n_embd_head_q * sizeof(float),
|
|
118
|
+
qkv->nb[1], q_offset * ggml_element_size(qkv));
|
|
119
|
+
ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, n_embd_head_k, n_head_kv, n_tokens, n_embd_head_k * sizeof(float),
|
|
120
|
+
qkv->nb[1], k_offset * ggml_element_size(qkv));
|
|
121
|
+
ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, n_embd_head_v, n_head_kv, n_tokens, n_embd_head_v * sizeof(float),
|
|
122
|
+
qkv->nb[1], v_offset * ggml_element_size(qkv));
|
|
123
|
+
|
|
124
|
+
cb(Qcur, "Qcur", il);
|
|
125
|
+
cb(Kcur, "Kcur", il);
|
|
126
|
+
cb(Vcur, "Vcur", il);
|
|
127
|
+
|
|
128
|
+
Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
|
|
129
|
+
cb(Qcur, "Qcur_normed", il);
|
|
130
|
+
|
|
131
|
+
Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
132
|
+
ext_factor, attn_factor, beta_fast, beta_slow);
|
|
133
|
+
|
|
134
|
+
Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
|
|
135
|
+
cb(Kcur, "Kcur_normed", il);
|
|
136
|
+
|
|
137
|
+
Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
138
|
+
ext_factor, attn_factor, beta_fast, beta_slow);
|
|
139
|
+
|
|
140
|
+
cur = build_attn(inp,
|
|
141
|
+
model.layers[il].wo, NULL,
|
|
142
|
+
Qcur, Kcur, Vcur, NULL, NULL, NULL, 1.0f / sqrtf(float(n_embd_head_v)), il);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
cb(cur, "attn_out", il);
|
|
146
|
+
|
|
147
|
+
return cur;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
ggml_tensor * llm_build_plamo2::build_plamo2_mamba_layer(llm_graph_input_rs * inp,
|
|
151
|
+
ggml_tensor * cur,
|
|
152
|
+
const llama_model & model,
|
|
153
|
+
const llama_ubatch & ubatch,
|
|
154
|
+
int il) {
|
|
155
|
+
const auto * mctx_cur = inp->mctx;
|
|
156
|
+
|
|
157
|
+
const auto kv_head = mctx_cur->get_head();
|
|
158
|
+
|
|
159
|
+
const int64_t d_conv = hparams.ssm_d_conv;
|
|
160
|
+
const int64_t d_inner = hparams.ssm_d_inner;
|
|
161
|
+
const int64_t d_state = hparams.ssm_d_state;
|
|
162
|
+
const int64_t n_heads = hparams.ssm_dt_rank;
|
|
163
|
+
const int64_t head_dim = d_inner / n_heads;
|
|
164
|
+
const int64_t n_group = hparams.ssm_n_group;
|
|
165
|
+
const int64_t n_seqs = ubatch.n_seqs;
|
|
166
|
+
|
|
167
|
+
const int64_t n_seq_tokens = ubatch.n_seq_tokens;
|
|
168
|
+
|
|
169
|
+
GGML_ASSERT(n_seqs != 0);
|
|
170
|
+
GGML_ASSERT(ubatch.equal_seqs());
|
|
171
|
+
GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
|
|
172
|
+
|
|
173
|
+
ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
|
|
174
|
+
ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
|
|
175
|
+
|
|
176
|
+
ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
|
|
177
|
+
conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2 * n_group * d_state, n_seqs);
|
|
178
|
+
|
|
179
|
+
// {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
|
|
180
|
+
cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
|
|
181
|
+
|
|
182
|
+
// in_proj: {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
|
|
183
|
+
ggml_tensor * zx = build_lora_mm(model.layers[il].ssm_in, cur);
|
|
184
|
+
cb(zx, "mamba_in_proj", il);
|
|
185
|
+
// {8192, 5, 1, 1} -> {8192, 1, 5, 1}
|
|
186
|
+
zx = ggml_permute(ctx0, zx, 0, 2, 1, 3);
|
|
187
|
+
zx = ggml_cont_4d(ctx0, zx, head_dim * 2, n_heads, n_seq_tokens, n_seqs);
|
|
188
|
+
cb(zx, "mamba_in_proj_out", il);
|
|
189
|
+
|
|
190
|
+
// split into z and x
|
|
191
|
+
// => {head_dim * n_heads, n_seq_tokens, n_seqs}
|
|
192
|
+
ggml_tensor * x = ggml_view_4d(ctx0, zx, head_dim, n_heads, n_seq_tokens, n_seqs, zx->nb[1], zx->nb[2], zx->nb[3],
|
|
193
|
+
head_dim * ggml_element_size(zx));
|
|
194
|
+
x = ggml_cont_3d(ctx0, x, head_dim * n_heads, n_seq_tokens, n_seqs);
|
|
195
|
+
// x = ggml_permute(ctx0, x, 0, 2, 1, 3);
|
|
196
|
+
cb(x, "mamba_x_split", il);
|
|
197
|
+
|
|
198
|
+
ggml_tensor * z =
|
|
199
|
+
ggml_view_4d(ctx0, zx, head_dim, n_heads, n_seq_tokens, n_seqs, zx->nb[1], zx->nb[2], zx->nb[3], 0);
|
|
200
|
+
cb(z, "mamba_z_split", il);
|
|
201
|
+
|
|
202
|
+
// conv1d
|
|
203
|
+
{
|
|
204
|
+
// => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
|
|
205
|
+
ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
|
|
206
|
+
cb(conv_x, "mamba_conv1d_input", il);
|
|
207
|
+
|
|
208
|
+
// copy last (d_conv - 1) columns back into the state cache
|
|
209
|
+
ggml_tensor * last_conv = ggml_view_3d(ctx0, conv_x, d_conv - 1, d_inner, n_seqs, conv_x->nb[1], conv_x->nb[2],
|
|
210
|
+
n_seq_tokens * (conv_x->nb[0]));
|
|
211
|
+
|
|
212
|
+
ggml_build_forward_expand(gf, ggml_cpy(ctx0, last_conv,
|
|
213
|
+
ggml_view_1d(ctx0, conv_states_all,
|
|
214
|
+
(d_conv - 1) * (d_inner + 2 * n_group * d_state) * (n_seqs),
|
|
215
|
+
kv_head * (d_conv - 1) * (d_inner + 2 * n_group * d_state) *
|
|
216
|
+
ggml_element_size(conv_states_all))));
|
|
217
|
+
cb(conv_states_all, "mamba_conv1d_state", il);
|
|
218
|
+
|
|
219
|
+
// 1D convolution
|
|
220
|
+
x = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
|
|
221
|
+
cb(x, "mamba_conv1d", il);
|
|
222
|
+
|
|
223
|
+
x = ggml_silu(ctx0, x);
|
|
224
|
+
cb(x, "mamba_conv1d_silu", il);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// SSM
|
|
228
|
+
{
|
|
229
|
+
// bcdt_proj: {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs}
|
|
230
|
+
ggml_tensor * x_bcdt = build_lora_mm(model.layers[il].ssm_x, x);
|
|
231
|
+
cb(x_bcdt, "mamba_bcdt_proj", il);
|
|
232
|
+
|
|
233
|
+
// split into dt, B, C
|
|
234
|
+
const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16));
|
|
235
|
+
ggml_tensor * B = ggml_view_3d(ctx0, x_bcdt, d_state, n_seq_tokens, n_seqs, x_bcdt->nb[1], x_bcdt->nb[2], 0);
|
|
236
|
+
ggml_tensor * C = ggml_view_3d(ctx0, x_bcdt, d_state, n_seq_tokens, n_seqs, x_bcdt->nb[1], x_bcdt->nb[2],
|
|
237
|
+
ggml_element_size(x_bcdt) * d_state);
|
|
238
|
+
ggml_tensor * dt = ggml_view_3d(ctx0, x_bcdt, dt_dim, n_seq_tokens, n_seqs, x_bcdt->nb[1], x_bcdt->nb[2],
|
|
239
|
+
ggml_element_size(x_bcdt) * (2 * d_state));
|
|
240
|
+
cb(B, "mamba_B_raw", il);
|
|
241
|
+
cb(C, "mamba_C_raw", il);
|
|
242
|
+
cb(dt, "mamba_dt_raw", il);
|
|
243
|
+
|
|
244
|
+
// Apply RMS norm to dt, B, C (PLaMo-2 specific)
|
|
245
|
+
B = build_norm(B, model.layers[il].ssm_b_norm, NULL, LLM_NORM_RMS, il);
|
|
246
|
+
C = build_norm(C, model.layers[il].ssm_c_norm, NULL, LLM_NORM_RMS, il);
|
|
247
|
+
dt = build_norm(dt, model.layers[il].ssm_dt_norm, NULL, LLM_NORM_RMS, il);
|
|
248
|
+
cb(B, "mamba_B_normed", il);
|
|
249
|
+
cb(C, "mamba_C_normed", il);
|
|
250
|
+
cb(dt, "mamba_dt_normed", il);
|
|
251
|
+
|
|
252
|
+
// dt_proj: {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
|
|
253
|
+
dt = build_lora_mm(model.layers[il].ssm_dt, dt);
|
|
254
|
+
dt = ggml_add(ctx0, dt, model.layers[il].ssm_dt_b);
|
|
255
|
+
cb(dt, "mamba_dt_proj", il);
|
|
256
|
+
|
|
257
|
+
ggml_tensor * A = ggml_reshape_2d(ctx0, model.layers[il].ssm_a, 1, n_heads);
|
|
258
|
+
cb(A, "mamba_A", il);
|
|
259
|
+
|
|
260
|
+
x = ggml_view_4d(ctx0, x, head_dim, n_heads, n_seq_tokens, n_seqs, head_dim * ggml_element_size(x),
|
|
261
|
+
head_dim * n_heads * ggml_element_size(x),
|
|
262
|
+
head_dim * n_heads * n_seq_tokens * ggml_element_size(x), 0);
|
|
263
|
+
B = ggml_view_4d(ctx0, B, d_state, 1, n_seq_tokens, n_seqs, d_state * B->nb[0], B->nb[1], B->nb[2], 0);
|
|
264
|
+
C = ggml_view_4d(ctx0, C, d_state, 1, n_seq_tokens, n_seqs, d_state * C->nb[0], C->nb[1], C->nb[2], 0);
|
|
265
|
+
|
|
266
|
+
// use the states and the indices provided by build_recurrent_state
|
|
267
|
+
// (this is necessary in order to properly use the states before they are overwritten,
|
|
268
|
+
// while avoiding to make unnecessary copies of the states)
|
|
269
|
+
auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
|
|
270
|
+
ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_heads, mctx_cur->get_size());
|
|
271
|
+
|
|
272
|
+
// Custom operator to optimize the parallel associative scan
|
|
273
|
+
// as described in the Annex D of the Mamba paper.
|
|
274
|
+
// => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
|
|
275
|
+
return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
|
|
279
|
+
cb(y_ssm, "mamba_ssm_scan", il);
|
|
280
|
+
|
|
281
|
+
// store last states
|
|
282
|
+
ggml_build_forward_expand(
|
|
283
|
+
gf, ggml_cpy(
|
|
284
|
+
ctx0,
|
|
285
|
+
ggml_view_1d(ctx0, y_ssm, n_heads * head_dim * d_state * n_seqs,
|
|
286
|
+
n_heads * head_dim * n_seq_tokens * n_seqs * ggml_element_size(y_ssm)),
|
|
287
|
+
ggml_view_1d(ctx0, ssm_states_all, n_heads * head_dim * d_state * n_seqs,
|
|
288
|
+
kv_head * n_seqs * n_heads * head_dim * d_state * ggml_element_size(ssm_states_all))));
|
|
289
|
+
cb(ssm_states_all, "mamba_ssm_states", il);
|
|
290
|
+
|
|
291
|
+
ggml_tensor * y = ggml_view_4d(ctx0, y_ssm, head_dim, n_heads, n_seq_tokens, n_seqs,
|
|
292
|
+
head_dim * ggml_element_size(x), head_dim * n_heads * ggml_element_size(x),
|
|
293
|
+
head_dim * n_heads * n_seq_tokens * ggml_element_size(x), 0);
|
|
294
|
+
cb(y, "mamba_y_view", il);
|
|
295
|
+
|
|
296
|
+
// Add D parameter and apply gating with z
|
|
297
|
+
// {d_inner, n_seq_tokens, n_seqs} * {d_inner} => {d_inner, n_seq_tokens, n_seqs}
|
|
298
|
+
ggml_tensor * D = ggml_reshape_2d(ctx0, model.layers[il].ssm_d, 1, n_heads);
|
|
299
|
+
y = ggml_add(ctx0, y, ggml_mul(ctx0, x, D));
|
|
300
|
+
cb(y, "mamba_y_add_d", il);
|
|
301
|
+
|
|
302
|
+
y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
|
|
303
|
+
cb(y, "mamba_y_swiglu_z", il);
|
|
304
|
+
|
|
305
|
+
// out_proj: {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
|
|
306
|
+
y = ggml_view_3d(ctx0, y, head_dim * n_heads, n_seq_tokens, n_seqs, y->nb[2], y->nb[3], 0);
|
|
307
|
+
cur = build_lora_mm(model.layers[il].ssm_out, y);
|
|
308
|
+
cb(cur, "mamba_out_proj", il);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
|
|
312
|
+
cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
|
|
313
|
+
cb(cur, "mamba_out", il);
|
|
314
|
+
|
|
315
|
+
return cur;
|
|
316
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
llm_build_plm::llm_build_plm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
|
|
4
|
+
const float kq_scale = 1.0f/sqrtf(float(hparams.n_embd_head_k));
|
|
5
|
+
|
|
6
|
+
const uint32_t n_embd_head_qk_rope = hparams.n_rot;
|
|
7
|
+
const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
|
|
8
|
+
const uint32_t kv_lora_rank = hparams.n_lora_kv;
|
|
9
|
+
|
|
10
|
+
ggml_tensor * cur;
|
|
11
|
+
ggml_tensor * inpL;
|
|
12
|
+
|
|
13
|
+
// {n_embd, n_tokens}
|
|
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_RMS, il);
|
|
30
|
+
cb(cur, "attn_norm", il);
|
|
31
|
+
|
|
32
|
+
// self_attention
|
|
33
|
+
{
|
|
34
|
+
ggml_tensor * q = NULL;
|
|
35
|
+
q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
|
|
36
|
+
cb(q, "q", il);
|
|
37
|
+
|
|
38
|
+
// split into {n_head * n_embd_head_qk_nope, n_tokens}
|
|
39
|
+
ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
|
|
40
|
+
ggml_row_size(q->type, hparams.n_embd_head_k),
|
|
41
|
+
ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
|
|
42
|
+
0);
|
|
43
|
+
cb(q_nope, "q_nope", il);
|
|
44
|
+
|
|
45
|
+
// and {n_head * n_embd_head_qk_rope, n_tokens}
|
|
46
|
+
ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
|
|
47
|
+
ggml_row_size(q->type, hparams.n_embd_head_k),
|
|
48
|
+
ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
|
|
49
|
+
ggml_row_size(q->type, n_embd_head_qk_nope));
|
|
50
|
+
cb(q_pe, "q_pe", il);
|
|
51
|
+
|
|
52
|
+
// {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
|
|
53
|
+
ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
|
|
54
|
+
cb(kv_pe_compresseed, "kv_pe_compresseed", il);
|
|
55
|
+
|
|
56
|
+
// split into {kv_lora_rank, n_tokens}
|
|
57
|
+
ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
|
|
58
|
+
kv_pe_compresseed->nb[1],
|
|
59
|
+
0);
|
|
60
|
+
cb(kv_compressed, "kv_compressed", il);
|
|
61
|
+
|
|
62
|
+
// and {n_embd_head_qk_rope, n_tokens}
|
|
63
|
+
ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
|
|
64
|
+
kv_pe_compresseed->nb[1],
|
|
65
|
+
kv_pe_compresseed->nb[1],
|
|
66
|
+
ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
|
|
67
|
+
cb(k_pe, "k_pe", il);
|
|
68
|
+
|
|
69
|
+
kv_compressed = build_norm(kv_compressed,
|
|
70
|
+
model.layers[il].attn_kv_a_norm, NULL,
|
|
71
|
+
LLM_NORM_RMS, il);
|
|
72
|
+
cb(kv_compressed, "kv_compressed", il);
|
|
73
|
+
|
|
74
|
+
// {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}
|
|
75
|
+
ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
|
|
76
|
+
cb(kv, "kv", il);
|
|
77
|
+
|
|
78
|
+
// split into {n_head * n_embd_head_qk_nope, n_tokens}
|
|
79
|
+
ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
|
|
80
|
+
ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
|
|
81
|
+
ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
|
|
82
|
+
0);
|
|
83
|
+
cb(k_nope, "k_nope", il);
|
|
84
|
+
|
|
85
|
+
// and {n_head * n_embd_head_v, n_tokens}
|
|
86
|
+
ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
|
|
87
|
+
ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
|
|
88
|
+
ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
|
|
89
|
+
ggml_row_size(kv->type, (n_embd_head_qk_nope)));
|
|
90
|
+
cb(v_states, "v_states", il);
|
|
91
|
+
|
|
92
|
+
v_states = ggml_cont(ctx0, v_states);
|
|
93
|
+
cb(v_states, "v_states", il);
|
|
94
|
+
|
|
95
|
+
v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v * n_head, n_tokens,
|
|
96
|
+
ggml_row_size(kv->type, hparams.n_embd_head_v * n_head),
|
|
97
|
+
0);
|
|
98
|
+
cb(v_states, "v_states", il);
|
|
99
|
+
|
|
100
|
+
q_pe = ggml_rope_ext(
|
|
101
|
+
ctx0, q_pe, inp_pos, nullptr,
|
|
102
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
103
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
104
|
+
);
|
|
105
|
+
cb(q_pe, "q_pe", il);
|
|
106
|
+
|
|
107
|
+
// shared RoPE key
|
|
108
|
+
k_pe = ggml_rope_ext(
|
|
109
|
+
ctx0, k_pe, inp_pos, nullptr,
|
|
110
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
111
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
112
|
+
);
|
|
113
|
+
cb(k_pe, "k_pe", il);
|
|
114
|
+
|
|
115
|
+
ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
|
|
116
|
+
cb(q_states, "q_states", il);
|
|
117
|
+
|
|
118
|
+
ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
|
|
119
|
+
cb(k_states, "k_states", il);
|
|
120
|
+
|
|
121
|
+
cur = build_attn(inp_attn,
|
|
122
|
+
model.layers[il].wo, NULL,
|
|
123
|
+
q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
|
|
124
|
+
}
|
|
125
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
126
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
127
|
+
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
128
|
+
}
|
|
129
|
+
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
130
|
+
cb(ffn_inp, "ffn_inp", il);
|
|
131
|
+
|
|
132
|
+
cur = build_norm(ffn_inp,
|
|
133
|
+
model.layers[il].ffn_norm, NULL,
|
|
134
|
+
LLM_NORM_RMS, il);
|
|
135
|
+
cb(cur, "ffn_norm", il);
|
|
136
|
+
|
|
137
|
+
cur = build_ffn(cur,
|
|
138
|
+
model.layers[il].ffn_up, NULL, NULL,
|
|
139
|
+
NULL, NULL, NULL,
|
|
140
|
+
model.layers[il].ffn_down, NULL, NULL,
|
|
141
|
+
NULL,
|
|
142
|
+
LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
|
|
143
|
+
cb(cur, "ffn_out", il);
|
|
144
|
+
|
|
145
|
+
cur = ggml_add(ctx0, cur, ffn_inp);
|
|
146
|
+
|
|
147
|
+
cur = build_cvec(cur, il);
|
|
148
|
+
cb(cur, "l_out", il);
|
|
149
|
+
|
|
150
|
+
// input for next layer
|
|
151
|
+
inpL = cur;
|
|
152
|
+
}
|
|
153
|
+
cur = inpL;
|
|
154
|
+
|
|
155
|
+
cur = build_norm(cur,
|
|
156
|
+
model.output_norm, NULL,
|
|
157
|
+
LLM_NORM_RMS, -1);
|
|
158
|
+
|
|
159
|
+
cb(cur, "result_norm", -1);
|
|
160
|
+
res->t_embd = cur;
|
|
161
|
+
|
|
162
|
+
cur = build_lora_mm(model.output, cur);
|
|
163
|
+
|
|
164
|
+
cb(cur, "result_output", -1);
|
|
165
|
+
res->t_logits = cur;
|
|
166
|
+
|
|
167
|
+
ggml_build_forward_expand(gf, cur);
|
|
168
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
llm_build_qwen::llm_build_qwen(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
|
+
|
|
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
|
+
cur = build_norm(inpL,
|
|
25
|
+
model.layers[il].attn_norm, NULL,
|
|
26
|
+
LLM_NORM_RMS, 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], 2*sizeof(float)*(n_embd));
|
|
40
|
+
|
|
41
|
+
// using mode = 2 for neox mode
|
|
42
|
+
Qcur = ggml_rope_ext(
|
|
43
|
+
ctx0, Qcur, inp_pos, nullptr,
|
|
44
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
45
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
Kcur = ggml_rope_ext(
|
|
49
|
+
ctx0, Kcur, 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
|
+
cb(Qcur, "Qcur", il);
|
|
55
|
+
cb(Kcur, "Kcur", il);
|
|
56
|
+
cb(Vcur, "Vcur", il);
|
|
57
|
+
|
|
58
|
+
cur = build_attn(inp_attn,
|
|
59
|
+
model.layers[il].wo, NULL,
|
|
60
|
+
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
61
|
+
}
|
|
62
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
63
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
64
|
+
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
65
|
+
}
|
|
66
|
+
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
67
|
+
cb(ffn_inp, "ffn_inp", il);
|
|
68
|
+
|
|
69
|
+
// feed-forward forward
|
|
70
|
+
{
|
|
71
|
+
cur = build_norm(ffn_inp,
|
|
72
|
+
model.layers[il].ffn_norm, NULL,
|
|
73
|
+
LLM_NORM_RMS, il);
|
|
74
|
+
cb(cur, "ffn_norm", il);
|
|
75
|
+
|
|
76
|
+
cur = build_ffn(cur,
|
|
77
|
+
model.layers[il].ffn_up, NULL, NULL,
|
|
78
|
+
model.layers[il].ffn_gate, NULL, NULL,
|
|
79
|
+
model.layers[il].ffn_down, NULL, NULL,
|
|
80
|
+
NULL,
|
|
81
|
+
LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
82
|
+
cb(cur, "ffn_out", il);
|
|
83
|
+
}
|
|
84
|
+
cur = ggml_add(ctx0, cur, ffn_inp);
|
|
85
|
+
|
|
86
|
+
cur = build_cvec(cur, il);
|
|
87
|
+
cb(cur, "l_out", il);
|
|
88
|
+
|
|
89
|
+
// input for next layer
|
|
90
|
+
inpL = cur;
|
|
91
|
+
}
|
|
92
|
+
cur = inpL;
|
|
93
|
+
|
|
94
|
+
cur = build_norm(cur,
|
|
95
|
+
model.output_norm, NULL,
|
|
96
|
+
LLM_NORM_RMS, -1);
|
|
97
|
+
|
|
98
|
+
cb(cur, "result_norm", -1);
|
|
99
|
+
res->t_embd = cur;
|
|
100
|
+
|
|
101
|
+
// lm_head
|
|
102
|
+
cur = build_lora_mm(model.output, cur);
|
|
103
|
+
|
|
104
|
+
cb(cur, "result_output", -1);
|
|
105
|
+
res->t_logits = cur;
|
|
106
|
+
|
|
107
|
+
ggml_build_forward_expand(gf, cur);
|
|
108
|
+
}
|