cui-llama.rn 1.4.6 → 1.5.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/android/src/main/CMakeLists.txt +9 -2
- package/android/src/main/jni.cpp +52 -34
- package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
- package/cpp/binary-ops.cpp +158 -0
- package/cpp/binary-ops.h +16 -0
- package/cpp/chat.cpp +1769 -1779
- package/cpp/chat.h +9 -1
- package/cpp/common.cpp +20 -522
- package/cpp/common.h +13 -36
- package/cpp/cpu-common.h +72 -0
- package/cpp/ggml-common.h +12 -6
- package/cpp/ggml-cpu-aarch64.cpp +1557 -80
- package/cpp/ggml-cpu-impl.h +2 -21
- package/cpp/ggml-cpu-quants.c +904 -405
- package/cpp/ggml-cpu.c +909 -13237
- package/cpp/ggml-impl.h +50 -23
- package/cpp/ggml-metal-impl.h +77 -3
- package/cpp/ggml-metal.m +794 -580
- package/cpp/ggml.c +92 -3
- package/cpp/ggml.h +29 -5
- package/cpp/gguf.cpp +1 -0
- package/cpp/llama-adapter.cpp +55 -20
- package/cpp/llama-adapter.h +11 -9
- package/cpp/llama-arch.cpp +217 -16
- package/cpp/llama-arch.h +25 -0
- package/cpp/llama-batch.h +2 -2
- package/cpp/llama-chat.cpp +54 -2
- package/cpp/llama-chat.h +3 -0
- package/cpp/llama-context.cpp +2294 -1238
- package/cpp/llama-context.h +214 -77
- package/cpp/llama-cparams.h +1 -0
- package/cpp/llama-graph.cpp +1695 -0
- package/cpp/llama-graph.h +592 -0
- package/cpp/llama-hparams.cpp +8 -0
- package/cpp/llama-hparams.h +17 -0
- package/cpp/llama-io.cpp +15 -0
- package/cpp/llama-io.h +35 -0
- package/cpp/llama-kv-cache.cpp +965 -303
- package/cpp/llama-kv-cache.h +145 -151
- package/cpp/llama-memory.cpp +1 -0
- package/cpp/llama-memory.h +21 -0
- package/cpp/llama-mmap.cpp +1 -1
- package/cpp/llama-model-loader.cpp +10 -5
- package/cpp/llama-model-loader.h +5 -3
- package/cpp/llama-model.cpp +9194 -201
- package/cpp/llama-model.h +40 -1
- package/cpp/llama-sampling.cpp +5 -0
- package/cpp/llama-vocab.cpp +36 -5
- package/cpp/llama.cpp +51 -9984
- package/cpp/llama.h +102 -22
- package/cpp/log.cpp +34 -0
- package/cpp/minja/chat-template.hpp +15 -7
- package/cpp/minja/minja.hpp +120 -94
- package/cpp/ops.cpp +8723 -0
- package/cpp/ops.h +128 -0
- package/cpp/rn-llama.cpp +44 -53
- package/cpp/rn-llama.h +2 -12
- package/cpp/sampling.cpp +3 -0
- package/cpp/sgemm.cpp +533 -88
- package/cpp/simd-mappings.h +888 -0
- package/cpp/speculative.cpp +4 -4
- package/cpp/unary-ops.cpp +186 -0
- package/cpp/unary-ops.h +28 -0
- package/cpp/vec.cpp +258 -0
- package/cpp/vec.h +802 -0
- package/ios/CMakeLists.txt +5 -2
- package/ios/RNLlama.mm +2 -2
- package/ios/RNLlamaContext.mm +40 -24
- package/package.json +1 -1
- package/src/NativeRNLlama.ts +6 -4
- package/src/index.ts +3 -1
- package/cpp/chat-template.hpp +0 -529
- package/cpp/minja.hpp +0 -2915
package/cpp/llama-kv-cache.cpp
CHANGED
@@ -6,86 +6,90 @@
|
|
6
6
|
#include "llama-model.h"
|
7
7
|
|
8
8
|
#include <algorithm>
|
9
|
+
#include <cassert>
|
9
10
|
#include <limits>
|
10
11
|
#include <map>
|
12
|
+
#include <stdexcept>
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
uint32_t llama_kv_cache_get_padding(const struct llama_cparams & cparams) {
|
15
|
-
// the FA kernels require padding to avoid extra runtime boundary checks
|
16
|
-
return cparams.flash_attn ? 256u : 32u;
|
14
|
+
llama_kv_cache_unified::llama_kv_cache_unified(const llama_hparams & hparams, callbacks cbs) : hparams(hparams), cbs(std::move(cbs)) {
|
17
15
|
}
|
18
16
|
|
19
|
-
bool
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
bool offload) {
|
27
|
-
const struct llama_hparams & hparams = model.hparams;
|
28
|
-
|
17
|
+
bool llama_kv_cache_unified::init(
|
18
|
+
const llama_model & model,
|
19
|
+
const llama_cparams & cparams,
|
20
|
+
lm_ggml_type type_k,
|
21
|
+
lm_ggml_type type_v,
|
22
|
+
uint32_t kv_size,
|
23
|
+
bool offload) {
|
29
24
|
const int32_t n_layer = hparams.n_layer;
|
30
25
|
|
31
|
-
|
26
|
+
has_shift = false;
|
32
27
|
|
33
|
-
|
34
|
-
|
35
|
-
|
28
|
+
recurrent = llama_model_is_recurrent(&model);
|
29
|
+
v_trans = !recurrent && !cparams.flash_attn;
|
30
|
+
can_shift = !recurrent && model.arch != LLM_ARCH_DEEPSEEK2; // not supported due to MLA
|
36
31
|
|
37
32
|
LLAMA_LOG_INFO("%s: kv_size = %d, offload = %d, type_k = '%s', type_v = '%s', n_layer = %d, can_shift = %d\n",
|
38
|
-
__func__, kv_size, offload, lm_ggml_type_name(type_k), lm_ggml_type_name(type_v), n_layer,
|
33
|
+
__func__, kv_size, offload, lm_ggml_type_name(type_k), lm_ggml_type_name(type_v), n_layer, can_shift);
|
39
34
|
|
40
|
-
|
41
|
-
|
42
|
-
|
35
|
+
head = 0;
|
36
|
+
size = kv_size;
|
37
|
+
used = 0;
|
43
38
|
|
44
|
-
|
45
|
-
|
39
|
+
this->type_k = type_k;
|
40
|
+
this->type_v = type_v;
|
46
41
|
|
47
|
-
|
48
|
-
|
42
|
+
cells.clear();
|
43
|
+
cells.resize(kv_size);
|
49
44
|
|
50
45
|
// create a context for each buffer type
|
51
46
|
std::map<lm_ggml_backend_buffer_type_t, lm_ggml_context *> ctx_map;
|
52
47
|
auto ctx_for_buft = [&](lm_ggml_backend_buffer_type_t buft) -> lm_ggml_context * {
|
53
48
|
auto it = ctx_map.find(buft);
|
54
49
|
if (it == ctx_map.end()) {
|
55
|
-
|
50
|
+
lm_ggml_init_params params = {
|
56
51
|
/*.mem_size =*/ size_t(2u*n_layer*lm_ggml_tensor_overhead()),
|
57
52
|
/*.mem_buffer =*/ NULL,
|
58
53
|
/*.no_alloc =*/ true,
|
59
54
|
};
|
55
|
+
|
60
56
|
lm_ggml_context * ctx = lm_ggml_init(params);
|
61
57
|
if (!ctx) {
|
62
58
|
return nullptr;
|
63
59
|
}
|
60
|
+
|
64
61
|
ctx_map[buft] = ctx;
|
65
|
-
|
62
|
+
ctxs.emplace_back(ctx);
|
63
|
+
|
66
64
|
return ctx;
|
67
65
|
}
|
66
|
+
|
68
67
|
return it->second;
|
69
68
|
};
|
70
69
|
|
71
|
-
|
72
|
-
|
70
|
+
k_l.reserve(n_layer);
|
71
|
+
v_l.reserve(n_layer);
|
73
72
|
|
74
73
|
for (int i = 0; i < n_layer; i++) {
|
75
74
|
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i) + hparams.n_embd_k_s();
|
76
75
|
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i) + hparams.n_embd_v_s();
|
77
76
|
|
78
|
-
|
77
|
+
const char * dev_name = "CPU";
|
79
78
|
|
80
79
|
lm_ggml_backend_buffer_type_t buft;
|
81
80
|
if (offload) {
|
82
81
|
auto * dev = model.dev_layer(i);
|
83
82
|
buft = lm_ggml_backend_dev_buffer_type(dev);
|
83
|
+
|
84
|
+
dev_name = lm_ggml_backend_dev_name(dev);
|
84
85
|
} else {
|
85
86
|
buft = lm_ggml_backend_cpu_buffer_type();
|
86
87
|
}
|
87
|
-
lm_ggml_context * ctx = ctx_for_buft(buft);
|
88
88
|
|
89
|
+
LLAMA_LOG_DEBUG("%s: layer %3d: n_embd_k_gqa = %d, n_embd_v_gqa = %d, dev = %s\n", __func__,
|
90
|
+
i, n_embd_k_gqa, n_embd_v_gqa, dev_name);
|
91
|
+
|
92
|
+
lm_ggml_context * ctx = ctx_for_buft(buft);
|
89
93
|
if (!ctx) {
|
90
94
|
LLAMA_LOG_ERROR("%s: failed to create ggml context for kv cache\n", __func__);
|
91
95
|
return false;
|
@@ -95,8 +99,8 @@ bool llama_kv_cache_init(
|
|
95
99
|
lm_ggml_tensor * v = lm_ggml_new_tensor_1d(ctx, type_v, n_embd_v_gqa*kv_size);
|
96
100
|
lm_ggml_format_name(k, "cache_k_l%d", i);
|
97
101
|
lm_ggml_format_name(v, "cache_v_l%d", i);
|
98
|
-
|
99
|
-
|
102
|
+
k_l.push_back(k);
|
103
|
+
v_l.push_back(v);
|
100
104
|
}
|
101
105
|
|
102
106
|
// allocate tensors and initialize the buffers to avoid NaNs in the padding
|
@@ -111,20 +115,403 @@ bool llama_kv_cache_init(
|
|
111
115
|
}
|
112
116
|
lm_ggml_backend_buffer_clear(buf, 0);
|
113
117
|
LLAMA_LOG_INFO("%s: %10s KV buffer size = %8.2f MiB\n", __func__, lm_ggml_backend_buffer_name(buf), lm_ggml_backend_buffer_get_size(buf)/1024.0/1024.0);
|
114
|
-
|
118
|
+
bufs.emplace_back(buf);
|
119
|
+
}
|
120
|
+
|
121
|
+
return true;
|
122
|
+
}
|
123
|
+
|
124
|
+
int32_t llama_kv_cache_unified::get_n_tokens() const {
|
125
|
+
int32_t result = 0;
|
126
|
+
|
127
|
+
for (uint32_t i = 0; i < size; i++) {
|
128
|
+
result += cells[i].seq_id.size();
|
129
|
+
}
|
130
|
+
|
131
|
+
return result;
|
132
|
+
}
|
133
|
+
|
134
|
+
int32_t llama_kv_cache_unified::get_used_cells() const {
|
135
|
+
return used;
|
136
|
+
}
|
137
|
+
|
138
|
+
size_t llama_kv_cache_unified::total_size() const {
|
139
|
+
size_t size = 0;
|
140
|
+
for (const auto & buf : bufs) {
|
141
|
+
size += lm_ggml_backend_buffer_get_size(buf.get());
|
142
|
+
}
|
143
|
+
|
144
|
+
return size;
|
145
|
+
}
|
146
|
+
|
147
|
+
llama_pos llama_kv_cache_unified::pos_max() const {
|
148
|
+
llama_pos pos_max = -1;
|
149
|
+
for (const auto & cell : cells) {
|
150
|
+
pos_max = std::max(pos_max, cell.pos);
|
151
|
+
}
|
152
|
+
|
153
|
+
return pos_max;
|
154
|
+
}
|
155
|
+
|
156
|
+
void llama_kv_cache_unified::clear() {
|
157
|
+
for (int32_t i = 0; i < (int32_t) size; ++i) {
|
158
|
+
cells[i].pos = -1;
|
159
|
+
cells[i].seq_id.clear();
|
160
|
+
cells[i].src = -1;
|
161
|
+
cells[i].tail = -1;
|
162
|
+
}
|
163
|
+
head = 0;
|
164
|
+
used = 0;
|
165
|
+
|
166
|
+
for (auto & buf : bufs) {
|
167
|
+
lm_ggml_backend_buffer_clear(buf.get(), 0);
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
bool llama_kv_cache_unified::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
|
172
|
+
uint32_t new_head = size;
|
173
|
+
|
174
|
+
if (p0 < 0) {
|
175
|
+
p0 = 0;
|
176
|
+
}
|
177
|
+
|
178
|
+
if (p1 < 0) {
|
179
|
+
p1 = std::numeric_limits<llama_pos>::max();
|
180
|
+
}
|
181
|
+
|
182
|
+
// models like Mamba or RWKV can't have a state partially erased
|
183
|
+
if (recurrent) {
|
184
|
+
if (seq_id >= (int64_t) size) {
|
185
|
+
// could be fatal
|
186
|
+
return false;
|
187
|
+
}
|
188
|
+
if (0 <= seq_id) {
|
189
|
+
int32_t & tail_id = cells[seq_id].tail;
|
190
|
+
if (tail_id >= 0) {
|
191
|
+
const llama_kv_cell & cell = cells[tail_id];
|
192
|
+
// partial intersection is invalid
|
193
|
+
if ((0 < p0 && p0 <= cell.pos) || (0 < p1 && p1 <= cell.pos)) {
|
194
|
+
return false;
|
195
|
+
}
|
196
|
+
// invalidate tails which will be cleared
|
197
|
+
if (p0 <= cell.pos && cell.pos < p1) {
|
198
|
+
tail_id = -1;
|
199
|
+
}
|
200
|
+
}
|
201
|
+
} else {
|
202
|
+
// seq_id is negative, then the range should include everything or nothing
|
203
|
+
if (p0 != p1 && (p0 != 0 || p1 != std::numeric_limits<llama_pos>::max())) {
|
204
|
+
return false;
|
205
|
+
}
|
206
|
+
}
|
207
|
+
|
208
|
+
return true;
|
209
|
+
}
|
210
|
+
|
211
|
+
for (uint32_t i = 0; i < size; ++i) {
|
212
|
+
if (cells[i].pos >= p0 && cells[i].pos < p1) {
|
213
|
+
if (seq_id < 0) {
|
214
|
+
cells[i].seq_id.clear();
|
215
|
+
} else if (cells[i].has_seq_id(seq_id)) {
|
216
|
+
cells[i].seq_id.erase(seq_id);
|
217
|
+
} else {
|
218
|
+
continue;
|
219
|
+
}
|
220
|
+
if (cells[i].is_empty()) {
|
221
|
+
// keep count of the number of used cells
|
222
|
+
if (cells[i].pos >= 0) {
|
223
|
+
used--;
|
224
|
+
}
|
225
|
+
|
226
|
+
cells[i].pos = -1;
|
227
|
+
cells[i].src = -1;
|
228
|
+
|
229
|
+
if (new_head == size) {
|
230
|
+
new_head = i;
|
231
|
+
}
|
232
|
+
}
|
233
|
+
}
|
234
|
+
}
|
235
|
+
|
236
|
+
// If we freed up a slot, set head to it so searching can start there.
|
237
|
+
if (new_head != size && new_head < head) {
|
238
|
+
head = new_head;
|
115
239
|
}
|
116
240
|
|
117
241
|
return true;
|
118
242
|
}
|
119
243
|
|
120
|
-
|
121
|
-
|
122
|
-
|
244
|
+
void llama_kv_cache_unified::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
|
245
|
+
if (seq_id_src == seq_id_dst) {
|
246
|
+
return;
|
247
|
+
}
|
248
|
+
|
249
|
+
if (p0 < 0) {
|
250
|
+
p0 = 0;
|
251
|
+
}
|
252
|
+
|
253
|
+
if (p1 < 0) {
|
254
|
+
p1 = std::numeric_limits<llama_pos>::max();
|
255
|
+
}
|
256
|
+
|
257
|
+
if (recurrent) {
|
258
|
+
if ((uint32_t) seq_id_dst < size && (uint32_t) seq_id_src < size) {
|
259
|
+
llama_kv_cell & tail_src = cells[seq_id_src];
|
260
|
+
llama_kv_cell & tail_dst = cells[seq_id_dst];
|
261
|
+
if (tail_dst.tail >= 0) {
|
262
|
+
// clear destination seq_id if it wasn't empty
|
263
|
+
llama_kv_cell & cell_dst = cells[tail_dst.tail];
|
264
|
+
|
265
|
+
cell_dst.seq_id.erase(seq_id_dst);
|
266
|
+
tail_dst.tail = -1;
|
267
|
+
if (cell_dst.seq_id.empty()) {
|
268
|
+
cell_dst.pos = -1;
|
269
|
+
cell_dst.delta = -1;
|
270
|
+
cell_dst.src = -1;
|
271
|
+
used -= 1;
|
272
|
+
}
|
273
|
+
}
|
274
|
+
if (tail_src.tail >= 0) {
|
275
|
+
llama_kv_cell & cell_src = cells[tail_src.tail];
|
276
|
+
|
277
|
+
cell_src.seq_id.insert(seq_id_dst);
|
278
|
+
tail_dst.tail = tail_src.tail;
|
279
|
+
}
|
280
|
+
}
|
281
|
+
|
282
|
+
return;
|
283
|
+
}
|
284
|
+
|
285
|
+
// otherwise, this is the KV of a Transformer-like model
|
286
|
+
head = 0;
|
287
|
+
|
288
|
+
for (uint32_t i = 0; i < size; ++i) {
|
289
|
+
if (cells[i].has_seq_id(seq_id_src) && cells[i].pos >= p0 && cells[i].pos < p1) {
|
290
|
+
cells[i].seq_id.insert(seq_id_dst);
|
291
|
+
}
|
292
|
+
}
|
293
|
+
}
|
294
|
+
|
295
|
+
void llama_kv_cache_unified::seq_keep(llama_seq_id seq_id) {
|
296
|
+
uint32_t new_head = size;
|
297
|
+
|
298
|
+
for (uint32_t i = 0; i < size; ++i) {
|
299
|
+
if (recurrent && (llama_seq_id) i != seq_id) {
|
300
|
+
cells[i].tail = -1;
|
301
|
+
}
|
302
|
+
|
303
|
+
if (!cells[i].has_seq_id(seq_id)) {
|
304
|
+
if (cells[i].pos >= 0) {
|
305
|
+
used--;
|
306
|
+
}
|
307
|
+
|
308
|
+
cells[i].pos = -1;
|
309
|
+
cells[i].src = -1;
|
310
|
+
cells[i].seq_id.clear();
|
311
|
+
|
312
|
+
if (new_head == size){
|
313
|
+
new_head = i;
|
314
|
+
}
|
315
|
+
} else {
|
316
|
+
cells[i].seq_id.clear();
|
317
|
+
cells[i].seq_id.insert(seq_id);
|
318
|
+
}
|
319
|
+
}
|
320
|
+
|
321
|
+
// If we freed up a slot, set head to it so searching can start there.
|
322
|
+
if (new_head != size && new_head < head) {
|
323
|
+
head = new_head;
|
324
|
+
}
|
325
|
+
}
|
326
|
+
|
327
|
+
void llama_kv_cache_unified::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos delta) {
|
328
|
+
if (delta == 0) {
|
329
|
+
return;
|
330
|
+
}
|
331
|
+
|
332
|
+
uint32_t new_head = size;
|
333
|
+
|
334
|
+
if (p0 < 0) {
|
335
|
+
p0 = 0;
|
336
|
+
}
|
337
|
+
|
338
|
+
if (p1 < 0) {
|
339
|
+
p1 = std::numeric_limits<llama_pos>::max();
|
340
|
+
}
|
341
|
+
|
342
|
+
// If there is no range then return early to avoid looping over the
|
343
|
+
if (p0 == p1) {
|
344
|
+
return;
|
345
|
+
}
|
346
|
+
|
347
|
+
if (recurrent) {
|
348
|
+
// for Mamba-like or RWKV models, only the pos needs to be shifted
|
349
|
+
if (0 <= seq_id && seq_id < (int64_t) size) {
|
350
|
+
const int32_t tail_id = cells[seq_id].tail;
|
351
|
+
if (tail_id >= 0) {
|
352
|
+
llama_kv_cell & cell = cells[tail_id];
|
353
|
+
if (cell.has_seq_id(seq_id) && p0 <= cell.pos && cell.pos < p1) {
|
354
|
+
cell.pos += delta;
|
355
|
+
}
|
356
|
+
}
|
357
|
+
}
|
358
|
+
return;
|
359
|
+
}
|
360
|
+
|
361
|
+
for (uint32_t i = 0; i < size; ++i) {
|
362
|
+
if (cells[i].has_seq_id(seq_id) && cells[i].pos >= p0 && cells[i].pos < p1) {
|
363
|
+
has_shift = true;
|
364
|
+
cells[i].pos += delta;
|
365
|
+
cells[i].delta += delta;
|
366
|
+
|
367
|
+
if (cells[i].pos < 0) {
|
368
|
+
if (!cells[i].is_empty()) {
|
369
|
+
used--;
|
370
|
+
}
|
371
|
+
cells[i].pos = -1;
|
372
|
+
cells[i].seq_id.clear();
|
373
|
+
if (new_head == size) {
|
374
|
+
new_head = i;
|
375
|
+
}
|
376
|
+
}
|
377
|
+
}
|
378
|
+
}
|
379
|
+
|
380
|
+
// If we freed up a slot, set head to it so searching can start there.
|
381
|
+
// Otherwise we just start the next search from the beginning.
|
382
|
+
head = new_head != size ? new_head : 0;
|
383
|
+
}
|
384
|
+
|
385
|
+
void llama_kv_cache_unified::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
|
386
|
+
if (d == 1) {
|
387
|
+
return;
|
388
|
+
}
|
389
|
+
|
390
|
+
if (p0 < 0) {
|
391
|
+
p0 = 0;
|
392
|
+
}
|
393
|
+
|
394
|
+
if (p1 < 0) {
|
395
|
+
p1 = std::numeric_limits<llama_pos>::max();
|
396
|
+
}
|
397
|
+
|
398
|
+
// If there is no range then return early to avoid looping over the cache.
|
399
|
+
if (p0 == p1) {
|
400
|
+
return;
|
401
|
+
}
|
402
|
+
|
403
|
+
if (recurrent) {
|
404
|
+
// for Mamba-like or RWKV models, only the pos needs to be changed
|
405
|
+
if (0 <= seq_id && seq_id < (int64_t) size) {
|
406
|
+
const int32_t tail_id = cells[seq_id].tail;
|
407
|
+
if (tail_id >= 0) {
|
408
|
+
llama_kv_cell & cell = cells[tail_id];
|
409
|
+
if (cell.has_seq_id(seq_id) && p0 <= cell.pos && cell.pos < p1) {
|
410
|
+
cell.pos /= d;
|
411
|
+
}
|
412
|
+
}
|
413
|
+
}
|
414
|
+
|
415
|
+
return;
|
416
|
+
}
|
417
|
+
|
418
|
+
for (uint32_t i = 0; i < size; ++i) {
|
419
|
+
if (cells[i].has_seq_id(seq_id) && cells[i].pos >= p0 && cells[i].pos < p1) {
|
420
|
+
has_shift = true;
|
421
|
+
|
422
|
+
{
|
423
|
+
llama_pos p_old = cells[i].pos;
|
424
|
+
cells[i].pos /= d;
|
425
|
+
cells[i].delta += cells[i].pos - p_old;
|
426
|
+
}
|
427
|
+
}
|
428
|
+
}
|
429
|
+
}
|
430
|
+
|
431
|
+
llama_pos llama_kv_cache_unified::seq_pos_max(llama_seq_id seq_id) const {
|
432
|
+
llama_pos result = 0;
|
433
|
+
|
434
|
+
for (uint32_t i = 0; i < size; ++i) {
|
435
|
+
if (cells[i].has_seq_id(seq_id)) {
|
436
|
+
result = std::max(result, cells[i].pos);
|
437
|
+
}
|
438
|
+
}
|
439
|
+
|
440
|
+
return result;
|
441
|
+
}
|
442
|
+
|
443
|
+
void llama_kv_cache_unified::defrag() {
|
444
|
+
if (!recurrent) {
|
445
|
+
do_defrag = true;
|
446
|
+
}
|
447
|
+
}
|
448
|
+
|
449
|
+
void llama_kv_cache_unified::restore() {
|
450
|
+
if (pending.ranges.empty()) {
|
451
|
+
return;
|
452
|
+
}
|
453
|
+
|
454
|
+
// TODO: tmp - move to llama_kv_cache_recurrent
|
455
|
+
if (recurrent) {
|
456
|
+
seq_rm(-1, -1, -1);
|
457
|
+
return;
|
458
|
+
}
|
459
|
+
|
460
|
+
uint32_t new_head = size;
|
461
|
+
|
462
|
+
for (auto & range : pending.ranges) {
|
463
|
+
for (uint32_t i = range.c0; i < range.c1; ++i) {
|
464
|
+
cells[i].seq_id.clear();
|
465
|
+
|
466
|
+
// keep count of the number of used cells
|
467
|
+
if (cells[i].pos >= 0) {
|
468
|
+
used--;
|
469
|
+
}
|
470
|
+
|
471
|
+
cells[i].pos = -1;
|
472
|
+
cells[i].src = -1;
|
473
|
+
}
|
474
|
+
|
475
|
+
new_head = std::min(new_head, range.c0);
|
476
|
+
}
|
477
|
+
|
478
|
+
if (new_head != size && new_head < head) {
|
479
|
+
head = new_head;
|
480
|
+
}
|
481
|
+
}
|
482
|
+
|
483
|
+
void llama_kv_cache_unified::commit() {
|
484
|
+
// TODO: tmp - move to llama_kv_cache_recurrent
|
485
|
+
if (recurrent) {
|
486
|
+
return;
|
487
|
+
}
|
488
|
+
|
489
|
+
if (pending.ranges.empty()) {
|
490
|
+
LLAMA_LOG_WARN("%s: no pending KV cache updates to commit - might indicate a bug (ref: %s)\n",
|
491
|
+
__func__, "https://github.com/ggml-org/llama.cpp/pull/12695");
|
492
|
+
return;
|
493
|
+
}
|
494
|
+
|
495
|
+
pending.ranges.clear();
|
496
|
+
}
|
497
|
+
|
498
|
+
bool llama_kv_cache_unified::get_can_shift() const {
|
499
|
+
return can_shift;
|
500
|
+
}
|
501
|
+
|
502
|
+
bool llama_kv_cache_unified::find_slot(
|
503
|
+
const llama_ubatch & ubatch) {
|
123
504
|
const uint32_t n_tokens = ubatch.n_tokens;
|
124
505
|
const uint32_t n_seqs = ubatch.n_seqs;
|
125
506
|
const uint32_t n_seq_tokens = ubatch.n_seq_tokens;
|
126
507
|
|
127
|
-
if
|
508
|
+
// if we have enough unused cells before the current head ->
|
509
|
+
// better to start searching from the beginning of the cache, hoping to fill it
|
510
|
+
if (head > used + 2*ubatch.n_tokens) {
|
511
|
+
head = 0;
|
512
|
+
}
|
513
|
+
|
514
|
+
if (recurrent) {
|
128
515
|
// For recurrent state architectures (like Mamba or RWKV),
|
129
516
|
// each cache cell can store the state for a whole sequence.
|
130
517
|
// A slot should be always be contiguous.
|
@@ -132,7 +519,7 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
132
519
|
// can only process batches with an equal number of new tokens in each sequence
|
133
520
|
LM_GGML_ASSERT(ubatch.equal_seqs);
|
134
521
|
|
135
|
-
int32_t min =
|
522
|
+
int32_t min = size - 1;
|
136
523
|
int32_t max = 0;
|
137
524
|
|
138
525
|
// everything should fit if all seq_ids are smaller than the max
|
@@ -141,16 +528,16 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
141
528
|
for (uint32_t j = 0; j < n_seq_id; ++j) {
|
142
529
|
const llama_seq_id seq_id = ubatch.seq_id[s][j];
|
143
530
|
|
144
|
-
if (seq_id < 0 || (uint32_t) seq_id >=
|
531
|
+
if (seq_id < 0 || (uint32_t) seq_id >= size) {
|
145
532
|
// too big seq_id
|
146
533
|
// TODO: would it be possible to resize the cache instead?
|
147
|
-
LLAMA_LOG_ERROR("%s: seq_id=%d >= n_seq_max=%d Try using a bigger --parallel value\n", __func__, seq_id,
|
148
|
-
return
|
534
|
+
LLAMA_LOG_ERROR("%s: seq_id=%d >= n_seq_max=%d Try using a bigger --parallel value\n", __func__, seq_id, size);
|
535
|
+
return false;
|
149
536
|
}
|
150
537
|
if (j > 0) {
|
151
|
-
llama_kv_cell & seq =
|
538
|
+
llama_kv_cell & seq = cells[seq_id];
|
152
539
|
if (seq.tail >= 0) {
|
153
|
-
llama_kv_cell & cell =
|
540
|
+
llama_kv_cell & cell = cells[seq.tail];
|
154
541
|
// clear cells from seq_ids that become shared
|
155
542
|
// (should not normally happen, but let's handle it anyway)
|
156
543
|
cell.seq_id.erase(seq_id);
|
@@ -158,7 +545,7 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
158
545
|
if (cell.seq_id.empty()) {
|
159
546
|
cell.pos = -1;
|
160
547
|
cell.src = -1;
|
161
|
-
|
548
|
+
used -= 1;
|
162
549
|
}
|
163
550
|
}
|
164
551
|
}
|
@@ -168,9 +555,9 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
168
555
|
#ifndef NDEBUG
|
169
556
|
{
|
170
557
|
std::vector<int32_t> tails_verif;
|
171
|
-
tails_verif.assign(
|
172
|
-
for (uint32_t i = 0; i <
|
173
|
-
llama_kv_cell & cell =
|
558
|
+
tails_verif.assign(size, -1);
|
559
|
+
for (uint32_t i = 0; i < size; ++i) {
|
560
|
+
llama_kv_cell & cell = cells[i];
|
174
561
|
for (llama_seq_id seq_id : cell.seq_id) {
|
175
562
|
if (tails_verif[seq_id] != -1) {
|
176
563
|
LLAMA_LOG_ERROR("%s: duplicate tail for seq_id %d in cell %d and %d\n", __func__, seq_id, i, tails_verif[seq_id]);
|
@@ -178,20 +565,20 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
178
565
|
tails_verif[seq_id] = i;
|
179
566
|
}
|
180
567
|
}
|
181
|
-
for (uint32_t i = 0; i <
|
182
|
-
if (tails_verif[i] !=
|
183
|
-
LLAMA_LOG_ERROR("%s: wrong tail for seq_id %d, (%d instead of %d)\n", __func__, i,
|
568
|
+
for (uint32_t i = 0; i < size; ++i) {
|
569
|
+
if (tails_verif[i] != cells[i].tail) {
|
570
|
+
LLAMA_LOG_ERROR("%s: wrong tail for seq_id %d, (%d instead of %d)\n", __func__, i, cells[i].tail, tails_verif[i]);
|
184
571
|
}
|
185
572
|
}
|
186
573
|
}
|
187
574
|
#endif
|
188
575
|
|
189
576
|
// find next empty cell
|
190
|
-
uint32_t next_empty_cell =
|
577
|
+
uint32_t next_empty_cell = head;
|
191
578
|
|
192
|
-
for (uint32_t i = 0; i <
|
193
|
-
if (next_empty_cell >=
|
194
|
-
llama_kv_cell & cell =
|
579
|
+
for (uint32_t i = 0; i < size; ++i) {
|
580
|
+
if (next_empty_cell >= size) { next_empty_cell -= size; }
|
581
|
+
llama_kv_cell & cell = cells[next_empty_cell];
|
195
582
|
if (cell.is_empty()) { break; }
|
196
583
|
next_empty_cell += 1;
|
197
584
|
}
|
@@ -199,20 +586,20 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
199
586
|
// find usable cell range
|
200
587
|
for (uint32_t s = 0; s < n_seqs; ++s) {
|
201
588
|
const llama_seq_id seq_id = ubatch.seq_id[s][0];
|
202
|
-
llama_kv_cell & seq_meta =
|
589
|
+
llama_kv_cell & seq_meta = cells[seq_id];
|
203
590
|
bool has_cell = false;
|
204
591
|
if (seq_meta.tail >= 0) {
|
205
|
-
llama_kv_cell & cell =
|
592
|
+
llama_kv_cell & cell = cells[seq_meta.tail];
|
206
593
|
LM_GGML_ASSERT(cell.has_seq_id(seq_id));
|
207
594
|
// does this seq_id "own" the cell?
|
208
595
|
if (cell.seq_id.size() == 1) { has_cell = true; }
|
209
596
|
}
|
210
597
|
if (!has_cell) {
|
211
|
-
llama_kv_cell & empty_cell =
|
598
|
+
llama_kv_cell & empty_cell = cells[next_empty_cell];
|
212
599
|
LM_GGML_ASSERT(empty_cell.is_empty());
|
213
600
|
// copy old tail into the empty cell
|
214
601
|
if (seq_meta.tail >= 0) {
|
215
|
-
llama_kv_cell & orig_cell =
|
602
|
+
llama_kv_cell & orig_cell = cells[seq_meta.tail];
|
216
603
|
empty_cell.pos = orig_cell.pos;
|
217
604
|
empty_cell.src = orig_cell.src;
|
218
605
|
orig_cell.seq_id.erase(seq_id);
|
@@ -222,9 +609,9 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
222
609
|
// find next empty cell
|
223
610
|
if (s + 1 < n_seqs) {
|
224
611
|
next_empty_cell += 1;
|
225
|
-
for (uint32_t i = 0; i <
|
226
|
-
if (next_empty_cell >=
|
227
|
-
llama_kv_cell & cell =
|
612
|
+
for (uint32_t i = 0; i < size; ++i) {
|
613
|
+
if (next_empty_cell >= size) { next_empty_cell -= size; }
|
614
|
+
llama_kv_cell & cell = cells[next_empty_cell];
|
228
615
|
if (cell.is_empty()) { break; }
|
229
616
|
next_empty_cell += 1;
|
230
617
|
}
|
@@ -237,10 +624,10 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
237
624
|
// gather and re-order
|
238
625
|
for (uint32_t s = 0; s < n_seqs; ++s) {
|
239
626
|
int32_t dst_id = s + min;
|
240
|
-
int32_t src_id =
|
627
|
+
int32_t src_id = cells[ubatch.seq_id[s][0]].tail;
|
241
628
|
if (dst_id != src_id) {
|
242
|
-
llama_kv_cell & dst_cell =
|
243
|
-
llama_kv_cell & src_cell =
|
629
|
+
llama_kv_cell & dst_cell = cells[dst_id];
|
630
|
+
llama_kv_cell & src_cell = cells[src_id];
|
244
631
|
|
245
632
|
std::swap(dst_cell.pos, src_cell.pos);
|
246
633
|
std::swap(dst_cell.src, src_cell.src);
|
@@ -248,10 +635,10 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
248
635
|
|
249
636
|
// swap tails (assuming they NEVER overlap)
|
250
637
|
for (const llama_seq_id seq_id : src_cell.seq_id) {
|
251
|
-
|
638
|
+
cells[seq_id].tail = src_id;
|
252
639
|
}
|
253
640
|
for (const llama_seq_id seq_id : dst_cell.seq_id) {
|
254
|
-
|
641
|
+
cells[seq_id].tail = dst_id;
|
255
642
|
}
|
256
643
|
}
|
257
644
|
}
|
@@ -260,7 +647,7 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
260
647
|
for (uint32_t s = 0; s < n_seqs; ++s) {
|
261
648
|
const llama_pos last_pos = ubatch.pos[n_seq_tokens * s + n_seq_tokens - 1];
|
262
649
|
int32_t cell_id = s + min;
|
263
|
-
llama_kv_cell & cell =
|
650
|
+
llama_kv_cell & cell = cells[cell_id];
|
264
651
|
|
265
652
|
if (cell.pos >= 0 && last_pos != cell.pos + (llama_pos) n_seq_tokens) {
|
266
653
|
// What should happen when the pos backtracks or skips a value?
|
@@ -273,41 +660,42 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
273
660
|
for (int32_t j = 0; j < ubatch.n_seq_id[s]; ++j) {
|
274
661
|
const llama_seq_id seq_id = ubatch.seq_id[s][j];
|
275
662
|
cell.seq_id.insert(seq_id);
|
276
|
-
|
663
|
+
cells[seq_id].tail = cell_id;
|
277
664
|
}
|
278
665
|
}
|
279
666
|
|
280
667
|
// allow getting the range of used cells, from head to head + n
|
281
|
-
|
282
|
-
|
283
|
-
|
668
|
+
head = min;
|
669
|
+
n = max - min + 1;
|
670
|
+
used = std::count_if(cells.begin(), cells.end(),
|
284
671
|
[](const llama_kv_cell& cell){ return !cell.is_empty(); });
|
285
672
|
|
286
673
|
// sanity check
|
287
|
-
return
|
674
|
+
return n >= n_seqs;
|
288
675
|
}
|
676
|
+
|
289
677
|
// otherwise, one cell per token.
|
290
678
|
|
291
|
-
if (n_tokens >
|
292
|
-
LLAMA_LOG_ERROR("%s: n_tokens
|
293
|
-
return
|
679
|
+
if (n_tokens > size) {
|
680
|
+
LLAMA_LOG_ERROR("%s: n_tokens = %d > size = %d\n", __func__, n_tokens, size);
|
681
|
+
return false;
|
294
682
|
}
|
295
683
|
|
296
684
|
uint32_t n_tested = 0;
|
297
685
|
|
298
686
|
while (true) {
|
299
|
-
if (
|
300
|
-
n_tested +=
|
301
|
-
|
687
|
+
if (head + n_tokens > size) {
|
688
|
+
n_tested += size - head;
|
689
|
+
head = 0;
|
302
690
|
continue;
|
303
691
|
}
|
304
692
|
|
305
693
|
bool found = true;
|
306
694
|
for (uint32_t i = 0; i < n_tokens; i++) {
|
307
|
-
if (
|
695
|
+
if (cells[head + i].pos >= 0) {
|
308
696
|
found = false;
|
309
|
-
|
310
|
-
n_tested
|
697
|
+
head += i + 1;
|
698
|
+
n_tested += i + 1;
|
311
699
|
break;
|
312
700
|
}
|
313
701
|
}
|
@@ -316,31 +704,38 @@ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
|
|
316
704
|
break;
|
317
705
|
}
|
318
706
|
|
319
|
-
if (n_tested >=
|
707
|
+
if (n_tested >= size) {
|
320
708
|
//LLAMA_LOG_ERROR("%s: failed to find a slot for %d tokens\n", __func__, n_tokens);
|
321
|
-
return
|
709
|
+
return false;
|
322
710
|
}
|
323
711
|
}
|
324
712
|
|
325
713
|
for (uint32_t s = 0; s < n_seqs; s++) {
|
326
714
|
for (uint32_t i = 0; i < n_seq_tokens; ++i) {
|
327
715
|
uint32_t k = s*n_seq_tokens + i;
|
328
|
-
|
716
|
+
cells[head + k].pos = ubatch.pos[k];
|
329
717
|
|
330
718
|
for (int32_t j = 0; j < ubatch.n_seq_id[s]; j++) {
|
331
|
-
|
719
|
+
cells[head + k].seq_id.insert(ubatch.seq_id[s][j]);
|
332
720
|
}
|
333
721
|
}
|
334
722
|
}
|
335
723
|
|
336
|
-
|
724
|
+
used += n_tokens;
|
725
|
+
|
726
|
+
pending.ranges.push_back({head, head + n_tokens});
|
727
|
+
|
728
|
+
return true;
|
729
|
+
}
|
337
730
|
|
338
|
-
|
731
|
+
uint32_t llama_kv_cache_unified::get_padding(const llama_cparams & cparams) const {
|
732
|
+
// the FA kernels require padding to avoid extra runtime boundary checks
|
733
|
+
return cparams.flash_attn ? 256u : 32u;
|
339
734
|
}
|
340
735
|
|
341
|
-
uint32_t
|
342
|
-
for (uint32_t i =
|
343
|
-
const llama_kv_cell & cell =
|
736
|
+
uint32_t llama_kv_cache_unified::cell_max() const {
|
737
|
+
for (uint32_t i = size; i > 0; --i) {
|
738
|
+
const llama_kv_cell & cell = cells[i - 1];
|
344
739
|
|
345
740
|
if (cell.pos >= 0 && !cell.is_empty()) {
|
346
741
|
return i;
|
@@ -350,289 +745,549 @@ uint32_t llama_kv_cache_cell_max(const struct llama_kv_cache & cache) {
|
|
350
745
|
return 0;
|
351
746
|
}
|
352
747
|
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
cache.cells[i].tail = -1;
|
748
|
+
size_t llama_kv_cache_unified::size_k_bytes() const {
|
749
|
+
size_t size_k_bytes = 0;
|
750
|
+
|
751
|
+
for (const auto & k : k_l) {
|
752
|
+
size_k_bytes += lm_ggml_nbytes(k);
|
359
753
|
}
|
360
|
-
cache.head = 0;
|
361
|
-
cache.used = 0;
|
362
754
|
|
363
|
-
|
364
|
-
|
755
|
+
return size_k_bytes;
|
756
|
+
}
|
757
|
+
|
758
|
+
size_t llama_kv_cache_unified::size_v_bytes() const {
|
759
|
+
size_t size_v_bytes = 0;
|
760
|
+
|
761
|
+
for (const auto & v : v_l) {
|
762
|
+
size_v_bytes += lm_ggml_nbytes(v);
|
365
763
|
}
|
764
|
+
|
765
|
+
return size_v_bytes;
|
366
766
|
}
|
367
767
|
|
368
|
-
bool
|
369
|
-
|
370
|
-
llama_seq_id seq_id,
|
371
|
-
llama_pos p0,
|
372
|
-
llama_pos p1) {
|
373
|
-
uint32_t new_head = cache.size;
|
768
|
+
bool llama_kv_cache_unified::defrag_prepare(int32_t n_max_nodes) {
|
769
|
+
const uint32_t n_layer = hparams.n_layer;
|
374
770
|
|
375
|
-
|
376
|
-
|
771
|
+
const uint32_t n_kv = cell_max();
|
772
|
+
const uint32_t n_used = used;
|
377
773
|
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
774
|
+
assert(n_used <= n_kv);
|
775
|
+
|
776
|
+
//const int64_t t_start = lm_ggml_time_us();
|
777
|
+
|
778
|
+
// number of cells moved
|
779
|
+
uint32_t n_moves = 0;
|
780
|
+
|
781
|
+
// each move requires 6*n_layer tensors (see graph_build_kv_self_defrag)
|
782
|
+
// - source view, destination view, copy operation
|
783
|
+
// - x2 for keys and values
|
784
|
+
//const uint32_t max_moves = max_nodes()/(6*n_layer);
|
785
|
+
// TODO: tmp fix https://github.com/ggerganov/llama.cpp/issues/6685#issuecomment-2057579516
|
786
|
+
const uint32_t max_moves = (n_max_nodes - 2*n_layer)/(6*n_layer);
|
787
|
+
|
788
|
+
// determine which KV cells to move where
|
789
|
+
//
|
790
|
+
// cell i moves to ids[i]
|
791
|
+
//
|
792
|
+
// if ids[i] == i || ids[i] == n_kv, then cell i is not moved
|
793
|
+
//
|
794
|
+
auto & ids = defrag_info.ids;
|
795
|
+
|
796
|
+
ids.clear();
|
797
|
+
ids.resize(n_kv, n_kv);
|
798
|
+
|
799
|
+
for (uint32_t i0 = 0; i0 < n_used; ++i0) {
|
800
|
+
const auto & cell0 = cells[i0];
|
801
|
+
|
802
|
+
if (!cell0.is_empty()) {
|
803
|
+
ids[i0] = i0;
|
804
|
+
|
805
|
+
continue;
|
383
806
|
}
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
// invalidate tails which will be cleared
|
393
|
-
if (p0 <= cell.pos && cell.pos < p1) {
|
394
|
-
tail_id = -1;
|
395
|
-
}
|
396
|
-
}
|
397
|
-
} else {
|
398
|
-
// seq_id is negative, then the range should include everything or nothing
|
399
|
-
if (p0 != p1 && (p0 != 0 || p1 != std::numeric_limits<llama_pos>::max())) {
|
400
|
-
return false;
|
401
|
-
}
|
807
|
+
|
808
|
+
// found a hole - fill it with data from the end of the cache
|
809
|
+
|
810
|
+
uint32_t nh = 1;
|
811
|
+
|
812
|
+
// determine the size of the hole
|
813
|
+
while (i0 + nh < n_used && cells[i0 + nh].is_empty()) {
|
814
|
+
nh++;
|
402
815
|
}
|
403
|
-
}
|
404
816
|
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
817
|
+
uint32_t nf = 0;
|
818
|
+
uint32_t is = n_kv - 1;
|
819
|
+
|
820
|
+
// starting from the end, find nh non-empty cells
|
821
|
+
for (; is > i0; --is) {
|
822
|
+
const auto & cell1 = cells[is];
|
823
|
+
|
824
|
+
if (cell1.is_empty() || ids[is] != n_kv) {
|
412
825
|
continue;
|
413
826
|
}
|
414
|
-
if (cache.cells[i].is_empty()) {
|
415
|
-
// keep count of the number of used cells
|
416
|
-
if (cache.cells[i].pos >= 0) cache.used--;
|
417
827
|
|
418
|
-
|
419
|
-
|
420
|
-
|
828
|
+
// non-empty cell which is not yet moved
|
829
|
+
nf++;
|
830
|
+
|
831
|
+
if (nf == nh) {
|
832
|
+
break;
|
421
833
|
}
|
422
834
|
}
|
423
|
-
}
|
424
835
|
|
425
|
-
|
426
|
-
|
836
|
+
// this can only happen if `n_used` is not accurate, which would be a bug
|
837
|
+
LM_GGML_ASSERT(nf == nh && "KV defrag bug: nf != nh");
|
427
838
|
|
428
|
-
|
429
|
-
}
|
839
|
+
nf = 0;
|
430
840
|
|
431
|
-
|
432
|
-
struct llama_kv_cache & cache,
|
433
|
-
llama_seq_id seq_id_src,
|
434
|
-
llama_seq_id seq_id_dst,
|
435
|
-
llama_pos p0,
|
436
|
-
llama_pos p1) {
|
437
|
-
if (p0 < 0) p0 = 0;
|
438
|
-
if (p1 < 0) p1 = std::numeric_limits<llama_pos>::max();
|
841
|
+
uint32_t i1 = is;
|
439
842
|
|
440
|
-
|
441
|
-
|
442
|
-
llama_kv_cell & tail_src = cache.cells[seq_id_src];
|
443
|
-
llama_kv_cell & tail_dst = cache.cells[seq_id_dst];
|
444
|
-
if (tail_dst.tail >= 0) {
|
445
|
-
// clear destination seq_id if it wasn't empty
|
446
|
-
llama_kv_cell & cell_dst = cache.cells[tail_dst.tail];
|
843
|
+
// are we moving a continuous block of memory?
|
844
|
+
bool cont = false;
|
447
845
|
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
846
|
+
// should we stop searching for the next move?
|
847
|
+
bool stop = false;
|
848
|
+
|
849
|
+
// go back and move the nf cells to the hole
|
850
|
+
for (; i1 < n_kv; ++i1) {
|
851
|
+
auto & cell1 = cells[i1];
|
852
|
+
|
853
|
+
if (cell1.is_empty() || ids[i1] != n_kv) {
|
854
|
+
if (n_moves == max_moves) {
|
855
|
+
stop = true;
|
856
|
+
break;
|
455
857
|
}
|
858
|
+
|
859
|
+
cont = false;
|
860
|
+
continue;
|
456
861
|
}
|
457
|
-
if (tail_src.tail >= 0) {
|
458
|
-
llama_kv_cell & cell_src = cache.cells[tail_src.tail];
|
459
862
|
|
460
|
-
|
461
|
-
|
863
|
+
// this cell goes to (i0 + nf)
|
864
|
+
ids[i1] = i0 + nf;
|
865
|
+
|
866
|
+
// move the cell meta data
|
867
|
+
cells[i0 + nf] = cell1;
|
868
|
+
|
869
|
+
// clear the old cell and move the head there
|
870
|
+
cell1 = llama_kv_cell();
|
871
|
+
head = n_used;
|
872
|
+
|
873
|
+
if (!cont) {
|
874
|
+
n_moves++;
|
875
|
+
cont = true;
|
876
|
+
}
|
877
|
+
|
878
|
+
nf++;
|
879
|
+
|
880
|
+
if (nf == nh) {
|
881
|
+
break;
|
462
882
|
}
|
463
883
|
}
|
464
884
|
|
465
|
-
|
885
|
+
if (stop || n_moves == max_moves) {
|
886
|
+
break;
|
887
|
+
}
|
888
|
+
|
889
|
+
//LLAMA_LOG_INFO("(tmp log) KV defrag: move [%u, %u) to [%u, %u)\n", is, i1 + 1, i0, i0 + nh);
|
890
|
+
|
891
|
+
i0 += nh - 1;
|
892
|
+
}
|
893
|
+
|
894
|
+
if (n_moves == 0) {
|
895
|
+
return false;
|
466
896
|
}
|
467
|
-
// otherwise, this is the KV cache of a Transformer-like model
|
468
897
|
|
469
|
-
|
898
|
+
LLAMA_LOG_DEBUG("(tmp log) KV defrag cell moves: %u\n", n_moves);
|
470
899
|
|
471
|
-
|
472
|
-
|
473
|
-
|
900
|
+
LLAMA_LOG_DEBUG("expected gf nodes: %u\n", 6*n_moves*n_layer);
|
901
|
+
|
902
|
+
return true;
|
903
|
+
}
|
904
|
+
|
905
|
+
void llama_kv_cache_unified::state_write(llama_io_write_i & io, llama_seq_id seq_id) const {
|
906
|
+
std::vector<std::pair<uint32_t, uint32_t>> cell_ranges; // ranges, from inclusive, to exclusive
|
907
|
+
uint32_t cell_count = 0;
|
908
|
+
|
909
|
+
// Count the number of cells with the specified seq_id
|
910
|
+
// Find all the ranges of cells with this seq id (or all, when -1)
|
911
|
+
uint32_t cell_range_begin = size;
|
912
|
+
for (uint32_t i = 0; i < size; ++i) {
|
913
|
+
const auto & cell = cells[i];
|
914
|
+
if ((seq_id == -1 && !cell.is_empty()) || cell.has_seq_id(seq_id)) {
|
915
|
+
++cell_count;
|
916
|
+
if (cell_range_begin == size) {
|
917
|
+
cell_range_begin = i;
|
918
|
+
}
|
919
|
+
} else {
|
920
|
+
if (cell_range_begin != size) {
|
921
|
+
cell_ranges.emplace_back(cell_range_begin, i);
|
922
|
+
cell_range_begin = size;
|
923
|
+
}
|
474
924
|
}
|
475
925
|
}
|
926
|
+
if (cell_range_begin != size) {
|
927
|
+
cell_ranges.emplace_back(cell_range_begin, size);
|
928
|
+
}
|
929
|
+
|
930
|
+
// DEBUG CHECK: Sum of cell counts in ranges should equal the total cell count
|
931
|
+
uint32_t cell_count_check = 0;
|
932
|
+
for (const auto & range : cell_ranges) {
|
933
|
+
cell_count_check += range.second - range.first;
|
934
|
+
}
|
935
|
+
LM_GGML_ASSERT(cell_count == cell_count_check);
|
936
|
+
|
937
|
+
io.write(&cell_count, sizeof(cell_count));
|
938
|
+
|
939
|
+
state_write_meta(io, cell_ranges, seq_id);
|
940
|
+
state_write_data(io, cell_ranges);
|
476
941
|
}
|
477
942
|
|
478
|
-
void
|
479
|
-
uint32_t
|
943
|
+
void llama_kv_cache_unified::state_read(llama_io_read_i & io, llama_seq_id seq_id) {
|
944
|
+
uint32_t cell_count;
|
945
|
+
io.read_to(&cell_count, sizeof(cell_count));
|
480
946
|
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
cache.cells[i].src = -1;
|
489
|
-
cache.cells[i].seq_id.clear();
|
490
|
-
if (new_head == cache.size) new_head = i;
|
947
|
+
bool res = true;
|
948
|
+
res = res && state_read_meta(io, cell_count, seq_id);
|
949
|
+
res = res && state_read_data(io, cell_count);
|
950
|
+
|
951
|
+
if (!res) {
|
952
|
+
if (seq_id == -1) {
|
953
|
+
clear();
|
491
954
|
} else {
|
492
|
-
|
493
|
-
cache.cells[i].seq_id.insert(seq_id);
|
955
|
+
seq_rm(seq_id, -1, -1);
|
494
956
|
}
|
957
|
+
throw std::runtime_error("failed to restore kv cache");
|
495
958
|
}
|
496
|
-
|
497
|
-
// If we freed up a slot, set head to it so searching can start there.
|
498
|
-
if (new_head != cache.size && new_head < cache.head) cache.head = new_head;
|
499
959
|
}
|
500
960
|
|
501
|
-
void
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
uint32_t new_head = cache.size;
|
961
|
+
void llama_kv_cache_unified::state_write_meta(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id) const {
|
962
|
+
for (const auto & range : cell_ranges) {
|
963
|
+
for (uint32_t i = range.first; i < range.second; ++i) {
|
964
|
+
const auto & cell = cells[i];
|
965
|
+
const llama_pos pos = cell.pos;
|
966
|
+
const uint32_t n_seq_id = seq_id == -1 ? cell.seq_id.size() : 0;
|
508
967
|
|
509
|
-
|
510
|
-
|
511
|
-
// If there is no range then return early to avoid looping over the cache.
|
512
|
-
if (p0 == p1) return;
|
968
|
+
io.write(&pos, sizeof(pos));
|
969
|
+
io.write(&n_seq_id, sizeof(n_seq_id));
|
513
970
|
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
const int32_t tail_id = cache.cells[seq_id].tail;
|
518
|
-
if (tail_id >= 0) {
|
519
|
-
llama_kv_cell & cell = cache.cells[tail_id];
|
520
|
-
if (cell.has_seq_id(seq_id) && p0 <= cell.pos && cell.pos < p1) {
|
521
|
-
cell.pos += delta;
|
971
|
+
if (n_seq_id) {
|
972
|
+
for (auto seq_id : cell.seq_id) {
|
973
|
+
io.write(&seq_id, sizeof(seq_id));
|
522
974
|
}
|
523
975
|
}
|
524
976
|
}
|
525
|
-
return;
|
526
977
|
}
|
978
|
+
}
|
527
979
|
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
cache.cells[i].pos += delta;
|
532
|
-
cache.cells[i].delta += delta;
|
980
|
+
void llama_kv_cache_unified::state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const {
|
981
|
+
const uint32_t v_trans = this->v_trans ? 1 : 0;
|
982
|
+
const uint32_t n_layer = hparams.n_layer;
|
533
983
|
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
984
|
+
io.write(&v_trans, sizeof(v_trans));
|
985
|
+
io.write(&n_layer, sizeof(n_layer));
|
986
|
+
|
987
|
+
std::vector<uint8_t> tmp_buf;
|
988
|
+
|
989
|
+
// Iterate and write all the keys first, each row is a cell
|
990
|
+
// Get whole range at a time
|
991
|
+
for (uint32_t il = 0; il < n_layer; ++il) {
|
992
|
+
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s();
|
993
|
+
|
994
|
+
// Write key type
|
995
|
+
const int32_t k_type_i = (int32_t)k_l[il]->type;
|
996
|
+
io.write(&k_type_i, sizeof(k_type_i));
|
997
|
+
|
998
|
+
// Write row size of key
|
999
|
+
const uint64_t k_size_row = lm_ggml_row_size(k_l[il]->type, n_embd_k_gqa);
|
1000
|
+
io.write(&k_size_row, sizeof(k_size_row));
|
1001
|
+
|
1002
|
+
// Read each range of cells of k_size length each into tmp_buf and write out
|
1003
|
+
for (const auto & range : cell_ranges) {
|
1004
|
+
const size_t range_size = range.second - range.first;
|
1005
|
+
const size_t buf_size = range_size * k_size_row;
|
1006
|
+
io.write_tensor(k_l[il], range.first * k_size_row, buf_size);
|
544
1007
|
}
|
545
1008
|
}
|
546
1009
|
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
}
|
1010
|
+
if (!v_trans) {
|
1011
|
+
for (uint32_t il = 0; il < n_layer; ++il) {
|
1012
|
+
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
|
551
1013
|
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
llama_pos p0,
|
556
|
-
llama_pos p1,
|
557
|
-
int d) {
|
558
|
-
if (p0 < 0) p0 = 0;
|
559
|
-
if (p1 < 0) p1 = std::numeric_limits<llama_pos>::max();
|
560
|
-
// If there is no range then return early to avoid looping over the cache.
|
561
|
-
if (p0 == p1) return;
|
1014
|
+
// Write value type
|
1015
|
+
const int32_t v_type_i = (int32_t)v_l[il]->type;
|
1016
|
+
io.write(&v_type_i, sizeof(v_type_i));
|
562
1017
|
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
1018
|
+
// Write row size of value
|
1019
|
+
const uint64_t v_size_row = lm_ggml_row_size(v_l[il]->type, n_embd_v_gqa);
|
1020
|
+
io.write(&v_size_row, sizeof(v_size_row));
|
1021
|
+
|
1022
|
+
// Read each range of cells of v_size length each into tmp_buf and write out
|
1023
|
+
for (const auto & range : cell_ranges) {
|
1024
|
+
const size_t range_size = range.second - range.first;
|
1025
|
+
const size_t buf_size = range_size * v_size_row;
|
1026
|
+
io.write_tensor(v_l[il], range.first * v_size_row, buf_size);
|
1027
|
+
}
|
1028
|
+
}
|
1029
|
+
} else {
|
1030
|
+
// When v is transposed, we also need the element size and get the element ranges from each row
|
1031
|
+
const uint32_t kv_size = size;
|
1032
|
+
for (uint32_t il = 0; il < n_layer; ++il) {
|
1033
|
+
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
|
1034
|
+
|
1035
|
+
// Write value type
|
1036
|
+
const int32_t v_type_i = (int32_t)v_l[il]->type;
|
1037
|
+
io.write(&v_type_i, sizeof(v_type_i));
|
1038
|
+
|
1039
|
+
// Write element size
|
1040
|
+
const uint32_t v_size_el = lm_ggml_type_size(v_l[il]->type);
|
1041
|
+
io.write(&v_size_el, sizeof(v_size_el));
|
1042
|
+
|
1043
|
+
// Write GQA embedding size
|
1044
|
+
io.write(&n_embd_v_gqa, sizeof(n_embd_v_gqa));
|
1045
|
+
|
1046
|
+
// For each row, we get the element values of each cell
|
1047
|
+
for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
|
1048
|
+
// Read each range of cells of v_size_el length each into tmp_buf and write out
|
1049
|
+
for (const auto & range : cell_ranges) {
|
1050
|
+
const size_t range_size = range.second - range.first;
|
1051
|
+
const size_t src_offset = (range.first + j * kv_size) * v_size_el;
|
1052
|
+
const size_t buf_size = range_size * v_size_el;
|
1053
|
+
io.write_tensor(v_l[il], src_offset, buf_size);
|
571
1054
|
}
|
572
1055
|
}
|
573
1056
|
}
|
574
|
-
return;
|
575
1057
|
}
|
1058
|
+
}
|
576
1059
|
|
577
|
-
|
578
|
-
|
579
|
-
|
1060
|
+
bool llama_kv_cache_unified::state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id) {
|
1061
|
+
if (dest_seq_id != -1) {
|
1062
|
+
// single sequence
|
580
1063
|
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
1064
|
+
seq_rm(dest_seq_id, -1, -1);
|
1065
|
+
|
1066
|
+
llama_sbatch sbatch;
|
1067
|
+
llama_ubatch batch = sbatch.reserve_ubatch(cell_count, /* has_embd */ false);
|
1068
|
+
|
1069
|
+
batch.n_tokens = cell_count;
|
1070
|
+
batch.n_seq_tokens = cell_count;
|
1071
|
+
batch.n_seqs = 1;
|
1072
|
+
|
1073
|
+
for (uint32_t i = 0; i < cell_count; ++i) {
|
1074
|
+
llama_pos pos;
|
1075
|
+
uint32_t n_seq_id;
|
1076
|
+
|
1077
|
+
io.read_to(&pos, sizeof(pos));
|
1078
|
+
io.read_to(&n_seq_id, sizeof(n_seq_id));
|
1079
|
+
|
1080
|
+
if (n_seq_id != 0) {
|
1081
|
+
LLAMA_LOG_ERROR("%s: invalid seq_id-agnostic kv cell\n", __func__);
|
1082
|
+
return false;
|
585
1083
|
}
|
1084
|
+
|
1085
|
+
batch.pos[i] = pos;
|
1086
|
+
}
|
1087
|
+
batch.n_seq_id[0] = 1;
|
1088
|
+
batch.seq_id[0] = &dest_seq_id;
|
1089
|
+
if (!find_slot(batch)) {
|
1090
|
+
LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__);
|
1091
|
+
return false;
|
1092
|
+
}
|
1093
|
+
commit();
|
1094
|
+
|
1095
|
+
// DEBUG CHECK: kv.head should be our first cell, kv.head + cell_count - 1 should be our last cell (verify seq_id and pos values)
|
1096
|
+
// Assume that this is one contiguous block of cells
|
1097
|
+
LM_GGML_ASSERT(head + cell_count <= size);
|
1098
|
+
LM_GGML_ASSERT(cells[head].pos == batch.pos[0]);
|
1099
|
+
LM_GGML_ASSERT(cells[head + cell_count - 1].pos == batch.pos[cell_count - 1]);
|
1100
|
+
LM_GGML_ASSERT(cells[head].has_seq_id(dest_seq_id));
|
1101
|
+
LM_GGML_ASSERT(cells[head + cell_count - 1].has_seq_id(dest_seq_id));
|
1102
|
+
} else {
|
1103
|
+
// whole KV cache restore
|
1104
|
+
|
1105
|
+
if (cell_count > size) {
|
1106
|
+
LLAMA_LOG_ERROR("%s: not enough cells in kv cache\n", __func__);
|
1107
|
+
return false;
|
586
1108
|
}
|
587
|
-
}
|
588
|
-
}
|
589
1109
|
|
590
|
-
|
591
|
-
|
1110
|
+
clear();
|
1111
|
+
|
1112
|
+
for (uint32_t i = 0; i < cell_count; ++i) {
|
1113
|
+
llama_kv_cell & cell = cells[i];
|
1114
|
+
|
1115
|
+
llama_pos pos;
|
1116
|
+
uint32_t n_seq_id;
|
1117
|
+
|
1118
|
+
io.read_to(&pos, sizeof(pos));
|
1119
|
+
io.read_to(&n_seq_id, sizeof(n_seq_id));
|
1120
|
+
|
1121
|
+
cell.pos = pos;
|
592
1122
|
|
593
|
-
|
594
|
-
|
595
|
-
|
1123
|
+
for (uint32_t j = 0; j < n_seq_id; ++j) {
|
1124
|
+
llama_seq_id seq_id;
|
1125
|
+
io.read_to(&seq_id, sizeof(seq_id));
|
1126
|
+
|
1127
|
+
// TODO: llama_kv_cache_unified should have a notion of max sequences
|
1128
|
+
//if (seq_id < 0 || (uint32_t) seq_id >= llama_n_seq_max(ctx)) {
|
1129
|
+
if (seq_id < 0) {
|
1130
|
+
//LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, %u)\n", __func__, seq_id, llama_n_seq_max(ctx));
|
1131
|
+
LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, inf)\n", __func__, seq_id);
|
1132
|
+
return false;
|
1133
|
+
}
|
1134
|
+
|
1135
|
+
cell.seq_id.insert(seq_id);
|
1136
|
+
|
1137
|
+
if (recurrent) {
|
1138
|
+
int32_t & tail = cells[seq_id].tail;
|
1139
|
+
if (tail != -1) {
|
1140
|
+
LLAMA_LOG_ERROR("%s: duplicate tail for seq_id %d in cell %d and %d\n", __func__, seq_id, i, tail);
|
1141
|
+
return false;
|
1142
|
+
}
|
1143
|
+
tail = i;
|
1144
|
+
}
|
1145
|
+
}
|
596
1146
|
}
|
1147
|
+
|
1148
|
+
head = 0;
|
1149
|
+
used = cell_count;
|
597
1150
|
}
|
598
1151
|
|
599
|
-
|
1152
|
+
if (recurrent) {
|
1153
|
+
for (uint32_t i = 0; i < cell_count; ++i) {
|
1154
|
+
uint32_t cell_id = head + i;
|
1155
|
+
// make sure the recurrent states will keep their restored state
|
1156
|
+
cells[cell_id].src = cell_id;
|
1157
|
+
}
|
1158
|
+
}
|
1159
|
+
|
1160
|
+
return true;
|
600
1161
|
}
|
601
1162
|
|
602
|
-
|
603
|
-
|
604
|
-
|
1163
|
+
bool llama_kv_cache_unified::state_read_data(llama_io_read_i & io, uint32_t cell_count) {
|
1164
|
+
uint32_t v_trans;
|
1165
|
+
uint32_t n_layer;
|
1166
|
+
io.read_to(&v_trans, sizeof(v_trans));
|
1167
|
+
io.read_to(&n_layer, sizeof(n_layer));
|
1168
|
+
|
1169
|
+
if (n_layer != hparams.n_layer) {
|
1170
|
+
LLAMA_LOG_ERROR("%s: mismatched layer count (%u instead of %u)\n", __func__, n_layer, hparams.n_layer);
|
1171
|
+
return false;
|
1172
|
+
}
|
1173
|
+
if (cell_count > size) {
|
1174
|
+
LLAMA_LOG_ERROR("%s: not enough cells in kv cache to restore state (%u > %u)\n", __func__, cell_count, size);
|
1175
|
+
return false;
|
1176
|
+
}
|
1177
|
+
if (v_trans != (bool) v_trans) {
|
1178
|
+
LLAMA_LOG_ERROR("%s: incompatible V transposition\n", __func__);
|
1179
|
+
return false;
|
605
1180
|
}
|
606
|
-
}
|
607
1181
|
|
608
|
-
|
609
|
-
|
1182
|
+
// For each layer, read the keys for each cell, one row is one cell, read as one contiguous block
|
1183
|
+
for (uint32_t il = 0; il < n_layer; ++il) {
|
1184
|
+
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s();
|
610
1185
|
|
611
|
-
|
612
|
-
|
1186
|
+
// Read type of key
|
1187
|
+
int32_t k_type_i_ref;
|
1188
|
+
io.read_to(&k_type_i_ref, sizeof(k_type_i_ref));
|
1189
|
+
const int32_t k_type_i = (int32_t) k_l[il]->type;
|
1190
|
+
if (k_type_i != k_type_i_ref) {
|
1191
|
+
LLAMA_LOG_ERROR("%s: mismatched key type (%d != %d, layer %d)\n", __func__, k_type_i, k_type_i_ref, il);
|
1192
|
+
return false;
|
1193
|
+
}
|
1194
|
+
|
1195
|
+
// Read row size of key
|
1196
|
+
uint64_t k_size_row_ref;
|
1197
|
+
io.read_to(&k_size_row_ref, sizeof(k_size_row_ref));
|
1198
|
+
const size_t k_size_row = lm_ggml_row_size(k_l[il]->type, n_embd_k_gqa);
|
1199
|
+
if (k_size_row != k_size_row_ref) {
|
1200
|
+
LLAMA_LOG_ERROR("%s: mismatched key row size (%zu != %zu, layer %d)\n", __func__, k_size_row, (size_t) k_size_row_ref, il);
|
1201
|
+
return false;
|
1202
|
+
}
|
1203
|
+
|
1204
|
+
if (cell_count) {
|
1205
|
+
// Read and set the keys for the whole cell range
|
1206
|
+
lm_ggml_backend_tensor_set(k_l[il], io.read(cell_count * k_size_row), head * k_size_row, cell_count * k_size_row);
|
1207
|
+
}
|
613
1208
|
}
|
614
1209
|
|
615
|
-
|
616
|
-
|
1210
|
+
if (!v_trans) {
|
1211
|
+
for (uint32_t il = 0; il < n_layer; ++il) {
|
1212
|
+
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
|
617
1213
|
|
618
|
-
|
619
|
-
|
620
|
-
|
1214
|
+
// Read type of value
|
1215
|
+
int32_t v_type_i_ref;
|
1216
|
+
io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
|
1217
|
+
const int32_t v_type_i = (int32_t)v_l[il]->type;
|
1218
|
+
if (v_type_i != v_type_i_ref) {
|
1219
|
+
LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
|
1220
|
+
return false;
|
1221
|
+
}
|
1222
|
+
|
1223
|
+
// Read row size of value
|
1224
|
+
uint64_t v_size_row_ref;
|
1225
|
+
io.read_to(&v_size_row_ref, sizeof(v_size_row_ref));
|
1226
|
+
const size_t v_size_row = lm_ggml_row_size(v_l[il]->type, n_embd_v_gqa);
|
1227
|
+
if (v_size_row != v_size_row_ref) {
|
1228
|
+
LLAMA_LOG_ERROR("%s: mismatched value row size (%zu != %zu, layer %d)\n", __func__, v_size_row, (size_t) v_size_row_ref, il);
|
1229
|
+
return false;
|
1230
|
+
}
|
621
1231
|
|
622
|
-
|
623
|
-
|
1232
|
+
if (cell_count) {
|
1233
|
+
// Read and set the values for the whole cell range
|
1234
|
+
lm_ggml_backend_tensor_set(v_l[il], io.read(cell_count * v_size_row), head * v_size_row, cell_count * v_size_row);
|
1235
|
+
}
|
1236
|
+
}
|
1237
|
+
} else {
|
1238
|
+
// For each layer, read the values for each cell (transposed)
|
1239
|
+
for (uint32_t il = 0; il < n_layer; ++il) {
|
1240
|
+
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
|
1241
|
+
|
1242
|
+
// Read type of value
|
1243
|
+
int32_t v_type_i_ref;
|
1244
|
+
io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
|
1245
|
+
const int32_t v_type_i = (int32_t)v_l[il]->type;
|
1246
|
+
if (v_type_i != v_type_i_ref) {
|
1247
|
+
LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
|
1248
|
+
return false;
|
1249
|
+
}
|
1250
|
+
|
1251
|
+
// Read element size of value
|
1252
|
+
uint32_t v_size_el_ref;
|
1253
|
+
io.read_to(&v_size_el_ref, sizeof(v_size_el_ref));
|
1254
|
+
const size_t v_size_el = lm_ggml_type_size(v_l[il]->type);
|
1255
|
+
if (v_size_el != v_size_el_ref) {
|
1256
|
+
LLAMA_LOG_ERROR("%s: mismatched value element size (%zu != %zu, layer %d)\n", __func__, v_size_el, (size_t) v_size_el_ref, il);
|
1257
|
+
return false;
|
1258
|
+
}
|
1259
|
+
|
1260
|
+
// Read GQA embedding size
|
1261
|
+
uint32_t n_embd_v_gqa_ref;
|
1262
|
+
io.read_to(&n_embd_v_gqa_ref, sizeof(n_embd_v_gqa_ref));
|
1263
|
+
if (n_embd_v_gqa != n_embd_v_gqa_ref) {
|
1264
|
+
LLAMA_LOG_ERROR("%s: mismatched GQA embedding size (%u != %u, layer %d)\n", __func__, n_embd_v_gqa, n_embd_v_gqa_ref, il);
|
1265
|
+
return false;
|
1266
|
+
}
|
1267
|
+
|
1268
|
+
if (cell_count) {
|
1269
|
+
// For each row in the transposed matrix, read the values for the whole cell range
|
1270
|
+
for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
|
1271
|
+
const size_t dst_offset = (head + j * size) * v_size_el;
|
1272
|
+
lm_ggml_backend_tensor_set(v_l[il], io.read(cell_count * v_size_el), dst_offset, cell_count * v_size_el);
|
1273
|
+
}
|
1274
|
+
}
|
1275
|
+
}
|
1276
|
+
}
|
1277
|
+
|
1278
|
+
return true;
|
624
1279
|
}
|
625
1280
|
|
626
1281
|
//
|
627
1282
|
// kv cache view
|
628
1283
|
//
|
629
1284
|
|
630
|
-
|
631
|
-
|
1285
|
+
llama_kv_cache_view llama_kv_cache_view_init(const llama_kv_cache & kv, int32_t n_seq_max) {
|
1286
|
+
llama_kv_cache_view result = {
|
632
1287
|
/*.n_cells = */ 0,
|
633
1288
|
/*.n_seq_max = */ n_seq_max,
|
634
1289
|
/*.token_count = */ 0,
|
635
|
-
/*.used_cells = */
|
1290
|
+
/*.used_cells = */ kv.get_used_cells(),
|
636
1291
|
/*.max_contiguous = */ 0,
|
637
1292
|
/*.max_contiguous_idx = */ -1,
|
638
1293
|
/*.cells = */ nullptr,
|
@@ -642,7 +1297,7 @@ struct llama_kv_cache_view llama_kv_cache_view_init(const struct llama_kv_cache
|
|
642
1297
|
return result;
|
643
1298
|
}
|
644
1299
|
|
645
|
-
void llama_kv_cache_view_free(
|
1300
|
+
void llama_kv_cache_view_free(llama_kv_cache_view * view) {
|
646
1301
|
if (view->cells != nullptr) {
|
647
1302
|
free(view->cells);
|
648
1303
|
view->cells = nullptr;
|
@@ -653,18 +1308,25 @@ void llama_kv_cache_view_free(struct llama_kv_cache_view * view) {
|
|
653
1308
|
}
|
654
1309
|
}
|
655
1310
|
|
656
|
-
void llama_kv_cache_view_update(
|
657
|
-
|
658
|
-
|
659
|
-
|
1311
|
+
void llama_kv_cache_view_update(llama_kv_cache_view * view, const llama_kv_cache * kv) {
|
1312
|
+
// TODO: rework this in the future, for now quick hack
|
1313
|
+
const llama_kv_cache_unified * kvu = dynamic_cast<const llama_kv_cache_unified *>(kv);
|
1314
|
+
if (kvu == nullptr) {
|
1315
|
+
LLAMA_LOG_ERROR("%s: the kv_cache_view currently works only with llama_kv_cache_unified\n", __func__);
|
1316
|
+
return;
|
1317
|
+
}
|
1318
|
+
|
1319
|
+
if (uint32_t(view->n_cells) < kvu->size || view->cells == nullptr) {
|
1320
|
+
view->n_cells = int32_t(kvu->size);
|
1321
|
+
void * p = realloc(view->cells, sizeof(llama_kv_cache_view_cell) * view->n_cells);
|
660
1322
|
LM_GGML_ASSERT(p != nullptr && "Failed to alloc kv_cache_view cells");
|
661
|
-
view->cells = (
|
1323
|
+
view->cells = (llama_kv_cache_view_cell *)p;
|
662
1324
|
p = realloc(view->cells_sequences, sizeof(llama_seq_id) * view->n_seq_max * view->n_cells);
|
663
1325
|
LM_GGML_ASSERT(p != nullptr && "Failed to alloc kv_cache_view cells sequences");
|
664
1326
|
view->cells_sequences = (llama_seq_id *)p;
|
665
1327
|
}
|
666
1328
|
|
667
|
-
const std::vector<llama_kv_cell> & kv_cells =
|
1329
|
+
const std::vector<llama_kv_cell> & kv_cells = kvu->cells;
|
668
1330
|
llama_kv_cache_view_cell * c_curr = view->cells;
|
669
1331
|
llama_seq_id * cs_curr = view->cells_sequences;
|
670
1332
|
int32_t used_cells = 0;
|
@@ -673,7 +1335,7 @@ void llama_kv_cache_view_update(struct llama_kv_cache_view * view, const struct
|
|
673
1335
|
uint32_t max_contig = 0;
|
674
1336
|
int32_t max_contig_idx = -1;
|
675
1337
|
|
676
|
-
for (int32_t i = 0; i < int32_t(
|
1338
|
+
for (int32_t i = 0; i < int32_t(kvu->size); i++, c_curr++, cs_curr += view->n_seq_max) {
|
677
1339
|
const size_t curr_size = kv_cells[i].seq_id.size();
|
678
1340
|
token_count += curr_size;
|
679
1341
|
c_curr->pos = kv_cells[i].pos + kv_cells[i].delta;
|
@@ -711,8 +1373,8 @@ void llama_kv_cache_view_update(struct llama_kv_cache_view * view, const struct
|
|
711
1373
|
view->max_contiguous_idx = max_contig_idx;
|
712
1374
|
view->token_count = token_count;
|
713
1375
|
view->used_cells = used_cells;
|
714
|
-
if (uint32_t(used_cells) !=
|
1376
|
+
if (uint32_t(used_cells) != kvu->used) {
|
715
1377
|
LLAMA_LOG_ERROR("%s: used cells mismatch. kv_cache says %d but we calculated %d\n",
|
716
|
-
__func__,
|
1378
|
+
__func__, kvu->used, used_cells);
|
717
1379
|
}
|
718
1380
|
}
|