forked from microsoft/onnxruntime-genai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindowed_kv_cache.cpp
More file actions
338 lines (275 loc) · 17.1 KB
/
windowed_kv_cache.cpp
File metadata and controls
338 lines (275 loc) · 17.1 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "windowed_kv_cache.h"
#include "../generators.h"
#include "../logging.h"
#include "../make_string.h"
#include "../narrow.h"
#include "model.h"
#include "threadpool.h"
#include "utils.h"
namespace Generators {
namespace {
std::vector<size_t> MakeAllLayerIndices(size_t layer_count) {
std::vector<size_t> v(layer_count);
std::iota(v.begin(), v.end(), size_t{0});
return v;
}
} // namespace
std::vector<WindowedKeyValueCache::LayerState> WindowedKeyValueCache::MakeInitialPerLayerStates(
size_t layer_count,
size_t initial_window_size,
const CacheTensorShape& initial_key_cache_shape_in,
const CacheTensorShape& initial_key_cache_shape_out,
const CacheTensorShape& initial_value_cache_shape_in,
const CacheTensorShape& initial_value_cache_shape_out) {
std::vector<LayerState> per_layer_states{};
per_layer_states.reserve(layer_count);
for (size_t i = 0; i < layer_count; ++i) {
LayerState layer_state{};
layer_state.window_size = initial_window_size;
layer_state.key_cache_shape_in = initial_key_cache_shape_in;
layer_state.value_cache_shape_in = initial_value_cache_shape_in;
layer_state.key_cache_shape_out = initial_key_cache_shape_out;
layer_state.value_cache_shape_out = initial_value_cache_shape_out;
per_layer_states.push_back(std::move(layer_state));
}
return per_layer_states;
}
WindowedKeyValueCache::WindowedKeyValueCache(State& state)
: state_{state},
layer_count_{narrow<size_t>(model_.config_->model.decoder.num_hidden_layers)},
all_layer_indices_(MakeAllLayerIndices(layer_count_)) {
if (layer_count_ == 0) {
throw std::runtime_error("Expected there to be at least 1 layer in the model. Actual: " +
std::to_string(layer_count_) + ". Please check the num_hidden_layers attribute in the model configuration.");
}
const auto initial_window_size = model_.config_->model.decoder.sliding_window->window_size;
if (initial_window_size <= 1) {
throw std::runtime_error("Initial window size must be greater than 1. Actual: " +
std::to_string(initial_window_size) +
". Please check the sliding_window.window_size attribute in the model configuration.");
}
const auto num_key_value_heads = model_.config_->model.decoder.num_key_value_heads;
const auto head_size = model_.config_->model.decoder.head_size;
const auto context_length = model_.config_->model.context_length;
const auto initial_key_cache_shape_in =
CacheTensorShape{num_key_value_heads, 1, head_size, context_length - initial_window_size};
const auto initial_key_cache_shape_out =
CacheTensorShape{num_key_value_heads, 1, head_size, initial_window_size};
const auto initial_value_cache_shape_in =
CacheTensorShape{num_key_value_heads, 1, context_length - initial_window_size, head_size};
const auto initial_value_cache_shape_out =
CacheTensorShape{num_key_value_heads, 1, initial_window_size, head_size};
per_layer_states_ = MakeInitialPerLayerStates(layer_count_, static_cast<size_t>(initial_window_size),
initial_key_cache_shape_in, initial_key_cache_shape_out,
initial_value_cache_shape_in, initial_value_cache_shape_out);
for (int i = 0; i < static_cast<int>(layer_count_); ++i) {
input_name_strings_.emplace_back(ComposeKeyValueName(model_.config_->model.decoder.inputs.past_key_names, i));
input_name_strings_.emplace_back(ComposeKeyValueName(model_.config_->model.decoder.inputs.past_value_names, i));
output_name_strings_.emplace_back(ComposeKeyValueName(model_.config_->model.decoder.outputs.present_key_names, i));
output_name_strings_.emplace_back(ComposeKeyValueName(model_.config_->model.decoder.outputs.present_value_names, i));
}
type_ = model_.session_info_.GetInputDataType(input_name_strings_[0]);
if (type_ != Ort::TypeToTensorType<uint8_t>) {
throw std::runtime_error("Expected input data type to be uint8_t for WindowedKeyValueCache. Actual: " +
std::to_string(type_));
}
for (size_t i = 0; i < layer_count_; ++i) {
key_caches_in_.push_back(
OrtValue::CreateTensor(Allocator(), initial_key_cache_shape_in, type_));
std::fill_n(key_caches_in_[i]->GetTensorMutableData<uint8_t>(),
ElementCountFromShape(initial_key_cache_shape_in),
static_cast<uint8_t>(model_.config_->model.decoder.sliding_window->pad_value));
value_caches_in_.push_back(
OrtValue::CreateTensor(Allocator(), initial_value_cache_shape_in, type_));
std::fill_n(value_caches_in_[i]->GetTensorMutableData<uint8_t>(),
ElementCountFromShape(initial_value_cache_shape_in),
static_cast<uint8_t>(model_.config_->model.decoder.sliding_window->pad_value));
key_caches_out_.push_back(
OrtValue::CreateTensor(Allocator(), initial_key_cache_shape_out, type_));
value_caches_out_.push_back(
OrtValue::CreateTensor(Allocator(), initial_value_cache_shape_out, type_));
}
}
void WindowedKeyValueCache::Add() {
input_index_ = state_.inputs_.size();
output_index_ = state_.outputs_.size();
for (size_t layer_idx = 0; layer_idx < layer_count_; ++layer_idx) {
state_.inputs_.push_back(key_caches_in_[layer_idx].get());
state_.input_names_.push_back(input_name_strings_[2 * layer_idx].c_str());
state_.inputs_.push_back(value_caches_in_[layer_idx].get());
state_.input_names_.push_back(input_name_strings_[2 * layer_idx + 1].c_str());
state_.outputs_.push_back(key_caches_out_[layer_idx].get());
state_.output_names_.push_back(output_name_strings_[2 * layer_idx].c_str());
state_.outputs_.push_back(value_caches_out_[layer_idx].get());
state_.output_names_.push_back(output_name_strings_[2 * layer_idx + 1].c_str());
}
}
void WindowedKeyValueCache::SlideLayer(size_t layer_idx) {
const auto& layer_state = per_layer_states_[layer_idx];
const auto window_size = layer_state.window_size;
const auto seq_len = layer_state.window_index * layer_state.window_size;
const auto& key_cache_shape_in = layer_state.key_cache_shape_in;
const auto& key_cache_shape_out = layer_state.key_cache_shape_out;
const auto& value_cache_shape_in = layer_state.value_cache_shape_in;
const auto& value_cache_shape_out = layer_state.value_cache_shape_out;
uint8_t* key_cache_in_data = key_caches_in_[layer_idx]->GetTensorMutableData<uint8_t>();
uint8_t* key_cache_out_data = key_caches_out_[layer_idx]->GetTensorMutableData<uint8_t>();
int64_t num_key_cache_chunks = key_cache_shape_in[0] * key_cache_shape_in[2];
for (int64_t j = 0; j < num_key_cache_chunks; ++j) {
{
cpu_span<uint8_t> key_cache_dst(key_cache_in_data + j * key_cache_shape_in[3] + key_cache_shape_in[3] - seq_len - window_size,
seq_len);
cpu_span<uint8_t> key_cache_src(key_cache_in_data + j * key_cache_shape_in[3] + key_cache_shape_in[3] - seq_len,
seq_len);
std::copy(key_cache_src.begin(), key_cache_src.end(), key_cache_dst.begin());
}
{
cpu_span<uint8_t> key_cache_dst(key_cache_in_data + j * key_cache_shape_in[3] + key_cache_shape_in[3] - window_size,
window_size);
cpu_span<uint8_t> key_cache_src(key_cache_out_data + j * key_cache_shape_out[3],
window_size);
std::copy(key_cache_src.begin(), key_cache_src.end(), key_cache_dst.begin());
}
}
uint8_t* value_cache_in_data = value_caches_in_[layer_idx]->GetTensorMutableData<uint8_t>();
uint8_t* value_cache_out_data = value_caches_out_[layer_idx]->GetTensorMutableData<uint8_t>();
for (int64_t j = 0; j < value_cache_shape_in[0]; ++j) {
{
cpu_span<uint8_t> value_cache_dst(value_cache_in_data + (j * value_cache_shape_in[2] * value_cache_shape_in[3]) +
((value_cache_shape_in[2] - seq_len - window_size) * value_cache_shape_in[3]),
seq_len * value_cache_shape_in[3]);
cpu_span<uint8_t> value_cache_src(value_cache_in_data + (j * value_cache_shape_in[2] * value_cache_shape_in[3]) +
((value_cache_shape_in[2] - seq_len) * value_cache_shape_in[3]),
seq_len * value_cache_shape_in[3]);
std::copy(value_cache_src.begin(), value_cache_src.end(), value_cache_dst.begin());
}
{
cpu_span<uint8_t> value_cache_dst(value_cache_in_data + (j * value_cache_shape_in[2] * value_cache_shape_in[3]) +
((value_cache_shape_in[2] - window_size) * value_cache_shape_in[3]),
window_size * value_cache_shape_in[3]);
cpu_span<uint8_t> value_cache_src(value_cache_out_data + (j * value_cache_shape_out[2] * value_cache_shape_out[3]),
window_size * value_cache_shape_out[3]);
std::copy(value_cache_src.begin(), value_cache_src.end(), value_cache_dst.begin());
}
}
}
void WindowedKeyValueCache::TransitionLayerToTokenGeneration(size_t layer_idx) {
// Transition from prompt processing to token generation.
// Concatenate the last window_size elements to the end of the cache
// key_caches_in = Concat(key_caches_in[:, :, :, 1:], key_caches_out)
// [num_key_value_heads, 1, head_size, context_length-1] = [num_key_value_heads, 1, head_size, context_length - window_size_ - 1] +
// [num_key_value_heads, 1, head_size, window_size]
// value_cache = Concat(value_caches_in[:, :, 1:, :], value_caches_out)
// [num_key_value_heads, 1, context_length - 1, head_size] = [num_key_value_heads, 1, context_length - window_size - 1, head_size] +
// [num_key_value_heads, 1, window_size_, head_size]
auto& layer_state = per_layer_states_[layer_idx];
const auto window_size = layer_state.window_size;
const auto& key_cache_shape_in = layer_state.key_cache_shape_in;
const auto& key_cache_shape_out = layer_state.key_cache_shape_out;
const auto& value_cache_shape_in = layer_state.value_cache_shape_in;
const auto& value_cache_shape_out = layer_state.value_cache_shape_out;
constexpr int updated_window_size = 1;
const auto num_key_value_heads = model_.config_->model.decoder.num_key_value_heads;
const auto head_size = model_.config_->model.decoder.head_size;
const auto context_length = model_.config_->model.context_length;
const auto updated_key_cache_shape_in = std::array<int64_t, 4>{num_key_value_heads, 1,
head_size,
context_length - updated_window_size};
const auto updated_value_cache_shape_in = std::array<int64_t, 4>{num_key_value_heads, 1,
context_length - updated_window_size,
head_size};
const auto updated_key_cache_shape_out = std::array<int64_t, 4>{num_key_value_heads, 1,
head_size,
updated_window_size};
const auto updated_value_cache_shape_out = std::array<int64_t, 4>{num_key_value_heads, 1,
updated_window_size,
head_size};
std::unique_ptr<OrtValue> key_cache = OrtValue::CreateTensor(Allocator(), updated_key_cache_shape_in, type_);
uint8_t* key_cache_data = key_cache->GetTensorMutableData<uint8_t>();
uint8_t* key_cache_in_data = key_caches_in_[layer_idx]->GetTensorMutableData<uint8_t>();
uint8_t* key_cache_out_data = key_caches_out_[layer_idx]->GetTensorMutableData<uint8_t>();
int64_t num_key_cache_chunks = updated_key_cache_shape_in[0] * updated_key_cache_shape_in[2];
for (int64_t j = 0; j < num_key_cache_chunks; ++j) {
{
cpu_span<uint8_t> key_cache_dst(key_cache_data + j * updated_key_cache_shape_in[3],
updated_key_cache_shape_in[3] - updated_window_size);
cpu_span<uint8_t> key_cache_src(key_cache_in_data + j * key_cache_shape_in[3] + updated_window_size,
key_cache_shape_in[3] - updated_window_size);
std::copy(key_cache_src.begin(), key_cache_src.end(), key_cache_dst.begin());
}
{
cpu_span<uint8_t> key_cache_dst(key_cache_data + j * updated_key_cache_shape_in[3] +
key_cache_shape_in[3] - updated_window_size,
window_size);
cpu_span<uint8_t> key_cache_src(key_cache_out_data + j * key_cache_shape_out[3],
window_size);
std::copy(key_cache_src.begin(), key_cache_src.end(), key_cache_dst.begin());
}
}
key_caches_in_[layer_idx] = std::move(key_cache);
key_caches_out_[layer_idx] = OrtValue::CreateTensor(Allocator(), updated_key_cache_shape_out, type_);
std::unique_ptr<OrtValue> value_cache = OrtValue::CreateTensor(Allocator(), updated_value_cache_shape_in, type_);
uint8_t* value_cache_data = value_cache->GetTensorMutableData<uint8_t>();
uint8_t* value_cache_in_data = value_caches_in_[layer_idx]->GetTensorMutableData<uint8_t>();
uint8_t* value_cache_out_data = value_caches_out_[layer_idx]->GetTensorMutableData<uint8_t>();
for (int64_t j = 0; j < updated_value_cache_shape_in[0]; ++j) {
{
cpu_span<uint8_t> value_cache_dst(value_cache_data + (j * updated_value_cache_shape_in[2] * updated_value_cache_shape_in[3]),
(value_cache_shape_in[2] - updated_window_size) * updated_value_cache_shape_in[3]);
cpu_span<uint8_t> value_cache_src(value_cache_in_data + (j * value_cache_shape_in[2] * value_cache_shape_in[3]) +
(updated_window_size * value_cache_shape_in[3]),
(value_cache_shape_in[2] - updated_window_size) * value_cache_shape_in[3]);
std::copy(value_cache_src.begin(), value_cache_src.end(), value_cache_dst.begin());
}
{
cpu_span<uint8_t> value_cache_dst(value_cache_data + (j * updated_value_cache_shape_in[2] * updated_value_cache_shape_in[3]) +
((value_cache_shape_in[2] - updated_window_size) * updated_value_cache_shape_in[3]),
value_cache_shape_out[2] * value_cache_shape_out[3]);
cpu_span<uint8_t> value_cache_src(value_cache_out_data + (j * value_cache_shape_out[2] * value_cache_shape_out[3]),
value_cache_shape_out[2] * value_cache_shape_out[3]);
std::copy(value_cache_src.begin(), value_cache_src.end(), value_cache_dst.begin());
}
}
value_caches_in_[layer_idx] = std::move(value_cache);
value_caches_out_[layer_idx] = OrtValue::CreateTensor(Allocator(), updated_value_cache_shape_out, type_);
// update values in per-layer state
layer_state.window_index = layer_state.window_index * layer_state.window_size / updated_window_size;
layer_state.window_size = updated_window_size;
layer_state.key_cache_shape_in = updated_key_cache_shape_in;
layer_state.value_cache_shape_in = updated_value_cache_shape_in;
layer_state.key_cache_shape_out = updated_key_cache_shape_out;
layer_state.value_cache_shape_out = updated_value_cache_shape_out;
state_.inputs_[input_index_ + 2 * layer_idx] = key_caches_in_[layer_idx].get();
state_.inputs_[input_index_ + 2 * layer_idx + 1] = value_caches_in_[layer_idx].get();
state_.outputs_[output_index_ + 2 * layer_idx] = key_caches_out_[layer_idx].get();
state_.outputs_[output_index_ + 2 * layer_idx + 1] = value_caches_out_[layer_idx].get();
}
void WindowedKeyValueCache::UpdateLayer(DeviceSpan<int32_t> /*beam_indices*/, int total_length, size_t layer_idx) {
assert(layer_idx < layer_count_);
auto& layer_state = per_layer_states_[layer_idx];
if (layer_state.is_first_update) {
layer_state.num_windows = (total_length + layer_state.window_size - 1) / layer_state.window_size;
layer_state.is_first_update = false;
++layer_state.window_index;
return;
}
if (layer_state.window_size == 1 || layer_state.window_index < layer_state.num_windows) {
SlideLayer(layer_idx);
++layer_state.window_index;
return;
}
TransitionLayerToTokenGeneration(layer_idx);
}
void WindowedKeyValueCache::Update(DeviceSpan<int32_t> beam_indices, int current_length) {
PartialUpdate(beam_indices, current_length, all_layer_indices_);
}
void WindowedKeyValueCache::PartialUpdate(DeviceSpan<int32_t> beam_indices, int total_length,
std::span<const size_t> layer_indices) {
ThreadPool thread_pool{layer_indices.size()};
thread_pool.Compute([&](size_t i) {
UpdateLayer(beam_indices, total_length, layer_indices[i]);
});
}
} // namespace Generators