@fugood/llama.node 1.0.0-beta.5 → 1.0.0-beta.7
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/lib/binding.ts +3 -1
- package/lib/index.js +2 -0
- package/lib/index.ts +3 -1
- package/package.json +14 -14
- package/scripts/llama.cpp.patch +27 -26
- package/src/EmbeddingWorker.cpp +1 -1
- package/src/LlamaCompletionWorker.cpp +28 -7
- package/src/LlamaCompletionWorker.h +4 -0
- package/src/LlamaContext.cpp +14 -17
- package/src/common.hpp +7 -6
- package/src/llama.cpp/CMakeLists.txt +15 -4
- package/src/llama.cpp/common/CMakeLists.txt +15 -24
- package/src/llama.cpp/common/arg.cpp +172 -110
- package/src/llama.cpp/common/chat-parser.cpp +385 -0
- package/src/llama.cpp/common/chat-parser.h +120 -0
- package/src/llama.cpp/common/chat.cpp +726 -596
- package/src/llama.cpp/common/chat.h +74 -8
- package/src/llama.cpp/common/common.cpp +56 -38
- package/src/llama.cpp/common/common.h +9 -3
- package/src/llama.cpp/common/json-partial.cpp +256 -0
- package/src/llama.cpp/common/json-partial.h +38 -0
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +2 -1
- package/src/llama.cpp/common/json-schema-to-grammar.h +4 -4
- package/src/llama.cpp/common/sampling.cpp +7 -8
- package/src/llama.cpp/common/speculative.cpp +6 -4
- package/src/llama.cpp/ggml/CMakeLists.txt +48 -3
- package/src/llama.cpp/ggml/include/ggml.h +22 -3
- package/src/llama.cpp/ggml/src/CMakeLists.txt +81 -22
- package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +131 -49
- package/src/llama.cpp/ggml/src/ggml-cpu/amx/amx.cpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/amx/mmq.cpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/arm/quants.c +4113 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/arm/repack.cpp +2162 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +2638 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/powerpc/cpu-feats.cpp +82 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/powerpc/quants.c +2731 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +2068 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/riscv/repack.cpp +396 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/s390/quants.c +1299 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/wasm/quants.c +1480 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/x86/quants.c +4310 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-aarch64.cpp → arch/x86/repack.cpp} +59 -3206
- package/src/llama.cpp/ggml/src/ggml-cpu/arch-fallback.h +184 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/common.h +1 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +12 -13
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +64 -88
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +8 -8
- package/src/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-hbm.cpp → hbm.cpp} +1 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +56 -7
- package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.h +5 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +282 -100
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +1 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/quants.c +1157 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-quants.h → quants.h} +26 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/repack.cpp +1570 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/repack.h +98 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +119 -5
- package/src/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-traits.cpp → traits.cpp} +1 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +85 -16
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.h +204 -49
- package/src/llama.cpp/include/llama.h +145 -40
- package/src/llama.cpp/src/CMakeLists.txt +5 -1
- package/src/llama.cpp/src/llama-arch.cpp +99 -3
- package/src/llama.cpp/src/llama-arch.h +10 -1
- package/src/llama.cpp/src/llama-batch.cpp +728 -272
- package/src/llama.cpp/src/llama-batch.h +112 -54
- package/src/llama.cpp/src/llama-chat.cpp +19 -2
- package/src/llama.cpp/src/llama-chat.h +1 -0
- package/src/llama.cpp/src/llama-context.cpp +525 -339
- package/src/llama.cpp/src/llama-context.h +38 -17
- package/src/llama.cpp/src/llama-cparams.cpp +4 -0
- package/src/llama.cpp/src/llama-cparams.h +2 -0
- package/src/llama.cpp/src/llama-grammar.cpp +12 -2
- package/src/llama.cpp/src/llama-graph.cpp +413 -353
- package/src/llama.cpp/src/llama-graph.h +112 -56
- package/src/llama.cpp/src/llama-hparams.cpp +10 -2
- package/src/llama.cpp/src/llama-hparams.h +13 -2
- package/src/llama.cpp/src/llama-kv-cache-unified-iswa.cpp +279 -0
- package/src/llama.cpp/src/llama-kv-cache-unified-iswa.h +128 -0
- package/src/llama.cpp/src/llama-kv-cache-unified.cpp +1815 -0
- package/src/llama.cpp/src/llama-kv-cache-unified.h +303 -0
- package/src/llama.cpp/src/llama-kv-cells.h +415 -0
- package/src/llama.cpp/src/llama-memory-hybrid.cpp +246 -0
- package/src/llama.cpp/src/llama-memory-hybrid.h +138 -0
- package/src/llama.cpp/src/llama-memory-recurrent.cpp +1112 -0
- package/src/llama.cpp/src/llama-memory-recurrent.h +183 -0
- package/src/llama.cpp/src/llama-memory.cpp +41 -0
- package/src/llama.cpp/src/llama-memory.h +86 -5
- package/src/llama.cpp/src/llama-mmap.cpp +1 -1
- package/src/llama.cpp/src/llama-model-loader.cpp +42 -17
- package/src/llama.cpp/src/llama-model-saver.cpp +1 -0
- package/src/llama.cpp/src/llama-model.cpp +1137 -528
- package/src/llama.cpp/src/llama-model.h +4 -0
- package/src/llama.cpp/src/llama-quant.cpp +2 -1
- package/src/llama.cpp/src/llama-sampling.cpp +2 -2
- package/src/llama.cpp/src/llama-vocab.cpp +69 -32
- package/src/llama.cpp/src/llama-vocab.h +1 -0
- package/src/llama.cpp/src/llama.cpp +11 -7
- package/src/llama.cpp/src/unicode.cpp +5 -0
- package/src/tts_utils.h +1 -1
- package/src/llama.cpp/common/json.hpp +0 -24766
- package/src/llama.cpp/common/minja/chat-template.hpp +0 -541
- package/src/llama.cpp/common/minja/minja.hpp +0 -2974
- package/src/llama.cpp/common/stb_image.h +0 -7988
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +0 -8
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +0 -13326
- package/src/llama.cpp/src/llama-kv-cache.cpp +0 -2827
- package/src/llama.cpp/src/llama-kv-cache.h +0 -515
- /package/src/llama.cpp/ggml/src/ggml-cpu/{cpu-feats-x86.cpp → arch/x86/cpu-feats.cpp} +0 -0
- /package/src/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-hbm.h → hbm.h} +0 -0
- /package/src/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-traits.h → traits.h} +0 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "llama-batch.h"
|
|
4
|
+
#include "llama-graph.h"
|
|
5
|
+
#include "llama-kv-cells.h"
|
|
6
|
+
#include "llama-memory.h"
|
|
7
|
+
|
|
8
|
+
#include <unordered_map>
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
struct llama_cparams;
|
|
12
|
+
struct llama_hparams;
|
|
13
|
+
struct llama_model;
|
|
14
|
+
struct llama_context;
|
|
15
|
+
|
|
16
|
+
//
|
|
17
|
+
// llama_kv_cache_unified
|
|
18
|
+
//
|
|
19
|
+
|
|
20
|
+
class llama_kv_cache_unified : public llama_memory_i {
|
|
21
|
+
public:
|
|
22
|
+
static uint32_t get_padding(const llama_cparams & cparams);
|
|
23
|
+
|
|
24
|
+
// this callback is used to filter out layers that should not be included in the cache
|
|
25
|
+
using layer_filter_cb = std::function<bool(int32_t il)>;
|
|
26
|
+
|
|
27
|
+
using ubatch_heads = std::vector<uint32_t>;
|
|
28
|
+
|
|
29
|
+
struct defrag_info {
|
|
30
|
+
bool empty() const {
|
|
31
|
+
return ids.empty();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// contains information about which cell moves where:
|
|
35
|
+
// - cell i moves to ids[i]
|
|
36
|
+
// - if ids[i] == i || ids[i] == ids.size(), then cell i is not moved
|
|
37
|
+
std::vector<uint32_t> ids;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
llama_kv_cache_unified(
|
|
41
|
+
const llama_model & model,
|
|
42
|
+
layer_filter_cb && filter,
|
|
43
|
+
ggml_type type_k,
|
|
44
|
+
ggml_type type_v,
|
|
45
|
+
bool v_trans,
|
|
46
|
+
bool offload,
|
|
47
|
+
uint32_t kv_size,
|
|
48
|
+
uint32_t n_seq_max,
|
|
49
|
+
uint32_t n_pad,
|
|
50
|
+
uint32_t n_swa,
|
|
51
|
+
llama_swa_type swa_type);
|
|
52
|
+
|
|
53
|
+
~llama_kv_cache_unified() = default;
|
|
54
|
+
|
|
55
|
+
//
|
|
56
|
+
// llama_memory_i
|
|
57
|
+
//
|
|
58
|
+
|
|
59
|
+
llama_memory_context_ptr init_batch(
|
|
60
|
+
llama_batch_allocr & balloc,
|
|
61
|
+
uint32_t n_ubatch,
|
|
62
|
+
bool embd_all) override;
|
|
63
|
+
|
|
64
|
+
llama_memory_context_ptr init_full() override;
|
|
65
|
+
|
|
66
|
+
llama_memory_context_ptr init_update(llama_context * lctx, bool optimize) override;
|
|
67
|
+
|
|
68
|
+
bool get_can_shift() const override;
|
|
69
|
+
|
|
70
|
+
void clear(bool data) override;
|
|
71
|
+
|
|
72
|
+
bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
|
|
73
|
+
void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
|
|
74
|
+
void seq_keep(llama_seq_id seq_id) override;
|
|
75
|
+
void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override;
|
|
76
|
+
void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override;
|
|
77
|
+
|
|
78
|
+
llama_pos seq_pos_min(llama_seq_id seq_id) const override;
|
|
79
|
+
llama_pos seq_pos_max(llama_seq_id seq_id) const override;
|
|
80
|
+
|
|
81
|
+
// state write/load
|
|
82
|
+
|
|
83
|
+
void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const override;
|
|
84
|
+
void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) override;
|
|
85
|
+
|
|
86
|
+
//
|
|
87
|
+
// llama_kv_cache_unified specific API
|
|
88
|
+
//
|
|
89
|
+
|
|
90
|
+
uint32_t get_size() const;
|
|
91
|
+
|
|
92
|
+
bool get_has_shift() const;
|
|
93
|
+
|
|
94
|
+
//
|
|
95
|
+
// graph_build API
|
|
96
|
+
//
|
|
97
|
+
|
|
98
|
+
uint32_t get_n_kv() const;
|
|
99
|
+
|
|
100
|
+
// get views of the current state of the cache
|
|
101
|
+
ggml_tensor * get_k(ggml_context * ctx, int32_t il, uint32_t n_kv) const;
|
|
102
|
+
ggml_tensor * get_v(ggml_context * ctx, int32_t il, uint32_t n_kv) const;
|
|
103
|
+
|
|
104
|
+
// store k_cur and v_cur in the cache based on the provided head location
|
|
105
|
+
ggml_tensor * cpy_k(ggml_context * ctx, ggml_tensor * k_cur, int32_t il, uint32_t head_cur) const;
|
|
106
|
+
ggml_tensor * cpy_v(ggml_context * ctx, ggml_tensor * v_cur, int32_t il, uint32_t head_cur) const;
|
|
107
|
+
|
|
108
|
+
//
|
|
109
|
+
// preparation API
|
|
110
|
+
//
|
|
111
|
+
|
|
112
|
+
// find places for the provided ubatches in the cache, returns the head locations
|
|
113
|
+
// return empty vector on failure
|
|
114
|
+
ubatch_heads prepare(const std::vector<llama_ubatch> & ubatches);
|
|
115
|
+
|
|
116
|
+
bool update(llama_context * lctx, bool do_shift, const defrag_info & dinfo);
|
|
117
|
+
|
|
118
|
+
// return the cell position where we can insert the ubatch
|
|
119
|
+
// return -1 on failure to find a contiguous slot of kv cells
|
|
120
|
+
int32_t find_slot(const llama_ubatch & ubatch) const;
|
|
121
|
+
|
|
122
|
+
// emplace the ubatch context into slot: [head_cur, head_cur + ubatch.n_tokens)
|
|
123
|
+
void apply_ubatch(uint32_t head_cur, const llama_ubatch & ubatch);
|
|
124
|
+
|
|
125
|
+
//
|
|
126
|
+
// set_input API
|
|
127
|
+
//
|
|
128
|
+
|
|
129
|
+
void set_input_kq_mask (ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const;
|
|
130
|
+
void set_input_k_shift (ggml_tensor * dst) const;
|
|
131
|
+
void set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const;
|
|
132
|
+
|
|
133
|
+
private:
|
|
134
|
+
const llama_model & model;
|
|
135
|
+
const llama_hparams & hparams;
|
|
136
|
+
|
|
137
|
+
struct kv_layer {
|
|
138
|
+
// layer index in the model
|
|
139
|
+
// note: can be different from the layer index in the KV cache
|
|
140
|
+
uint32_t il;
|
|
141
|
+
|
|
142
|
+
ggml_tensor * k;
|
|
143
|
+
ggml_tensor * v;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
bool v_trans = true; // the value tensor is transposed
|
|
147
|
+
|
|
148
|
+
// the current index from where we start searching for a free slot in the ring buffer of KV cells (see find_slot())
|
|
149
|
+
// note: this is not part of the KV state and it's only used to speed-up the find_slot() method
|
|
150
|
+
uint32_t head = 0;
|
|
151
|
+
|
|
152
|
+
const uint32_t n_seq_max = 1;
|
|
153
|
+
|
|
154
|
+
// required padding
|
|
155
|
+
const uint32_t n_pad = 1;
|
|
156
|
+
|
|
157
|
+
// SWA
|
|
158
|
+
const uint32_t n_swa = 0;
|
|
159
|
+
|
|
160
|
+
int debug = 0;
|
|
161
|
+
|
|
162
|
+
const llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE;
|
|
163
|
+
|
|
164
|
+
std::vector<ggml_context_ptr> ctxs;
|
|
165
|
+
std::vector<ggml_backend_buffer_ptr> bufs;
|
|
166
|
+
|
|
167
|
+
llama_kv_cells_unified cells;
|
|
168
|
+
|
|
169
|
+
std::vector<kv_layer> layers;
|
|
170
|
+
|
|
171
|
+
// model layer id -> KV cache layer id
|
|
172
|
+
std::unordered_map<int32_t, int32_t> map_layer_ids;
|
|
173
|
+
|
|
174
|
+
// return non-empty vector if cells have been moved
|
|
175
|
+
defrag_info defrag_prepare(int32_t n_max_nodes) const;
|
|
176
|
+
|
|
177
|
+
size_t total_size() const;
|
|
178
|
+
|
|
179
|
+
size_t size_k_bytes() const;
|
|
180
|
+
size_t size_v_bytes() const;
|
|
181
|
+
|
|
182
|
+
bool is_masked_swa(llama_pos p0, llama_pos p1) const;
|
|
183
|
+
|
|
184
|
+
ggml_tensor * build_rope_shift(
|
|
185
|
+
const llama_cparams & cparams,
|
|
186
|
+
ggml_context * ctx,
|
|
187
|
+
ggml_tensor * cur,
|
|
188
|
+
ggml_tensor * shift,
|
|
189
|
+
ggml_tensor * factors,
|
|
190
|
+
float freq_base,
|
|
191
|
+
float freq_scale) const;
|
|
192
|
+
|
|
193
|
+
llm_graph_result_ptr build_graph_shift(
|
|
194
|
+
const llama_cparams & cparams,
|
|
195
|
+
ggml_context * ctx,
|
|
196
|
+
ggml_cgraph * gf) const;
|
|
197
|
+
|
|
198
|
+
llm_graph_result_ptr build_graph_defrag(
|
|
199
|
+
const llama_cparams & cparams,
|
|
200
|
+
ggml_context * ctx,
|
|
201
|
+
ggml_cgraph * gf,
|
|
202
|
+
const defrag_info & dinfo) const;
|
|
203
|
+
|
|
204
|
+
void state_write_meta(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id = -1) const;
|
|
205
|
+
void state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const;
|
|
206
|
+
|
|
207
|
+
bool state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id = -1);
|
|
208
|
+
bool state_read_data(llama_io_read_i & io, uint32_t cell_count);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
class llama_kv_cache_unified_context : public llama_memory_context_i {
|
|
212
|
+
public:
|
|
213
|
+
// some shorthands
|
|
214
|
+
using ubatch_heads = llama_kv_cache_unified::ubatch_heads;
|
|
215
|
+
using defrag_info = llama_kv_cache_unified::defrag_info;
|
|
216
|
+
|
|
217
|
+
// used for errors
|
|
218
|
+
llama_kv_cache_unified_context(llama_memory_status status);
|
|
219
|
+
|
|
220
|
+
// used to create a full-cache context
|
|
221
|
+
llama_kv_cache_unified_context(
|
|
222
|
+
llama_kv_cache_unified * kv);
|
|
223
|
+
|
|
224
|
+
// used to create an update context
|
|
225
|
+
llama_kv_cache_unified_context(
|
|
226
|
+
llama_kv_cache_unified * kv,
|
|
227
|
+
llama_context * lctx,
|
|
228
|
+
bool do_shift,
|
|
229
|
+
defrag_info dinfo);
|
|
230
|
+
|
|
231
|
+
// used to create a batch procesing context from a batch
|
|
232
|
+
llama_kv_cache_unified_context(
|
|
233
|
+
llama_kv_cache_unified * kv,
|
|
234
|
+
ubatch_heads heads,
|
|
235
|
+
std::vector<llama_ubatch> ubatches);
|
|
236
|
+
|
|
237
|
+
virtual ~llama_kv_cache_unified_context();
|
|
238
|
+
|
|
239
|
+
//
|
|
240
|
+
// llama_memory_context_i
|
|
241
|
+
//
|
|
242
|
+
|
|
243
|
+
bool next() override;
|
|
244
|
+
bool apply() override;
|
|
245
|
+
|
|
246
|
+
llama_memory_status get_status() const override;
|
|
247
|
+
const llama_ubatch & get_ubatch() const override;
|
|
248
|
+
|
|
249
|
+
//
|
|
250
|
+
// llama_kv_cache_unified_context specific API
|
|
251
|
+
//
|
|
252
|
+
|
|
253
|
+
uint32_t get_n_kv() const;
|
|
254
|
+
|
|
255
|
+
// get views of the current state of the cache
|
|
256
|
+
ggml_tensor * get_k(ggml_context * ctx, int32_t il) const;
|
|
257
|
+
ggml_tensor * get_v(ggml_context * ctx, int32_t il) const;
|
|
258
|
+
|
|
259
|
+
// store k_cur and v_cur in the cache based on the provided head location
|
|
260
|
+
ggml_tensor * cpy_k(ggml_context * ctx, ggml_tensor * k_cur, int32_t il) const;
|
|
261
|
+
ggml_tensor * cpy_v(ggml_context * ctx, ggml_tensor * v_cur, int32_t il) const;
|
|
262
|
+
|
|
263
|
+
void set_input_k_shift(ggml_tensor * dst) const;
|
|
264
|
+
|
|
265
|
+
void set_input_kq_mask (ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const;
|
|
266
|
+
void set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const;
|
|
267
|
+
|
|
268
|
+
private:
|
|
269
|
+
llama_memory_status status;
|
|
270
|
+
|
|
271
|
+
llama_kv_cache_unified * kv;
|
|
272
|
+
llama_context * lctx;
|
|
273
|
+
|
|
274
|
+
//
|
|
275
|
+
// update context
|
|
276
|
+
//
|
|
277
|
+
|
|
278
|
+
bool do_shift = false;
|
|
279
|
+
|
|
280
|
+
defrag_info dinfo;
|
|
281
|
+
|
|
282
|
+
//
|
|
283
|
+
// batch processing context
|
|
284
|
+
//
|
|
285
|
+
|
|
286
|
+
// the index of the next ubatch to process
|
|
287
|
+
size_t i_next = 0;
|
|
288
|
+
|
|
289
|
+
ubatch_heads heads;
|
|
290
|
+
|
|
291
|
+
std::vector<llama_ubatch> ubatches;
|
|
292
|
+
|
|
293
|
+
//
|
|
294
|
+
// data needed for building the compute graph for the current ubatch:
|
|
295
|
+
//
|
|
296
|
+
|
|
297
|
+
// a heuristic, to avoid attending the full cache if it is not yet utilized
|
|
298
|
+
// as the cache gets filled, the benefit from this heuristic disappears
|
|
299
|
+
int32_t n_kv;
|
|
300
|
+
|
|
301
|
+
// the beginning of the current slot in which the ubatch will be inserted
|
|
302
|
+
int32_t head;
|
|
303
|
+
};
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "llama.h"
|
|
4
|
+
#include "llama-cparams.h"
|
|
5
|
+
|
|
6
|
+
#include <bitset>
|
|
7
|
+
#include <cassert>
|
|
8
|
+
#include <vector>
|
|
9
|
+
#include <set>
|
|
10
|
+
|
|
11
|
+
// meta information about KV cells that can be part of multiple sequences at the same time
|
|
12
|
+
// TODO: add unit tests
|
|
13
|
+
class llama_kv_cells_unified {
|
|
14
|
+
public:
|
|
15
|
+
void reset() {
|
|
16
|
+
for (uint32_t i = 0; i < pos.size(); ++i) {
|
|
17
|
+
pos[i] = -1;
|
|
18
|
+
shift[i] = 0;
|
|
19
|
+
seq[i].reset();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
has_shift = false;
|
|
23
|
+
|
|
24
|
+
used.clear();
|
|
25
|
+
|
|
26
|
+
for (uint32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
|
27
|
+
seq_pos[s].clear();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
void reset_shift() {
|
|
32
|
+
has_shift = false;
|
|
33
|
+
|
|
34
|
+
for (uint32_t i = 0; i < shift.size(); ++i) {
|
|
35
|
+
shift[i] = 0;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
uint32_t size() const {
|
|
40
|
+
return pos.size();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
void resize(uint32_t n) {
|
|
44
|
+
pos.resize(n);
|
|
45
|
+
shift.resize(n);
|
|
46
|
+
seq.resize(n);
|
|
47
|
+
|
|
48
|
+
reset();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
bool is_empty(uint32_t i) const {
|
|
52
|
+
assert(i < pos.size());
|
|
53
|
+
assert((pos[i] < 0 && pos[i] == -1) || pos[i] >= 0);
|
|
54
|
+
|
|
55
|
+
return pos[i] == -1;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
uint32_t get_used() const {
|
|
59
|
+
return used.size();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// the index of the first cell that is used
|
|
63
|
+
// return 0 if no cells are used
|
|
64
|
+
uint32_t used_min() const {
|
|
65
|
+
return used.empty() ? 0 : *used.begin();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// the index of the last cell that is used + 1
|
|
69
|
+
// return 0 if no cells are used
|
|
70
|
+
uint32_t used_max_p1() const {
|
|
71
|
+
return used.empty() ? 0 : *used.rbegin() + 1;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
bool get_has_shift() const {
|
|
75
|
+
return has_shift;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// move cell isrc to idst (used during defrag)
|
|
79
|
+
void mv(uint32_t isrc, uint32_t idst) {
|
|
80
|
+
assert(isrc < pos.size());
|
|
81
|
+
assert(idst < pos.size());
|
|
82
|
+
|
|
83
|
+
assert(pos[idst] == -1);
|
|
84
|
+
assert(pos[isrc] != -1);
|
|
85
|
+
|
|
86
|
+
pos [idst] = pos [isrc];
|
|
87
|
+
shift[idst] = shift[isrc];
|
|
88
|
+
seq [idst] = seq [isrc];
|
|
89
|
+
|
|
90
|
+
pos [isrc] = -1;
|
|
91
|
+
shift[isrc] = 0;
|
|
92
|
+
seq [isrc].reset();
|
|
93
|
+
|
|
94
|
+
used.erase (isrc);
|
|
95
|
+
used.insert(idst);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// copy the state of cells [i, i + n) (used for save/restore the state of the cells)
|
|
99
|
+
llama_kv_cells_unified cp(uint32_t i, uint32_t n) const {
|
|
100
|
+
assert(i + n <= pos.size());
|
|
101
|
+
|
|
102
|
+
llama_kv_cells_unified res;
|
|
103
|
+
|
|
104
|
+
res.resize(n);
|
|
105
|
+
|
|
106
|
+
for (uint32_t j = 0; j < n; ++j) {
|
|
107
|
+
res.pos[j] = pos[i + j];
|
|
108
|
+
res.seq[j] = seq[i + j];
|
|
109
|
+
|
|
110
|
+
assert(shift[i + j] == 0);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return res;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// set the state of cells [i, i + other.pos.size()) (used for save/restore the state of the cells)
|
|
117
|
+
void set(uint32_t i, const llama_kv_cells_unified & other) {
|
|
118
|
+
assert(i + other.pos.size() <= pos.size());
|
|
119
|
+
|
|
120
|
+
for (uint32_t j = 0; j < other.pos.size(); ++j) {
|
|
121
|
+
if (pos[i + j] == -1 && other.pos[j] != -1) {
|
|
122
|
+
used.insert(i + j);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (pos[i + j] != -1 && other.pos[j] == -1) {
|
|
126
|
+
used.erase(i + j);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (pos[i + j] != -1) {
|
|
130
|
+
seq_pos_rm(i + j);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
pos[i + j] = other.pos[j];
|
|
134
|
+
seq[i + j] = other.seq[j];
|
|
135
|
+
|
|
136
|
+
if (pos[i + j] != -1) {
|
|
137
|
+
seq_pos_add(i + j);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
assert(shift[i + j] == 0);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// clear a non-empty cell
|
|
145
|
+
void rm(uint32_t i) {
|
|
146
|
+
assert(i < pos.size());
|
|
147
|
+
assert(pos[i] != -1);
|
|
148
|
+
|
|
149
|
+
seq_pos_rm(i);
|
|
150
|
+
seq[i].reset();
|
|
151
|
+
|
|
152
|
+
pos[i] = -1;
|
|
153
|
+
shift[i] = 0;
|
|
154
|
+
|
|
155
|
+
used.erase(i);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// note: call only if the cell has seq_id
|
|
159
|
+
// return true if the cell becomes empty
|
|
160
|
+
bool seq_rm(uint32_t i, llama_seq_id seq_id) {
|
|
161
|
+
assert(i < pos.size());
|
|
162
|
+
assert(seq[i].test(seq_id));
|
|
163
|
+
assert(pos[i] != -1);
|
|
164
|
+
assert(seq_id >= 0);
|
|
165
|
+
|
|
166
|
+
seq[i].reset(seq_id);
|
|
167
|
+
seq_pos[seq_id].erase(pos[i]);
|
|
168
|
+
|
|
169
|
+
if (seq[i].none()) {
|
|
170
|
+
pos[i] = -1;
|
|
171
|
+
shift[i] = 0;
|
|
172
|
+
|
|
173
|
+
used.erase(i);
|
|
174
|
+
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// return true if the cell becomes empty (i.e. it did not contain seq_id before the call)
|
|
182
|
+
bool seq_keep(uint32_t i, llama_seq_id seq_id) {
|
|
183
|
+
assert(i < pos.size());
|
|
184
|
+
|
|
185
|
+
if (seq[i].test(seq_id)) {
|
|
186
|
+
seq_pos_rm(i);
|
|
187
|
+
seq[i].reset();
|
|
188
|
+
|
|
189
|
+
seq[i].set(seq_id);
|
|
190
|
+
seq_pos[seq_id].insert(pos[i]);
|
|
191
|
+
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (seq[i].any()) {
|
|
196
|
+
seq_pos_rm(i);
|
|
197
|
+
seq[i].reset();
|
|
198
|
+
|
|
199
|
+
pos[i] = -1;
|
|
200
|
+
shift[i] = 0;
|
|
201
|
+
|
|
202
|
+
used.erase(i);
|
|
203
|
+
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
assert(pos[i] == -1);
|
|
208
|
+
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// number of different sequences in the cell
|
|
213
|
+
int seq_count(uint32_t i) const {
|
|
214
|
+
assert(i < pos.size());
|
|
215
|
+
assert(pos[i] != -1);
|
|
216
|
+
|
|
217
|
+
return seq[i].count();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// check if the cell contains seq_id
|
|
221
|
+
bool seq_has(uint32_t i, llama_seq_id seq_id) const {
|
|
222
|
+
assert(i < pos.size());
|
|
223
|
+
assert(seq_id >= 0);
|
|
224
|
+
|
|
225
|
+
return seq[i].test(seq_id);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// note: call only if the cell is not empty and the seq_id is not in the cell
|
|
229
|
+
void seq_add(uint32_t i, llama_seq_id seq_id) {
|
|
230
|
+
assert(i < pos.size());
|
|
231
|
+
assert(pos[i] != -1);
|
|
232
|
+
assert(!seq[i].test(seq_id));
|
|
233
|
+
|
|
234
|
+
seq[i].set(seq_id);
|
|
235
|
+
seq_pos[seq_id].insert(pos[i]);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// return the sequence id of this cell
|
|
239
|
+
// note: call only for cells with exactly one sequence
|
|
240
|
+
llama_seq_id seq_get(uint32_t i) const {
|
|
241
|
+
assert(seq[i].count() == 1);
|
|
242
|
+
|
|
243
|
+
for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
|
244
|
+
if (seq[i].test(s)) {
|
|
245
|
+
return s;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return -1;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// the minimum position of sequence seq_id currently present in any of the cells
|
|
253
|
+
// return -1 if the sequence is not present
|
|
254
|
+
llama_pos seq_pos_min(llama_seq_id seq_id) const {
|
|
255
|
+
assert(seq_id >= 0);
|
|
256
|
+
assert(seq_id < LLAMA_MAX_SEQ);
|
|
257
|
+
|
|
258
|
+
if (seq_pos[seq_id].empty()) {
|
|
259
|
+
return -1;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return *seq_pos[seq_id].begin();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// the maximum position of sequence seq_id currently present in any of the cells
|
|
266
|
+
// return -1 if the sequence is not present
|
|
267
|
+
llama_pos seq_pos_max(llama_seq_id seq_id) const {
|
|
268
|
+
assert(seq_id >= 0);
|
|
269
|
+
assert(seq_id < LLAMA_MAX_SEQ);
|
|
270
|
+
|
|
271
|
+
if (seq_pos[seq_id].empty()) {
|
|
272
|
+
return -1;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return *seq_pos[seq_id].rbegin();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// note: call only if the cell is not empty
|
|
279
|
+
llama_pos pos_get(uint32_t i) const {
|
|
280
|
+
assert(i < pos.size());
|
|
281
|
+
assert(pos[i] != -1);
|
|
282
|
+
|
|
283
|
+
return pos[i];
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// note: call only if the cell is not empty
|
|
287
|
+
llama_pos get_shift(uint32_t i) const {
|
|
288
|
+
assert(i < pos.size());
|
|
289
|
+
assert(pos[i] != -1);
|
|
290
|
+
|
|
291
|
+
return shift[i];
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// check if a cell is not empty and its position is within [p0, p1)
|
|
295
|
+
bool pos_in(uint32_t i, llama_pos p0, llama_pos p1) const {
|
|
296
|
+
assert(i < pos.size());
|
|
297
|
+
|
|
298
|
+
return pos[i] >= p0 && pos[i] < p1;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// set the position of an empty cell
|
|
302
|
+
// does not modify "has_shift"
|
|
303
|
+
// note: call only if the cell is empty
|
|
304
|
+
void pos_set(uint32_t i, llama_pos p) {
|
|
305
|
+
assert(i < pos.size());
|
|
306
|
+
assert(pos[i] == -1);
|
|
307
|
+
assert(seq[i].none());
|
|
308
|
+
|
|
309
|
+
pos[i] = p;
|
|
310
|
+
|
|
311
|
+
used.insert(i);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// pos[i] = pos[i] + d
|
|
315
|
+
// sets "has_shift" to true
|
|
316
|
+
// note: call only if the cell is not empty
|
|
317
|
+
bool pos_add(uint32_t i, llama_pos d) {
|
|
318
|
+
assert(i < pos.size());
|
|
319
|
+
assert(pos[i] != -1);
|
|
320
|
+
|
|
321
|
+
seq_pos_rm(i);
|
|
322
|
+
|
|
323
|
+
pos[i] += d;
|
|
324
|
+
shift[i] += d;
|
|
325
|
+
|
|
326
|
+
has_shift = true;
|
|
327
|
+
|
|
328
|
+
if (pos[i] < 0) {
|
|
329
|
+
seq[i].reset();
|
|
330
|
+
pos[i] = -1;
|
|
331
|
+
shift[i] = 0;
|
|
332
|
+
|
|
333
|
+
used.erase(i);
|
|
334
|
+
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
seq_pos_add(i);
|
|
339
|
+
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// pos[i] = pos[i] / d
|
|
344
|
+
// sets "has_shift" to true
|
|
345
|
+
// note: call only if the cell is not empty
|
|
346
|
+
void pos_div(uint32_t i, int d) {
|
|
347
|
+
assert(i < pos.size());
|
|
348
|
+
assert(pos[i] != -1);
|
|
349
|
+
|
|
350
|
+
const llama_pos p_old = pos[i];
|
|
351
|
+
|
|
352
|
+
seq_pos_rm(i);
|
|
353
|
+
|
|
354
|
+
pos[i] /= d;
|
|
355
|
+
shift[i] += p_old - pos[i];
|
|
356
|
+
|
|
357
|
+
seq_pos_add(i);
|
|
358
|
+
|
|
359
|
+
has_shift = true;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
private:
|
|
363
|
+
bool has_shift = false;
|
|
364
|
+
|
|
365
|
+
// set of indices of used cells (i.e. pos[i] != -1, allowed to not have any seq_id)
|
|
366
|
+
std::set<uint32_t> used;
|
|
367
|
+
|
|
368
|
+
std::vector<llama_pos> pos;
|
|
369
|
+
|
|
370
|
+
// this array accumulates any applied shifts to the pos array since the last reset_shift() call
|
|
371
|
+
// this is used to queue multiple updates to the pos array, which in the end can be applied in one go:
|
|
372
|
+
//
|
|
373
|
+
// cells.pos_add(x, shift_x);
|
|
374
|
+
// cells.pos_div(y, shift_y);
|
|
375
|
+
// ...
|
|
376
|
+
//
|
|
377
|
+
// if (cells.has_shift()) {
|
|
378
|
+
// for (int i = 0; i < n; ++i) {
|
|
379
|
+
// auto shift_i = cells.get_shift(i);
|
|
380
|
+
// ...
|
|
381
|
+
// }
|
|
382
|
+
// cells.reset_shift();
|
|
383
|
+
// }
|
|
384
|
+
//
|
|
385
|
+
std::vector<llama_pos> shift;
|
|
386
|
+
|
|
387
|
+
using seq_set_t = std::bitset<LLAMA_MAX_SEQ>;
|
|
388
|
+
|
|
389
|
+
// the bitset seq[i] tells us which sequences are currently occupying the i-th cell
|
|
390
|
+
std::vector<seq_set_t> seq;
|
|
391
|
+
|
|
392
|
+
// the set seq_pos[s] tells us which positions are currently present for sequence s
|
|
393
|
+
// this way seq_pos[s].begin() and seq_pos[s].rbegin() give us the min/max positions currently in the cache
|
|
394
|
+
std::set<llama_pos> seq_pos[LLAMA_MAX_SEQ];
|
|
395
|
+
|
|
396
|
+
// helper functions for updating `seq_pos`, once cell at a time:
|
|
397
|
+
|
|
398
|
+
// remove cell i
|
|
399
|
+
void seq_pos_rm(uint32_t i) {
|
|
400
|
+
for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
|
401
|
+
if (seq[i].test(s)) {
|
|
402
|
+
seq_pos[s].erase(pos[i]);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// add cell i
|
|
408
|
+
void seq_pos_add(uint32_t i) {
|
|
409
|
+
for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
|
410
|
+
if (seq[i].test(s)) {
|
|
411
|
+
seq_pos[s].insert(pos[i]);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
};
|