@fugood/llama.node 0.4.7 → 0.6.0
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 +4 -0
- package/bin/darwin/arm64/llama-node.node +0 -0
- package/bin/darwin/x64/llama-node.node +0 -0
- package/bin/linux/arm64/llama-node.node +0 -0
- package/bin/linux/x64/llama-node.node +0 -0
- package/bin/linux-cuda/arm64/llama-node.node +0 -0
- package/bin/linux-cuda/x64/llama-node.node +0 -0
- package/bin/linux-vulkan/arm64/llama-node.node +0 -0
- package/bin/linux-vulkan/x64/llama-node.node +0 -0
- package/lib/binding.ts +66 -6
- package/lib/index.js +59 -17
- package/lib/index.ts +74 -23
- package/package.json +1 -1
- package/src/DecodeAudioTokenWorker.cpp +40 -0
- package/src/DecodeAudioTokenWorker.h +22 -0
- package/src/EmbeddingWorker.cpp +7 -5
- package/src/LlamaCompletionWorker.cpp +68 -54
- package/src/LlamaCompletionWorker.h +7 -8
- package/src/LlamaContext.cpp +551 -235
- package/src/LlamaContext.h +26 -4
- package/src/LoadSessionWorker.cpp +4 -2
- package/src/SaveSessionWorker.cpp +10 -6
- package/src/TokenizeWorker.cpp +23 -14
- package/src/TokenizeWorker.h +2 -2
- package/src/addons.cc +8 -11
- package/src/common.hpp +129 -126
- package/src/llama.cpp/.github/workflows/build.yml +2 -2
- package/src/llama.cpp/.github/workflows/release.yml +152 -129
- package/src/llama.cpp/.github/workflows/winget.yml +42 -0
- package/src/llama.cpp/common/arg.cpp +14 -13
- package/src/llama.cpp/common/common.cpp +4 -75
- package/src/llama.cpp/common/common.h +7 -12
- package/src/llama.cpp/examples/lookahead/lookahead.cpp +0 -13
- package/src/llama.cpp/examples/lookup/lookup.cpp +0 -11
- package/src/llama.cpp/examples/parallel/parallel.cpp +0 -9
- package/src/llama.cpp/examples/retrieval/retrieval.cpp +6 -6
- package/src/llama.cpp/examples/simple/simple.cpp +1 -1
- package/src/llama.cpp/examples/simple-chat/simple-chat.cpp +2 -2
- package/src/llama.cpp/examples/sycl/run-llama2.sh +4 -4
- package/src/llama.cpp/examples/sycl/run-llama3.sh +28 -0
- package/src/llama.cpp/examples/sycl/win-run-llama2.bat +1 -1
- package/src/llama.cpp/examples/sycl/win-run-llama3.bat +9 -0
- package/src/llama.cpp/ggml/include/ggml-opt.h +2 -0
- package/src/llama.cpp/ggml/include/ggml.h +11 -0
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +274 -0
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +27 -0
- package/src/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +18 -2
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +1 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +107 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.h +16 -0
- package/src/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +8 -2
- package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +315 -155
- package/src/llama.cpp/ggml/src/ggml-opt.cpp +5 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +43 -12
- package/src/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +171 -112
- package/src/llama.cpp/ggml/src/ggml.c +64 -18
- package/src/llama.cpp/include/llama.h +24 -124
- package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf.txt +5 -1
- package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf_update.txt +5 -1
- package/src/llama.cpp/requirements/requirements-convert_lora_to_gguf.txt +2 -0
- package/src/llama.cpp/src/llama-batch.cpp +3 -1
- package/src/llama.cpp/src/llama-context.cpp +60 -110
- package/src/llama.cpp/src/llama-graph.cpp +137 -233
- package/src/llama.cpp/src/llama-graph.h +49 -7
- package/src/llama.cpp/src/llama-hparams.cpp +17 -1
- package/src/llama.cpp/src/llama-hparams.h +34 -5
- package/src/llama.cpp/src/llama-kv-cache.cpp +654 -321
- package/src/llama.cpp/src/llama-kv-cache.h +201 -85
- package/src/llama.cpp/src/llama-memory.h +3 -2
- package/src/llama.cpp/src/llama-model.cpp +273 -94
- package/src/llama.cpp/src/llama-model.h +4 -1
- package/src/llama.cpp/tests/test-arg-parser.cpp +1 -1
- package/src/llama.cpp/tools/llama-bench/llama-bench.cpp +1 -0
- package/src/llama.cpp/tools/mtmd/CMakeLists.txt +13 -2
- package/src/llama.cpp/tools/mtmd/clip-impl.h +108 -11
- package/src/llama.cpp/tools/mtmd/clip.cpp +466 -88
- package/src/llama.cpp/tools/mtmd/clip.h +6 -4
- package/src/llama.cpp/tools/mtmd/miniaudio.h +93468 -0
- package/src/llama.cpp/tools/mtmd/mtmd-audio.cpp +855 -0
- package/src/llama.cpp/tools/mtmd/mtmd-audio.h +62 -0
- package/src/llama.cpp/tools/mtmd/mtmd-cli.cpp +21 -14
- package/src/llama.cpp/tools/mtmd/mtmd-helper.cpp +36 -49
- package/src/llama.cpp/tools/mtmd/mtmd.cpp +362 -98
- package/src/llama.cpp/tools/mtmd/mtmd.h +52 -21
- package/src/llama.cpp/tools/run/run.cpp +2 -2
- package/src/llama.cpp/tools/server/server.cpp +158 -47
- package/src/llama.cpp/tools/server/utils.hpp +71 -43
- package/src/llama.cpp/tools/tts/tts.cpp +4 -2
- package/src/tts_utils.cpp +342 -0
- package/src/tts_utils.h +62 -0
- package/bin/win32/arm64/llama-node.node +0 -0
- package/bin/win32/arm64/node.lib +0 -0
- package/bin/win32/x64/llama-node.node +0 -0
- package/bin/win32/x64/node.lib +0 -0
- package/bin/win32-vulkan/arm64/llama-node.node +0 -0
- package/bin/win32-vulkan/arm64/node.lib +0 -0
- package/bin/win32-vulkan/x64/llama-node.node +0 -0
- package/bin/win32-vulkan/x64/node.lib +0 -0
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
#include "ggml-cpp.h"
|
|
9
9
|
|
|
10
10
|
#include <set>
|
|
11
|
+
#include <unordered_map>
|
|
11
12
|
#include <vector>
|
|
12
13
|
|
|
13
14
|
struct llama_cparams;
|
|
@@ -40,6 +41,9 @@ struct llama_kv_cache : public llama_memory_i {
|
|
|
40
41
|
// batch processing
|
|
41
42
|
//
|
|
42
43
|
|
|
44
|
+
// =============================================================================================================
|
|
45
|
+
// TODO: refactor and simplify this
|
|
46
|
+
|
|
43
47
|
virtual llama_sbatch sbatch_init(const llama_batch & batch, bool logits_all) = 0;
|
|
44
48
|
|
|
45
49
|
// different KV caches require different batch splitting strategies
|
|
@@ -48,11 +52,10 @@ struct llama_kv_cache : public llama_memory_i {
|
|
|
48
52
|
// find an empty slot of size "n_tokens" in the cache
|
|
49
53
|
virtual bool find_slot(const llama_ubatch & batch) = 0;
|
|
50
54
|
|
|
55
|
+
// =============================================================================================================
|
|
56
|
+
|
|
51
57
|
// getters
|
|
52
|
-
virtual
|
|
53
|
-
virtual int32_t get_used_cells() const = 0; // TODO: remove, this is too-specific to the unified cache
|
|
54
|
-
virtual llama_pos get_pos_max() const = 0;
|
|
55
|
-
virtual bool get_can_shift() const = 0;
|
|
58
|
+
virtual bool get_can_shift() const = 0;
|
|
56
59
|
|
|
57
60
|
bool get_can_edit() const override { return get_can_shift(); }
|
|
58
61
|
|
|
@@ -87,38 +90,25 @@ private:
|
|
|
87
90
|
// llama_kv_cache_unified
|
|
88
91
|
//
|
|
89
92
|
|
|
90
|
-
// TODO: add notion of max sequences
|
|
91
93
|
class llama_kv_cache_unified : public llama_kv_cache {
|
|
92
94
|
public:
|
|
93
|
-
struct kv_cell {
|
|
94
|
-
llama_pos pos = -1;
|
|
95
|
-
llama_pos delta = 0;
|
|
96
|
-
|
|
97
|
-
std::set<llama_seq_id> seq_id;
|
|
98
|
-
|
|
99
|
-
bool has_seq_id(const llama_seq_id & id) const {
|
|
100
|
-
return seq_id.find(id) != seq_id.end();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
bool is_empty() const {
|
|
104
|
-
return seq_id.empty();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
bool is_same_seq(const kv_cell & other) const {
|
|
108
|
-
return seq_id == other.seq_id;
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
95
|
static uint32_t get_padding(const llama_cparams & cparams);
|
|
113
96
|
|
|
97
|
+
// this callback is used to filter out layers that should not be included in the cache
|
|
98
|
+
using layer_filter_cb = std::function<bool(int32_t il)>;
|
|
99
|
+
|
|
114
100
|
llama_kv_cache_unified(
|
|
115
|
-
const llama_model &
|
|
116
|
-
|
|
117
|
-
ggml_type
|
|
118
|
-
|
|
119
|
-
bool
|
|
120
|
-
|
|
121
|
-
uint32_t
|
|
101
|
+
const llama_model & model,
|
|
102
|
+
layer_filter_cb && filter,
|
|
103
|
+
ggml_type type_k,
|
|
104
|
+
ggml_type type_v,
|
|
105
|
+
bool v_trans,
|
|
106
|
+
bool offload,
|
|
107
|
+
uint32_t kv_size,
|
|
108
|
+
uint32_t n_seq_max,
|
|
109
|
+
uint32_t n_pad,
|
|
110
|
+
uint32_t n_swa,
|
|
111
|
+
llama_swa_type swa_type);
|
|
122
112
|
|
|
123
113
|
~llama_kv_cache_unified() = default;
|
|
124
114
|
|
|
@@ -130,10 +120,11 @@ public:
|
|
|
130
120
|
|
|
131
121
|
bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
|
|
132
122
|
void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
|
|
133
|
-
void seq_keep(llama_seq_id seq_id)
|
|
123
|
+
void seq_keep(llama_seq_id seq_id) override;
|
|
134
124
|
void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos delta) override;
|
|
135
125
|
void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override;
|
|
136
126
|
|
|
127
|
+
llama_pos seq_pos_min(llama_seq_id seq_id) const override;
|
|
137
128
|
llama_pos seq_pos_max(llama_seq_id seq_id) const override;
|
|
138
129
|
|
|
139
130
|
//
|
|
@@ -150,7 +141,6 @@ public:
|
|
|
150
141
|
void set_full() override;
|
|
151
142
|
|
|
152
143
|
llama_sbatch sbatch_init(const llama_batch & batch, bool logits_all) override;
|
|
153
|
-
|
|
154
144
|
llama_ubatch ubatch_next(llama_sbatch & sbatch, uint32_t n_ubatch, bool embd_pooled) const override;
|
|
155
145
|
|
|
156
146
|
// updates the cache head
|
|
@@ -158,50 +148,106 @@ public:
|
|
|
158
148
|
// to the first cell of the slot.
|
|
159
149
|
bool find_slot(const llama_ubatch & batch) override;
|
|
160
150
|
|
|
161
|
-
int32_t get_n_tokens() const override;
|
|
162
|
-
int32_t get_used_cells() const override;
|
|
163
|
-
|
|
164
|
-
// TODO: better data structures to reduce the cost of this operation
|
|
165
|
-
llama_pos get_pos_max() const override;
|
|
166
|
-
|
|
167
151
|
bool get_can_shift() const override;
|
|
168
152
|
|
|
169
153
|
// state write/load
|
|
170
154
|
|
|
171
155
|
void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const override;
|
|
172
|
-
void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1)
|
|
156
|
+
void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) override;
|
|
173
157
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
158
|
+
//
|
|
159
|
+
// llama_kv_cache_unified specific API
|
|
160
|
+
//
|
|
177
161
|
|
|
178
|
-
|
|
179
|
-
uint32_t
|
|
162
|
+
uint32_t get_n() const;
|
|
163
|
+
uint32_t get_size() const;
|
|
180
164
|
|
|
181
|
-
|
|
165
|
+
// get views of the current state of the cache
|
|
166
|
+
ggml_tensor * get_k(ggml_context * ctx, int32_t il) const;
|
|
167
|
+
ggml_tensor * get_v(ggml_context * ctx, int32_t il) const;
|
|
182
168
|
|
|
183
|
-
|
|
184
|
-
|
|
169
|
+
// store k_cur and v_cur in the cache based on the current head location
|
|
170
|
+
ggml_tensor * cpy_k(ggml_context * ctx, ggml_tensor * k_cur, int32_t il) const;
|
|
171
|
+
ggml_tensor * cpy_v(ggml_context * ctx, ggml_tensor * v_cur, int32_t il) const;
|
|
172
|
+
|
|
173
|
+
void prune_swa(llama_seq_id seq_id, llama_pos pmin, llama_pos pmax);
|
|
174
|
+
|
|
175
|
+
void set_input_kq_mask (ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const;
|
|
176
|
+
void set_input_k_shift (ggml_tensor * dst) const;
|
|
177
|
+
void set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const;
|
|
185
178
|
|
|
186
179
|
private:
|
|
187
180
|
const llama_model & model;
|
|
188
181
|
const llama_hparams & hparams;
|
|
189
182
|
|
|
183
|
+
struct kv_cell {
|
|
184
|
+
llama_pos pos = -1;
|
|
185
|
+
llama_pos delta = 0;
|
|
186
|
+
|
|
187
|
+
// TODO: replace with bitset uint64_t
|
|
188
|
+
std::set<llama_seq_id> seq_id;
|
|
189
|
+
|
|
190
|
+
bool has_seq_id(const llama_seq_id & id) const {
|
|
191
|
+
return seq_id.find(id) != seq_id.end();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
bool is_empty() const {
|
|
195
|
+
return seq_id.empty();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
bool is_same_seq(const kv_cell & other) const {
|
|
199
|
+
return seq_id == other.seq_id;
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
struct kv_layer {
|
|
204
|
+
// layer index in the model
|
|
205
|
+
// note: can be different from the layer index in the KV cache
|
|
206
|
+
uint32_t il;
|
|
207
|
+
|
|
208
|
+
ggml_tensor * k;
|
|
209
|
+
ggml_tensor * v;
|
|
210
|
+
};
|
|
211
|
+
|
|
190
212
|
bool has_shift = false;
|
|
191
213
|
bool do_defrag = false;
|
|
192
|
-
|
|
193
214
|
bool v_trans = true; // the value tensor is transposed
|
|
194
|
-
|
|
215
|
+
|
|
216
|
+
uint32_t head = 0; // the location where the batch will be placed in the cache (see find_slot())
|
|
217
|
+
uint32_t size = 0; // total number of cells, shared across all sequences
|
|
218
|
+
uint32_t used = 0; // used cells (i.e. at least one seq_id) (TODO: add `struct kv_cells` and keep track automaticallt)
|
|
219
|
+
|
|
220
|
+
// computed before each graph build
|
|
221
|
+
uint32_t n = 0;
|
|
222
|
+
|
|
223
|
+
const uint32_t n_seq_max = 1;
|
|
195
224
|
|
|
196
225
|
// required padding
|
|
197
|
-
uint32_t
|
|
226
|
+
const uint32_t n_pad = 1;
|
|
227
|
+
|
|
228
|
+
// SWA
|
|
229
|
+
const uint32_t n_swa = 0;
|
|
198
230
|
|
|
199
|
-
|
|
200
|
-
ggml_type type_v = GGML_TYPE_F16;
|
|
231
|
+
const llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE;
|
|
201
232
|
|
|
202
233
|
std::vector<ggml_context_ptr> ctxs;
|
|
203
234
|
std::vector<ggml_backend_buffer_ptr> bufs;
|
|
204
235
|
|
|
236
|
+
std::vector<kv_cell> cells; // TODO: replace with `struct kv_cells`
|
|
237
|
+
std::vector<kv_layer> layers;
|
|
238
|
+
|
|
239
|
+
// model layer id -> KV cache layer id
|
|
240
|
+
std::unordered_map<int32_t, int32_t> map_layer_ids;
|
|
241
|
+
|
|
242
|
+
// recovery information used to restore the KV cells to their original state in case of a failure
|
|
243
|
+
struct {
|
|
244
|
+
void clear() {
|
|
245
|
+
cells.clear();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
std::unordered_map<uint32_t, kv_cell> cells;
|
|
249
|
+
} recovery;
|
|
250
|
+
|
|
205
251
|
// defrag
|
|
206
252
|
struct {
|
|
207
253
|
std::vector<uint32_t> ids;
|
|
@@ -210,17 +256,6 @@ private:
|
|
|
210
256
|
// return true if cells have been moved
|
|
211
257
|
bool defrag_prepare(int32_t n_max_nodes);
|
|
212
258
|
|
|
213
|
-
// commit/restore cache
|
|
214
|
-
struct slot_range {
|
|
215
|
-
uint32_t c0 = 0; // note: these are cell indices, not sequence positions
|
|
216
|
-
uint32_t c1 = 0;
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
// pending cell updates that are not yet committed
|
|
220
|
-
struct {
|
|
221
|
-
std::vector<slot_range> ranges;
|
|
222
|
-
} pending;
|
|
223
|
-
|
|
224
259
|
// find how many cells are currently in use
|
|
225
260
|
uint32_t cell_max() const;
|
|
226
261
|
|
|
@@ -229,6 +264,8 @@ private:
|
|
|
229
264
|
size_t size_k_bytes() const;
|
|
230
265
|
size_t size_v_bytes() const;
|
|
231
266
|
|
|
267
|
+
bool is_masked_swa(llama_pos p0, llama_pos p1) const;
|
|
268
|
+
|
|
232
269
|
ggml_tensor * build_rope_shift(
|
|
233
270
|
const llama_cparams & cparams,
|
|
234
271
|
ggml_context * ctx,
|
|
@@ -255,6 +292,100 @@ private:
|
|
|
255
292
|
bool state_read_data(llama_io_read_i & io, uint32_t cell_count);
|
|
256
293
|
};
|
|
257
294
|
|
|
295
|
+
//
|
|
296
|
+
// llama_kv_cache_unified_iswa
|
|
297
|
+
//
|
|
298
|
+
|
|
299
|
+
// utilizes two instances of llama_kv_cache_unified
|
|
300
|
+
// the first instance is for the non-SWA layers of the model and the second instance is for the SWA layers
|
|
301
|
+
// upon successful commit, the SWA cache removes old tokens outside the n_swa window
|
|
302
|
+
|
|
303
|
+
class llama_kv_cache_unified_iswa : public llama_kv_cache {
|
|
304
|
+
public:
|
|
305
|
+
llama_kv_cache_unified_iswa(
|
|
306
|
+
const llama_model & model,
|
|
307
|
+
ggml_type type_k,
|
|
308
|
+
ggml_type type_v,
|
|
309
|
+
bool v_trans,
|
|
310
|
+
bool offload,
|
|
311
|
+
bool swa_full,
|
|
312
|
+
uint32_t kv_size,
|
|
313
|
+
uint32_t n_seq_max,
|
|
314
|
+
uint32_t n_batch,
|
|
315
|
+
uint32_t n_pad);
|
|
316
|
+
|
|
317
|
+
~llama_kv_cache_unified_iswa() = default;
|
|
318
|
+
|
|
319
|
+
//
|
|
320
|
+
// llama_memory_i
|
|
321
|
+
//
|
|
322
|
+
|
|
323
|
+
void clear() override;
|
|
324
|
+
|
|
325
|
+
bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
|
|
326
|
+
void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
|
|
327
|
+
void seq_keep(llama_seq_id seq_id) override;
|
|
328
|
+
void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos delta) override;
|
|
329
|
+
void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override;
|
|
330
|
+
|
|
331
|
+
llama_pos seq_pos_min(llama_seq_id seq_id) const override;
|
|
332
|
+
llama_pos seq_pos_max(llama_seq_id seq_id) const override;
|
|
333
|
+
|
|
334
|
+
//
|
|
335
|
+
// llama_kv_cache
|
|
336
|
+
//
|
|
337
|
+
|
|
338
|
+
void restore() override;
|
|
339
|
+
void commit() override;
|
|
340
|
+
|
|
341
|
+
bool update(llama_context & ctx) override;
|
|
342
|
+
|
|
343
|
+
void defrag_sched(float thold) override;
|
|
344
|
+
|
|
345
|
+
void set_full() override;
|
|
346
|
+
|
|
347
|
+
llama_sbatch sbatch_init(const llama_batch & batch, bool logits_all) override;
|
|
348
|
+
llama_ubatch ubatch_next(llama_sbatch & sbatch, uint32_t n_ubatch, bool embd_pooled) const override;
|
|
349
|
+
|
|
350
|
+
bool find_slot(const llama_ubatch & batch) override;
|
|
351
|
+
|
|
352
|
+
bool get_can_shift() const override;
|
|
353
|
+
|
|
354
|
+
// state write/load
|
|
355
|
+
|
|
356
|
+
void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const override;
|
|
357
|
+
void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) override;
|
|
358
|
+
|
|
359
|
+
//
|
|
360
|
+
// llama_kv_cache_unified_iswa specific API
|
|
361
|
+
//
|
|
362
|
+
|
|
363
|
+
llama_kv_cache_unified * get_kv_base() const;
|
|
364
|
+
llama_kv_cache_unified * get_kv_swa () const;
|
|
365
|
+
|
|
366
|
+
private:
|
|
367
|
+
const llama_hparams & hparams;
|
|
368
|
+
|
|
369
|
+
bool do_prune = true;
|
|
370
|
+
|
|
371
|
+
struct {
|
|
372
|
+
struct entry {
|
|
373
|
+
llama_pos pmin;
|
|
374
|
+
llama_pos pmax;
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
void clear() {
|
|
378
|
+
pos.clear();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// used to perform SWA pruning of old tokens
|
|
382
|
+
std::unordered_map<llama_seq_id, entry> pos;
|
|
383
|
+
} pending;
|
|
384
|
+
|
|
385
|
+
std::unique_ptr<llama_kv_cache_unified> kv_base;
|
|
386
|
+
std::unique_ptr<llama_kv_cache_unified> kv_swa;
|
|
387
|
+
};
|
|
388
|
+
|
|
258
389
|
//
|
|
259
390
|
// llama_kv_cache_recurrent
|
|
260
391
|
//
|
|
@@ -286,7 +417,8 @@ public:
|
|
|
286
417
|
ggml_type type_k,
|
|
287
418
|
ggml_type type_v,
|
|
288
419
|
bool offload,
|
|
289
|
-
uint32_t kv_size
|
|
420
|
+
uint32_t kv_size,
|
|
421
|
+
uint32_t n_seq_max);
|
|
290
422
|
|
|
291
423
|
~llama_kv_cache_recurrent() = default;
|
|
292
424
|
|
|
@@ -298,10 +430,11 @@ public:
|
|
|
298
430
|
|
|
299
431
|
bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
|
|
300
432
|
void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
|
|
301
|
-
void seq_keep(llama_seq_id seq_id)
|
|
433
|
+
void seq_keep(llama_seq_id seq_id) override;
|
|
302
434
|
void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos delta) override;
|
|
303
435
|
void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override;
|
|
304
436
|
|
|
437
|
+
llama_pos seq_pos_min(llama_seq_id seq_id) const override;
|
|
305
438
|
llama_pos seq_pos_max(llama_seq_id seq_id) const override;
|
|
306
439
|
|
|
307
440
|
//
|
|
@@ -311,24 +444,17 @@ public:
|
|
|
311
444
|
void restore() override;
|
|
312
445
|
void commit() override;
|
|
313
446
|
|
|
314
|
-
bool update(llama_context &
|
|
447
|
+
bool update(llama_context & ctx) override;
|
|
315
448
|
|
|
316
449
|
void defrag_sched(float thold) override;
|
|
317
450
|
|
|
318
451
|
void set_full() override;
|
|
319
452
|
|
|
320
453
|
llama_sbatch sbatch_init(const llama_batch & batch, bool logits_all) override;
|
|
321
|
-
|
|
322
454
|
llama_ubatch ubatch_next(llama_sbatch & sbatch, uint32_t n_ubatch, bool embd_pooled) const override;
|
|
323
455
|
|
|
324
456
|
bool find_slot(const llama_ubatch & batch) override;
|
|
325
457
|
|
|
326
|
-
int32_t get_n_tokens() const override;
|
|
327
|
-
int32_t get_used_cells() const override;
|
|
328
|
-
|
|
329
|
-
// TODO: better data structures to reduce the cost of this operation
|
|
330
|
-
llama_pos get_pos_max() const override;
|
|
331
|
-
|
|
332
458
|
bool get_can_shift() const override;
|
|
333
459
|
|
|
334
460
|
// TODO: temporary methods - they are not really const as they do const_cast<>, fix this
|
|
@@ -368,8 +494,7 @@ private:
|
|
|
368
494
|
std::vector<slot_range> ranges;
|
|
369
495
|
} pending;
|
|
370
496
|
|
|
371
|
-
|
|
372
|
-
ggml_type type_v = GGML_TYPE_F16;
|
|
497
|
+
const uint32_t n_seq_max = 1;
|
|
373
498
|
|
|
374
499
|
std::vector<ggml_context_ptr> ctxs;
|
|
375
500
|
std::vector<ggml_backend_buffer_ptr> bufs;
|
|
@@ -388,12 +513,3 @@ private:
|
|
|
388
513
|
bool state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id = -1);
|
|
389
514
|
bool state_read_data(llama_io_read_i & io, uint32_t cell_count);
|
|
390
515
|
};
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
//
|
|
394
|
-
// kv cache view
|
|
395
|
-
//
|
|
396
|
-
|
|
397
|
-
llama_kv_cache_view llama_kv_cache_view_init(const llama_kv_cache & kv, int32_t n_seq_max);
|
|
398
|
-
|
|
399
|
-
void llama_kv_cache_view_update(llama_kv_cache_view * view, const llama_kv_cache * kv);
|
|
@@ -7,8 +7,8 @@ struct llama_memory_params {
|
|
|
7
7
|
ggml_type type_k;
|
|
8
8
|
ggml_type type_v;
|
|
9
9
|
|
|
10
|
-
//
|
|
11
|
-
|
|
10
|
+
// use full-size SWA cache
|
|
11
|
+
bool swa_full;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
// general concept of LLM memory
|
|
@@ -25,6 +25,7 @@ public:
|
|
|
25
25
|
virtual void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos delta) = 0;
|
|
26
26
|
virtual void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) = 0;
|
|
27
27
|
|
|
28
|
+
virtual llama_pos seq_pos_min(llama_seq_id seq_id) const = 0;
|
|
28
29
|
virtual llama_pos seq_pos_max(llama_seq_id seq_id) const = 0;
|
|
29
30
|
|
|
30
31
|
virtual bool get_can_edit() const = 0;
|