@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,101 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
llm_build_bloom::llm_build_bloom(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
|
|
4
|
+
const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
5
|
+
const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
6
|
+
|
|
7
|
+
GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
8
|
+
|
|
9
|
+
ggml_tensor * cur;
|
|
10
|
+
ggml_tensor * inpL;
|
|
11
|
+
|
|
12
|
+
inpL = build_inp_embd(model.tok_embd);
|
|
13
|
+
|
|
14
|
+
auto * inp_attn = build_attn_inp_kv();
|
|
15
|
+
|
|
16
|
+
inpL = build_norm(inpL,
|
|
17
|
+
model.tok_norm,
|
|
18
|
+
model.tok_norm_b,
|
|
19
|
+
LLM_NORM, -1);
|
|
20
|
+
cb(inpL, "inp_norm", -1);
|
|
21
|
+
|
|
22
|
+
ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
23
|
+
|
|
24
|
+
for (int il = 0; il < n_layer; ++il) {
|
|
25
|
+
cur = build_norm(inpL,
|
|
26
|
+
model.layers[il].attn_norm,
|
|
27
|
+
model.layers[il].attn_norm_b,
|
|
28
|
+
LLM_NORM, il);
|
|
29
|
+
cb(cur, "attn_norm", il);
|
|
30
|
+
|
|
31
|
+
// self-attention
|
|
32
|
+
{
|
|
33
|
+
cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
34
|
+
cb(cur, "wqkv", il);
|
|
35
|
+
|
|
36
|
+
cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
37
|
+
cb(cur, "bqkv", il);
|
|
38
|
+
|
|
39
|
+
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));
|
|
40
|
+
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));
|
|
41
|
+
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));
|
|
42
|
+
|
|
43
|
+
cb(Qcur, "Qcur", il);
|
|
44
|
+
cb(Kcur, "Kcur", il);
|
|
45
|
+
cb(Vcur, "Vcur", il);
|
|
46
|
+
|
|
47
|
+
cur = build_attn(inp_attn,
|
|
48
|
+
model.layers[il].wo, model.layers[il].bo,
|
|
49
|
+
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
53
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
54
|
+
inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Add the input
|
|
58
|
+
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
59
|
+
cb(ffn_inp, "ffn_inp", il);
|
|
60
|
+
|
|
61
|
+
// FF
|
|
62
|
+
{
|
|
63
|
+
cur = build_norm(ffn_inp,
|
|
64
|
+
model.layers[il].ffn_norm,
|
|
65
|
+
model.layers[il].ffn_norm_b,
|
|
66
|
+
LLM_NORM, il);
|
|
67
|
+
cb(cur, "ffn_norm", il);
|
|
68
|
+
|
|
69
|
+
cur = build_ffn(cur,
|
|
70
|
+
model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
71
|
+
NULL, NULL, NULL,
|
|
72
|
+
model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
73
|
+
NULL,
|
|
74
|
+
LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
75
|
+
cb(cur, "ffn_out", il);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
cur = ggml_add(ctx0, cur, ffn_inp);
|
|
79
|
+
|
|
80
|
+
cur = build_cvec(cur, il);
|
|
81
|
+
cb(cur, "l_out", il);
|
|
82
|
+
|
|
83
|
+
// input for next layer
|
|
84
|
+
inpL = cur;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
cur = build_norm(inpL,
|
|
88
|
+
model.output_norm,
|
|
89
|
+
model.output_norm_b,
|
|
90
|
+
LLM_NORM, -1);
|
|
91
|
+
|
|
92
|
+
cb(cur, "result_norm", -1);
|
|
93
|
+
res->t_embd = cur;
|
|
94
|
+
|
|
95
|
+
cur = build_lora_mm(model.output, cur);
|
|
96
|
+
|
|
97
|
+
cb(cur, "result_output", -1);
|
|
98
|
+
res->t_logits = cur;
|
|
99
|
+
|
|
100
|
+
ggml_build_forward_expand(gf, cur);
|
|
101
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
#include <float.h>
|
|
4
|
+
|
|
5
|
+
llm_build_chameleon::llm_build_chameleon(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
|
+
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
|
+
if (hparams.swin_norm) {
|
|
28
|
+
cur = inpL;
|
|
29
|
+
} else {
|
|
30
|
+
cur = build_norm(inpL,
|
|
31
|
+
model.layers[il].attn_norm, NULL,
|
|
32
|
+
LLM_NORM_RMS, il);
|
|
33
|
+
cb(cur, "attn_norm", il);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// self-attention
|
|
37
|
+
{
|
|
38
|
+
// compute Q and K and RoPE them
|
|
39
|
+
ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
40
|
+
cb(Qcur, "Qcur", il);
|
|
41
|
+
|
|
42
|
+
ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
43
|
+
cb(Kcur, "Kcur", il);
|
|
44
|
+
|
|
45
|
+
ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
46
|
+
cb(Vcur, "Vcur", il);
|
|
47
|
+
|
|
48
|
+
if (model.layers[il].attn_q_norm) {
|
|
49
|
+
Qcur = ggml_view_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens,
|
|
50
|
+
ggml_element_size(Qcur) * n_embd_head,
|
|
51
|
+
ggml_element_size(Qcur) * n_embd_head * n_head,
|
|
52
|
+
0);
|
|
53
|
+
cb(Qcur, "Qcur", il);
|
|
54
|
+
|
|
55
|
+
Qcur = build_norm(Qcur,
|
|
56
|
+
model.layers[il].attn_q_norm,
|
|
57
|
+
model.layers[il].attn_q_norm_b,
|
|
58
|
+
LLM_NORM, il);
|
|
59
|
+
cb(Qcur, "Qcur", il);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (model.layers[il].attn_k_norm) {
|
|
63
|
+
Kcur = ggml_view_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens,
|
|
64
|
+
ggml_element_size(Kcur) * n_embd_head,
|
|
65
|
+
ggml_element_size(Kcur) * n_embd_head * n_head_kv,
|
|
66
|
+
0);
|
|
67
|
+
cb(Kcur, "Kcur", il);
|
|
68
|
+
|
|
69
|
+
Kcur = build_norm(Kcur,
|
|
70
|
+
model.layers[il].attn_k_norm,
|
|
71
|
+
model.layers[il].attn_k_norm_b,
|
|
72
|
+
LLM_NORM, il);
|
|
73
|
+
cb(Kcur, "Kcur", il);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
77
|
+
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
78
|
+
Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
|
|
79
|
+
|
|
80
|
+
Qcur = ggml_rope_ext(
|
|
81
|
+
ctx0, Qcur, inp_pos, nullptr,
|
|
82
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
83
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
Kcur = ggml_rope_ext(
|
|
87
|
+
ctx0, Kcur, inp_pos, nullptr,
|
|
88
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
89
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
cb(Qcur, "Qcur", il);
|
|
93
|
+
cb(Kcur, "Kcur", il);
|
|
94
|
+
cb(Vcur, "Vcur", il);
|
|
95
|
+
|
|
96
|
+
cur = build_attn(inp_attn,
|
|
97
|
+
model.layers[il].wo, nullptr,
|
|
98
|
+
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
102
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
103
|
+
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (hparams.swin_norm) {
|
|
107
|
+
cur = build_norm(cur,
|
|
108
|
+
model.layers[il].attn_norm, NULL,
|
|
109
|
+
LLM_NORM_RMS, il);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
113
|
+
cb(ffn_inp, "ffn_inp", il);
|
|
114
|
+
|
|
115
|
+
// feed-forward network
|
|
116
|
+
if (!hparams.swin_norm) {
|
|
117
|
+
cur = build_norm(ffn_inp,
|
|
118
|
+
model.layers[il].ffn_norm, NULL,
|
|
119
|
+
LLM_NORM_RMS, il);
|
|
120
|
+
cb(cur, "ffn_norm", il);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
cur = build_ffn(cur,
|
|
124
|
+
model.layers[il].ffn_up, NULL, NULL,
|
|
125
|
+
model.layers[il].ffn_gate, NULL, NULL,
|
|
126
|
+
model.layers[il].ffn_down, NULL, NULL,
|
|
127
|
+
NULL,
|
|
128
|
+
LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
129
|
+
cb(cur, "ffn_out", il);
|
|
130
|
+
|
|
131
|
+
if (hparams.swin_norm) {
|
|
132
|
+
cur = build_norm(cur,
|
|
133
|
+
model.layers[il].ffn_norm, NULL,
|
|
134
|
+
LLM_NORM_RMS, il);
|
|
135
|
+
cb(cur, "ffn_norm", il);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
cur = ggml_add(ctx0, cur, ffn_inp);
|
|
139
|
+
cb(cur, "ffn_out", il);
|
|
140
|
+
|
|
141
|
+
cur = build_cvec(cur, il);
|
|
142
|
+
cb(cur, "l_out", il);
|
|
143
|
+
|
|
144
|
+
// input for next layer
|
|
145
|
+
inpL = cur;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
cur = inpL;
|
|
149
|
+
|
|
150
|
+
cur = build_norm(cur,
|
|
151
|
+
model.output_norm, NULL,
|
|
152
|
+
LLM_NORM_RMS, -1);
|
|
153
|
+
|
|
154
|
+
cb(cur, "result_norm", -1);
|
|
155
|
+
res->t_embd = cur;
|
|
156
|
+
|
|
157
|
+
// lm_head
|
|
158
|
+
cur = build_lora_mm(model.output, cur);
|
|
159
|
+
cb(cur, "result_output_with_img_logits", -1);
|
|
160
|
+
|
|
161
|
+
// TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
|
|
162
|
+
// Needs to be removed once image outputs are supported.
|
|
163
|
+
int img_token_end_idx = 8196;
|
|
164
|
+
int img_token_start_idx = 4;
|
|
165
|
+
int num_img_tokens = img_token_end_idx - img_token_start_idx;
|
|
166
|
+
// creates 1d tensor of size num_img_tokens and values -FLT_MAX,
|
|
167
|
+
// which ensures that text token values are always at least larger than image token values
|
|
168
|
+
ggml_tensor * img_logits = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, num_img_tokens);
|
|
169
|
+
img_logits = ggml_clamp(ctx0, img_logits, -FLT_MAX, -FLT_MAX);
|
|
170
|
+
cb(img_logits, "img_logits", -1);
|
|
171
|
+
|
|
172
|
+
cur = ggml_set_1d(ctx0, cur, img_logits, ggml_element_size(cur) * img_token_start_idx);
|
|
173
|
+
|
|
174
|
+
cb(cur, "result_output", -1);
|
|
175
|
+
res->t_logits = cur;
|
|
176
|
+
|
|
177
|
+
ggml_build_forward_expand(gf, cur);
|
|
178
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
llm_build_chatglm::llm_build_chatglm(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
|
+
ggml_tensor * inpSA = inpL;
|
|
24
|
+
|
|
25
|
+
cur = build_norm(inpL,
|
|
26
|
+
model.layers[il].attn_norm,
|
|
27
|
+
NULL,
|
|
28
|
+
LLM_NORM_RMS, il);
|
|
29
|
+
cb(cur, "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 == nullptr) {
|
|
38
|
+
Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
39
|
+
if (model.layers[il].bq) {
|
|
40
|
+
Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
41
|
+
}
|
|
42
|
+
Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
43
|
+
if (model.layers[il].bk) {
|
|
44
|
+
Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
45
|
+
}
|
|
46
|
+
Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
47
|
+
if (model.layers[il].bv) {
|
|
48
|
+
Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
49
|
+
}
|
|
50
|
+
Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
51
|
+
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
52
|
+
Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
|
|
53
|
+
} else {
|
|
54
|
+
cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
55
|
+
cb(cur, "wqkv", il);
|
|
56
|
+
if (model.layers[il].bqkv) {
|
|
57
|
+
cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
58
|
+
cb(cur, "bqkv", il);
|
|
59
|
+
}
|
|
60
|
+
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));
|
|
61
|
+
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));
|
|
62
|
+
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));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
//printf("freq_base: %f freq_scale: %f ext_factor: %f attn_factor: %f\n", freq_base, freq_scale, ext_factor, attn_factor);
|
|
66
|
+
Qcur = ggml_rope_ext(
|
|
67
|
+
ctx0, Qcur, inp_pos, nullptr,
|
|
68
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
69
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
Kcur = ggml_rope_ext(
|
|
73
|
+
ctx0, Kcur, inp_pos, nullptr,
|
|
74
|
+
n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
75
|
+
ext_factor, attn_factor, beta_fast, beta_slow
|
|
76
|
+
);
|
|
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, NULL,
|
|
84
|
+
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (il == n_layer - 1 && inp_out_ids) {
|
|
88
|
+
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
89
|
+
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Add the input
|
|
93
|
+
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
94
|
+
cb(ffn_inp, "ffn_inp", il);
|
|
95
|
+
|
|
96
|
+
// FF
|
|
97
|
+
{
|
|
98
|
+
cur = build_norm(ffn_inp,
|
|
99
|
+
model.layers[il].ffn_norm,
|
|
100
|
+
NULL,
|
|
101
|
+
LLM_NORM_RMS, il);
|
|
102
|
+
cb(cur, "ffn_norm", il);
|
|
103
|
+
|
|
104
|
+
cur = build_ffn(cur,
|
|
105
|
+
model.layers[il].ffn_up, NULL, NULL,
|
|
106
|
+
NULL, NULL, NULL,
|
|
107
|
+
model.layers[il].ffn_down, NULL, NULL,
|
|
108
|
+
NULL,
|
|
109
|
+
LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
|
|
110
|
+
cb(cur, "ffn_out", il);
|
|
111
|
+
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
inpL = ggml_add(ctx0, cur, ffn_inp);
|
|
115
|
+
cb(inpL, "l_out", il);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
cur = build_norm(inpL,
|
|
119
|
+
model.output_norm,
|
|
120
|
+
NULL,
|
|
121
|
+
LLM_NORM_RMS, -1);
|
|
122
|
+
|
|
123
|
+
cb(cur, "result_norm", -1);
|
|
124
|
+
res->t_embd = cur;
|
|
125
|
+
|
|
126
|
+
cur = build_lora_mm(model.output, cur);
|
|
127
|
+
|
|
128
|
+
cb(cur, "result_output", -1);
|
|
129
|
+
res->t_logits = cur;
|
|
130
|
+
|
|
131
|
+
ggml_build_forward_expand(gf, cur);
|
|
132
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
llm_build_codeshell::llm_build_codeshell(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
|
+
GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
9
|
+
|
|
10
|
+
ggml_tensor * cur;
|
|
11
|
+
ggml_tensor * inpL;
|
|
12
|
+
|
|
13
|
+
inpL = build_inp_embd(model.tok_embd);
|
|
14
|
+
|
|
15
|
+
// inp_pos - contains the positions
|
|
16
|
+
ggml_tensor * inp_pos = build_inp_pos();
|
|
17
|
+
|
|
18
|
+
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
|
+
// add the input
|
|
68
|
+
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
69
|
+
cb(ffn_inp, "ffn_inp", il);
|
|
70
|
+
|
|
71
|
+
// FF
|
|
72
|
+
{
|
|
73
|
+
cur = build_norm(ffn_inp,
|
|
74
|
+
model.layers[il].ffn_norm,
|
|
75
|
+
model.layers[il].ffn_norm_b,
|
|
76
|
+
LLM_NORM, il);
|
|
77
|
+
cb(cur, "ffn_norm", il);
|
|
78
|
+
|
|
79
|
+
cur = build_ffn(cur,
|
|
80
|
+
model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
81
|
+
NULL, NULL, NULL,
|
|
82
|
+
model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
83
|
+
NULL,
|
|
84
|
+
LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
85
|
+
cb(cur, "ffn_out", il);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
cur = ggml_add(ctx0, cur, ffn_inp);
|
|
89
|
+
|
|
90
|
+
cur = build_cvec(cur, il);
|
|
91
|
+
cb(cur, "l_out", il);
|
|
92
|
+
|
|
93
|
+
// input for next layer
|
|
94
|
+
inpL = cur;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
cur = build_norm(inpL,
|
|
98
|
+
model.output_norm,
|
|
99
|
+
model.output_norm_b,
|
|
100
|
+
LLM_NORM, -1);
|
|
101
|
+
|
|
102
|
+
cb(cur, "result_norm", -1);
|
|
103
|
+
res->t_embd = cur;
|
|
104
|
+
|
|
105
|
+
cur = build_lora_mm(model.output, cur);
|
|
106
|
+
|
|
107
|
+
cb(cur, "result_output", -1);
|
|
108
|
+
res->t_logits = cur;
|
|
109
|
+
|
|
110
|
+
ggml_build_forward_expand(gf, cur);
|
|
111
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#include "models.h"
|
|
2
|
+
|
|
3
|
+
llm_build_cogvlm::llm_build_cogvlm(const llama_model & model, const llm_graph_params & params) :
|
|
4
|
+
llm_graph_context(params) {
|
|
5
|
+
const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
6
|
+
float kq_scale = 1.0f / sqrtf(float(n_embd_head));
|
|
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 *inpL, *cur;
|
|
12
|
+
inpL = build_inp_embd(model.tok_embd);
|
|
13
|
+
|
|
14
|
+
ggml_tensor * inp_pos = build_inp_pos();
|
|
15
|
+
|
|
16
|
+
auto * inp_attn = build_attn_inp_kv();
|
|
17
|
+
|
|
18
|
+
// check ubatch to see if we have input tokens (text)
|
|
19
|
+
// or an input embedding vector (image)
|
|
20
|
+
bool is_text;
|
|
21
|
+
if (ubatch.token) {
|
|
22
|
+
is_text = true;
|
|
23
|
+
} else {
|
|
24
|
+
is_text = false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
for (int il = 0; il < n_layer; ++il) {
|
|
28
|
+
// get either the text or image weight tensors
|
|
29
|
+
ggml_tensor *wqkv, *wo;
|
|
30
|
+
ggml_tensor *ffn_gate, *ffn_down, *ffn_up;
|
|
31
|
+
|
|
32
|
+
if (is_text) {
|
|
33
|
+
wqkv = model.layers[il].wqkv;
|
|
34
|
+
wo = model.layers[il].wo;
|
|
35
|
+
ffn_gate = model.layers[il].ffn_gate;
|
|
36
|
+
ffn_down = model.layers[il].ffn_down;
|
|
37
|
+
ffn_up = model.layers[il].ffn_up;
|
|
38
|
+
} else {
|
|
39
|
+
wqkv = model.layers[il].visexp_attn_wqkv;
|
|
40
|
+
wo = model.layers[il].visexp_attn_wo;
|
|
41
|
+
ffn_gate = model.layers[il].visexp_ffn_gate;
|
|
42
|
+
ffn_down = model.layers[il].visexp_ffn_down;
|
|
43
|
+
ffn_up = model.layers[il].visexp_ffn_up;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
ggml_tensor * inpSA = inpL;
|
|
47
|
+
cur = build_norm(inpSA, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
|
|
48
|
+
|
|
49
|
+
// build self attention
|
|
50
|
+
{
|
|
51
|
+
ggml_tensor * qkv = build_lora_mm(wqkv, cur);
|
|
52
|
+
|
|
53
|
+
// split qkv into Q, K, V along the first dimension
|
|
54
|
+
ggml_tensor * Qcur =
|
|
55
|
+
ggml_view_3d(ctx0, qkv, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), qkv->nb[1], 0);
|
|
56
|
+
ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
|
|
57
|
+
qkv->nb[1], n_embd * ggml_element_size(qkv));
|
|
58
|
+
ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
|
|
59
|
+
qkv->nb[1], 2 * n_embd * ggml_element_size(qkv));
|
|
60
|
+
|
|
61
|
+
Qcur = ggml_rope(ctx0, Qcur, inp_pos, n_embd_head, rope_type);
|
|
62
|
+
Kcur = ggml_rope(ctx0, Kcur, inp_pos, n_embd_head, rope_type);
|
|
63
|
+
|
|
64
|
+
cur = build_attn(inp_attn,
|
|
65
|
+
wo, nullptr,
|
|
66
|
+
Qcur, Kcur, Vcur,
|
|
67
|
+
nullptr, nullptr, nullptr,
|
|
68
|
+
kq_scale, il);
|
|
69
|
+
cb(cur, "attn_out", il);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
73
|
+
cb(ffn_inp, "ffn_inp", il);
|
|
74
|
+
|
|
75
|
+
cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
|
|
76
|
+
cb(cur, "ffn_norm", il);
|
|
77
|
+
|
|
78
|
+
cur = build_ffn(cur,
|
|
79
|
+
ffn_up, NULL, NULL,
|
|
80
|
+
ffn_gate, NULL, NULL,
|
|
81
|
+
ffn_down, NULL, NULL,
|
|
82
|
+
NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
83
|
+
|
|
84
|
+
cur = ggml_add(ctx0, cur, ffn_inp);
|
|
85
|
+
cb(cur, "ffn_out", il);
|
|
86
|
+
|
|
87
|
+
inpL = cur;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
cur = inpL;
|
|
91
|
+
|
|
92
|
+
cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
|
|
93
|
+
cb(cur, "result_norm", -1);
|
|
94
|
+
res->t_embd = cur;
|
|
95
|
+
|
|
96
|
+
cur = build_lora_mm(model.output, cur);
|
|
97
|
+
cb(cur, "result_output", -1);
|
|
98
|
+
res->t_logits = cur;
|
|
99
|
+
ggml_build_forward_expand(gf, cur);
|
|
100
|
+
}
|