Repository navigation
Expand file tree
/
Copy pathkv_cache.cpp
More file actions
514 lines (478 loc) · 21.4 KB
/
Copy pathkv_cache.cpp
File metadata and controls
514 lines (478 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include "engine/framework/runtime/kv_cache.h"
#include "engine/framework/core/backend.h"
#include "engine/framework/debug/trace.h"
#include <algorithm>
#include <stdexcept>
#include <string>
namespace engine::runtime {
namespace {
void validate_cache_tensor(const core::TensorValue & tensor, const TransformerKVCacheOptions & options) {
if (tensor.type == GGML_TYPE_F32) {
return;
}
if (options.allow_f16_storage && tensor.type == GGML_TYPE_F16) {
return;
}
if (options.allow_bf16_storage && tensor.type == GGML_TYPE_BF16) {
return;
}
throw std::runtime_error(
options.allow_f16_storage || options.allow_bf16_storage
? "TransformerKVCache supports only f32/f16/bf16 cache tensors when enabled"
: "TransformerKVCache requires f32 cache tensors");
}
void write_cache_tensor(
const core::TensorValue & tensor,
const std::vector<float> & values,
const TransformerKVCacheOptions & options) {
validate_cache_tensor(tensor, options);
if (tensor.type == GGML_TYPE_F32) {
core::write_tensor_f32(tensor, values);
return;
}
if (options.allow_f16_storage && tensor.type == GGML_TYPE_F16) {
core::write_tensor_f16(tensor, values);
return;
}
if (options.allow_bf16_storage && tensor.type == GGML_TYPE_BF16) {
core::write_tensor_bf16(tensor, values);
return;
}
throw std::runtime_error("TransformerKVCache requires f32 cache tensors");
}
void copy_cache_row(
const std::vector<float> & source,
int64_t source_row,
std::vector<float> & dest,
int64_t dest_row,
int64_t step_elems) {
const size_t src_begin = static_cast<size_t>(source_row * step_elems);
const size_t dst_begin = static_cast<size_t>(dest_row * step_elems);
const size_t count = static_cast<size_t>(step_elems);
std::copy(source.begin() + static_cast<std::ptrdiff_t>(src_begin),
source.begin() + static_cast<std::ptrdiff_t>(src_begin + count),
dest.begin() + static_cast<std::ptrdiff_t>(dst_begin));
}
std::vector<float> read_cache_tensor(const core::TensorValue & tensor, const TransformerKVCacheOptions & options) {
validate_cache_tensor(tensor, options);
if (tensor.type == GGML_TYPE_F32) {
return core::read_tensor_f32(tensor.tensor);
}
if (options.allow_f16_storage && tensor.type == GGML_TYPE_F16) {
return core::read_tensor_f16(tensor.tensor);
}
if (options.allow_bf16_storage && tensor.type == GGML_TYPE_BF16) {
return core::read_tensor_bf16(tensor.tensor);
}
throw std::runtime_error("TransformerKVCache requires f32 cache tensors");
}
} // namespace
TransformerKVCache::TransformerKVCache(
int64_t cache_steps,
int64_t step_elems,
std::vector<core::TensorValue> keys,
std::vector<core::TensorValue> values)
: TransformerKVCache(cache_steps, step_elems, std::move(keys), std::move(values), {}) {}
TransformerKVCache::TransformerKVCache(
int64_t cache_steps,
int64_t step_elems,
std::vector<core::TensorValue> keys,
std::vector<core::TensorValue> values,
TransformerKVCacheOptions options)
: cache_steps_(std::max<int64_t>(0, cache_steps)),
step_elems_(std::max<int64_t>(0, step_elems)),
options_(options) {
if (step_elems_ <= 0) {
throw std::runtime_error("TransformerKVCache requires positive step_elems");
}
if (options_.ring_mode) {
if (cache_steps_ <= 0) {
throw std::runtime_error("TransformerKVCache ring_mode requires positive cache_steps");
}
if (options_.ring_pinned_steps < 0 || options_.ring_pinned_steps >= cache_steps_) {
throw std::runtime_error(
"TransformerKVCache ring_pinned_steps must satisfy 0 <= pinned < cache_steps");
}
}
if (keys.size() != values.size()) {
throw std::runtime_error("TransformerKVCache key/value layer counts must match");
}
const size_t cache_elems = static_cast<size_t>(cache_steps_ * step_elems_);
layers_.reserve(keys.size());
for (size_t layer = 0; layer < keys.size(); ++layer) {
validate_cache_tensor(keys[layer], options_);
validate_cache_tensor(values[layer], options_);
layers_.push_back(LayerCache{
std::move(keys[layer]),
std::move(values[layer]),
std::vector<float>(cache_elems, 0.0F),
std::vector<float>(cache_elems, 0.0F),
});
}
}
void TransformerKVCache::clear_on_backend() {
for (auto & layer : layers_) {
ggml_backend_tensor_memset(layer.key_tensor.tensor, 0, 0, ggml_nbytes(layer.key_tensor.tensor));
ggml_backend_tensor_memset(layer.value_tensor.tensor, 0, 0, ggml_nbytes(layer.value_tensor.tensor));
}
current_end_ = 0;
valid_steps_ = 0;
}
void TransformerKVCache::import_state(const TransformerKVState & state) {
current_end_ = state.current_end;
if (layers_.empty()) {
valid_steps_ = 0;
return;
}
if (state.layers.size() != layers_.size()) {
throw std::runtime_error("TransformerKVCache state layer count does not match cache layer count");
}
const int64_t state_steps = state.layers.empty() ? 0 : state.layers.front().valid_steps;
if (state_steps > cache_steps_) {
throw std::runtime_error("TransformerKVCache state valid_steps exceeds cache capacity");
}
valid_steps_ = state_steps;
for (size_t layer = 0; layer < layers_.size(); ++layer) {
auto & cache = layers_[layer];
const auto & source = state.layers[layer];
if (source.valid_steps != state_steps) {
throw std::runtime_error("TransformerKVCache requires consistent valid_steps across all layers");
}
const size_t keep_elems = static_cast<size_t>(source.valid_steps * step_elems_);
if (source.key.size() != source.value.size()) {
throw std::runtime_error("TransformerKVCache source key/value sizes must match");
}
if (source.key.size() != keep_elems) {
throw std::runtime_error("TransformerKVCache source tensors do not match valid_steps * step_elems");
}
if (cache_steps_ > 0) {
std::fill(cache.import_key_scratch.begin(), cache.import_key_scratch.end(), 0.0F);
std::fill(cache.import_value_scratch.begin(), cache.import_value_scratch.end(), 0.0F);
if (keep_elems > 0) {
if (!options_.ring_mode) {
std::copy(source.key.begin(), source.key.end(), cache.import_key_scratch.begin());
std::copy(source.value.begin(), source.value.end(), cache.import_value_scratch.begin());
} else {
for (int64_t row = 0; row < state_steps; ++row) {
const int64_t position = ring_stored_position(
row, state_steps, current_end_, options_.ring_pinned_steps);
copy_cache_row(
source.key, row, cache.import_key_scratch, slot_for_position(position), step_elems_);
copy_cache_row(
source.value, row, cache.import_value_scratch, slot_for_position(position), step_elems_);
}
}
}
write_cache_tensor(cache.key_tensor, cache.import_key_scratch, options_);
write_cache_tensor(cache.value_tensor, cache.import_value_scratch, options_);
}
}
}
TransformerKVState TransformerKVCache::export_state() const {
TransformerKVState state;
state.current_end = current_end_;
state.layers.resize(layers_.size());
const size_t keep_elems = static_cast<size_t>(valid_steps_ * step_elems_);
for (size_t layer = 0; layer < layers_.size(); ++layer) {
auto & out = state.layers[layer];
out.valid_steps = valid_steps_;
if (keep_elems == 0) {
continue;
}
const auto key_values = read_cache_tensor(layers_[layer].key_tensor, options_);
const auto value_values = read_cache_tensor(layers_[layer].value_tensor, options_);
if (!options_.ring_mode) {
out.key.assign(key_values.begin(), key_values.begin() + static_cast<ptrdiff_t>(keep_elems));
out.value.assign(value_values.begin(), value_values.begin() + static_cast<ptrdiff_t>(keep_elems));
continue;
}
out.key.resize(keep_elems);
out.value.resize(keep_elems);
for (int64_t row = 0; row < valid_steps_; ++row) {
const int64_t position =
ring_stored_position(row, valid_steps_, current_end_, options_.ring_pinned_steps);
copy_cache_row(key_values, slot_for_position(position), out.key, row, step_elems_);
copy_cache_row(value_values, slot_for_position(position), out.value, row, step_elems_);
}
}
return state;
}
int64_t TransformerKVCache::slot_for_position(int64_t position) const {
if (position < 0) {
throw std::runtime_error("TransformerKVCache slot_for_position requires a non-negative position");
}
if (!options_.ring_mode || position < options_.ring_pinned_steps) {
return position;
}
return options_.ring_pinned_steps +
((position - options_.ring_pinned_steps) % (cache_steps_ - options_.ring_pinned_steps));
}
void TransformerKVCache::advance_after_direct_append(int64_t steps) {
if (steps <= 0) {
return;
}
if (options_.ring_mode) {
valid_steps_ = std::min(cache_steps_, valid_steps_ + steps);
current_end_ += steps;
return;
}
if (valid_steps_ + steps > cache_steps_) {
throw std::runtime_error("TransformerKVCache direct append exceeds cache capacity");
}
valid_steps_ += steps;
current_end_ += steps;
}
void TransformerKVCache::retain_prefix(int64_t prefix_steps) {
if (prefix_steps < 0 || prefix_steps > valid_steps_) {
throw std::runtime_error("TransformerKVCache prefix length exceeds current state");
}
valid_steps_ = prefix_steps;
current_end_ = prefix_steps;
}
int64_t TransformerKVCache::valid_steps() const noexcept {
return valid_steps_;
}
int64_t TransformerKVCache::current_end() const noexcept {
return current_end_;
}
int64_t TransformerKVCache::cache_steps() const noexcept {
return cache_steps_;
}
const core::TensorValue & TransformerKVCache::key_tensor(size_t layer) const {
return layers_.at(layer).key_tensor;
}
const core::TensorValue & TransformerKVCache::value_tensor(size_t layer) const {
return layers_.at(layer).value_tensor;
}
void TransformerKVCache::trace_log_state(const std::string & name, int64_t num_heads, int64_t head_dim) const {
if (!debug::trace_log_enabled()) {
return;
}
debug::trace_log_scalar(name + ".current_end", current_end_);
if (layers_.empty() || valid_steps_ <= 0) {
return;
}
const size_t keep_elems = static_cast<size_t>(valid_steps_ * step_elems_);
const auto first_key = read_cache_tensor(layers_.front().key_tensor, options_);
std::vector<float> first_key_keep(first_key.begin(), first_key.begin() + static_cast<ptrdiff_t>(keep_elems));
debug::trace_log_f32(name + ".layer0.key", {1, valid_steps_, num_heads, head_dim}, first_key_keep);
if (layers_.size() > 1) {
const auto last_key = read_cache_tensor(layers_.back().key_tensor, options_);
std::vector<float> last_key_keep(last_key.begin(), last_key.begin() + static_cast<ptrdiff_t>(keep_elems));
debug::trace_log_f32(name + ".layer_last.key", {1, valid_steps_, num_heads, head_dim}, last_key_keep);
}
}
TransformerBatchedKVCache::TransformerBatchedKVCache(
int64_t cache_steps,
int64_t batch_size,
int64_t row_elems,
std::vector<core::TensorValue> keys,
std::vector<core::TensorValue> values)
: TransformerBatchedKVCache(cache_steps, batch_size, row_elems, std::move(keys), std::move(values), {}) {}
TransformerBatchedKVCache::TransformerBatchedKVCache(
int64_t cache_steps,
int64_t batch_size,
int64_t row_elems,
std::vector<core::TensorValue> keys,
std::vector<core::TensorValue> values,
TransformerKVCacheOptions options)
: cache_steps_(std::max<int64_t>(0, cache_steps)),
batch_size_(std::max<int64_t>(0, batch_size)),
row_elems_(std::max<int64_t>(0, row_elems)),
options_(options) {
if (cache_steps_ <= 0 || batch_size_ <= 0 || row_elems_ <= 0) {
throw std::runtime_error("TransformerBatchedKVCache requires positive cache_steps, batch_size, and row_elems");
}
if (keys.size() != values.size()) {
throw std::runtime_error("TransformerBatchedKVCache key/value layer counts must match");
}
const size_t cache_elems = static_cast<size_t>(batch_size_ * cache_steps_ * row_elems_);
layers_.reserve(keys.size());
for (size_t layer = 0; layer < keys.size(); ++layer) {
validate_cache_tensor(keys[layer], options_);
validate_cache_tensor(values[layer], options_);
layers_.push_back(LayerCache{
std::move(keys[layer]),
std::move(values[layer]),
std::vector<float>(cache_elems, 0.0F),
std::vector<float>(cache_elems, 0.0F),
});
}
}
void TransformerBatchedKVCache::import_state(const TransformerBatchedKVState & state) {
if (state.batch_size != batch_size_) {
throw std::runtime_error("TransformerBatchedKVCache state batch size does not match cache batch size");
}
if (!state.valid_steps_by_batch.empty() &&
state.valid_steps_by_batch.size() != static_cast<size_t>(batch_size_)) {
throw std::runtime_error("TransformerBatchedKVCache valid_steps_by_batch size mismatch");
}
if (!state.current_end_by_batch.empty() &&
state.current_end_by_batch.size() != static_cast<size_t>(batch_size_)) {
throw std::runtime_error("TransformerBatchedKVCache current_end_by_batch size mismatch");
}
current_end_by_batch_ = state.current_end_by_batch;
valid_steps_by_batch_ = state.valid_steps_by_batch;
current_end_ = current_end_by_batch_.empty()
? state.current_end
: *std::max_element(current_end_by_batch_.begin(), current_end_by_batch_.end());
if (layers_.empty()) {
valid_steps_ = 0;
return;
}
if (state.layers.size() != layers_.size()) {
throw std::runtime_error("TransformerBatchedKVCache state layer count does not match cache layer count");
}
const int64_t state_steps = valid_steps_by_batch_.empty()
? (state.layers.empty() ? 0 : state.layers.front().valid_steps)
: *std::max_element(valid_steps_by_batch_.begin(), valid_steps_by_batch_.end());
if (state_steps > cache_steps_) {
throw std::runtime_error("TransformerBatchedKVCache state valid_steps exceeds cache capacity");
}
valid_steps_ = state_steps;
for (size_t layer = 0; layer < layers_.size(); ++layer) {
auto & cache = layers_[layer];
const auto & source = state.layers[layer];
if (valid_steps_by_batch_.empty() && source.valid_steps != state_steps) {
throw std::runtime_error("TransformerBatchedKVCache requires consistent valid_steps across all layers");
}
std::fill(cache.import_key_scratch.begin(), cache.import_key_scratch.end(), 0.0F);
std::fill(cache.import_value_scratch.begin(), cache.import_value_scratch.end(), 0.0F);
const bool variable_rows = !valid_steps_by_batch_.empty();
size_t src_offset = 0;
if (!variable_rows) {
const size_t source_row_elems = static_cast<size_t>(state_steps * row_elems_);
const size_t state_elems = static_cast<size_t>(batch_size_) * source_row_elems;
if (source.key.size() != source.value.size() || source.key.size() != state_elems) {
throw std::runtime_error(
"TransformerBatchedKVCache source tensors do not match batch * valid_steps * row_elems");
}
}
for (int64_t batch = 0; batch < batch_size_; ++batch) {
const int64_t row_steps = variable_rows ? valid_steps_by_batch_[static_cast<size_t>(batch)] : state_steps;
if (row_steps < 0 || row_steps > state_steps) {
throw std::runtime_error("TransformerBatchedKVCache row valid_steps is invalid");
}
const size_t copy_elems = static_cast<size_t>(row_steps * row_elems_);
const size_t dst_offset = static_cast<size_t>(batch * cache_steps_ * row_elems_);
if (!variable_rows) {
src_offset = static_cast<size_t>(batch) * static_cast<size_t>(state_steps * row_elems_);
}
if (src_offset + copy_elems > source.key.size() || source.key.size() != source.value.size()) {
throw std::runtime_error("TransformerBatchedKVCache compact source tensor size mismatch");
}
std::copy(
source.key.begin() + static_cast<std::ptrdiff_t>(src_offset),
source.key.begin() + static_cast<std::ptrdiff_t>(src_offset + copy_elems),
cache.import_key_scratch.begin() + static_cast<std::ptrdiff_t>(dst_offset));
std::copy(
source.value.begin() + static_cast<std::ptrdiff_t>(src_offset),
source.value.begin() + static_cast<std::ptrdiff_t>(src_offset + copy_elems),
cache.import_value_scratch.begin() + static_cast<std::ptrdiff_t>(dst_offset));
if (variable_rows) {
src_offset += copy_elems;
}
}
if (variable_rows && src_offset != source.key.size()) {
throw std::runtime_error("TransformerBatchedKVCache compact source tensor has trailing values");
}
write_cache_tensor(cache.key_tensor, cache.import_key_scratch, options_);
write_cache_tensor(cache.value_tensor, cache.import_value_scratch, options_);
}
}
TransformerBatchedKVState TransformerBatchedKVCache::export_state() const {
TransformerBatchedKVState state;
state.batch_size = batch_size_;
state.current_end = current_end_;
state.current_end_by_batch = current_end_by_batch_;
state.valid_steps_by_batch = valid_steps_by_batch_;
state.layers.resize(layers_.size());
const size_t copy_elems = static_cast<size_t>(valid_steps_ * row_elems_);
const size_t state_elems = static_cast<size_t>(batch_size_) * copy_elems;
for (size_t layer = 0; layer < layers_.size(); ++layer) {
auto & out = state.layers[layer];
out.valid_steps = valid_steps_;
out.key.resize(state_elems);
out.value.resize(state_elems);
if (copy_elems == 0) {
continue;
}
const auto key_values = read_cache_tensor(layers_[layer].key_tensor, options_);
const auto value_values = read_cache_tensor(layers_[layer].value_tensor, options_);
for (int64_t batch = 0; batch < batch_size_; ++batch) {
const size_t src_offset = static_cast<size_t>(batch * cache_steps_ * row_elems_);
const size_t dst_offset = static_cast<size_t>(batch) * copy_elems;
std::copy(
key_values.begin() + static_cast<std::ptrdiff_t>(src_offset),
key_values.begin() + static_cast<std::ptrdiff_t>(src_offset + copy_elems),
out.key.begin() + static_cast<std::ptrdiff_t>(dst_offset));
std::copy(
value_values.begin() + static_cast<std::ptrdiff_t>(src_offset),
value_values.begin() + static_cast<std::ptrdiff_t>(src_offset + copy_elems),
out.value.begin() + static_cast<std::ptrdiff_t>(dst_offset));
}
}
return state;
}
void TransformerBatchedKVCache::advance_after_direct_append(int64_t steps) {
if (steps <= 0) {
return;
}
if (valid_steps_ + steps > cache_steps_) {
throw std::runtime_error("TransformerBatchedKVCache direct append exceeds cache capacity");
}
valid_steps_ += steps;
current_end_ += steps;
for (auto & value : valid_steps_by_batch_) {
value += steps;
}
for (auto & value : current_end_by_batch_) {
value += steps;
}
}
int64_t TransformerBatchedKVCache::batch_size() const noexcept {
return batch_size_;
}
int64_t TransformerBatchedKVCache::valid_steps() const noexcept {
return valid_steps_;
}
int64_t TransformerBatchedKVCache::current_end() const noexcept {
return current_end_;
}
int64_t TransformerBatchedKVCache::cache_steps() const noexcept {
return cache_steps_;
}
const std::vector<int64_t> & TransformerBatchedKVCache::valid_steps_by_batch() const noexcept {
return valid_steps_by_batch_;
}
const std::vector<int64_t> & TransformerBatchedKVCache::current_end_by_batch() const noexcept {
return current_end_by_batch_;
}
core::TensorValue view_transformer_kv_cache_steps(
core::ModuleBuildContext & ctx,
const core::TensorValue & cache,
int64_t start,
int64_t steps,
int64_t heads,
int64_t head_dim,
const char * label,
ggml_type view_type) {
if (start < 0 || steps <= 0 || start + steps > cache.shape.dims[1]) {
throw std::runtime_error(std::string(label) + " cache view range is invalid");
}
return core::wrap_tensor(
ggml_view_4d(
ctx.ggml,
cache.tensor,
head_dim,
heads,
steps,
1,
cache.tensor->nb[1],
cache.tensor->nb[2],
cache.tensor->nb[3],
static_cast<size_t>(start) * cache.tensor->nb[2]),
core::TensorShape::from_dims({1, steps, heads, head_dim}),
view_type);
}
} // namespace engine::runtime