Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] Paged attention cache rotation (#28232)

Sergey Shlyapnikovc8c1438061df
diff --git a/src/core/src/op/paged_attention.cpp b/src/core/src/op/paged_attention.cpp
index 36f51b2aa39..a724e46499a 100644
--- a/src/core/src/op/paged_attention.cpp
+++ b/src/core/src/op/paged_attention.cpp
@@ -177,7 +177,8 @@ void PagedAttentionExtension::validate_and_infer_types() {
get_input_partial_shape(15).rank().get_length(),
".");
NODE_VALIDATION_CHECK(this,
- get_input_element_type(15).is_dynamic() || get_input_element_type(15) == element::f32,
+ get_input_element_type(15).is_dynamic() || get_input_element_type(15) == element::f32 ||
+ get_input_element_type(15) == element::f16,
"Element type of `rotation_trig_lut` input should be f32, but it is ",
get_input_element_type(15),
".");
diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/paged_attention.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/paged_attention.hpp
index ad79e5178f2..095f5f1271a 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/primitives/paged_attention.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/paged_attention.hpp
@@ -21,7 +21,7 @@ struct paged_attention : public primitive_base<paged_attention> {
paged_attention(const primitive_id& id,
const std::vector<input_info>& inputs)
: primitive_base(id, inputs) {
- OPENVINO_ASSERT((inputs.size() == 13) || (inputs.size() == 15),
+ OPENVINO_ASSERT((inputs.size() == 13) || (inputs.size() == 16),
"[GPU] Unexpected inputs number for PagedAttention primitive: ",
inputs.size());
}
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/paged_attention.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/paged_attention.cpp
index 15a1632a8a2..ab7d1a4f2ee 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/paged_attention.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/paged_attention.cpp
@@ -12,6 +12,7 @@
#include "sdpa/sdpa_kernel_base.h"
#include "sdpa/sdpa_kernel_selector.h"
+#include "sdpa/pa_kv_cache_rotate_kernel_ref.h"
#include "sdpa/pa_kv_cache_update_kernel_ref.h"
#include "sdpa/pa_sdpa_kernel_opt.h"
@@ -28,6 +29,9 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
using pa_sdpa_kernel_selector_t = kernel_selector::pa_sdpa_kernel_selector;
using pa_sdpa_kernel_params_t = kernel_selector::pa_sdpa_params;
+ using kv_cache_rotate_kernel_selector_t = kernel_selector::kv_cache_rotate_kernel_selector;
+ using kv_cache_rotate_kernel_params_t = kernel_selector::kv_cache_rotate_params;
+
using kv_cache_update_kernel_selector_t = kernel_selector::kv_cache_update_kernel_selector;
using kv_cache_update_kernel_params_t = kernel_selector::kv_cache_update_params;
@@ -50,6 +54,7 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
KV_CACHE_UPDATE,
SDPA,
PA_SDPA,
+ KV_CACHE_ROTATE,
};
bool requires_update(primitive_inst& inst, const kernel_impl_params& impl_params) const override {
@@ -64,6 +69,7 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
void load(BinaryInputBuffer& ib) override {
parent::load(ib);
ib >> make_data(&has_scores_output, sizeof(bool));
+ ib >> make_data(&has_rotated_blocks, sizeof(bool));
if (is_dynamic()) {
auto& kv_cache_update_kernel_selector = kv_cache_update_kernel_selector_t::Instance();
auto kv_cache_update_kernel_impl = kv_cache_update_kernel_selector.GetImplementation(_kernels_data[Stage::KV_CACHE_UPDATE].kernelName);
@@ -76,12 +82,19 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
auto& pa_sdpa_kernel_selector = pa_sdpa_kernel_selector_t::Instance();
auto pa_sdpa_kernel_impl = pa_sdpa_kernel_selector.GetImplementation(_kernels_data[Stage::PA_SDPA].kernelName);
pa_sdpa_kernel_impl->GetUpdateDispatchDataFunc(_kernels_data[Stage::PA_SDPA]);
+
+ if (has_rotated_blocks) {
+ auto& kv_cache_rotate_kernel_selector = kv_cache_rotate_kernel_selector_t::Instance();
+ auto kv_cache_rotate_kernel_impl = kv_cache_rotate_kernel_selector.GetImplementation(_kernels_data[Stage::KV_CACHE_ROTATE].kernelName);
+ kv_cache_rotate_kernel_impl->GetUpdateDispatchDataFunc(_kernels_data[Stage::KV_CACHE_ROTATE]);
+ }
}
}
void save(BinaryOutputBuffer& ob) const override {
parent::save(ob);
ob << make_data(&has_scores_output, sizeof(bool));
+ ob << make_data(&has_rotated_blocks, sizeof(bool));
}
std::vector<layout> get_internal_buffer_layouts_impl() const override {
@@ -142,10 +155,16 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
const auto desc = instance.get_node().as<paged_attention>().get_primitive();
kernel_arguments_data args;
- if (stage == Stage::KV_CACHE_UPDATE || stage == Stage::SDPA)
+ if (stage == Stage::KV_CACHE_UPDATE || stage == Stage::SDPA || stage == Stage::KV_CACHE_ROTATE)
args.shape_info = instance.shape_info_memory_ptr();
- if (stage == Stage::KV_CACHE_UPDATE) {
+ if (stage == Stage::KV_CACHE_ROTATE) {
+ args.inputs = { instance.rotated_block_indices_memory_ptr(),
+ instance.rotation_deltas_memory_ptr(),
+ instance.rotation_trig_lut_memory_ptr() };
+
+ args.outputs = { instance.key_cache_memory_ptr() };
+ } else if (stage == Stage::KV_CACHE_UPDATE) {
args.inputs = { instance.key_memory_ptr(),
instance.value_memory_ptr(),
instance.past_lens_memory_ptr(),
@@ -195,6 +214,12 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
if (desc->has_alibi) {
args.inputs.push_back(instance.alibi_memory_ptr());
}
+
+ if (desc->has_rotated_blocks) {
+ args.inputs.push_back(instance.rotated_block_indices_memory_ptr());
+ args.inputs.push_back(instance.rotation_deltas_memory_ptr());
+ args.inputs.push_back(instance.rotation_trig_lut_memory_ptr());
+ }
} else if (kernel_idx == 2 || kernel_idx == 3) {
// Finalization kernel or mixed stage finalization kernel
args.inputs = { instance.past_lens_memory_ptr() };
@@ -322,19 +347,25 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
}
event::ptr execute_impl(const std::vector<event::ptr>& events, paged_attention_inst& instance) override {
- std::vector<event::ptr> res_events;
const auto stage = get_paged_attention_stage(*instance.get_impl_params());
const auto is_mixed_mode = stage == PagedAttentionStage::MIXED;
- execute_stage(events, instance, res_events, Stage::KV_CACHE_UPDATE, is_mixed_mode);
+ std::vector<event::ptr> res_events;
+ std::vector<event::ptr> dep_events = events;
+ if (has_rotated_blocks) {
+ execute_stage(dep_events, instance, res_events, Stage::KV_CACHE_ROTATE, is_mixed_mode);
+ dep_events = res_events;
+ }
+
+ execute_stage(dep_events, instance, res_events, Stage::KV_CACHE_UPDATE, is_mixed_mode);
if (stage == PagedAttentionStage::PREFILL) {
- std::vector<event::ptr> dep_events(res_events.begin(), res_events.end());
+ dep_events = res_events;
execute_stage(dep_events, instance, res_events, Stage::SDPA, is_mixed_mode);
}
if (stage == PagedAttentionStage::GENERATE || stage == PagedAttentionStage::MIXED || has_scores_output) {
- std::vector<event::ptr> dep_events(res_events.begin(), res_events.end());
+ dep_events = res_events;
execute_stage(dep_events, instance, res_events, Stage::PA_SDPA, is_mixed_mode);
}
@@ -446,6 +477,8 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
config.has_const_scale_val = false;
}
+ config.has_rotated_blocks = desc->has_rotated_blocks;
+
if (desc->heads_num != desc->kv_heads_num) {
config.broadcast_axis = 1;
config.group_size = desc->heads_num / desc->kv_heads_num;
@@ -461,6 +494,42 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
return config;
}
+ static kv_cache_rotate_kernel_params_t get_kv_cache_rotate_kernel_params(const kernel_impl_params& impl_param,
+ const kernel_selector::MultiDataTensor& input_tensors,
+ bool is_dynamic = false) {
+ auto params = get_default_params<kv_cache_rotate_kernel_params_t>(impl_param, is_dynamic);
+
+ const auto& key_cache_tensor = input_tensors[3];
+ const auto& rotated_block_indices_tensor = input_tensors[13];
+ const auto& rotation_deltas_tensor = input_tensors[14];
+ const auto& rotation_trig_lut_tensor = input_tensors[15];
+
+ const auto inputs_number = 3;
+ const auto outputs_number = 1;
+ params.inputs.resize(inputs_number);
+ params.outputs.resize(outputs_number);
+ params.inputs[0] = rotated_block_indices_tensor;
+ params.inputs[1] = rotation_deltas_tensor;
+ params.inputs[2] = rotation_trig_lut_tensor;
+ params.outputs[0] = key_cache_tensor;
+
+ params.conf = get_sdpa_configuration(impl_param, is_dynamic);
+
+ const auto& in_offsets_map = impl_param.in_port_to_shape_info_offset;
+ std::map<size_t, size_t> in_tensor_to_offset_map = {
+ {0, in_offsets_map.at(13)},
+ {1, in_offsets_map.at(14)},
+ {2, in_offsets_map.at(15)},
+ };
+ std::map<size_t, size_t> out_tensor_to_offset_map = {
+ {0, in_offsets_map.at(3)},
+ };
+
+ params.set_dynamic_shape_offsets(in_tensor_to_offset_map, out_tensor_to_offset_map);
+
+ return params;
+ }
+
static kv_cache_update_kernel_params_t get_kv_cache_update_kernel_params(const kernel_impl_params& impl_param,
const PagedAttentionStage& stage,
const kernel_selector::MultiDataTensor& input_tensors,
@@ -618,6 +687,10 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
if (has_alibi)
inputs_number++;
+ const auto has_rotation = impl_param.input_layouts.size() == 16;
+ if (has_rotation)
+ inputs_number += 3;
+
auto input_idx = 0;
params.inputs.resize(inputs_number);
params.inputs[input_idx++] = query_tensor;
@@ -636,6 +709,12 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
if (has_alibi)
params.inputs[input_idx++] = alibi_tensor;
+ if (has_rotation) {
+ params.inputs[input_idx++] = input_tensors[13];
+ params.inputs[input_idx++] = input_tensors[14];
+ params.inputs[input_idx++] = input_tensors[15];
+ }
+
if (has_scores_output) {
params.outputs.resize(2);
params.outputs[1] = convert_data_tensor(impl_param.get_output_layout(1));
@@ -673,6 +752,12 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
if (has_alibi)
in_tensor_to_offset_map.insert({input_idx++, in_offsets_map.at(11)});
+ if (has_rotation) {
+ in_tensor_to_offset_map.insert({input_idx++, in_offsets_map.at(13)});
+ in_tensor_to_offset_map.insert({input_idx++, in_offsets_map.at(14)});
+ in_tensor_to_offset_map.insert({input_idx++, in_offsets_map.at(15)});
+ }
+
if (has_scores_output)
out_tensor_to_offset_map.insert({1, out_offsets_map.at(1)});
@@ -688,6 +773,11 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
for (const auto& input_layout : impl_param.input_layouts)
input_tensors.emplace_back(convert_data_tensor(input_layout));
+ if (has_rotated_blocks) {
+ auto kv_cache_rotate_kernel_params = get_kv_cache_rotate_kernel_params(impl_param, input_tensors, impl_param.is_dynamic());
+ (_kernels_data[Stage::KV_CACHE_ROTATE].update_dispatch_data_func)(kv_cache_rotate_kernel_params, _kernels_data[Stage::KV_CACHE_ROTATE]);
+ }
+
auto kv_cache_update_kernel_params = get_kv_cache_update_kernel_params(impl_param, stage, input_tensors, impl_param.is_dynamic());
(_kernels_data[Stage::KV_CACHE_UPDATE].update_dispatch_data_func)(kv_cache_update_kernel_params, _kernels_data[Stage::KV_CACHE_UPDATE]);
@@ -710,6 +800,7 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
for (const auto& input_layout : impl_param.input_layouts)
input_tensors.emplace_back(convert_data_tensor(input_layout));
+ const auto& desc = impl_param.typed_desc<paged_attention>();
auto kv_cache_update_kernel_params = get_kv_cache_update_kernel_params(impl_param, stage, input_tensors, impl_param.is_dynamic());
auto& kv_cache_update_kernel_selector = kv_cache_update_kernel_selector_t::Instance();
kernels_data.push_back(kv_cache_update_kernel_selector.get_best_kernel(kv_cache_update_kernel_params));
@@ -722,16 +813,22 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {
auto& pa_sdpa_kernel_selector = pa_sdpa_kernel_selector_t::Instance();
kernels_data.push_back(pa_sdpa_kernel_selector.get_best_kernel(pa_sdpa_kernel_params));
- auto impl = cldnn::make_unique<paged_attention_impl>(kernels_data);
+ if (desc->has_rotated_blocks) {
+ auto kv_cache_rotate_kernel_params = get_kv_cache_rotate_kernel_params(impl_param, input_tensors, impl_param.is_dynamic());
+ auto& kv_cache_rotate_kernel_selector = kv_cache_rotate_kernel_selector_t::Instance();
+ kernels_data.push_back(kv_cache_rotate_kernel_selector.get_best_kernel(kv_cache_rotate_kernel_params));
+ }
- const auto& desc = impl_param.typed_desc<paged_attention>();
+ auto impl = cldnn::make_unique<paged_attention_impl>(kernels_data);
impl->has_scores_output = desc->has_scores_output();
+ impl->has_rotated_blocks = desc->has_rotated_blocks;
return impl;
}
private:
bool has_scores_output = false;
+ bool has_rotated_blocks = false;
};
namespace detail {
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/pa_kv_cache_rotate_ref.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/pa_kv_cache_rotate_ref.cl
new file mode 100644
index 00000000000..0bf685847d9
--- /dev/null
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/pa_kv_cache_rotate_ref.cl
@@ -0,0 +1,83 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "include/batch_headers/common.cl"
+
+#define SUBGROUPS_PER_WG KV_HEADS_NUM
+
+REQD_SUB_GROUP_SIZE(SUBGROUP_SIZE)
+__attribute__((reqd_work_group_size(SUBGROUP_SIZE, KV_HEADS_NUM, 1)))
+KERNEL(pa_kv_cache_rotate)(
+ OPTIONAL_SHAPE_INFO_ARG
+ __global const INPUT0_TYPE* rotated_block_indices,
+ __global const INPUT1_TYPE* rotation_deltas,
+ __global const INPUT2_TYPE* rotation_trig_lut,
+ __global OUTPUT_TYPE* key_cache
+) {
+ // Input shapes:
+ // rotated_block_indices: [num_blocks_to_rotate]
+ // rotation_deltas: [num_blocks_to_rotate, PAGED_ATTENTION_BLOCK_SIZE] || [num_blocks_to_rotate, 1]
+ // rotation_trig_lut: [max_num_batched_tokens / PAGED_ATTENTION_BLOCK_SIZE, HEAD_SIZE] || [max_num_batched_tokens, HEAD_SIZE]
+ // key_cache: [num_blocks, HEADS_NUM, HEAD_SIZE, PAGED_ATTENTION_BLOCK_SIZE]
+
+ // Output shapes:
+ // key_cache (updated): [num_blocks, HEADS_NUM, HEAD_SIZE, PAGED_ATTENTION_BLOCK_SIZE]
+
+ const uint head_idx = get_global_id(1);
+ const uint block_idx = get_global_id(2);
+ const uint sglid = get_sub_group_local_id();
+ const uint sgid = get_sub_group_id();
+
+ __local INPUT2_TYPE rotation_coefficients[HEAD_SIZE][PAGED_ATTENTION_BLOCK_SIZE];
+
+ const bool per_token_rotation = INPUT1_FEATURE_NUM == PAGED_ATTENTION_BLOCK_SIZE;
+
+ if (per_token_rotation) {
+ // Need to load HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE coefficients in total, each subgroup loads SUBGROUP_SIZE values
+ for (uint i = sgid; i < HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE / SUBGROUP_SIZE; i += SUBGROUPS_PER_WG) {
+ const uint token_idx = (i / (HEAD_SIZE / SUBGROUP_SIZE));
+ const uint rotation_trig_lut_start_offset = rotation_deltas[block_idx * INPUT1_FEATURE_NUM + token_idx] * HEAD_SIZE;
+ const uint inner_offset = (i % (HEAD_SIZE / SUBGROUP_SIZE)) * SUBGROUP_SIZE;
+ const uint rotation_trig_lut_offset = rotation_trig_lut_start_offset + inner_offset;
+
+ INPUT2_TYPE coefficient = rotation_trig_lut[rotation_trig_lut_offset + sglid];
+
+ rotation_coefficients[inner_offset + sglid][token_idx] = coefficient;
+ }
+ } else {
+ // Need to load HEAD_SIZE coefficients in total, each subgroup loads SUBGROUP_SIZE values
+ for (uint i = sgid; i < HEAD_SIZE / SUBGROUP_SIZE; i += SUBGROUPS_PER_WG) {
+ const uint token_idx = 0;
+ const uint rotation_trig_lut_start_offset = rotation_deltas[block_idx * INPUT1_FEATURE_NUM + token_idx] * HEAD_SIZE;
+ const uint inner_offset = i * SUBGROUP_SIZE;
+ const uint rotation_trig_lut_offset = rotation_trig_lut_start_offset + inner_offset;
+
+ INPUT2_TYPE coefficient = rotation_trig_lut[rotation_trig_lut_offset + sglid];
+
+ rotation_coefficients[inner_offset + sglid][token_idx] = coefficient;
+ }
+ }
+
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ const uint token_coefficient_idx = per_token_rotation ? sglid : 0;
+ const uint block_offset = rotated_block_indices[block_idx] * KV_HEADS_NUM * HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE +
+ head_idx * HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE + sglid;
+ for (uint i = 0; i < HEAD_SIZE / 2; i++) {
+ const uint cache_offset = block_offset + i * PAGED_ATTENTION_BLOCK_SIZE;
+ OUTPUT_TYPE cache_value_first = key_cache[cache_offset];
+ OUTPUT_TYPE cache_value_second = key_cache[cache_offset + (HEAD_SIZE / 2) * PAGED_ATTENTION_BLOCK_SIZE];
+
+ INPUT2_TYPE rotation_value_cos = rotation_coefficients[i][token_coefficient_idx];
+ INPUT2_TYPE rotation_value_sin = rotation_coefficients[i + (HEAD_SIZE / 2)][token_coefficient_idx];
+
+ OUTPUT_TYPE new_cache_value_first = cache_value_first * rotation_value_cos - cache_value_second * rotation_value_sin;
+ OUTPUT_TYPE new_cache_value_second = cache_value_first * rotation_value_sin + cache_value_second * rotation_value_cos;
+
+ key_cache[cache_offset] = new_cache_value_first;
+ key_cache[cache_offset + (HEAD_SIZE / 2) * PAGED_ATTENTION_BLOCK_SIZE] = new_cache_value_second;
+ }
+}
+
+#undef SUBGROUPS_PER_WG
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/pa_sdpa_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/pa_sdpa_opt.cl
index 2d6598e0a65..ae0f7a666c4 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/pa_sdpa_opt.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/pa_sdpa_opt.cl
@@ -43,10 +43,11 @@ KERNEL(pa_sdpa_opt)(
#if HAS_ALIBI
const __global ALIBI_INPUT_TYPE* alibi_slopes,
#endif
+
#if HAS_ROTATED_BLOCKS
- const __global INPUT8_TYPE* rotated_block_indices,
- const __global INPUT9_TYPE* rotation_deltas,
- const __global INPUT10_TYPE* rotated_block_indices,
+ const __global INPUT7_TYPE* rotated_block_indices,
+ const __global INPUT8_TYPE* rotation_deltas,
+ const __global INPUT9_TYPE* rotation_trig_lut,
#endif
__global OUTPUT_TYPE* output,
#if PAGED_ATTENTION_SCORES_OUTPUT
@@ -156,10 +157,6 @@ KERNEL(pa_sdpa_opt)(
}
#endif
-#ifdef HAS_ROTATED_BLOCKS
- // TODO (vshampor): add cache block rotation at this spot
-#endif
-
const uint blocks_num_per_partition = min(total_blocks_num - partition_idx * PAGED_ATTENTION_BLOCKS_PER_PARTITION, (uint)PAGED_ATTENTION_BLOCKS_PER_PARTITION);
uint blocks_num = blocks_num_per_partition / SUBGROUPS_PER_WG;
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_opt.cl
index f6447ff1245..e79c58a8f0a 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_opt.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_opt.cl
@@ -1004,6 +1004,7 @@ KERNEL(sdpa_opt)(
const uint partition_seq_len = min((uint)SOURCE_SEQ_LEN - start_partition_idx, (uint)SEQ_LEN_PARTITION_SIZE);
#endif
+MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZERO;
#if IS_CAUSAL
if (seq_len <= target_seq_idx) { // keep tril i.e. m >= n
#endif
@@ -1037,11 +1038,7 @@ KERNEL(sdpa_opt)(
#endif
int seq_len_calc_size = min((int)(SOURCE_SEQ_LEN) - (int)seq_len, (int)SUBGROUP_SIZE);
-#if IS_CAUSAL
- MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZERO;
-#else // !IS_CAUSAL
- MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc;
-
+#if !IS_CAUSAL
qk_acc = FUNC_CALL(load_attn_mask)(OPTIONAL_SHAPE_INFO_TENSOR
b0_idx,
b1_idx,
diff --git a/src/plugins/intel_gpu/src/kernel_selector/common_types.h b/src/plugins/intel_gpu/src/kernel_selector/common_types.h
index c645aa59877..2797212463f 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/common_types.h
+++ b/src/plugins/intel_gpu/src/kernel_selector/common_types.h
@@ -16,6 +16,7 @@ enum class KernelType {
ARG_MAX_MIN,
BEAM_TABLE_UPDATE,
PA_KV_CACHE_UPDATE,
+ PA_KV_CACHE_ROTATE,
PA_SDPA,
CONVOLUTION,
DECONVOLUTION,
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/pa_kv_cache_rotate_kernel_ref.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/pa_kv_cache_rotate_kernel_ref.cpp
new file mode 100644
index 00000000000..7033ae34291
--- /dev/null
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/pa_kv_cache_rotate_kernel_ref.cpp
@@ -0,0 +1,136 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "pa_kv_cache_rotate_kernel_ref.h"
+#include "sdpa_kernel_base.h"
+
+#include "kernel_selector_params.h"
+#include "kernel_selector_utils.h"
+
+namespace kernel_selector {
+
+constexpr size_t subgroup_size = 16;
+constexpr size_t paged_attention_block_size = 16;
+
+KernelsData KVCacheRotateKernelRef::GetKernelsData(const Params& p) const {
+ if (!Validate(p)) {
+ return {};
+ }
+
+ KernelData kd = KernelData::Default<kv_cache_rotate_params>(p);
+ kd.needs_sub_kernels_sync = false;
+ GetUpdateDispatchDataFunc(kd);
+
+ const auto& params = static_cast<const kv_cache_rotate_params&>(p);
+ const auto dispatch_data = SetDefault(params);
+ const auto entry_point = GetEntryPoint(kernelName, params.layerID, p);
+ const auto jit_constants = GetJitConstants(params);
+ const auto jit = CreateJit(kernelName, jit_constants, entry_point);
+
+ auto& kernel = kd.kernels[0];
+ FillCLKernelData(kernel,
+ dispatch_data,
+ params.engineInfo,
+ kernelName,
+ jit,
+ entry_point,
+ {},
+ false,
+ false,
+ static_cast<int>(params.inputs.size()),
+ GetFusedPrimitiveInputsCount(params),
+ static_cast<int>(params.outputs.size()),
+ params.is_shape_agnostic);
+
+ return {kd};
+}
+
+ParamsKey KVCacheRotateKernelRef::GetSupportedKey() const {
+ ParamsKey k;
+
+ k.EnableInputDataType(Datatype::F16);
+ k.EnableInputDataType(Datatype::F32);
+ k.EnableInputDataType(Datatype::INT32);
+
+ k.EnableOutputDataType(Datatype::F16);
+ k.EnableOutputDataType(Datatype::F32);
+ k.EnableOutputDataType(Datatype::INT32);
+
+ k.EnableInputLayout(DataLayout::bfyx);
+ k.EnableOutputLayout(DataLayout::bfyx);
+ k.EnableOutputLayout(DataLayout::bfzyx);
+
+ k.EnableDifferentTypes();
+ k.EnableTensorOffset();
+ k.EnableTensorPitches();
+ k.EnableBatching();
+ k.EnableDynamicShapesSupport();
+
+ return k;
+}
+
+bool KVCacheRotateKernelRef::Validate(const Params& params) const {
+ if (params.GetType() != KernelType::PA_KV_CACHE_ROTATE)
+ return false;
+
+ const auto& kernel_params = dynamic_cast<const kv_cache_rotate_params&>(params);
+ if (kernel_params.inputs.size() != 3)
+ return false;
+
+ if (kernel_params.outputs.size() != 1)
+ return false;
+
+ if (!kernel_params.conf.is_paged_attention)
+ return false;
+
+ if (kernel_params.conf.paged_attention_block_size != static_cast<int64_t>(paged_attention_block_size))
+ return false;
+
+ return true;
+}
+
+JitConstants KVCacheRotateKernelRef::GetJitConstants(const kv_cache_rotate_params& params) const {
+ JitConstants jit = MakeBaseParamsJitConstants(params);
+
+ jit.AddConstant(MakeJitConstant("HEAD_SIZE", params.conf.head_size));
+ jit.AddConstant(MakeJitConstant("HEADS_NUM", params.conf.heads_num));
+ jit.AddConstant(MakeJitConstant("KV_HEADS_NUM", params.conf.kv_heads_num));
+ jit.AddConstant(MakeJitConstant("PAGED_ATTENTION_BLOCK_SIZE", paged_attention_block_size));
+ jit.AddConstant(MakeJitConstant("SUBGROUP_SIZE", subgroup_size));
+
+ return jit;
+}
+
+CommonDispatchData KVCacheRotateKernelRef::SetDefault(const kv_cache_rotate_params& params) {
+ CommonDispatchData dispatch_data;
+
+ const auto& rotated_block_indices_input = params.inputs[0];
+ if (!rotated_block_indices_input.is_dynamic()) {
+ auto heads_number = static_cast<size_t>(params.conf.kv_heads_num);
+ auto blocks_to_rotate = static_cast<size_t>(rotated_block_indices_input.Batch().v);
+
+ dispatch_data.gws = { subgroup_size,
+ heads_number,
+ blocks_to_rotate };
+ dispatch_data.lws = { subgroup_size, heads_number, 1 };
+ }
+
+ return dispatch_data;
+}
+
+void KVCacheRotateKernelRef::GetUpdateDispatchDataFunc(KernelData& kd) const {
+ kd.update_dispatch_data_func = [](const Params& params, KernelData& kd) {
+ const auto& prim_params = static_cast<const kv_cache_rotate_params&>(params);
+ const auto& rotated_block_indices_input = prim_params.inputs[0];
+
+ auto dispatch_data = SetDefault(prim_params);
+
+ OPENVINO_ASSERT(kd.kernels.size() == 1, "[GPU] Invalid kernels size for update dispatch data func");
+ kd.kernels[0].params.workGroups.global = dispatch_data.gws;
+ kd.kernels[0].params.workGroups.local = dispatch_data.lws;
+ kd.kernels[0].skip_execution = rotated_block_indices_input.Batch().v == 0;
+ };
+}
+
+} // namespace kernel_selector
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/pa_kv_cache_rotate_kernel_ref.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/pa_kv_cache_rotate_kernel_ref.h
new file mode 100644
index 00000000000..514a209e00d
--- /dev/null
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/pa_kv_cache_rotate_kernel_ref.h
@@ -0,0 +1,33 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "kernel_base_opencl.h"
+#include "sdpa_kernel_base.h"
+
+namespace kernel_selector {
+
+struct kv_cache_rotate_params : base_params {
+ kv_cache_rotate_params() : base_params(KernelType::PA_KV_CACHE_ROTATE) {}
+
+ bool is_prefill = false;
+ sdpa_configuration conf;
+};
+
+class KVCacheRotateKernelRef : public KernelBaseOpenCL {
+public:
+ KVCacheRotateKernelRef() : KernelBaseOpenCL{"pa_kv_cache_rotate_ref"} {}
+ KernelsData GetKernelsData(const Params& params) const override;
+ ParamsKey GetSupportedKey() const override;
+ virtual ~KVCacheRotateKernelRef() {}
+
+protected:
+ bool Validate(const Params& params) const override;
+ JitConstants GetJitConstants(const kv_cache_rotate_params& kernel_params) const;
+ static CommonDispatchData SetDefault(const kv_cache_rotate_params& kernel_params);
+ void GetUpdateDispatchDataFunc(KernelData& kd) const override;
+};
+
+} // namespace kernel_selector
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_selector.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_selector.cpp
index e65fd7fd109..20cfecd2c49 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_selector.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_selector.cpp
@@ -9,6 +9,7 @@
#include "pa_sdpa_kernel_opt.h"
#include "pa_kv_cache_update_kernel_ref.h"
+#include "pa_kv_cache_rotate_kernel_ref.h"
namespace kernel_selector {
@@ -32,6 +33,14 @@ KernelsData kv_cache_update_kernel_selector::GetBestKernels(const Params& params
return GetNaiveBestKernel(params, KernelType::PA_KV_CACHE_UPDATE);
}
+kv_cache_rotate_kernel_selector::kv_cache_rotate_kernel_selector() {
+ Attach<KVCacheRotateKernelRef>();
+}
+
+KernelsData kv_cache_rotate_kernel_selector::GetBestKernels(const Params& params) const {
+ return GetNaiveBestKernel(params, KernelType::PA_KV_CACHE_ROTATE);
+}
+
pa_sdpa_kernel_selector::pa_sdpa_kernel_selector() {
Attach<PagedAttentionSDPAKernelOpt>();
}
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_selector.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_selector.h
index 17d62a39d9c..724d3c11b26 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_selector.h
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_selector.h
@@ -35,6 +35,20 @@ public:
KernelsData GetBestKernels(const Params& params) const override;
};
+class kv_cache_rotate_kernel_selector : public kernel_selector_base {
+public:
+ static kv_cache_rotate_kernel_selector& Instance() {
+ static kv_cache_rotate_kernel_selector instance_;
+ return instance_;
+ }
+
+ kv_cache_rotate_kernel_selector();
+
+ virtual ~kv_cache_rotate_kernel_selector() {}
+
+ KernelsData GetBestKernels(const Params& params) const override;
+};
+
class pa_sdpa_kernel_selector : public kernel_selector_base {
public:
static pa_sdpa_kernel_selector& Instance() {
diff --git a/src/plugins/intel_gpu/src/plugin/ops/paged_attention.cpp b/src/plugins/intel_gpu/src/plugin/ops/paged_attention.cpp
index 94bd461716d..f8f14102eb9 100644
--- a/src/plugins/intel_gpu/src/plugin/ops/paged_attention.cpp
+++ b/src/plugins/intel_gpu/src/plugin/ops/paged_attention.cpp
@@ -22,7 +22,7 @@ namespace ov {
namespace intel_gpu {
static void CreatePagedAttentionExtensionOp(ProgramBuilder& p, const std::shared_ptr<ov::op::PagedAttentionExtension>& op) {
- validate_inputs_count(op, {13});
+ validate_inputs_count(op, {13, 16});
auto inputs = p.GetInputInfo(op);
auto prim = cldnn::paged_attention(layer_type_name_ID(op), inputs);
@@ -61,6 +61,7 @@ static void CreatePagedAttentionExtensionOp(ProgramBuilder& p, const std::shared
std::shared_ptr<ov::op::v0::Constant> alibi_const = ov::as_type_ptr<ov::op::v0::Constant>(op->get_input_node_shared_ptr(alibi_idx));
OPENVINO_ASSERT(alibi_const != nullptr);
prim.has_alibi = ov::shape_size(alibi_const->get_output_shape(0)) > 0;
+ prim.has_rotated_blocks = op->get_input_size() == 16;
prim.num_outputs = 1;
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp
index a32ef3325cd..7076b863c45 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp
@@ -43,6 +43,12 @@ using namespace ::tests;
* [11]: alibi_slopes, optional
* [12]: max_context_len
* shape: [], type: i32
+* [13]: rotated_block_indices​, optional​
+* shape: [num_rotated_blocks]​, type: i32
+* [14]: rotation_deltas​, optional​
+* shape: [num_rotated_blocks, BLOCK_SIZE]​ || [num_rotated_blocks, 1]​, type: i32
+* [15]: rotation_trig_lut​, optional​
+* shape: [max_num_batched_tokens / BLOCK_SIZE, head_size]​ || [max_num_batched_tokens, head_size], type: f16
*/
struct SubsequenceDescriptor {
@@ -50,10 +56,19 @@ struct SubsequenceDescriptor {
int past_len;
};
+struct CacheRotationDescriptor {
+ bool apply_rotation;
+ // configures 2nd dimension of rotation_deltas
+ // if per_block is true, single value is used for all tokens inside the block
+ // otherwise, each token uses an independent value
+ bool per_block;
+};
+
struct PagedAttentionManager {
int num_heads;
int head_size;
int block_size;
+ CacheRotationDescriptor rotation_config;
std::vector<SubsequenceDescriptor> subsequence_descs;
// per-subsequence QKV inputs
@@ -68,6 +83,11 @@ struct PagedAttentionManager {
std::vector<int> block_indices_begins;
std::vector<int> max_context_len;
+ // rotation related inputs
+ std::vector<int> rotated_block_indices;
+ std::vector<int> rotation_deltas;
+ std::vector<ov::float16> rotation_trig_lut;
+
cldnn::engine& test_engine;
cldnn::stream& test_stream;
tests::random_generator& rg;
@@ -78,10 +98,12 @@ struct PagedAttentionManager {
const std::vector<SubsequenceDescriptor>& subsequence_descs,
int num_heads,
int head_size,
- int block_size)
+ int block_size,
+ CacheRotationDescriptor rotation_config)
: num_heads(num_heads)
, head_size(head_size)
, block_size(block_size)
+ , rotation_config(rotation_config)
, subsequence_descs(subsequence_descs)
, test_engine(engine)
, test_stream(stream)
@@ -117,6 +139,31 @@ struct PagedAttentionManager {
block_indices_begins.push_back(block_indices_end_pos);
}
max_context_len.push_back(max_len);
+
+ if (rotation_config.apply_rotation) {
+ // iterate over KV-cache blocks and apply cache rotation to every second
+ // fully occupied block
+ for (size_t i = 0; i < subsequence_descs.size(); i++) {
+ const auto& subsequence_desc = subsequence_descs[i];
+ int past_len = subsequence_desc.past_len;
+ int start_block_idx = block_indices_begins[i];
+ for (int block_idx = 1; block_idx < past_len / block_size; block_idx++) {
+ if (block_idx % 2 != 0) {
+ std::cout << "\tadd: " << start_block_idx + block_idx << "\n";
+ rotated_block_indices.push_back(start_block_idx + block_idx);
+ }
+ }
+ }
+
+ if (!rotated_block_indices.empty()) {
+ rotation_deltas = generate_rotation_deltas_data(rg,
+ max_context_len[0],
+ rotated_block_indices.size(),
+ block_size,
+ rotation_config.per_block);
+ rotation_trig_lut = generate_rotation_trig_lut_data(rg, max_context_len[0], head_size);
+ }
+ }
}
memory::ptr get_query_memory() {
@@ -241,6 +288,27 @@ struct PagedAttentionManager {
return get_memory_from_vec(max_context_len);
}
+ memory::ptr get_rotated_block_indices_memory() {
+ return get_memory_from_vec(rotated_block_indices);
+ }
+
+ memory::ptr get_rotation_deltas_memory() {
+ auto mem = get_memory_from_vec(rotation_deltas);
+ auto layout = mem->get_layout();
+ auto last_dim = rotation_config.per_block ? 1 : block_size;
+ layout.set_partial_shape(ov::PartialShape{ static_cast<long int>(rotated_block_indices.size()), last_dim });
+
+ return test_engine.reinterpret_buffer(*mem, layout);
+ }
+
+ memory::ptr get_rotation_trig_lut_memory() {
+ auto mem = get_memory_from_vec(rotation_trig_lut);
+ auto layout = mem->get_layout();
+ layout.set_partial_shape(ov::PartialShape{ max_context_len[0], head_size });
+
+ return test_engine.reinterpret_buffer(*mem, layout);
+ }
+
float get_default_scale() {
return static_cast<float>(1.f / std::sqrt(head_size));
}
@@ -311,8 +379,32 @@ private:
return data;
}
+
+ static std::vector<int> generate_rotation_deltas_data(tests::random_generator& rg, size_t max_tokens_num, size_t rotated_blocks_num, size_t block_size, bool per_block) {
+ const size_t total_elements_num = per_block ? rotated_blocks_num
+ : rotated_blocks_num * block_size;
+ auto data = rg.generate_random_1d<int>(total_elements_num, 0, static_cast<int>(max_tokens_num - 1));
+
+ return data;
+ }
+
+ static std::vector<ov::float16> generate_rotation_trig_lut_data(tests::random_generator& rg, size_t max_tokens_num, size_t head_size) {
+ const size_t total_elements_num = max_tokens_num * head_size;
+ auto data = rg.generate_random_1d<ov::float16>(total_elements_num, -1, 1);
+
+ return data;
+ }
};
+namespace std {
+template <>
+ struct hash<ov::float16> {
+ uint64_t operator()(const ov::float16 __val) const {
+ return std::hash<float>()(__val);
+ }
+};
+}
+
struct PagedAttentionReference {
PagedAttentionReference(PagedAttentionManager& pam)
: pam(pam)
@@ -326,8 +418,36 @@ struct PagedAttentionReference {
for (size_t i = 0; i < pam.subsequence_descs.size(); i++) {
const auto& subsequence_desc = pam.subsequence_descs[i];
const auto kv_seq_len = subsequence_desc.num_tokens + subsequence_desc.past_len;
+
+ auto key_data = pam.key_data[i];
+ if (pam.rotation_config.apply_rotation) {
+ auto blocks_start = pam.block_indices_begins[i];
+ auto blocks_end = pam.block_indices_begins[i + 1];
+
+ std::vector<int> block_indices(pam.block_indices.begin() + blocks_start,
+ pam.block_indices.begin() + blocks_end);
+
+ for (const auto& block_idx : block_indices) {
+ auto it = std::find(pam.rotated_block_indices.begin(), pam.rotated_block_indices.end(), block_idx);
+ if (it != pam.rotated_block_indices.end()) {
+ int index = std::distance(pam.rotated_block_indices.begin(), it);
+ int subsequence_rotated_block_idx = *it - blocks_start;
+
+ rotate_block(key_data,
+ pam.rotation_deltas,
+ pam.rotation_trig_lut,
+ index,
+ subsequence_rotated_block_idx,
+ pam.num_heads,
+ pam.head_size,
+ pam.block_size,
+ pam.rotation_config.per_block);
+ }
+ }
+ }
+
auto subsequence_ref_results = run_reference(pam.query_data[i],
- pam.key_data[i],
+ key_data,
pam.value_data[i],
subsequence_desc.num_tokens,
kv_seq_len,
@@ -497,6 +617,39 @@ private:
return mask_mem;
}
+ void rotate_block(std::vector<ov::float16>& cache_data,
+ std::vector<int> rotation_deltas,
+ std::vector<ov::float16> rotation_trig_lut_mem,
+ int rotated_block_idx,
+ int subsequence_rotated_block_idx,
+ int num_heads,
+ int head_size,
+ int block_size,
+ bool per_block) {
+ // cache_data shape: [1, num_tokens, num_heads, head_size]
+ int start_token_idx = subsequence_rotated_block_idx * block_size;
+
+ for (int token_idx = 0; token_idx < block_size; token_idx++) {
+ auto rotation_deltas_offset = per_block ? rotated_block_idx : rotated_block_idx * block_size + token_idx;
+ auto rotation_trig_lut_idx = rotation_deltas[rotation_deltas_offset];
+ for (int head_idx = 0; head_idx < num_heads; head_idx++) {
+ for (int head_size_idx = 0; head_size_idx < head_size / 2; head_size_idx++) {
+ auto input_offset = (start_token_idx + token_idx) * num_heads * head_size +
+ head_idx * head_size +
+ head_size_idx;
+
+ auto cache_value_0 = cache_data[input_offset];
+ auto cache_value_1 = cache_data[input_offset + head_size / 2];
+
+ ov::float16 rotation_value_cos = rotation_trig_lut_mem[rotation_trig_lut_idx * head_size + head_size_idx];
+ ov::float16 rotation_value_sin = rotation_trig_lut_mem[rotation_trig_lut_idx * head_size + head_size_idx + head_size / 2];
+
+ cache_data[input_offset] = cache_value_0 * rotation_value_cos - cache_value_1 * rotation_value_sin;
+ cache_data[input_offset + head_size / 2] = cache_value_0 * rotation_value_sin + cache_value_1 * rotation_value_cos;
+ }
+ }
+ }
+ }
PagedAttentionManager& pam;
cldnn::engine& test_engine;
@@ -515,7 +668,7 @@ public:
}
void execute(T& p) {
- PagedAttentionManager pam(rg, get_test_engine(), get_test_stream(), p.subsequences, p.num_heads, p.head_size, p.block_size);
+ PagedAttentionManager pam(rg, get_test_engine(), get_test_stream(), p.subsequences, p.num_heads, p.head_size, p.block_size, p.rotation_config);
auto query_mem = pam.get_query_memory();
auto key_mem = pam.get_key_memory();
@@ -534,6 +687,11 @@ public:
auto alibi_mem = pam.get_alibi_memory();
auto max_context_len_mem = pam.get_max_context_len_memory();
+ // cache rotation related memory buffers
+ auto rotated_block_indices_mem = pam.get_rotated_block_indices_memory();
+ auto rotation_deltas_mem = pam.get_rotation_deltas_memory();
+ auto rotation_trig_lut_mem = pam.get_rotation_trig_lut_memory();
+
auto query_layout = query_mem->get_layout();
auto key_layout = key_mem->get_layout();
auto value_layout = value_mem->get_layout();
@@ -547,6 +705,9 @@ public:
auto sliding_window_layout = sliding_window_mem->get_layout();
auto alibi_layout = alibi_mem->get_layout();
auto max_context_len_layout = max_context_len_mem->get_layout();
+ auto rotated_block_indices_layout = rotated_block_indices_mem->get_layout();
+ auto rotation_deltas_layout = rotation_deltas_mem->get_layout();
+ auto rotation_trig_lut_layout = rotation_trig_lut_mem->get_layout();
// make layouts dynamic
query_layout.set_partial_shape(ov::PartialShape{ -1, p.num_heads * p.head_size });
@@ -558,20 +719,32 @@ public:
subsequence_begins_layout.set_partial_shape(ov::PartialShape{ -1 });
block_indices_layout.set_partial_shape(ov::PartialShape{ -1 });
block_indices_begins_layout.set_partial_shape(ov::PartialShape{ -1 });
+ rotated_block_indices_layout.set_partial_shape(ov::PartialShape{ -1 });
+ rotation_deltas_layout.set_partial_shape(ov::PartialShape{ -1, -1 });
+ rotation_trig_lut_layout.set_partial_shape(ov::PartialShape{ -1, p.head_size });
+
+ std::vector<input_info> pa_inputs = {
+ input_info("query"),
+ input_info("key"),
+ input_info("value"),
+ input_info("key_cache"),
+ input_info("value_cache"),
+ input_info("past_lens"),
+ input_info("subsequence_begins"),
+ input_info("block_indices"),
+ input_info("block_indices_begins"),
+ input_info("scale"),
+ input_info("sliding_window"),
+ input_info("alibi"),
+ input_info("max_context_len") };
+
+ if (p.rotation_config.apply_rotation) {
+ pa_inputs.push_back(input_info("rotated_block_indices"));
+ pa_inputs.push_back(input_info("rotation_deltas"));
+ pa_inputs.push_back(input_info("rotation_trig_lut"));
+ }
- auto pa_prim = paged_attention("paged_attention", { input_info("query"),
- input_info("key"),
- input_info("value"),
- input_info("key_cache"),
- input_info("value_cache"),
- input_info("past_lens"),
- input_info("subsequence_begins"),
- input_info("block_indices"),
- input_info("block_indices_begins"),
- input_info("scale"),
- input_info("sliding_window"),
- input_info("alibi"),
- input_info("max_context_len") });
+ auto pa_prim = paged_attention("paged_attention", pa_inputs);
pa_prim.head_size = p.head_size;
pa_prim.kv_heads_num = p.num_heads;
@@ -579,8 +752,10 @@ public:
pa_prim.scale_val = pam.get_default_scale();
pa_prim.has_alibi = false;
pa_prim.num_outputs = p.scores_output ? 2 : 1;
+ pa_prim.has_rotated_blocks = p.rotation_config.apply_rotation;
topology topology;
+
topology.add(
input_layout("query", query_layout),
input_layout("key", key_layout),
@@ -603,6 +778,12 @@ public:
topology.add(reorder("output_scores", input_info("paged_attention", 1), format::bfyx, data_types::f16));
}
+ if (p.rotation_config.apply_rotation) {
+ topology.add(input_layout("rotated_block_indices", rotated_block_indices_layout));
+ topology.add(input_layout("rotation_deltas", rotation_deltas_layout));
+ topology.add(input_layout("rotation_trig_lut", rotation_trig_lut_layout));
+ }
+
ExecutionConfig config = get_test_default_config(get_test_engine());
config.set_property(ov::intel_gpu::optimize_data(true));
config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
@@ -622,6 +803,12 @@ public:
network->set_input_data("alibi", alibi_mem);
network->set_input_data("max_context_len", max_context_len_mem);
+ if (p.rotation_config.apply_rotation) {
+ network->set_input_data("rotated_block_indices", rotated_block_indices_mem);
+ network->set_input_data("rotation_deltas", rotation_deltas_mem);
+ network->set_input_data("rotation_trig_lut", rotation_trig_lut_mem);
+ }
+
auto outputs = network->execute();
cldnn::memory::ptr output_data_mem = nullptr;
@@ -661,6 +848,7 @@ struct paged_attention_test_params {
int head_size;
int block_size;
bool scores_output;
+ CacheRotationDescriptor rotation_config;
};
class paged_attention_test : public PagedAttentionTest<paged_attention_test_params> {};
@@ -670,18 +858,36 @@ TEST_P(paged_attention_test, basic) {
execute(p);
}
+const auto ENABLE_SCORES = true;
+const auto DISABLE_SCORES = false;
+const auto PER_BLOCK_ROTATION = CacheRotationDescriptor{ true, true };
+const auto PER_TOKEN_ROTATION = CacheRotationDescriptor{ true, false };
+const auto DISABLE_ROTATION = CacheRotationDescriptor{ false, false };
+
INSTANTIATE_TEST_SUITE_P(smoke_paged_attention, paged_attention_test, ::testing::ValuesIn(std::vector<paged_attention_test_params>{
/* with scores output */
- paged_attention_test_params{ {{10, 0}}, 2, 64, 16, true }, // 1st token
- paged_attention_test_params{ {{36, 0}}, 2, 64, 16, true }, // 1st token
- paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, true }, // 1st token long
- paged_attention_test_params{ {{10, 0}, {30, 0}}, 2, 64, 16, true }, // 1st token + 1st token
- paged_attention_test_params{ {{128, 0}, {256, 0}}, 2, 64, 16, true }, // 1st token + 1st token
- paged_attention_test_params{ {{1, 10}}, 2, 64, 16, true }, // 2nd token
- paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, true }, // 2nd token + 2nd token
- paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, true }, // mixed: 2nd token + 1st token + part of 1st token
+ paged_attention_test_params{ {{10, 0}}, 2, 64, 16, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token
+ paged_attention_test_params{ {{36, 0}}, 2, 64, 16, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token
+ paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token long
+ paged_attention_test_params{ {{10, 0}, {30, 0}}, 2, 64, 16, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token + 1st token
+ paged_attention_test_params{ {{128, 0}, {256, 0}}, 2, 64, 16, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token + 1st token
+ paged_attention_test_params{ {{1, 10}}, 2, 64, 16, ENABLE_SCORES, DISABLE_ROTATION }, // 2nd token
+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, ENABLE_SCORES, DISABLE_ROTATION }, // 2nd token + 2nd token
+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, ENABLE_SCORES, DISABLE_ROTATION }, // mixed: 2nd token + 1st token + part of 1st token
/* without scores output */
- paged_attention_test_params{ {{10, 0}}, 2, 64, 16, false }, // 1st token
- paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, false }, // 1st token long
- paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, false }, // 2nd token + 2nd token
+ paged_attention_test_params{ {{10, 0}}, 2, 64, 16, DISABLE_SCORES, DISABLE_ROTATION }, // 1st token
+ paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, DISABLE_SCORES, DISABLE_ROTATION }, // 1st token long
+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, DISABLE_SCORES, DISABLE_ROTATION }, // 2nd token + 2nd token
+ /* with scores, per_block rotation */
+ paged_attention_test_params{ {{10, 0}}, 2, 64, 16, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token
+ paged_attention_test_params{ {{36, 0}}, 2, 64, 16, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token
+ paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token long
+ paged_attention_test_params{ {{10, 0}, {30, 0}}, 2, 64, 16, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token + 1st token
+ paged_attention_test_params{ {{128, 0}, {256, 0}}, 2, 64, 16, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token + 1st token
+ paged_attention_test_params{ {{1, 10}}, 2, 64, 16, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 2nd token
+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 2nd token + 2nd token
+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, ENABLE_SCORES, PER_BLOCK_ROTATION }, // mixed: 2nd token + 1st token + part of 1st token
+ /* with scores, per_token rotation */
+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, ENABLE_SCORES, PER_TOKEN_ROTATION }, // 2nd token + 2nd token
+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, ENABLE_SCORES, PER_TOKEN_ROTATION }, // mixed: 2nd token + 1st token + part of 1st token
}));