forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_pagedattn_inputs.cpp
More file actions
380 lines (359 loc) · 22.9 KB
/
convert_pagedattn_inputs.cpp
File metadata and controls
380 lines (359 loc) · 22.9 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
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "transformations/common_optimizations/convert_pagedattn_inputs.hpp"
#include <gtest/gtest.h>
#include "common_test_utils/ov_test_utils.hpp"
#include "openvino/core/rt_info.hpp"
#include "openvino/op/paged_attention.hpp"
#include "openvino/runtime/properties.hpp"
#include "transformations/utils/gen_pattern.hpp"
using namespace ov::test;
using namespace ov;
using namespace ov::gen_pattern;
using ConvertPagedAttnInputsParams = std::tuple<std::vector<ov::element::Type>, // cache_precision
std::vector<size_t>, // cache_group_size
std::vector<size_t>, // block_size
ov::element::Type, // infer_precsion
bool, // quant_key_by_channel
bool, // accuracy_mode
bool // is_ir_kv_cache_f16
>;
namespace v0 = ov::op::v0;
namespace {
class ConvertPagedAttnInputsTest : public TransformationTestsF,
public testing::WithParamInterface<ConvertPagedAttnInputsParams> {
public:
static std::string getTestCaseName(const testing::TestParamInfo<ConvertPagedAttnInputsParams>& obj) {
const auto& [cachePrecision,
cacheGroupSize,
blcokSize,
inferPrec,
quantKeybychannel,
isAccuracyMode,
isIRKVCacheF16] = obj.param;
std::ostringstream result;
result << "KeyPrc=" << cachePrecision[0] << "_";
result << "ValuePrc=" << cachePrecision[1] << "_";
result << "KeyBlockSize=" << blcokSize[0] << "_";
result << "ValueBlockSize=" << blcokSize[1] << "_";
result << "InferPrec=" << inferPrec << "_";
result << "KeyGS=" << cacheGroupSize[0] << "_";
result << "ValueGS=" << cacheGroupSize[1] << "_";
result << "KeyByChannel=" << quantKeybychannel << "_";
result << "isAccuracyMode=" << isAccuracyMode << "_";
result << "isIRKVCacheF16=" << isIRKVCacheF16;
return result.str();
}
public:
size_t keyHeadSize;
size_t valueHeadSize;
size_t numKeyHeads;
size_t numValueHeads;
ov::element::Type inferPrec;
ov::element::Type keyCachePrecision;
ov::element::Type valueCachePrecision;
std::vector<size_t> blockSize;
size_t keyCacheGroupSize;
size_t valueCacheGroupSize;
bool quantKeybychannel;
bool isAccuracyMode;
bool isIRKVCacheF16;
};
TEST_P(ConvertPagedAttnInputsTest, checkPrecisionAndShape) {
const auto& [cachePrecision,
cacheGroupSize,
_blockSize,
_inferPrec,
_quantKeybychannel,
_isAccuracyMode,
_isIRKVCacheF16] = this->GetParam();
blockSize = _blockSize;
inferPrec = _inferPrec;
quantKeybychannel = _quantKeybychannel;
isAccuracyMode = _isAccuracyMode;
isIRKVCacheF16 = _isIRKVCacheF16;
keyCachePrecision = cachePrecision[0];
valueCachePrecision = cachePrecision[1];
keyCacheGroupSize = cacheGroupSize[0];
valueCacheGroupSize = cacheGroupSize[1];
numKeyHeads = 2;
keyHeadSize = 32;
numValueHeads = 2;
valueHeadSize = 64;
{
auto Q = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{-1, 4 * 32});
auto K = std::make_shared<v0::Parameter>(
ov::element::f32,
PartialShape{-1, static_cast<ov::Dimension::value_type>(numKeyHeads * keyHeadSize)});
auto V = std::make_shared<v0::Parameter>(
ov::element::f32,
PartialShape{-1, static_cast<ov::Dimension::value_type>(numValueHeads * valueHeadSize)});
auto max_context_len = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{});
auto block_indices_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto subsequence_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto past_lens = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto key_cache_0 = std::make_shared<v0::Parameter>(ov::element::dynamic, PartialShape::dynamic(4));
auto value_cache_0 = std::make_shared<v0::Parameter>(ov::element::dynamic, PartialShape::dynamic(4));
auto scale = std::make_shared<v0::Constant>(element::f32, Shape{}, 0.5f);
auto sliding_window = std::make_shared<v0::Constant>(element::i32, Shape{}, 0);
auto alibi_slopes = std::make_shared<v0::Constant>(element::f32, Shape{0});
auto score_aggregation_window = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto rotated_block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto rotation_deltas = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto rotation_trig_lut = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{DYN});
auto xattention_threshold = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{DYN});
auto xattention_block_size = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
auto xattention_stride = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
auto sinks = std::make_shared<v0::Constant>(element::f32, Shape{0, 0, 0, 0});
auto adaptive_rkv_start_size = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
auto adaptive_rkv_evictable_sizes = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto adaptive_rkv_diversity_block_set_indices =
std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto adaptive_rkv_diversity_block_set_indices_begins =
std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto token_type_ids = std::make_shared<op::v0::Parameter>(ov::element::i32, ov::Shape{0});
auto pa =
std::make_shared<op::PagedAttentionExtension>(OutputVector{Q,
K,
V,
key_cache_0,
value_cache_0,
past_lens,
subsequence_begins,
block_indices,
block_indices_begins,
scale,
sliding_window,
alibi_slopes,
max_context_len,
score_aggregation_window,
rotated_block_indices,
rotation_deltas,
rotation_trig_lut,
xattention_threshold,
xattention_block_size,
xattention_stride,
sinks,
adaptive_rkv_start_size,
adaptive_rkv_evictable_sizes,
adaptive_rkv_diversity_block_set_indices,
adaptive_rkv_diversity_block_set_indices_begins,
token_type_ids});
pa->get_rt_info()["num_k_heads"] = numKeyHeads;
pa->get_rt_info()["k_head_size"] = keyHeadSize;
pa->get_rt_info()["num_v_heads"] = numValueHeads;
pa->get_rt_info()["v_head_size"] = valueHeadSize;
model = std::make_shared<ov::Model>(ov::OutputVector{pa},
ov::ParameterVector{Q,
K,
V,
key_cache_0,
value_cache_0,
past_lens,
subsequence_begins,
block_indices,
block_indices_begins,
max_context_len,
score_aggregation_window,
rotated_block_indices,
rotation_deltas,
rotation_trig_lut,
xattention_threshold,
xattention_block_size,
xattention_stride,
adaptive_rkv_start_size,
adaptive_rkv_evictable_sizes,
adaptive_rkv_diversity_block_set_indices,
adaptive_rkv_diversity_block_set_indices_begins,
token_type_ids});
if (isIRKVCacheF16) {
model->set_rt_info("f16", "runtime_options", ov::hint::kv_cache_precision.name());
}
}
{
auto getCachePrec = [&](ov::element::Type tensorPrec) {
tensorPrec =
tensorPrec == ov::element::f16 && inferPrec == ov::element::bf16 ? ov::element::bf16 : tensorPrec;
if (isAccuracyMode)
tensorPrec = ov::element::f32;
return tensorPrec;
};
auto getCacheShape = [&](ov::element::Type cachePrec,
size_t headNums,
size_t headSize,
size_t groupSize,
size_t blockSize,
bool quantBychannel) {
auto targeShape = PartialShape::dynamic(4);
targeShape[1] = headNums;
groupSize = groupSize ? groupSize : headSize;
const size_t paramSize = 2 * sizeof(float) * 8 / cachePrec.bitwidth();
if (!cachePrec.is_integral()) {
targeShape[2] = blockSize;
targeShape[3] = headSize;
} else if (quantBychannel) {
targeShape[2] = paramSize + blockSize;
targeShape[3] = headSize;
} else {
size_t groupNum = headSize / groupSize;
targeShape[2] = blockSize;
targeShape[3] = headSize + paramSize * groupNum;
}
return targeShape;
};
keyCachePrecision = getCachePrec(keyCachePrecision);
valueCachePrecision = getCachePrec(valueCachePrecision);
auto Q = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{-1, 4 * 32});
auto K = std::make_shared<v0::Parameter>(
ov::element::f32,
PartialShape{-1, static_cast<ov::Dimension::value_type>(numKeyHeads * keyHeadSize)});
auto V = std::make_shared<v0::Parameter>(
ov::element::f32,
PartialShape{-1, static_cast<ov::Dimension::value_type>(numValueHeads * valueHeadSize)});
auto max_context_len = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{});
auto block_indices_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto subsequence_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto past_lens = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto key_cache_0 = std::make_shared<v0::Parameter>(keyCachePrecision,
getCacheShape(keyCachePrecision,
numKeyHeads,
keyHeadSize,
keyCacheGroupSize,
blockSize[0],
quantKeybychannel));
auto value_cache_0 = std::make_shared<v0::Parameter>(
valueCachePrecision,
getCacheShape(valueCachePrecision, numKeyHeads, valueHeadSize, valueCacheGroupSize, blockSize[1], false));
auto scale = std::make_shared<v0::Constant>(element::f32, Shape{}, 0.5f);
auto sliding_window = std::make_shared<v0::Constant>(element::i32, Shape{}, 0);
auto alibi_slopes = std::make_shared<v0::Constant>(element::f32, Shape{0});
auto score_aggregation_window = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto rotated_block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto rotation_deltas = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto rotation_trig_lut = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{DYN});
auto xattention_threshold = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{DYN});
auto xattention_block_size = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
auto xattention_stride = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
auto sinks = std::make_shared<v0::Constant>(element::f32, Shape{0, 0, 0, 0});
auto adaptive_rkv_start_size = std::make_shared<v0::Parameter>(ov::element::i32, Shape{});
auto adaptive_rkv_evictable_sizes = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto adaptive_rkv_diversity_block_set_indices =
std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto adaptive_rkv_diversity_block_set_indices_begins =
std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
auto token_type_ids = std::make_shared<v0::Parameter>(ov::element::i32, ov::Shape{0});
auto pa =
std::make_shared<op::PagedAttentionExtension>(OutputVector{Q,
K,
V,
key_cache_0,
value_cache_0,
past_lens,
subsequence_begins,
block_indices,
block_indices_begins,
scale,
sliding_window,
alibi_slopes,
max_context_len,
score_aggregation_window,
rotated_block_indices,
rotation_deltas,
rotation_trig_lut,
xattention_threshold,
xattention_block_size,
xattention_stride,
sinks,
adaptive_rkv_start_size,
adaptive_rkv_evictable_sizes,
adaptive_rkv_diversity_block_set_indices,
adaptive_rkv_diversity_block_set_indices_begins,
token_type_ids});
pa->get_rt_info()["num_k_heads"] = numKeyHeads;
pa->get_rt_info()["k_head_size"] = keyHeadSize;
pa->get_rt_info()["num_v_heads"] = numValueHeads;
pa->get_rt_info()["v_head_size"] = valueHeadSize;
model_ref = std::make_shared<ov::Model>(ov::OutputVector{pa},
ov::ParameterVector{Q,
K,
V,
key_cache_0,
value_cache_0,
past_lens,
subsequence_begins,
block_indices,
block_indices_begins,
max_context_len,
score_aggregation_window,
rotated_block_indices,
rotation_deltas,
rotation_trig_lut,
xattention_threshold,
xattention_block_size,
xattention_stride,
adaptive_rkv_start_size,
adaptive_rkv_evictable_sizes,
adaptive_rkv_diversity_block_set_indices,
adaptive_rkv_diversity_block_set_indices_begins,
token_type_ids});
}
ov::pass::ConvertPagedAttnInputs::KVCacheConfig cacheConfig;
cacheConfig.keyCacheBlockSize = blockSize[0];
cacheConfig.valueCacheBlockSize = blockSize[1];
if (isAccuracyMode) {
cacheConfig.inferencePrecision = ov::element::f32;
cacheConfig.keyCachePrecision = ov::element::f32;
cacheConfig.valueCachePrecision = ov::element::f32;
} else {
cacheConfig.keyCachePrecision = keyCachePrecision;
cacheConfig.valueCachePrecision = valueCachePrecision;
cacheConfig.keyCacheGroupSize = keyCacheGroupSize;
cacheConfig.valueCacheGroupSize = valueCacheGroupSize;
}
auto update_paged_attention_shape_func = [](const ov::element::Type& precision,
const bool bychannel,
const size_t group_num,
int64_t& head_size,
int64_t& block_size) {
if (precision == ov::element::u8) {
if (bychannel) {
block_size += 2 * sizeof(float);
} else {
head_size += sizeof(float) * 2 * group_num;
}
} else if (precision == ov::element::u4) {
head_size += sizeof(float) * 2 * group_num * 2;
}
};
manager.register_pass<ov::pass::ConvertPagedAttnInputs>(cacheConfig, update_paged_attention_shape_func);
comparator.disable(FunctionsComparator::ACCURACY);
comparator.disable(FunctionsComparator::RUNTIME_KEYS);
disable_result_friendly_names_check();
disable_rt_info_check();
}
std::vector<std::vector<ov::element::Type>> get_cache_prec() {
return {
{ov::element::f32, ov::element::f32},
{ov::element::f16, ov::element::f16},
{ov::element::u8, ov::element::u8},
{ov::element::u8, ov::element::u4},
};
}
// group size
const std::vector<std::vector<size_t>> cache_gs = {{32, 16}, {0, 0}};
// block size
const std::vector<std::vector<size_t>> cache_bs = {{32, 16}};
INSTANTIATE_TEST_SUITE_P(smoke_ConvertPagedAttnInputsTest,
ConvertPagedAttnInputsTest,
::testing::Combine(::testing::ValuesIn(get_cache_prec()),
::testing::ValuesIn(cache_gs),
::testing::ValuesIn(cache_bs),
::testing::Values(ov::element::f32, ov::element::bf16),
::testing::Values(false),
::testing::Values(true, false),
::testing::Values(true, false)),
ConvertPagedAttnInputsTest::getTestCaseName);
} // namespace