Skip to content

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

[pa support SD]support pa kv cache reorder for post kv-update after speculative decode (#34875)

yanlan song96b59f7fdc16
diff --git a/src/plugins/intel_gpu/include/intel_gpu/op/pa_kv_reorder.hpp b/src/plugins/intel_gpu/include/intel_gpu/op/pa_kv_reorder.hpp
new file mode 100644
index 00000000000..0976a93040a
--- /dev/null
+++ b/src/plugins/intel_gpu/include/intel_gpu/op/pa_kv_reorder.hpp
@@ -0,0 +1,31 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/op/op.hpp"
+
+namespace ov::intel_gpu::op {
+
+class PA_KV_Reorder : public ov::op::Op {
+public:
+ OPENVINO_OP("PaKVReorder", "gpu_opset");
+
+ PA_KV_Reorder() = default;
+
+ PA_KV_Reorder(const Output<Node>& key_cache,
+ const Output<Node>& value_cache,
+ const Output<Node>& block_indices,
+ const Output<Node>& block_indices_begins,
+ const Output<Node>& block_update_indices,
+ const Output<Node>& block_update_indices_begins);
+
+ bool visit_attributes(ov::AttributeVisitor& visitor) override;
+
+ void validate_and_infer_types() override;
+
+ std::shared_ptr<Node> clone_with_new_inputs(const ov::OutputVector& new_args) const override;
+};
+
+} // namespace ov::intel_gpu::op
diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/primitives_list.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/primitives_list.hpp
index e52b5b7bb33..867abdf6afd 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/primitives_list.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/primitives_list.hpp
@@ -311,6 +311,7 @@ REGISTER_FACTORY(internal, IndirectSDPA);
REGISTER_FACTORY(internal, RoPE);
REGISTER_FACTORY(internal, DynamicQuantize);
REGISTER_FACTORY(internal, PagedAttentionExtension);
+REGISTER_FACTORY(internal, PA_KV_Reorder);
REGISTER_FACTORY(internal, LoraSubgraph);
REGISTER_FACTORY(internal, LoraSubgraphFused);
REGISTER_FACTORY(internal, VLSDPA);
diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/pa_kv_reorder.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/pa_kv_reorder.hpp
new file mode 100644
index 00000000000..52ae3df8bc7
--- /dev/null
+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/pa_kv_reorder.hpp
@@ -0,0 +1,112 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <array>
+
+#include "primitive.hpp"
+
+namespace cldnn {
+
+struct pa_kv_reorder : public primitive_base<pa_kv_reorder> {
+ CLDNN_DECLARE_PRIMITIVE(pa_kv_reorder)
+
+ enum PaKVReorderInputIdx {
+ KEY_CACHE = 0,
+ VALUE_CACHE = 1,
+ BLOCK_INDICES = 2,
+ BLOCK_INDICES_BEGINS = 3,
+ BLOCK_UPDATE_INDICES = 4,
+ BLOCK_UPDATE_INDICES_BEGINS = 5,
+ };
+
+ pa_kv_reorder() : primitive_base("", {}, 1) {}
+
+ pa_kv_reorder(primitive_id id, std::vector<input_info> inputs) : primitive_base(std::move(id), std::move(inputs), 1) {
+ OPENVINO_ASSERT(input.size() == 6, "[GPU] Unexpected input number for pa_kv_reorder primitive: ", input.size());
+ }
+
+ size_t hash() const override {
+ size_t seed = primitive::hash();
+ seed = hash_combine(seed, is_key_by_channel);
+ seed = hash_combine(seed, scales_zp_size);
+ seed = hash_combine(seed, kv_heads_num);
+ seed = hash_combine(seed, adjusted_k_head_size);
+ seed = hash_combine(seed, adjusted_paged_attention_block_size);
+ seed = hash_combine(seed, adjusted_v_head_size);
+ seed = hash_combine(seed, static_cast<size_t>(cache_dt));
+ seed = hash_combine(seed, is_kv_compressed);
+ for (const auto value : key_cache_dim_order) {
+ seed = hash_combine(seed, value);
+ }
+ for (const auto value : value_cache_dim_order) {
+ seed = hash_combine(seed, value);
+ }
+ return seed;
+ }
+
+ bool operator==(const primitive& rhs) const override {
+ if (!compare_common_params(rhs))
+ return false;
+
+ auto rhs_casted = downcast<const pa_kv_reorder>(rhs);
+ return is_key_by_channel == rhs_casted.is_key_by_channel && scales_zp_size == rhs_casted.scales_zp_size && kv_heads_num == rhs_casted.kv_heads_num &&
+ adjusted_k_head_size == rhs_casted.adjusted_k_head_size &&
+ adjusted_paged_attention_block_size == rhs_casted.adjusted_paged_attention_block_size &&
+ adjusted_v_head_size == rhs_casted.adjusted_v_head_size && cache_dt == rhs_casted.cache_dt && is_kv_compressed == rhs_casted.is_kv_compressed &&
+ key_cache_dim_order == rhs_casted.key_cache_dim_order && value_cache_dim_order == rhs_casted.value_cache_dim_order;
+ }
+
+ void save(BinaryOutputBuffer& ob) const override {
+ primitive_base<pa_kv_reorder>::save(ob);
+ ob << is_key_by_channel;
+ ob << scales_zp_size;
+ ob << kv_heads_num;
+ ob << adjusted_k_head_size;
+ ob << adjusted_paged_attention_block_size;
+ ob << adjusted_v_head_size;
+ ob << static_cast<int64_t>(cache_dt);
+ ob << is_kv_compressed;
+ for (const auto value : key_cache_dim_order) {
+ ob << value;
+ }
+ for (const auto value : value_cache_dim_order) {
+ ob << value;
+ }
+ }
+
+ void load(BinaryInputBuffer& ib) override {
+ primitive_base<pa_kv_reorder>::load(ib);
+ ib >> is_key_by_channel;
+ ib >> scales_zp_size;
+ ib >> kv_heads_num;
+ ib >> adjusted_k_head_size;
+ ib >> adjusted_paged_attention_block_size;
+ ib >> adjusted_v_head_size;
+ int64_t cache_dt_val = static_cast<int64_t>(data_types::f16);
+ ib >> cache_dt_val;
+ cache_dt = static_cast<data_types>(cache_dt_val);
+ ib >> is_kv_compressed;
+ for (auto& value : key_cache_dim_order) {
+ ib >> value;
+ }
+ for (auto& value : value_cache_dim_order) {
+ ib >> value;
+ }
+ }
+
+ bool is_key_by_channel = false;
+ size_t scales_zp_size = 0;
+ size_t kv_heads_num = 0;
+ size_t adjusted_k_head_size = 0;
+ size_t adjusted_paged_attention_block_size = 0;
+ size_t adjusted_v_head_size = 0;
+ data_types cache_dt = data_types::f16;
+ bool is_kv_compressed = false;
+ std::array<size_t, 4> key_cache_dim_order = {0, 1, 3, 2};
+ std::array<size_t, 4> value_cache_dim_order = {0, 1, 2, 3};
+};
+
+} // namespace cldnn
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_reorder_ref.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_reorder_ref.cl
new file mode 100644
index 00000000000..331b998e086
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_cache_reorder_ref.cl
@@ -0,0 +1,375 @@
+// Copyright (C) 2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "include/batch_headers/common.cl"
+#include "include/batch_headers/int4_utils.cl"
+
+// Reference kernel for KV-cache partial reorder.
+
+// - src_id / dst_id in block_update_indices are treated as per-sequence global token positions
+// - The corresponding (block_in_seq, slot_in_block) are derived as:
+// block_in_seq = id / PAGED_ATTENTION_BLOCK_SIZE
+// slot = id % PAGED_ATTENTION_BLOCK_SIZE
+// - block_in_seq is mapped to physical block id as:
+// phys_block = block_indices[block_indices_begins[seq] + block_in_seq]
+
+// block_update_indices as int32 pairs:
+// { src_id_0, dst_id_0, src_id_1, dst_id_1, ... }
+//
+// block_update_indices_begins:
+// { 0, seq0_slots, seq0_slots + seq1_slots, ... }
+//
+
+#define VEC_BLK_SIZE 8
+#define VLOAD CAT(vload, VEC_BLK_SIZE)
+#define VSTORE CAT(vstore, VEC_BLK_SIZE)
+#define UINT4_RANGE 15
+
+#if defined(IS_KV_COMPRESSED) && defined(IS_KEY_BY_CHANNEL)
+inline void FUNC(requantize_and_store_by_channel_block)(__global OUTPUT_TYPE* key_cache,
+ const uint dst_base,
+ UNCOMPRESSED_TYPE* dequant_vals) {
+ UNCOMPRESSED_TYPE min_value = UNCOMPRESSED_VAL_MAX;
+ UNCOMPRESSED_TYPE max_value = UNCOMPRESSED_VAL_MIN;
+
+ for (uint token = 0; token < PAGED_ATTENTION_BLOCK_SIZE; ++token) {
+ UNCOMPRESSED_TYPE val = dequant_vals[token];
+ min_value = fmin(min_value, val);
+ max_value = fmax(max_value, val);
+ }
+
+ // Re-quantize and store
+ #define ACCUMULATOR_TYPE float
+ ACCUMULATOR_TYPE range = max_value - min_value;
+ const ACCUMULATOR_TYPE min_range = fabs(max_value * 0.1f);
+ if (range <= min_range) {
+ // When the range is very small, expand the range to avoid zp overflow
+ range += fmax(1.0f, min_range);
+ }
+ ACCUMULATOR_TYPE scale_tmp = (ACCUMULATOR_TYPE)((CHAR_MAX - CHAR_MIN) / range);
+ ACCUMULATOR_TYPE zp_tmp = (ACCUMULATOR_TYPE)(-min_value * scale_tmp) + CHAR_MIN;
+ UNCOMPRESSED_TYPE scale = (UNCOMPRESSED_TYPE)(scale_tmp);
+ UNCOMPRESSED_TYPE zp = (UNCOMPRESSED_TYPE)(zp_tmp);
+ #undef ACCUMULATOR_TYPE
+
+ for (uint token = 0; token < PAGED_ATTENTION_BLOCK_SIZE; ++token) {
+ OUTPUT_TYPE quantized = convert_char_rte(dequant_vals[token] * scale + zp);
+ key_cache[dst_base + token] = quantized;
+ }
+
+ UNCOMPRESSED_TYPE* comp_ptr = (UNCOMPRESSED_TYPE*)(key_cache + (dst_base + PAGED_ATTENTION_BLOCK_SIZE));
+ comp_ptr[0] = 1.0f / scale;
+ comp_ptr[1] = zp;
+}
+#endif
+
+#if defined(IS_KV_COMPRESSED) && defined(IS_KEY_BY_CHANNEL) && IS_INT4_COMPRESSED
+inline void FUNC(requantize_and_store_by_channel_block_int4)(__global OUTPUT_TYPE* key_cache,
+ const uint dst_base,
+ UNCOMPRESSED_TYPE* dequant_vals0,
+ UNCOMPRESSED_TYPE* dequant_vals1) {
+ UNCOMPRESSED_TYPE min_value0 = UNCOMPRESSED_VAL_MAX;
+ UNCOMPRESSED_TYPE max_value0 = UNCOMPRESSED_VAL_MIN;
+ UNCOMPRESSED_TYPE min_value1 = UNCOMPRESSED_VAL_MAX;
+ UNCOMPRESSED_TYPE max_value1 = UNCOMPRESSED_VAL_MIN;
+
+ for (uint token = 0; token < PAGED_ATTENTION_BLOCK_SIZE; ++token) {
+ UNCOMPRESSED_TYPE val0 = dequant_vals0[token];
+ UNCOMPRESSED_TYPE val1 = dequant_vals1[token];
+ min_value0 = fmin(min_value0, val0);
+ max_value0 = fmax(max_value0, val0);
+ min_value1 = fmin(min_value1, val1);
+ max_value1 = fmax(max_value1, val1);
+ }
+
+ #define ACCUMULATOR_TYPE float
+ ACCUMULATOR_TYPE range0 = max_value0 - min_value0;
+ ACCUMULATOR_TYPE range1 = max_value1 - min_value1;
+ const ACCUMULATOR_TYPE min_range0 = fabs(max_value0 * 0.1f);
+ const ACCUMULATOR_TYPE min_range1 = fabs(max_value1 * 0.1f);
+ if (range0 <= min_range0) {
+ range0 += fmax(1.0f, min_range0);
+ }
+ if (range1 <= min_range1) {
+ range1 += fmax(1.0f, min_range1);
+ }
+ ACCUMULATOR_TYPE scale_tmp0 = (ACCUMULATOR_TYPE)((UINT4_RANGE) / range0);
+ ACCUMULATOR_TYPE zp_tmp0 = (ACCUMULATOR_TYPE)(-min_value0 * scale_tmp0);
+ ACCUMULATOR_TYPE scale_tmp1 = (ACCUMULATOR_TYPE)((UINT4_RANGE) / range1);
+ ACCUMULATOR_TYPE zp_tmp1 = (ACCUMULATOR_TYPE)(-min_value1 * scale_tmp1);
+ UNCOMPRESSED_TYPE scale0 = (UNCOMPRESSED_TYPE)(scale_tmp0);
+ UNCOMPRESSED_TYPE zp0 = (UNCOMPRESSED_TYPE)(zp_tmp0);
+ UNCOMPRESSED_TYPE scale1 = (UNCOMPRESSED_TYPE)(scale_tmp1);
+ UNCOMPRESSED_TYPE zp1 = (UNCOMPRESSED_TYPE)(zp_tmp1);
+ #undef ACCUMULATOR_TYPE
+
+ for (uint token = 0; token < PAGED_ATTENTION_BLOCK_SIZE; ++token) {
+ char2 quantized = 0;
+ quantized.s0 = convert_char_rte(dequant_vals0[token] * scale0 + zp0);
+ quantized.s1 = convert_char_rte(dequant_vals1[token] * scale1 + zp1);
+ key_cache[dst_base + token] = cvt_int8x2_to_uint4x2(quantized);
+ }
+
+ UNCOMPRESSED_TYPE* comp_ptr = (UNCOMPRESSED_TYPE*)(key_cache + (dst_base + PAGED_ATTENTION_BLOCK_SIZE));
+ comp_ptr[0] = 1.0f / scale0;
+ comp_ptr[1] = zp0;
+ comp_ptr[2] = 1.0f / scale1;
+ comp_ptr[3] = zp1;
+}
+#endif
+
+REQD_SUB_GROUP_SIZE(SUBGROUP_SIZE)
+KERNEL(pa_kv_cache_reorder)(
+ OPTIONAL_SHAPE_INFO_ARG
+ __global const INPUT0_TYPE* block_indices,
+ __global const INPUT1_TYPE* block_indices_begins,
+ __global const INPUT2_TYPE* block_update_indices,
+ __global const INPUT3_TYPE* block_update_indices_begins,
+ __global OUTPUT_TYPE* key_cache,
+ __global OUTPUT1_TYPE* value_cache
+) {
+ const uint seq_idx = (uint)get_global_id(0);
+ const uint head_idx = (uint)get_global_id(1);
+ const uint sglid = (uint)get_local_id(2);
+
+ const uint block_indices_base = (uint)block_indices_begins[seq_idx];
+ const uint pos_begin = (uint)block_update_indices_begins[seq_idx];
+ const uint pos_end = (uint)block_update_indices_begins[seq_idx + 1];
+ const uint blocks_in_seq = (uint)block_indices_begins[seq_idx + 1] - block_indices_base;
+
+ // Sequentially apply all reorder slots for this sequence to avoid data races
+ // for overlapping src/dst slots.
+ for (uint pos = pos_begin; pos < pos_end; pos++) {
+ const uint pair_base = pos * 2;
+ const int src_i = (int)block_update_indices[pair_base + 0];
+ const int dst_i = (int)block_update_indices[pair_base + 1];
+
+ if (src_i < 0 || dst_i < 0)
+ continue;
+
+ const uint local_src = (uint)src_i;
+ const uint local_dst = (uint)dst_i;
+
+ uint src_block = 0;
+ uint src_slot = 0;
+ uint dst_block = 0;
+ uint dst_slot = 0;
+ const uint src_block_in_seq = local_src / PAGED_ATTENTION_BLOCK_SIZE;
+ if (src_block_in_seq >= blocks_in_seq)
+ continue;
+ src_slot = local_src - src_block_in_seq * PAGED_ATTENTION_BLOCK_SIZE;
+ src_block = (uint)block_indices[block_indices_base + src_block_in_seq];
+
+ const uint dst_block_in_seq = local_dst / PAGED_ATTENTION_BLOCK_SIZE;
+ if (dst_block_in_seq >= blocks_in_seq)
+ continue;
+ dst_slot = local_dst - dst_block_in_seq * PAGED_ATTENTION_BLOCK_SIZE;
+ dst_block = (uint)block_indices[block_indices_base + dst_block_in_seq];
+ // KEY_CACHE: [block, head, k_hidden, token]
+ #if IS_KV_COMPRESSED && IS_INT4_COMPRESSED
+ const uint phys_adjusted_k_head_size = PACKED_ADJUSTED_K_HEAD_SIZE;
+ const uint phys_adjusted_v_head_size = PACKED_ADJUSTED_V_HEAD_SIZE;
+ #else
+ const uint phys_adjusted_k_head_size = ADJUSTED_K_HEAD_SIZE;
+ const uint phys_adjusted_v_head_size = ADJUSTED_V_HEAD_SIZE;
+ #endif
+
+ #ifdef IS_KEY_BY_CHANNEL
+ const uint key_src_base = OUTPUT_OFFSET + src_block * KV_HEADS_NUM * phys_adjusted_k_head_size * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE +
+ head_idx * phys_adjusted_k_head_size * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;
+ const uint key_dst_base = OUTPUT_OFFSET + dst_block * KV_HEADS_NUM * phys_adjusted_k_head_size * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE +
+ head_idx * phys_adjusted_k_head_size * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;
+ #else
+ const uint key_src_base = OUTPUT_OFFSET + src_block * KV_HEADS_NUM * phys_adjusted_k_head_size * PAGED_ATTENTION_BLOCK_SIZE +
+ head_idx * phys_adjusted_k_head_size * PAGED_ATTENTION_BLOCK_SIZE;
+ const uint key_dst_base = OUTPUT_OFFSET + dst_block * KV_HEADS_NUM * phys_adjusted_k_head_size * PAGED_ATTENTION_BLOCK_SIZE +
+ head_idx * phys_adjusted_k_head_size * PAGED_ATTENTION_BLOCK_SIZE;
+ #endif
+ // VALUE_CACHE: [block, head, token, v_hidden]
+ const uint val_src_base = OUTPUT1_OFFSET + src_block * KV_HEADS_NUM * PAGED_ATTENTION_BLOCK_SIZE * phys_adjusted_v_head_size +
+ head_idx * PAGED_ATTENTION_BLOCK_SIZE * phys_adjusted_v_head_size;
+ const uint val_dst_base = OUTPUT1_OFFSET + dst_block * KV_HEADS_NUM * PAGED_ATTENTION_BLOCK_SIZE * phys_adjusted_v_head_size +
+ head_idx * PAGED_ATTENTION_BLOCK_SIZE * phys_adjusted_v_head_size;
+ #if !IS_KV_COMPRESSED
+ for (uint k = sglid; k < (uint)K_HEAD_SIZE; k += (uint)SUBGROUP_SIZE) {
+ const uint src_off = key_src_base + k * PAGED_ATTENTION_BLOCK_SIZE + src_slot;
+ const uint dst_off = key_dst_base + k * PAGED_ATTENTION_BLOCK_SIZE + dst_slot;
+ key_cache[dst_off] = key_cache[src_off];
+ }
+
+ for (uint v = sglid; v < (uint)V_HEAD_SIZE; v += (uint)SUBGROUP_SIZE) {
+ const uint src_off = val_src_base + src_slot * V_HEAD_SIZE + v;
+ const uint dst_off = val_dst_base + dst_slot * V_HEAD_SIZE + v;
+ value_cache[dst_off] = value_cache[src_off];
+ }
+ #else // IS_KV_COMPRESSED
+ #if IS_INT4_COMPRESSED
+ const uint phys_k_head_size = PACKED_K_HEAD_SIZE;
+ const uint phys_v_head_size = PACKED_V_HEAD_SIZE;
+ #else
+ const uint phys_k_head_size = K_HEAD_SIZE;
+ const uint phys_v_head_size = V_HEAD_SIZE;
+ #endif
+
+ #ifdef IS_KEY_BY_CHANNEL
+ // Key by channel compressed mode
+ // 1. load the original key value
+ // 2. copy the src slot to dst slot
+ // 3. if dst is in a different block, re-quantize dst block with updated scale/zp
+ #if IS_INT4_COMPRESSED
+ for (uint k = sglid; k < phys_k_head_size; k += (uint)SUBGROUP_SIZE) {
+ const uint src_off = key_src_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE + src_slot;
+ const uint dst_base = key_dst_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;
+
+ const uint src_comp_off = key_src_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE + PAGED_ATTENTION_BLOCK_SIZE;
+ const uint dst_comp_off = key_dst_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE + PAGED_ATTENTION_BLOCK_SIZE;
+ UNCOMPRESSED_TYPE* src_comp_ptr = key_cache + src_comp_off;
+ UNCOMPRESSED_TYPE* dst_comp_ptr = key_cache + dst_comp_off;
+
+ const UNCOMPRESSED_TYPE src_scale_inv0 = src_comp_ptr[0];
+ const UNCOMPRESSED_TYPE src_zp0 = src_comp_ptr[1];
+ const UNCOMPRESSED_TYPE src_scale_inv1 = src_comp_ptr[2];
+ const UNCOMPRESSED_TYPE src_zp1 = src_comp_ptr[3];
+ const UNCOMPRESSED_TYPE dst_scale_inv0 = dst_comp_ptr[0];
+ const UNCOMPRESSED_TYPE dst_zp0 = dst_comp_ptr[1];
+ const UNCOMPRESSED_TYPE dst_scale_inv1 = dst_comp_ptr[2];
+ const UNCOMPRESSED_TYPE dst_zp1 = dst_comp_ptr[3];
+
+ OUTPUT_TYPE src_packed = key_cache[src_off];
+ char2 src_unpacked = unpack_to_char(*(uint4x2_t *)&src_packed);
+
+ const UNCOMPRESSED_TYPE src_val0 = ((UNCOMPRESSED_TYPE)(src_unpacked.s0) - src_zp0) * src_scale_inv0;
+ const UNCOMPRESSED_TYPE src_val1 = ((UNCOMPRESSED_TYPE)(src_unpacked.s1) - src_zp1) * src_scale_inv1;
+
+ UNCOMPRESSED_TYPE dequant_vals0[PAGED_ATTENTION_BLOCK_SIZE];
+ UNCOMPRESSED_TYPE dequant_vals1[PAGED_ATTENTION_BLOCK_SIZE];
+
+ for (uint token = 0; token < PAGED_ATTENTION_BLOCK_SIZE; ++token) {
+ OUTPUT_TYPE dst_packed = key_cache[dst_base + token];
+ char2 dst_unpacked = unpack_to_char(*(uint4x2_t *)&dst_packed);
+
+ UNCOMPRESSED_TYPE val0 = ((UNCOMPRESSED_TYPE)(dst_unpacked.s0) - dst_zp0) * dst_scale_inv0;
+ UNCOMPRESSED_TYPE val1 = ((UNCOMPRESSED_TYPE)(dst_unpacked.s1) - dst_zp1) * dst_scale_inv1;
+ if (token == dst_slot) {
+ val0 = src_val0;
+ val1 = src_val1;
+ }
+ dequant_vals0[token] = val0;
+ dequant_vals1[token] = val1;
+ }
+
+ FUNC_CALL(requantize_and_store_by_channel_block_int4)(key_cache, dst_base, dequant_vals0, dequant_vals1);
+ }
+ #else
+ if (src_block == dst_block) {
+ // src and dst slot are in the same block
+ for (uint k = sglid; k < (uint)K_HEAD_SIZE; k += (uint)SUBGROUP_SIZE) {
+ const uint src_off = key_src_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE + src_slot;
+ const uint dst_base = key_dst_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;
+ const uint dst_off = dst_base + dst_slot;
+
+ const uint dst_comp_off = key_dst_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE + PAGED_ATTENTION_BLOCK_SIZE;
+ UNCOMPRESSED_TYPE* dst_comp_ptr = key_cache + dst_comp_off;
+
+ const UNCOMPRESSED_TYPE dst_scale_inv = dst_comp_ptr[0];
+ const UNCOMPRESSED_TYPE dst_zp = dst_comp_ptr[1];
+
+ key_cache[dst_off] = key_cache[src_off];
+
+ UNCOMPRESSED_TYPE dequant_vals[PAGED_ATTENTION_BLOCK_SIZE];
+
+ for (uint token = 0; token < PAGED_ATTENTION_BLOCK_SIZE; ++token) {
+ UNCOMPRESSED_TYPE val =
+ ((UNCOMPRESSED_TYPE)(key_cache[dst_base + token]) - dst_zp) * dst_scale_inv;
+ dequant_vals[token] = val;
+ }
+ FUNC_CALL(requantize_and_store_by_channel_block)(key_cache, dst_base, dequant_vals);
+ }
+ } else {
+ // in this case, the dst_block is a full PAGED_ATTENTION_BLOCK_SIZE block
+ for (uint k = sglid; k < (uint)K_HEAD_SIZE; k += (uint)SUBGROUP_SIZE) {
+ const uint src_off = key_src_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE + src_slot;
+ const uint dst_base = key_dst_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE;
+
+ const uint src_comp_off = key_src_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE + PAGED_ATTENTION_BLOCK_SIZE;
+ const uint dst_comp_off = key_dst_base + k * ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE + PAGED_ATTENTION_BLOCK_SIZE;
+ UNCOMPRESSED_TYPE* src_comp_ptr = key_cache + src_comp_off;
+ UNCOMPRESSED_TYPE* dst_comp_ptr = key_cache + dst_comp_off;
+
+ const UNCOMPRESSED_TYPE src_scale_inv = src_comp_ptr[0];
+ const UNCOMPRESSED_TYPE src_zp = src_comp_ptr[1];
+ const UNCOMPRESSED_TYPE dst_scale_inv = dst_comp_ptr[0];
+ const UNCOMPRESSED_TYPE dst_zp = dst_comp_ptr[1];
+
+ const UNCOMPRESSED_TYPE src_val =
+ ((UNCOMPRESSED_TYPE)(key_cache[src_off]) - src_zp) * src_scale_inv;
+
+ UNCOMPRESSED_TYPE dequant_vals[PAGED_ATTENTION_BLOCK_SIZE];
+
+ for (uint token = 0; token < PAGED_ATTENTION_BLOCK_SIZE; ++token) {
+ UNCOMPRESSED_TYPE val =
+ ((UNCOMPRESSED_TYPE)(key_cache[dst_base + token]) - dst_zp) * dst_scale_inv;
+ if (token == dst_slot) {
+ val = src_val;
+ }
+ dequant_vals[token] = val;
+ }
+ FUNC_CALL(requantize_and_store_by_channel_block)(key_cache, dst_base, dequant_vals);
+ }
+ }
+ #endif
+ #else
+ // per-token quantization: copy quantized values and comp data for token
+ for (uint k = sglid; k < phys_k_head_size; k += (uint)SUBGROUP_SIZE) {
+ const uint src_off = key_src_base + k * PAGED_ATTENTION_BLOCK_SIZE + src_slot;
+ const uint dst_off = key_dst_base + k * PAGED_ATTENTION_BLOCK_SIZE + dst_slot;
+ key_cache[dst_off] = key_cache[src_off];
+ }
+
+ if (sglid == 0) {
+ const uint comp_src_base = key_src_base + phys_k_head_size * PAGED_ATTENTION_BLOCK_SIZE;
+ const uint comp_dst_base = key_dst_base + phys_k_head_size * PAGED_ATTENTION_BLOCK_SIZE;
+ UNCOMPRESSED_TYPE* src_comp_ptr = key_cache + comp_src_base;
+ UNCOMPRESSED_TYPE* dst_comp_ptr = key_cache + comp_dst_base;
+ dst_comp_ptr[dst_slot] = src_comp_ptr[src_slot];
+ dst_comp_ptr[PAGED_ATTENTION_BLOCK_SIZE + dst_slot] = src_comp_ptr[PAGED_ATTENTION_BLOCK_SIZE + src_slot];
+ }
+ #endif
+
+ // value cache: per-token quantization
+ #if IS_INT4_COMPRESSED
+ for (uint v = sglid; v < phys_v_head_size; v += (uint)SUBGROUP_SIZE) {
+ const uint src_off = val_src_base + src_slot * phys_v_head_size + v;
+ const uint dst_off = val_dst_base + dst_slot * phys_v_head_size + v;
+ value_cache[dst_off] = value_cache[src_off];
+ }
+ #else
+ #define VEC_SIZE 8
+ uint v = sglid * VEC_SIZE;
+ for (; v + VEC_SIZE - 1 < (uint)V_HEAD_SIZE; v += SUBGROUP_SIZE * VEC_SIZE) {
+ const uint src_off = val_src_base + src_slot * V_HEAD_SIZE + v;
+ const uint dst_off = val_dst_base + dst_slot * V_HEAD_SIZE + v;
+ VSTORE(VLOAD(0, value_cache + src_off), 0, value_cache + dst_off);
+ }
+
+ // Handle leftovers from the first non-vectorized index.
+ const uint vec_tail_begin = ((uint)V_HEAD_SIZE / VEC_SIZE) * VEC_SIZE;
+ for (v = vec_tail_begin + sglid; v < (uint)V_HEAD_SIZE; v += SUBGROUP_SIZE) {
+ uint src_off = val_src_base + src_slot * V_HEAD_SIZE + v;
+ uint dst_off = val_dst_base + dst_slot * V_HEAD_SIZE + v;
+ value_cache[dst_off] = value_cache[src_off];
+ }
+ #endif
+
+ if (sglid == 0) {
+ const uint comp_src_base = val_src_base + phys_v_head_size * PAGED_ATTENTION_BLOCK_SIZE;
+ const uint comp_dst_base = val_dst_base + phys_v_head_size * PAGED_ATTENTION_BLOCK_SIZE;
+ UNCOMPRESSED_TYPE* src_comp_ptr = value_cache + comp_src_base;
+ UNCOMPRESSED_TYPE* dst_comp_ptr = value_cache + comp_dst_base;
+ dst_comp_ptr[dst_slot] = src_comp_ptr[src_slot];
+ dst_comp_ptr[PAGED_ATTENTION_BLOCK_SIZE + dst_slot] = src_comp_ptr[PAGED_ATTENTION_BLOCK_SIZE + src_slot];
+ }
+ #endif
+ }
+}
\ No newline at end of file
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.cpp
new file mode 100644
index 00000000000..f285d0ee47a
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.cpp
@@ -0,0 +1,186 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "pa_kv_reorder.hpp"
+
+#include "common_utils/jitter.hpp"
+#include "intel_gpu/primitives/pa_kv_reorder.hpp"
+#include "intel_gpu/primitives/paged_attention.hpp"
+#include "primitive_ocl_base.hpp"
+#include "utils/jitter.hpp"
+#include "utils/kernel_generator.hpp"
+
+namespace ov::intel_gpu::ocl {
+
+namespace {
+constexpr size_t subgroup_size = 16;
+constexpr size_t u4_elems_per_byte = 2;
+class PaKVReorderGenerator : public KernelGenerator {
+public:
+ PaKVReorderGenerator() : KernelGenerator("pa_kv_cache_reorder_ref") {}
+
+protected:
+ [[nodiscard]] JitConstants get_jit_constants(const kernel_impl_params& params) const override {
+ auto jit = make_base_jit_constants(params);
+
+ const auto& in_offsets_map = params.in_port_to_shape_info_offset;
+
+ constexpr static std::array input_ids = {cldnn::pa_kv_reorder::PaKVReorderInputIdx::BLOCK_INDICES,
+ cldnn::pa_kv_reorder::PaKVReorderInputIdx::BLOCK_INDICES_BEGINS,
+ cldnn::pa_kv_reorder::PaKVReorderInputIdx::BLOCK_UPDATE_INDICES,
+ cldnn::pa_kv_reorder::PaKVReorderInputIdx::BLOCK_UPDATE_INDICES_BEGINS};
+
+ for (size_t i = 0; i < input_ids.size(); i++) {
+ const size_t tensor_id = input_ids.at(i);
+ jit.add(make_layout_jit_constants("INPUT" + to_code_string(i), params.input_layouts[tensor_id], in_offsets_map.at(tensor_id)));
+ }
+
+ constexpr size_t key_cache_id = cldnn::pa_kv_reorder::PaKVReorderInputIdx::KEY_CACHE;
+ constexpr size_t value_cache_id = cldnn::pa_kv_reorder::PaKVReorderInputIdx::VALUE_CACHE;
+
+ jit.add(make_layout_jit_constants("OUTPUT", params.input_layouts[key_cache_id], in_offsets_map.at(key_cache_id)));
+ jit.add(make_layout_jit_constants("OUTPUT" + to_code_string(1), params.input_layouts[value_cache_id], in_offsets_map.at(value_cache_id)));
+
+ const auto desc = params.typed_desc<cldnn::pa_kv_reorder>();
+ OPENVINO_ASSERT(desc->kv_heads_num > 0, "[GPU] pa_kv_reorder expects positive kv_heads_num in descriptor");
+ OPENVINO_ASSERT(desc->adjusted_k_head_size > 0 && desc->adjusted_v_head_size > 0, "[GPU] pa_kv_reorder expects positive head sizes in descriptor");
+ OPENVINO_ASSERT(desc->adjusted_paged_attention_block_size > 0, "[GPU] pa_kv_reorder expects positive block size in descriptor");
+
+ const size_t kv_heads_num = desc->kv_heads_num;
+ const size_t adjusted_k_head_size = desc->adjusted_k_head_size;
+ const size_t adjusted_paged_attention_block_size = desc->adjusted_paged_attention_block_size;
+ const size_t adjusted_v_head_size = desc->adjusted_v_head_size;
+
+ jit.make("KV_HEADS_NUM", kv_heads_num);
+ jit.make("PAGED_ATTENTION_BLOCK_SIZE", cldnn::paged_attention::block_size);
+ jit.make("SUBGROUP_SIZE", subgroup_size);
+
+ const auto key_cache_dt = desc->cache_dt;
+ const bool is_kv_compressed = desc->is_kv_compressed;
+ const bool is_int4_compressed = data_type_traits::is_i4_u4(key_cache_dt);
+ jit.make("IS_KV_COMPRESSED", is_kv_compressed ? 1 : 0);
+ jit.make("IS_INT4_COMPRESSED", is_int4_compressed ? 1 : 0);
+ jit.make("U4_ELEMS_PER_BYTE", u4_elems_per_byte);
+ const size_t scales_zp_size = desc->scales_zp_size;
+ const bool is_key_by_channel = desc->is_key_by_channel;
+
+ const auto align_to = [](size_t value, size_t alignment) {
+ return ((value + alignment - 1) / alignment) * alignment;
+ };
+
+ if (is_kv_compressed) {
+ if (is_key_by_channel) {
+ jit.make("IS_KEY_BY_CHANNEL", 1);
+ jit.make("K_HEAD_SIZE", adjusted_k_head_size);
+ jit.make("ADJUSTED_K_HEAD_SIZE", adjusted_k_head_size);
+ jit.make("ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE", adjusted_paged_attention_block_size);
+ } else {
+ OPENVINO_ASSERT(adjusted_k_head_size >= scales_zp_size, "[GPU] Invalid pa_kv_reorder key cache shape for compressed per-token mode");
+ jit.make("K_HEAD_SIZE", adjusted_k_head_size - scales_zp_size);
+ jit.make("ADJUSTED_K_HEAD_SIZE", adjusted_k_head_size);
+ jit.make("ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE", cldnn::paged_attention::block_size);
+ }
+ OPENVINO_ASSERT(adjusted_v_head_size >= scales_zp_size, "[GPU] Invalid pa_kv_reorder value cache shape for compressed mode");
+ const size_t k_head_size = is_key_by_channel ? adjusted_k_head_size : (adjusted_k_head_size - scales_zp_size);
+ const size_t v_head_size = adjusted_v_head_size - scales_zp_size;
+ jit.make("V_HEAD_SIZE", v_head_size);
+ jit.make("ADJUSTED_V_HEAD_SIZE", adjusted_v_head_size);
+
+ if (is_int4_compressed) {
+ const size_t packed_k_head_size = align_to(k_head_size / u4_elems_per_byte, subgroup_size);
+ const size_t packed_v_head_size = align_to(v_head_size / u4_elems_per_byte, subgroup_size);
+ const size_t packed_adjusted_k_head_size = is_key_by_channel ? packed_k_head_size : packed_k_head_size + scales_zp_size;
+ const size_t packed_adjusted_v_head_size = packed_v_head_size + scales_zp_size;
+ jit.make("PACKED_K_HEAD_SIZE", packed_k_head_size);
+ jit.make("PACKED_V_HEAD_SIZE", packed_v_head_size);
+ jit.make("PACKED_ADJUSTED_K_HEAD_SIZE", packed_adjusted_k_head_size);
+ jit.make("PACKED_ADJUSTED_V_HEAD_SIZE", packed_adjusted_v_head_size);
+ }
+ } else {
+ jit.make("ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE", cldnn::paged_attention::block_size);
+ jit.make("K_HEAD_SIZE", adjusted_k_head_size);
+ jit.make("V_HEAD_SIZE", adjusted_v_head_size);
+ jit.make("ADJUSTED_K_HEAD_SIZE", adjusted_k_head_size);
+ jit.make("ADJUSTED_V_HEAD_SIZE", adjusted_v_head_size);
+ }
+
+ jit.add(make_type_jit_constants("UNCOMPRESSED", is_kv_compressed ? cldnn::data_types::f16 : key_cache_dt));
+ return jit;
+ }
+
+ [[nodiscard]] Arguments get_arguments_desc(const kernel_impl_params& params) const override {
+ Arguments args;
+
+ if (params.is_dynamic()) {
+ args.push_back({ArgumentDescriptor::Types::SHAPE_INFO, 0});
+ }
+
+ args.push_back({ArgumentDescriptor::Types::INPUT, cldnn::pa_kv_reorder::PaKVReorderInputIdx::BLOCK_INDICES});
+ args.push_back({ArgumentDescriptor::Types::INPUT, cldnn::pa_kv_reorder::PaKVReorderInputIdx::BLOCK_INDICES_BEGINS});
+ args.push_back({ArgumentDescriptor::Types::INPUT, cldnn::pa_kv_reorder::PaKVReorderInputIdx::BLOCK_UPDATE_INDICES});
+ args.push_back({ArgumentDescriptor::Types::INPUT, cldnn::pa_kv_reorder::PaKVReorderInputIdx::BLOCK_UPDATE_INDICES_BEGINS});
+
+ args.push_back({ArgumentDescriptor::Types::INPUT, cldnn::pa_kv_reorder::PaKVReorderInputIdx::KEY_CACHE});
+ args.push_back({ArgumentDescriptor::Types::INPUT, cldnn::pa_kv_reorder::PaKVReorderInputIdx::VALUE_CACHE});
+
+ return args;
+ }
+
+ [[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override {
+ return DispatchDataFunc{[](const RuntimeParams& params, KernelData& kd, ImplRuntimeParams* rt_params) {
+ assert(!params.is_dynamic());
+ auto& wgs = kd.params.workGroups;
+ auto& scalars = kd.params.scalars;
+ scalars.clear();
+
+ const auto desc = params.typed_desc<cldnn::pa_kv_reorder>();
+ OPENVINO_ASSERT(desc->kv_heads_num > 0, "[GPU] pa_kv_reorder expects positive kv_heads_num in descriptor");
+ const auto heads_number = desc->kv_heads_num;
+
+ const auto& begins_input = params.input_layouts[cldnn::pa_kv_reorder::PaKVReorderInputIdx::BLOCK_UPDATE_INDICES_BEGINS];
+ const auto begins_len = static_cast<size_t>(begins_input.get_partial_shape()[0].get_length());
+ OPENVINO_ASSERT(begins_len >= 1, "[GPU] BLOCK_UPDATE_INDICES_BEGINS must have at least 1 element");
+ const auto sequences_number = begins_len - 1;
+
+ const bool is_kv_compressed = desc->is_kv_compressed;
+
+ const auto max_wg_size = params.get_device_info().max_work_group_size;
+ size_t max_heads_per_wg = static_cast<size_t>(max_wg_size / subgroup_size);
+ if (max_heads_per_wg == 0) {
+ max_heads_per_wg = 1;
+ }
+ auto wg_heads_number = std::min(heads_number, max_heads_per_wg);
+ wgs.global = {sequences_number, heads_number, subgroup_size};
+ wgs.local = {1, is_kv_compressed ? 1 : wg_heads_number, subgroup_size};
+ }};
+ }
+};
+
+class PaKVReorderImpl : public PrimitiveImplOCL {
+public:
+ DECLARE_OBJECT_TYPE_SERIALIZATION(ov::intel_gpu::ocl::PaKVReorderImpl)
+
+ Stage::Ptr kv_reorder = make_stage<PaKVReorderGenerator>();
+
+ PaKVReorderImpl() : PrimitiveImplOCL(PA_KV_reorder::get_type_info_static()) {}
+ PaKVReorderImpl(const program_node& node, const RuntimeParams& params) : PaKVReorderImpl() {
+ add_stage(kv_reorder, params);
+ }
+
+ [[nodiscard]] std::unique_ptr<primitive_impl> clone() const override {
+ return make_deep_copy<PaKVReorderImpl>(this);
+ }
+};
+
+} // namespace
+
+std::unique_ptr<primitive_impl> PA_KV_reorder::create_impl(const program_node& node, const RuntimeParams& params) const {
+ assert(node.is_type<cldnn::pa_kv_reorder>());
+ return std::make_unique<PaKVReorderImpl>(node, params);
+}
+
+} // namespace ov::intel_gpu::ocl
+
+BIND_BINARY_BUFFER_WITH_TYPE(cldnn::pa_kv_reorder)
+BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::PaKVReorderImpl)
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.hpp
new file mode 100644
index 00000000000..a6084b7463c
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.hpp
@@ -0,0 +1,30 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <memory>
+#include <utility>
+
+#include "program_node.h"
+#include "registry/implementation_manager.hpp"
+
+using namespace cldnn;
+
+namespace ov::intel_gpu::ocl {
+
+struct PA_KV_reorder : public ImplementationManager {
+ OV_GPU_PRIMITIVE_IMPL("ocl::pa_kv_reorder")
+ explicit PA_KV_reorder(shape_types shape_type, ValidateFunc vf = nullptr) : ImplementationManager(impl_types::ocl, shape_type, std::move(vf)) {}
+
+ [[nodiscard]] std::unique_ptr<primitive_impl> create_impl(const program_node& node, const RuntimeParams& params) const override;
+
+ [[nodiscard]] bool validate_impl(const program_node& node) const override {
+ if (node.has_fused_primitives())
+ return false;
+ return true;
+ }
+};
+
+} // namespace ov::intel_gpu::ocl
diff --git a/src/plugins/intel_gpu/src/graph/include/pa_kv_reorder_inst.h b/src/plugins/intel_gpu/src/graph/include/pa_kv_reorder_inst.h
new file mode 100644
index 00000000000..d5e4ff764a3
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/include/pa_kv_reorder_inst.h
@@ -0,0 +1,47 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "intel_gpu/primitives/pa_kv_reorder.hpp"
+#include "primitive_inst.h"
+
+namespace cldnn {
+
+using PaKVReorderInputIdx = cldnn::pa_kv_reorder::PaKVReorderInputIdx;
+
+template <>
+struct typed_program_node<pa_kv_reorder> : public typed_program_node_base<pa_kv_reorder> {
+ using parent = typed_program_node_base<pa_kv_reorder>;
+
+public:
+ using parent::parent;
+ program_node& input(size_t index = 0) const { return get_dependency(index); }
+ std::vector<size_t> get_shape_infer_dependencies() const override { return {}; }
+};
+
+using pa_kv_reorder_node = typed_program_node<pa_kv_reorder>;
+
+template <>
+class typed_primitive_inst<pa_kv_reorder> : public typed_primitive_inst_base<pa_kv_reorder> {
+ using parent = typed_primitive_inst_base<pa_kv_reorder>;
+ using parent::parent;
+
+public:
+ template <typename ShapeType>
+ static std::vector<layout> calc_output_layouts(pa_kv_reorder_node const& node, const kernel_impl_params& impl_param);
+ static layout calc_output_layout(pa_kv_reorder_node const& node, kernel_impl_params const& impl_param);
+
+ static std::string to_string(pa_kv_reorder_node const& node);
+
+ typed_primitive_inst(network& network, pa_kv_reorder_node const& node);
+ void update_output_memory() override;
+
+private:
+ void on_execute() override;
+};
+
+using pa_kv_reorder_inst = typed_primitive_inst<pa_kv_reorder>;
+
+} // namespace cldnn
diff --git a/src/plugins/intel_gpu/src/graph/pa_kv_reorder.cpp b/src/plugins/intel_gpu/src/graph/pa_kv_reorder.cpp
new file mode 100644
index 00000000000..78223448858
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/pa_kv_reorder.cpp
@@ -0,0 +1,46 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <sstream>
+
+#include "json_object.h"
+#include "pa_kv_reorder_inst.h"
+#include "primitive_type_base.h"
+
+namespace cldnn {
+GPU_DEFINE_PRIMITIVE_TYPE_ID(pa_kv_reorder)
+
+layout pa_kv_reorder_inst::calc_output_layout(const pa_kv_reorder_node& node, const kernel_impl_params& impl_param) {
+ (void)node;
+ (void)impl_param;
+ return {ov::PartialShape{1}, data_types::u8, format::bfyx};
+}
+
+template <typename ShapeType>
+std::vector<layout> pa_kv_reorder_inst::calc_output_layouts(const pa_kv_reorder_node& node, const kernel_impl_params& impl_param) {
+ (void)node;
+ (void)impl_param;
+ return {{ov::PartialShape{1}, data_types::u8, format::bfyx}};
+}
+
+template std::vector<layout> pa_kv_reorder_inst::calc_output_layouts<ov::PartialShape>(const pa_kv_reorder_node& node, const kernel_impl_params& impl_param);
+
+std::string pa_kv_reorder_inst::to_string(const pa_kv_reorder_node& node) {
+ auto node_info = node.desc_to_json();
+
+ std::stringstream primitive_description;
+ node_info->dump(primitive_description);
+
+ return primitive_description.str();
+}
+
+pa_kv_reorder_inst::typed_primitive_inst(network& network, const pa_kv_reorder_node& node) : parent(network, node) {}
+
+void pa_kv_reorder_inst::on_execute() {}
+
+void pa_kv_reorder_inst::update_output_memory() {
+ return;
+}
+
+} // namespace cldnn
diff --git a/src/plugins/intel_gpu/src/graph/registry/pa_kv_reorder_impls.cpp b/src/plugins/intel_gpu/src/graph/registry/pa_kv_reorder_impls.cpp
new file mode 100644
index 00000000000..33519183504
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/registry/pa_kv_reorder_impls.cpp
@@ -0,0 +1,25 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "intel_gpu/primitives/pa_kv_reorder.hpp"
+#include "primitive_inst.h"
+#include "registry.hpp"
+
+#if OV_GPU_WITH_OCL
+# include "impls/ocl_v2/pa_kv_reorder.hpp"
+#endif
+
+namespace ov {
+namespace intel_gpu {
+
+using namespace cldnn;
+
+const std::vector<std::shared_ptr<cldnn::ImplementationManager>>& Registry<pa_kv_reorder>::get_implementations() {
+ static const std::vector<std::shared_ptr<ImplementationManager>> impls = {OV_GPU_CREATE_INSTANCE_OCL(ocl::PA_KV_reorder, shape_types::any)};
+
+ return impls;
+}
+
+} // namespace intel_gpu
+} // namespace ov
diff --git a/src/plugins/intel_gpu/src/graph/registry/registry.hpp b/src/plugins/intel_gpu/src/graph/registry/registry.hpp
index f916cb87ef0..e1bce52f907 100644
--- a/src/plugins/intel_gpu/src/graph/registry/registry.hpp
+++ b/src/plugins/intel_gpu/src/graph/registry/registry.hpp
@@ -151,6 +151,7 @@ REGISTER_IMPLS(lstm_seq);
REGISTER_IMPLS(gru_seq);
REGISTER_IMPLS(non_max_suppression);
REGISTER_IMPLS(paged_attention);
+REGISTER_IMPLS(pa_kv_reorder);
REGISTER_IMPLS(pooling);
REGISTER_IMPLS(reduce);
REGISTER_IMPLS(reorder);
diff --git a/src/plugins/intel_gpu/src/plugin/ops/pa_kv_reorder.cpp b/src/plugins/intel_gpu/src/plugin/ops/pa_kv_reorder.cpp
new file mode 100644
index 00000000000..d4fcf4401ee
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/ops/pa_kv_reorder.cpp
@@ -0,0 +1,81 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "intel_gpu/op/pa_kv_reorder.hpp"
+
+#include <algorithm>
+
+#include "intel_gpu/plugin/common_utils.hpp"
+#include "intel_gpu/plugin/program_builder.hpp"
+#include "intel_gpu/primitives/pa_kv_reorder.hpp"
+#include "intel_gpu/primitives/paged_attention.hpp"
+
+namespace ov {
+namespace op {
+namespace internal {
+using PA_KV_Reorder = ov::intel_gpu::op::PA_KV_Reorder;
+} // namespace internal
+} // namespace op
+} // namespace ov
+
+namespace ov::intel_gpu {
+
+static void CreatePA_KV_ReorderOp(ProgramBuilder& p, const std::shared_ptr<ov::op::internal::PA_KV_Reorder>& op) {
+ validate_inputs_count(op, {6});
+
+ auto inputs = p.GetInputInfo(op);
+ auto prim = cldnn::pa_kv_reorder(cldnn::primitive_id(layer_type_name_ID(op)), std::vector<cldnn::input_info>(inputs.begin(), inputs.end()));
+ const auto& rt_info = op->get_rt_info();
+ const auto k_head_size_id = "k_head_size";
+ const auto v_head_size_id = "v_head_size";
+ const auto num_k_heads_id = "num_k_heads";
+ const auto has_rt_head_info =
+ rt_info.find(k_head_size_id) != rt_info.end() && rt_info.find(v_head_size_id) != rt_info.end() && rt_info.find(num_k_heads_id) != rt_info.end();
+ if (!has_rt_head_info)
+ OPENVINO_THROW("[GPU] pa_kv_reorder op missing runtime head size information in rt_info");
+
+ constexpr const char* rt_is_key_by_channel = "pa_kv_reorder.is_key_by_channel";
+ constexpr const char* rt_scales_zp_size = "pa_kv_reorder.scales_zp_size";
+ constexpr const char* rt_key_dim_order = "pa_kv_reorder.key_cache_dim_order";
+ constexpr const char* rt_value_dim_order = "pa_kv_reorder.value_cache_dim_order";
+ const auto has_rt_compress_info = rt_info.find(rt_is_key_by_channel) != rt_info.end() && rt_info.find(rt_scales_zp_size) != rt_info.end() &&
+ rt_info.find(rt_key_dim_order) != rt_info.end() && rt_info.find(rt_value_dim_order) != rt_info.end();
+
+ if (!has_rt_compress_info) {
+ OPENVINO_THROW("[GPU] pa_kv_reorder op missing runtime compression information in rt_info");
+ }
+ prim.kv_heads_num = static_cast<size_t>(rt_info.at(num_k_heads_id).as<int64_t>());
+ prim.adjusted_paged_attention_block_size = cldnn::paged_attention::block_size;
+ prim.adjusted_k_head_size = static_cast<size_t>(rt_info.at(k_head_size_id).as<int64_t>());
+ prim.adjusted_v_head_size = static_cast<size_t>(rt_info.at(v_head_size_id).as<int64_t>());
+
+ prim.cache_dt = cldnn::element_type_to_data_type(op->get_input_element_type(cldnn::pa_kv_reorder::PaKVReorderInputIdx::KEY_CACHE));
+ prim.scales_zp_size = static_cast<size_t>(rt_info.at(rt_scales_zp_size).as<int64_t>());
+ prim.is_kv_compressed = prim.scales_zp_size > 0;
+
+ if (prim.is_kv_compressed) {
+ if (rt_info.at(rt_is_key_by_channel).as<bool>()) {
+ prim.is_key_by_channel = true;
+ prim.adjusted_paged_attention_block_size = prim.adjusted_paged_attention_block_size + prim.scales_zp_size;
+ } else {
+ prim.adjusted_k_head_size = prim.adjusted_k_head_size + prim.scales_zp_size;
+ }
+ prim.adjusted_v_head_size = prim.adjusted_v_head_size + prim.scales_zp_size;
+ const auto key_dim_order = rt_info.at(rt_key_dim_order).as<std::vector<size_t>>();
+ const auto value_dim_order = rt_info.at(rt_value_dim_order).as<std::vector<size_t>>();
+ OPENVINO_ASSERT(key_dim_order.size() == 4 && value_dim_order.size() == 4, "[GPU] Invalid pa_kv_reorder dim order metadata size");
+
+ std::copy(key_dim_order.begin(), key_dim_order.end(), prim.key_cache_dim_order.begin());
+ std::copy(value_dim_order.begin(), value_dim_order.end(), prim.value_cache_dim_order.begin());
+ }
+
+ prim.num_outputs = op->get_output_size();
+ prim.output_data_types = get_output_data_types(op);
+
+ p.add_primitive(*op, prim);
+}
+
+REGISTER_FACTORY_IMPL(internal, PA_KV_Reorder);
+
+} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/op/pa_kv_reorder.cpp b/src/plugins/intel_gpu/src/plugin/transformations/op/pa_kv_reorder.cpp
new file mode 100644
index 00000000000..48cedc2f5bd
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/transformations/op/pa_kv_reorder.cpp
@@ -0,0 +1,36 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "intel_gpu/op/pa_kv_reorder.hpp"
+
+namespace ov::intel_gpu::op {
+
+PA_KV_Reorder::PA_KV_Reorder(const Output<Node>& key_cache,
+ const Output<Node>& value_cache,
+ const Output<Node>& block_indices,
+ const Output<Node>& block_indices_begins,
+ const Output<Node>& block_update_indices,
+ const Output<Node>& block_update_indices_begins)
+ : Op({key_cache, value_cache, block_indices, block_indices_begins, block_update_indices, block_update_indices_begins}) {
+ set_output_size(1);
+ validate_and_infer_types();
+}
+
+bool PA_KV_Reorder::visit_attributes(ov::AttributeVisitor& visitor) {
+ (void)visitor;
+ return true;
+}
+
+void PA_KV_Reorder::validate_and_infer_types() {
+ NODE_VALIDATION_CHECK(this, get_input_size() == 6, "PaKVReorder expects 6 inputs, but got ", get_input_size());
+
+ set_output_type(0, ov::element::u8, ov::PartialShape{1});
+}
+
+std::shared_ptr<Node> PA_KV_Reorder::clone_with_new_inputs(const ov::OutputVector& new_args) const {
+ check_new_args_count(this, new_args);
+ return std::make_shared<PA_KV_Reorder>(new_args.at(0), new_args.at(1), new_args.at(2), new_args.at(3), new_args.at(4), new_args.at(5));
+}
+
+} // namespace ov::intel_gpu::op
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/pa_kv_reorder_fusion.cpp b/src/plugins/intel_gpu/src/plugin/transformations/pa_kv_reorder_fusion.cpp
new file mode 100644
index 00000000000..c2c687c0075
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/transformations/pa_kv_reorder_fusion.cpp
@@ -0,0 +1,283 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "pa_kv_reorder_fusion.hpp"
+
+#include <optional>
+#include <regex>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "intel_gpu/op/pa_kv_reorder.hpp"
+#include "openvino/core/graph_util.hpp"
+#include "openvino/core/rt_info.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/scatter_update.hpp"
+#include "transformations/utils/utils.hpp"
+
+namespace ov::intel_gpu {
+namespace {
+
+struct CacheReorderPath {
+ std::shared_ptr<ov::op::v3::ScatterUpdate> scatter;
+ std::shared_ptr<ov::op::v8::Gather> gather;
+ std::shared_ptr<ov::op::v0::Parameter> cache;
+ ov::Output<ov::Node> block_indices;
+ ov::Output<ov::Node> block_update_indices;
+ bool is_key = false;
+ std::string index;
+};
+
+bool same_output(const ov::Output<ov::Node>& lhs, const ov::Output<ov::Node>& rhs) {
+ return lhs.get_node() == rhs.get_node() && lhs.get_index() == rhs.get_index();
+}
+
+std::optional<int64_t> get_axis_value(const std::shared_ptr<ov::op::v0::Constant>& axis_const) {
+ if (!axis_const) {
+ return std::nullopt;
+ }
+
+ const auto axis_values = axis_const->cast_vector<int64_t>();
+ if (axis_values.size() != 1) {
+ return std::nullopt;
+ }
+
+ return axis_values[0];
+}
+
+std::optional<std::pair<bool, std::string>> parse_cache_role_and_index(const std::string& node_name) {
+ static const std::regex cache_param_regex(R"((key|value)_cache\.(\d+)_clone_for_[kv]_update)");
+ std::smatch cache_match;
+ if (std::regex_match(node_name, cache_match, cache_param_regex)) {
+ const bool is_key = cache_match[1].str() == "key";
+ return std::make_pair(is_key, cache_match[2].str());
+ }
+
+ static const std::regex scatter_name_regex(R"(updated_(key|value)_cache_(\d+))");
+ std::smatch scatter_match;
+ if (std::regex_match(node_name, scatter_match, scatter_name_regex)) {
+ const bool is_key = scatter_match[1].str() == "key";
+ return std::make_pair(is_key, scatter_match[2].str());
+ }
+
+ return std::nullopt;
+}
+
+std::optional<CacheReorderPath> parse_cache_reorder_path(const std::shared_ptr<ov::op::v3::ScatterUpdate>& scatter) {
+ if (!scatter || scatter->get_input_size() != 4) {
+ return std::nullopt;
+ }
+
+ auto gather = ov::as_type_ptr<ov::op::v8::Gather>(scatter->input_value(2).get_node_shared_ptr());
+ if (!gather || gather->get_input_size() != 3) {
+ return std::nullopt;
+ }
+
+ auto cache = ov::as_type_ptr<ov::op::v0::Parameter>(scatter->input_value(0).get_node_shared_ptr());
+ if (!cache) {
+ return std::nullopt;
+ }
+
+ if (!same_output(gather->input_value(0), scatter->input_value(0))) {
+ return std::nullopt;
+ }
+
+ auto gather_axis_const = ov::as_type_ptr<ov::op::v0::Constant>(gather->input_value(2).get_node_shared_ptr());
+ auto scatter_axis_const = ov::as_type_ptr<ov::op::v0::Constant>(scatter->input_value(3).get_node_shared_ptr());
+
+ const auto gather_axis = get_axis_value(gather_axis_const);
+ const auto scatter_axis = get_axis_value(scatter_axis_const);
+ if (!gather_axis.has_value() || !scatter_axis.has_value() || gather_axis.value() != scatter_axis.value()) {
+ return std::nullopt;
+ }
+
+ auto role_and_index = parse_cache_role_and_index(cache->get_friendly_name());
+ if (!role_and_index.has_value()) {
+ role_and_index = parse_cache_role_and_index(scatter->get_friendly_name());
+ }
+
+ if (!role_and_index.has_value()) {
+ return std::nullopt;
+ }
+
+ CacheReorderPath path;
+ path.scatter = scatter;
+ path.gather = gather;
+ path.cache = cache;
+ path.block_indices = scatter->input_value(1);
+ path.block_update_indices = gather->input_value(1);
+ path.is_key = role_and_index->first;
+ path.index = role_and_index->second;
+ return path;
+}
+
+std::shared_ptr<ov::op::v0::Parameter> get_parameter_by_name(const std::shared_ptr<ov::Model>& m, const std::string& parameter_name) {
+ for (const auto& parameter : m->get_parameters()) {
+ if (parameter && parameter->get_friendly_name() == parameter_name) {
+ return parameter;
+ }
+ }
+
+ return nullptr;
+}
+
+ov::element::Type format_cache_precision(const ov::element::Type& cache_precision, const ov::element::Type& infer_precision) {
+ return cache_precision == ov::element::f16 && infer_precision == ov::element::bf16 ? infer_precision : cache_precision;
+}
+
+bool is_compressed_cache_type(const ov::element::Type& type) {
+ return type == ov::element::i8 || type == ov::element::u8 || type == ov::element::i4 || type == ov::element::u4;
+}
+
+size_t infer_scales_zp_size(const ov::element::Type& cache_type, const ov::element::Type& infer_precision) {
+ if (!is_compressed_cache_type(cache_type)) {
+ return 0;
+ }
+
+ OPENVINO_ASSERT(cache_type.size() > 0 && infer_precision.size() > 0, "[GPU] Invalid element size for pa_kv_reorder metadata inference");
+ if (cache_type == ov::element::i4 || cache_type == ov::element::u4) {
+ return 4 * infer_precision.size();
+ }
+ return (2 * infer_precision.size()) / cache_type.size();
+}
+
+std::vector<std::shared_ptr<ov::op::v0::Concat>> get_joint_concat_consumers(const ov::Output<ov::Node>& key_scatter_output,
+ const ov::Output<ov::Node>& value_scatter_output) {
+ std::vector<std::shared_ptr<ov::op::v0::Concat>> concats;
+ for (const auto& target_input : key_scatter_output.get_target_inputs()) {
+ auto concat = ov::as_type_ptr<ov::op::v0::Concat>(target_input.get_node()->shared_from_this());
+ if (!concat) {
+ continue;
+ }
+
+ bool has_key_input = false;
+ bool has_value_input = false;
+ for (const auto& input : concat->input_values()) {
+ has_key_input = has_key_input || same_output(input, key_scatter_output);
+ has_value_input = has_value_input || same_output(input, value_scatter_output);
+ }
+
+ if (!has_key_input || !has_value_input) {
+ continue;
+ }
+
+ bool has_result_consumer = false;
+ for (const auto& concat_target : concat->output(0).get_target_inputs()) {
+ if (ov::as_type_ptr<ov::op::v0::Result>(concat_target.get_node()->shared_from_this())) {
+ has_result_consumer = true;
+ break;
+ }
+ }
+
+ if (has_result_consumer) {
+ concats.push_back(concat);
+ }
+ }
+
+ return concats;
+}
+
+} // namespace
+
+bool PaKVReorderFusion::run_on_model(const std::shared_ptr<ov::Model>& m) {
+ auto block_indices_begins = get_parameter_by_name(m, "block_indices_begins");
+ auto block_update_indices_begins = get_parameter_by_name(m, "block_update_indices_begins");
+ if (!block_indices_begins || !block_update_indices_begins) {
+ return false;
+ }
+
+ std::unordered_map<std::string, CacheReorderPath> key_paths;
+ std::unordered_map<std::string, CacheReorderPath> value_paths;
+
+ for (const auto& node : m->get_ordered_ops()) {
+ auto scatter = ov::as_type_ptr<ov::op::v3::ScatterUpdate>(node);
+ auto path = parse_cache_reorder_path(scatter);
+ if (!path.has_value()) {
+ continue;
+ }
+
+ if (path->is_key) {
+ key_paths[path->index] = std::move(path.value());
+ } else {
+ value_paths[path->index] = std::move(path.value());
+ }
+ }
+
+ bool rewritten = false;
+
+ for (const auto& [index, key_path] : key_paths) {
+ if (value_paths.count(index) == 0) {
+ continue;
+ }
+
+ const auto& value_path = value_paths.at(index);
+ if (!same_output(key_path.block_indices, value_path.block_indices) || !same_output(key_path.block_update_indices, value_path.block_update_indices)) {
+ continue;
+ }
+
+ const auto concat_consumers = get_joint_concat_consumers(key_path.scatter->output(0), value_path.scatter->output(0));
+ if (concat_consumers.empty()) {
+ continue;
+ }
+
+ auto pa_kv_reorder = std::make_shared<ov::intel_gpu::op::PA_KV_Reorder>(key_path.cache->output(0),
+ value_path.cache->output(0),
+ key_path.block_indices,
+ block_indices_begins->output(0),
+ key_path.block_update_indices,
+ block_update_indices_begins->output(0));
+
+ constexpr const char* rt_is_key_by_channel = "pa_kv_reorder.is_key_by_channel";
+ constexpr const char* rt_scales_zp_size = "pa_kv_reorder.scales_zp_size";
+ constexpr const char* rt_key_dim_order = "pa_kv_reorder.key_cache_dim_order";
+ constexpr const char* rt_value_dim_order = "pa_kv_reorder.value_cache_dim_order";
+
+ const auto key_cache_type = format_cache_precision(m_key_cache_precision, m_inference_precision);
+ const auto value_cache_type = format_cache_precision(m_value_cache_precision, m_inference_precision);
+ const auto key_scales_zp_size = infer_scales_zp_size(key_cache_type, m_inference_precision);
+ const auto value_scales_zp_size = infer_scales_zp_size(value_cache_type, m_inference_precision);
+ OPENVINO_ASSERT(key_scales_zp_size == value_scales_zp_size,
+ "[GPU] PaKVReorderFusion expects equal scales/zp size for key/value cache, but got ",
+ key_scales_zp_size,
+ " and ",
+ value_scales_zp_size);
+ // update key/value type
+ key_path.cache->set_element_type(key_cache_type);
+ value_path.cache->set_element_type(value_cache_type);
+ const bool is_key_by_channel = m_key_cache_quant_by_channel;
+
+ pa_kv_reorder->get_rt_info()[rt_is_key_by_channel] = is_key_by_channel;
+ pa_kv_reorder->get_rt_info()[rt_scales_zp_size] = key_scales_zp_size;
+ pa_kv_reorder->get_rt_info()[rt_key_dim_order] = m_key_cache_dim_order;
+ pa_kv_reorder->get_rt_info()[rt_value_dim_order] = m_value_cache_dim_order;
+
+ ov::copy_runtime_info(key_path.cache, pa_kv_reorder);
+ pa_kv_reorder->set_friendly_name("pa_kv_reorder_" + index);
+ ov::NodeVector runtime_info_nodes{key_path.scatter, key_path.gather, value_path.scatter, value_path.gather};
+ for (const auto& concat : concat_consumers) {
+ runtime_info_nodes.push_back(concat);
+ }
+ ov::copy_runtime_info(runtime_info_nodes, pa_kv_reorder);
+
+ pa_kv_reorder->output(0).get_tensor().set_names(concat_consumers.front()->output(0).get_tensor().get_names());
+
+ for (const auto& concat : concat_consumers) {
+ for (const auto& target_input : concat->output(0).get_target_inputs()) {
+ if (auto result = ov::as_type_ptr<ov::op::v0::Result>(target_input.get_node()->shared_from_this())) {
+ result->set_argument(0, pa_kv_reorder->output(0));
+ }
+ }
+ ov::replace_output_update_name(concat->output(0), pa_kv_reorder->output(0));
+ }
+ rewritten = true;
+ }
+
+ return rewritten;
+}
+
+} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/pa_kv_reorder_fusion.hpp b/src/plugins/intel_gpu/src/plugin/transformations/pa_kv_reorder_fusion.hpp
new file mode 100644
index 00000000000..d7f3a562aeb
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/transformations/pa_kv_reorder_fusion.hpp
@@ -0,0 +1,82 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <vector>
+
+#include "openvino/core/type/element_type.hpp"
+#include "openvino/pass/pass.hpp"
+
+namespace ov::intel_gpu {
+
+/// Fuses paired key/value cache reorder paths into a single PA_KV_Reorder op.
+///
+/// ┌───────────────┐ ┌───────────────┐
+/// │ Parameter │ │ Parameter │
+/// │ key_cache │ │ value_cache │
+/// └───────┬───────┘ └───────┬───────┘
+/// │ │
+/// ┌─────┴─────┐ ┌─────┴─────┐
+/// │ Gather │<────────── block_update_indices ────────│ Gather │
+/// └─────┬─────┘ └─────┬─────┘
+/// │ │
+/// ┌───────┴────────┐ ┌───────┴────────┐
+/// │ ScatterUpdate │<──────────── block_indices ────────│ ScatterUpdate │
+/// └───────┬────────┘ └───────┬────────┘
+/// │ │
+/// └───────────────────┐ ┌───────────────────────┘
+/// │ │
+/// ┌──┴─────────┴───┐
+/// │ Concat │
+/// └──────┬─────────┘
+/// │
+/// Result
+///
+/// =>
+///
+/// ┌───────────────┐ ┌───────────────┐
+/// │ Parameter │ │ Parameter │
+/// │ key_cache │ │ value_cache │
+/// └───────┬───────┘ └───────┬───────┘
+/// │ │
+/// └───────────────►┌────────────────────────┐◄────┘
+/// ┌───────────────┐ │ PA_KV_Reorder │ ┌─────────────────────┐
+/// │ Parameter │────────►│ │◄───────│ Parameter │
+/// │ block_indices │ │ │ │ block_update_indices│
+/// └───────────────┘ │ │ └─────────────────────┘
+/// ┌────────────────────┐ │ │ ┌────────────────────────────┐
+/// │ Parameter │──────►│ │◄──────│ Parameter │
+/// │ block_indices_begins│ └────────────┬───────────┘ │ block_update_indices_begins│
+/// └────────────────────┘ │ └────────────────────────────┘
+/// │
+/// Result
+
+class PaKVReorderFusion : public ov::pass::ModelPass {
+public:
+ OPENVINO_MODEL_PASS_RTTI("PaKVReorderFusion");
+ explicit PaKVReorderFusion(bool key_cache_quant_by_channel = false,
+ std::vector<size_t> key_cache_dim_order = {0, 1, 3, 2},
+ std::vector<size_t> value_cache_dim_order = {0, 1, 2, 3},
+ ov::element::Type key_cache_precision = ov::element::dynamic,
+ ov::element::Type value_cache_precision = ov::element::dynamic,
+ ov::element::Type inference_precision = ov::element::f16)
+ : m_key_cache_quant_by_channel(key_cache_quant_by_channel),
+ m_key_cache_dim_order(std::move(key_cache_dim_order)),
+ m_value_cache_dim_order(std::move(value_cache_dim_order)),
+ m_key_cache_precision(key_cache_precision),
+ m_value_cache_precision(value_cache_precision),
+ m_inference_precision(inference_precision) {}
+ bool run_on_model(const std::shared_ptr<ov::Model>& m) override;
+
+private:
+ bool m_key_cache_quant_by_channel = false;
+ std::vector<size_t> m_key_cache_dim_order = {0, 1, 3, 2};
+ std::vector<size_t> m_value_cache_dim_order = {0, 1, 2, 3};
+ ov::element::Type m_key_cache_precision = ov::element::dynamic;
+ ov::element::Type m_value_cache_precision = ov::element::dynamic;
+ ov::element::Type m_inference_precision = ov::element::f16;
+};
+
+} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index c247c52f01f..58eec00a30f 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -97,6 +97,7 @@
#include "plugin/transformations/lora_subgraph_horizontal_fusion.hpp"
#include "plugin/transformations/move_fc_reshape_to_weights.hpp"
#include "plugin/transformations/optimize_subsequent_reshapes.hpp"
+#include "plugin/transformations/pa_kv_reorder_fusion.hpp"
#include "plugin/transformations/print_model_statistics.hpp"
#include "plugin/transformations/sink_reshape.hpp"
#include "plugin/transformations/transpose_fusion.hpp"
@@ -719,6 +720,12 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
precision = (config.get_kv_cache_precision() == ov::element::i4) ? ov::element::i8 : ov::element::u8;
}
});
+ manager.register_pass<ov::intel_gpu::PaKVReorderFusion>(kv_cache_config.keyCacheQuantBychannel,
+ kv_cache_config.keyCacheDimOrder,
+ kv_cache_config.valueCacheDimOrder,
+ kv_cache_config.keyCachePrecision,
+ kv_cache_config.valueCachePrecision,
+ kv_cache_config.inferencePrecision);
}
pass_config->set_callback<ov::pass::ScaledDotProductAttentionDecomposition>([&](const std::shared_ptr<const ov::Node> node){
diff --git a/src/plugins/intel_gpu/src/runtime/execution_config.cpp b/src/plugins/intel_gpu/src/runtime/execution_config.cpp
index b58acae2a70..54d71cf484d 100644
--- a/src/plugins/intel_gpu/src/runtime/execution_config.cpp
+++ b/src/plugins/intel_gpu/src/runtime/execution_config.cpp
@@ -241,9 +241,14 @@ void ExecutionConfig::apply_model_specific_options(const IRemoteContext* context
}
const auto& info = dynamic_cast<const RemoteContextImpl*>(context)->get_engine().get_device_info();
-
+ auto is_auxiliary_kv_update_model = [](const ov::Model& model) {
+ if (model.get_rt_info().count("auxiliary_kv_update_model")) {
+ return model.get_rt_info<ov::Any>("auxiliary_kv_update_model").template as<bool>();
+ }
+ return false;
+ };
if (!is_set_by_user(ov::hint::kv_cache_precision) || get_kv_cache_precision() == ov::element::dynamic) {
- if (is_paged_attention_model || !info.supports_immad) {
+ if (is_paged_attention_model || !info.supports_immad || is_auxiliary_kv_update_model(model)) {
// Enable KV-cache compression by default for:
// 1) Non-systolic platforms in case of SDPA-based models
// 2) For any platforms in case of PagedAttention-based model
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/pa_kv_reorder_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/pa_kv_reorder_gpu_test.cpp
new file mode 100644
index 00000000000..c487d146b2e
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/pa_kv_reorder_gpu_test.cpp
@@ -0,0 +1,1091 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "test_utils.h"
+
+#include <intel_gpu/primitives/input_layout.hpp>
+#include <intel_gpu/primitives/mutable_data.hpp>
+#include <intel_gpu/primitives/pa_kv_reorder.hpp>
+#include <intel_gpu/primitives/paged_attention.hpp>
+
+#include <cstring>
+#include <type_traits>
+#include <tuple>
+#include <vector>
+
+using namespace cldnn;
+using namespace ::tests;
+
+namespace {
+
+size_t key_offset(size_t block, size_t head, size_t k, size_t token, size_t kv_heads, size_t k_head_size, size_t block_size) {
+ return block * kv_heads * k_head_size * block_size +
+ head * k_head_size * block_size +
+ k * block_size +
+ token;
+}
+
+size_t value_offset(size_t block, size_t head, size_t token, size_t v, size_t kv_heads, size_t v_head_size, size_t block_size) {
+ return block * kv_heads * block_size * v_head_size +
+ head * block_size * v_head_size +
+ token * v_head_size +
+ v;
+}
+
+size_t key_comp_byte_offset(size_t block,
+ size_t head,
+ size_t token,
+ size_t byte_in_fp16,
+ bool is_zp,
+ size_t kv_heads,
+ size_t k_head_size,
+ size_t adjusted_k_head_size,
+ size_t block_size) {
+ const size_t block_base = block * kv_heads * adjusted_k_head_size * block_size +
+ head * adjusted_k_head_size * block_size;
+ const size_t comp_base = block_base + k_head_size * block_size;
+ const size_t token_base = (is_zp ? (block_size + token) : token) * sizeof(ov::float16);
+ return comp_base + token_base + byte_in_fp16;
+}
+
+size_t value_comp_byte_offset(size_t block,
+ size_t head,
+ size_t token,
+ size_t byte_in_fp16,
+ bool is_zp,
+ size_t kv_heads,
+ size_t v_head_size,
+ size_t adjusted_v_head_size,
+ size_t block_size) {
+ const size_t block_base = block * kv_heads * block_size * adjusted_v_head_size +
+ head * block_size * adjusted_v_head_size;
+ const size_t comp_base = block_base + v_head_size * block_size;
+ const size_t token_base = (is_zp ? (block_size + token) : token) * sizeof(ov::float16);
+ return comp_base + token_base + byte_in_fp16;
+}
+
+size_t value_data_offset_compressed(size_t block,
+ size_t head,
+ size_t token,
+ size_t v,
+ size_t kv_heads,
+ size_t v_head_size,
+ size_t adjusted_v_head_size,
+ size_t block_size) {
+ const size_t block_base = block * kv_heads * block_size * adjusted_v_head_size +
+ head * block_size * adjusted_v_head_size;
+ return block_base + token * v_head_size + v;
+}
+
+void run_copy_between_blocks_single_sequence_compressed_int4_test(data_types cache_dt) {
+ auto& engine = get_test_engine();
+
+ constexpr size_t blocks_num = 2;
+ constexpr size_t kv_heads = 1;
+ constexpr size_t k_head_size = 32;
+ constexpr size_t v_head_size = 32;
+ constexpr size_t packed_k_head_size = k_head_size / 2;
+ constexpr size_t packed_v_head_size = v_head_size / 2;
+ constexpr size_t scales_zp_size = sizeof(ov::float16) * 4;
+ constexpr size_t adjusted_k_head_size = packed_k_head_size + scales_zp_size;
+ constexpr size_t adjusted_v_head_size = packed_v_head_size + scales_zp_size;
+ constexpr size_t block_size = cldnn::paged_attention::block_size;
+
+ auto key_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, adjusted_k_head_size, block_size}, data_types::u8, format::bfyx};
+ auto value_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, block_size, adjusted_v_head_size}, data_types::u8, format::bfyx};
+ auto block_indices_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_update_indices_layout = layout{ov::PartialShape{4}, data_types::i32, format::bfyx};
+ auto block_update_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+
+ auto key_cache_mem = engine.allocate_memory(key_cache_layout);
+ auto value_cache_mem = engine.allocate_memory(value_cache_layout);
+ auto block_indices_mem = engine.allocate_memory(block_indices_layout);
+ auto block_indices_begins_mem = engine.allocate_memory(block_indices_begins_layout);
+ auto block_update_indices_mem = engine.allocate_memory(block_update_indices_layout);
+ auto block_update_indices_begins_mem = engine.allocate_memory(block_update_indices_begins_layout);
+
+ std::vector<uint8_t> key_cache_ref(key_cache_mem->count());
+ std::vector<uint8_t> value_cache_ref(value_cache_mem->count());
+ for (size_t i = 0; i < key_cache_ref.size(); i++) {
+ key_cache_ref[i] = static_cast<uint8_t>(i % 251);
+ }
+ for (size_t i = 0; i < value_cache_ref.size(); i++) {
+ value_cache_ref[i] = static_cast<uint8_t>((3 * i + 17) % 251);
+ }
+ set_values<uint8_t>(key_cache_mem, key_cache_ref);
+ set_values<uint8_t>(value_cache_mem, value_cache_ref);
+
+ set_values<int32_t>(block_indices_mem, {0, 1});
+ set_values<int32_t>(block_indices_begins_mem, {0, 2});
+ set_values<int32_t>(block_update_indices_mem, {
+ 0, 17,
+ 15, 16,
+ });
+ set_values<int32_t>(block_update_indices_begins_mem, {0, 2});
+
+ topology topo;
+ topo.add(mutable_data("key_cache", key_cache_mem));
+ topo.add(mutable_data("value_cache", value_cache_mem));
+ topo.add(input_layout("block_indices", block_indices_layout));
+ topo.add(input_layout("block_indices_begins", block_indices_begins_layout));
+ topo.add(input_layout("block_update_indices", block_update_indices_layout));
+ topo.add(input_layout("block_update_indices_begins", block_update_indices_begins_layout));
+
+ auto pa_reorder = pa_kv_reorder("pa_kv_reorder",
+ {input_info("key_cache"),
+ input_info("value_cache"),
+ input_info("block_indices"),
+ input_info("block_indices_begins"),
+ input_info("block_update_indices"),
+ input_info("block_update_indices_begins")});
+ pa_reorder.kv_heads_num = kv_heads;
+ pa_reorder.adjusted_k_head_size = adjusted_k_head_size;
+ pa_reorder.adjusted_paged_attention_block_size = block_size;
+ pa_reorder.adjusted_v_head_size = adjusted_v_head_size;
+ pa_reorder.cache_dt = cache_dt;
+ pa_reorder.is_kv_compressed = true;
+ pa_reorder.scales_zp_size = scales_zp_size;
+ topo.add(pa_reorder);
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ auto network = get_network(engine, topo, config, get_test_stream_ptr(), false);
+
+ network->set_input_data("block_indices", block_indices_mem);
+ network->set_input_data("block_indices_begins", block_indices_begins_mem);
+ network->set_input_data("block_update_indices", block_update_indices_mem);
+ network->set_input_data("block_update_indices_begins", block_update_indices_begins_mem);
+ network->execute();
+ network->get_stream().finish();
+
+ cldnn::mem_lock<uint8_t, mem_lock_type::read> key_ptr(key_cache_mem, network->get_stream());
+ cldnn::mem_lock<uint8_t, mem_lock_type::read> value_ptr(value_cache_mem, network->get_stream());
+
+ for (size_t k = 0; k < packed_k_head_size; k++) {
+ const auto src0 = key_cache_ref[key_offset(0, 0, k, 0, kv_heads, adjusted_k_head_size, block_size)];
+ const auto dst17 = key_ptr[key_offset(1, 0, k, 1, kv_heads, adjusted_k_head_size, block_size)];
+ ASSERT_EQ(dst17, src0);
+
+ const auto src15 = key_cache_ref[key_offset(0, 0, k, 15, kv_heads, adjusted_k_head_size, block_size)];
+ const auto dst16 = key_ptr[key_offset(1, 0, k, 0, kv_heads, adjusted_k_head_size, block_size)];
+ ASSERT_EQ(dst16, src15);
+ }
+
+ for (size_t v = 0; v < packed_v_head_size; v++) {
+ const auto src0 = value_cache_ref[value_data_offset_compressed(0, 0, 0, v, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)];
+ const auto dst17 = value_ptr[value_data_offset_compressed(1, 0, 1, v, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)];
+ ASSERT_EQ(dst17, src0);
+
+ const auto src15 = value_cache_ref[value_data_offset_compressed(0, 0, 15, v, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)];
+ const auto dst16 = value_ptr[value_data_offset_compressed(1, 0, 0, v, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)];
+ ASSERT_EQ(dst16, src15);
+ }
+
+ for (size_t byte = 0; byte < sizeof(ov::float16); byte++) {
+ ASSERT_EQ(key_ptr[key_comp_byte_offset(1, 0, 1, byte, false, kv_heads, packed_k_head_size, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_comp_byte_offset(0, 0, 0, byte, false, kv_heads, packed_k_head_size, adjusted_k_head_size, block_size)]);
+ ASSERT_EQ(key_ptr[key_comp_byte_offset(1, 0, 1, byte, true, kv_heads, packed_k_head_size, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_comp_byte_offset(0, 0, 0, byte, true, kv_heads, packed_k_head_size, adjusted_k_head_size, block_size)]);
+
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(1, 0, 1, byte, false, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 0, byte, false, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(1, 0, 1, byte, true, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 0, byte, true, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)]);
+ }
+}
+
+template <typename ByteT>
+ov::float16 read_fp16_from_byte_buffer(const cldnn::mem_lock<ByteT, mem_lock_type::read>& ptr, size_t byte_offset) {
+ static_assert(std::is_same_v<ByteT, int8_t> || std::is_same_v<ByteT, uint8_t>, "ByteT must be int8_t or uint8_t");
+ const auto lo = static_cast<uint8_t>(ptr[byte_offset]);
+ const auto hi = static_cast<uint8_t>(ptr[byte_offset + 1]);
+ const uint16_t bits = static_cast<uint16_t>(lo) | (static_cast<uint16_t>(hi) << 8);
+ return ov::float16::from_bits(bits);
+}
+
+ov::float16 read_fp16_from_u8_vector(const std::vector<uint8_t>& buffer, size_t byte_offset) {
+ const auto lo = buffer[byte_offset];
+ const auto hi = buffer[byte_offset + 1];
+ const uint16_t bits = static_cast<uint16_t>(lo) | (static_cast<uint16_t>(hi) << 8);
+ return ov::float16::from_bits(bits);
+}
+
+void fill_key_cache(memory::ptr key_cache_mem,
+ size_t blocks_num,
+ size_t kv_heads,
+ size_t k_head_size,
+ size_t block_size,
+ std::vector<ov::float16>& values) {
+ values.resize(key_cache_mem->count());
+ for (size_t b = 0; b < blocks_num; b++) {
+ for (size_t h = 0; h < kv_heads; h++) {
+ for (size_t k = 0; k < k_head_size; k++) {
+ for (size_t t = 0; t < block_size; t++) {
+ const size_t off = key_offset(b, h, k, t, kv_heads, k_head_size, block_size);
+ values[off] = ov::float16(static_cast<float>(1000 * b + 100 * h + 10 * k + t));
+ }
+ }
+ }
+ }
+ set_values(key_cache_mem, values);
+}
+
+void fill_value_cache(memory::ptr value_cache_mem,
+ size_t blocks_num,
+ size_t kv_heads,
+ size_t v_head_size,
+ size_t block_size,
+ std::vector<ov::float16>& values) {
+ values.resize(value_cache_mem->count());
+ for (size_t b = 0; b < blocks_num; b++) {
+ for (size_t h = 0; h < kv_heads; h++) {
+ for (size_t t = 0; t < block_size; t++) {
+ for (size_t v = 0; v < v_head_size; v++) {
+ const size_t off = value_offset(b, h, t, v, kv_heads, v_head_size, block_size);
+ values[off] = ov::float16(static_cast<float>(1000 * b + 100 * h + 10 * t + v));
+ }
+ }
+ }
+ }
+ set_values(value_cache_mem, values);
+}
+
+} // namespace
+
+TEST(pa_kv_reorder_gpu, copy_between_blocks_single_sequence) {
+ auto& engine = get_test_engine();
+
+ constexpr size_t blocks_num = 2;
+ constexpr size_t kv_heads = 1;
+ constexpr size_t k_head_size = 4;
+ constexpr size_t v_head_size = 3;
+ constexpr size_t block_size = cldnn::paged_attention::block_size;
+
+ auto key_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, k_head_size, block_size}, data_types::f16, format::bfyx};
+ auto value_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, block_size, v_head_size}, data_types::f16, format::bfyx};
+ auto block_indices_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_update_indices_layout = layout{ov::PartialShape{4}, data_types::i32, format::bfyx};
+ auto block_update_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+
+ auto key_cache_mem = engine.allocate_memory(key_cache_layout);
+ auto value_cache_mem = engine.allocate_memory(value_cache_layout);
+ auto block_indices_mem = engine.allocate_memory(block_indices_layout);
+ auto block_indices_begins_mem = engine.allocate_memory(block_indices_begins_layout);
+ auto block_update_indices_mem = engine.allocate_memory(block_update_indices_layout);
+ auto block_update_indices_begins_mem = engine.allocate_memory(block_update_indices_begins_layout);
+
+ std::vector<ov::float16> key_cache_ref;
+ std::vector<ov::float16> value_cache_ref;
+ fill_key_cache(key_cache_mem, blocks_num, kv_heads, k_head_size, block_size, key_cache_ref);
+ fill_value_cache(value_cache_mem, blocks_num, kv_heads, v_head_size, block_size, value_cache_ref);
+
+ set_values<int32_t>(block_indices_mem, {0, 1});
+ set_values<int32_t>(block_indices_begins_mem, {0, 2});
+ set_values<int32_t>(block_update_indices_mem, {
+ 0, 17,
+ 15, 16,
+ });
+ set_values<int32_t>(block_update_indices_begins_mem, {0, 2});
+
+ topology topo;
+ topo.add(mutable_data("key_cache", key_cache_mem));
+ topo.add(mutable_data("value_cache", value_cache_mem));
+ topo.add(input_layout("block_indices", block_indices_layout));
+ topo.add(input_layout("block_indices_begins", block_indices_begins_layout));
+ topo.add(input_layout("block_update_indices", block_update_indices_layout));
+ topo.add(input_layout("block_update_indices_begins", block_update_indices_begins_layout));
+
+ auto pa_reorder = pa_kv_reorder("pa_kv_reorder",
+ {input_info("key_cache"),
+ input_info("value_cache"),
+ input_info("block_indices"),
+ input_info("block_indices_begins"),
+ input_info("block_update_indices"),
+ input_info("block_update_indices_begins")});
+ pa_reorder.kv_heads_num = kv_heads;
+ pa_reorder.adjusted_k_head_size = k_head_size;
+ pa_reorder.adjusted_paged_attention_block_size = block_size;
+ pa_reorder.adjusted_v_head_size = v_head_size;
+ pa_reorder.cache_dt = data_types::f16;
+ pa_reorder.is_kv_compressed = false;
+ topo.add(pa_reorder);
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ auto network = get_network(engine, topo, config, get_test_stream_ptr(), false);
+
+ network->set_input_data("block_indices", block_indices_mem);
+ network->set_input_data("block_indices_begins", block_indices_begins_mem);
+ network->set_input_data("block_update_indices", block_update_indices_mem);
+ network->set_input_data("block_update_indices_begins", block_update_indices_begins_mem);
+ network->execute();
+ network->get_stream().finish();
+
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> key_ptr(key_cache_mem, network->get_stream());
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> value_ptr(value_cache_mem, network->get_stream());
+
+ for (size_t k = 0; k < k_head_size; k++) {
+ const auto src0 = key_cache_ref[key_offset(0, 0, k, 0, kv_heads, k_head_size, block_size)];
+ const auto dst17 = key_ptr[key_offset(1, 0, k, 1, kv_heads, k_head_size, block_size)];
+ ASSERT_EQ(dst17, src0);
+
+ const auto src15 = key_cache_ref[key_offset(0, 0, k, 15, kv_heads, k_head_size, block_size)];
+ const auto dst16 = key_ptr[key_offset(1, 0, k, 0, kv_heads, k_head_size, block_size)];
+ ASSERT_EQ(dst16, src15);
+ }
+
+ for (size_t v = 0; v < v_head_size; v++) {
+ const auto src0 = value_cache_ref[value_offset(0, 0, 0, v, kv_heads, v_head_size, block_size)];
+ const auto dst17 = value_ptr[value_offset(1, 0, 1, v, kv_heads, v_head_size, block_size)];
+ ASSERT_EQ(dst17, src0);
+
+ const auto src15 = value_cache_ref[value_offset(0, 0, 15, v, kv_heads, v_head_size, block_size)];
+ const auto dst16 = value_ptr[value_offset(1, 0, 0, v, kv_heads, v_head_size, block_size)];
+ ASSERT_EQ(dst16, src15);
+ }
+
+ ASSERT_EQ(key_ptr[key_offset(0, 0, 0, 0, kv_heads, k_head_size, block_size)],
+ key_cache_ref[key_offset(0, 0, 0, 0, kv_heads, k_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_offset(0, 0, 0, 0, kv_heads, v_head_size, block_size)],
+ value_cache_ref[value_offset(0, 0, 0, 0, kv_heads, v_head_size, block_size)]);
+}
+
+TEST(pa_kv_reorder_gpu, updates_are_scoped_per_sequence) {
+ auto& engine = get_test_engine();
+
+ constexpr size_t blocks_num = 3;
+ constexpr size_t kv_heads = 1;
+ constexpr size_t k_head_size = 2;
+ constexpr size_t v_head_size = 2;
+ constexpr size_t block_size = cldnn::paged_attention::block_size;
+
+ auto key_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, k_head_size, block_size}, data_types::f16, format::bfyx};
+ auto value_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, block_size, v_head_size}, data_types::f16, format::bfyx};
+ auto block_indices_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_indices_begins_layout = layout{ov::PartialShape{3}, data_types::i32, format::bfyx};
+ auto block_update_indices_layout = layout{ov::PartialShape{4}, data_types::i32, format::bfyx};
+ auto block_update_indices_begins_layout = layout{ov::PartialShape{3}, data_types::i32, format::bfyx};
+
+ auto key_cache_mem = engine.allocate_memory(key_cache_layout);
+ auto value_cache_mem = engine.allocate_memory(value_cache_layout);
+ auto block_indices_mem = engine.allocate_memory(block_indices_layout);
+ auto block_indices_begins_mem = engine.allocate_memory(block_indices_begins_layout);
+ auto block_update_indices_mem = engine.allocate_memory(block_update_indices_layout);
+ auto block_update_indices_begins_mem = engine.allocate_memory(block_update_indices_begins_layout);
+
+ std::vector<ov::float16> key_cache_ref;
+ std::vector<ov::float16> value_cache_ref;
+ fill_key_cache(key_cache_mem, blocks_num, kv_heads, k_head_size, block_size, key_cache_ref);
+ fill_value_cache(value_cache_mem, blocks_num, kv_heads, v_head_size, block_size, value_cache_ref);
+
+ // Sequence 0 uses physical block 0, sequence 1 uses physical block 2.
+ set_values<int32_t>(block_indices_mem, {0, 2});
+ set_values<int32_t>(block_indices_begins_mem, {0, 1, 2});
+ set_values<int32_t>(block_update_indices_mem, {
+ 1, 3, // seq0: slot1 -> slot3 in block0
+ 2, 4, // seq1: slot2 -> slot4 in block2
+ });
+ set_values<int32_t>(block_update_indices_begins_mem, {0, 1, 2});
+
+ topology topo;
+ topo.add(mutable_data("key_cache", key_cache_mem));
+ topo.add(mutable_data("value_cache", value_cache_mem));
+ topo.add(input_layout("block_indices", block_indices_layout));
+ topo.add(input_layout("block_indices_begins", block_indices_begins_layout));
+ topo.add(input_layout("block_update_indices", block_update_indices_layout));
+ topo.add(input_layout("block_update_indices_begins", block_update_indices_begins_layout));
+
+ auto pa_reorder = pa_kv_reorder("pa_kv_reorder",
+ {input_info("key_cache"),
+ input_info("value_cache"),
+ input_info("block_indices"),
+ input_info("block_indices_begins"),
+ input_info("block_update_indices"),
+ input_info("block_update_indices_begins")});
+ pa_reorder.kv_heads_num = kv_heads;
+ pa_reorder.adjusted_k_head_size = k_head_size;
+ pa_reorder.adjusted_paged_attention_block_size = block_size;
+ pa_reorder.adjusted_v_head_size = v_head_size;
+ pa_reorder.cache_dt = data_types::f16;
+ pa_reorder.is_kv_compressed = false;
+ topo.add(pa_reorder);
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ auto network = get_network(engine, topo, config, get_test_stream_ptr(), false);
+
+ network->set_input_data("block_indices", block_indices_mem);
+ network->set_input_data("block_indices_begins", block_indices_begins_mem);
+ network->set_input_data("block_update_indices", block_update_indices_mem);
+ network->set_input_data("block_update_indices_begins", block_update_indices_begins_mem);
+ network->execute();
+ network->get_stream().finish();
+
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> key_ptr(key_cache_mem, network->get_stream());
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> value_ptr(value_cache_mem, network->get_stream());
+
+ for (size_t k = 0; k < k_head_size; k++) {
+ ASSERT_EQ(key_ptr[key_offset(0, 0, k, 3, kv_heads, k_head_size, block_size)],
+ key_cache_ref[key_offset(0, 0, k, 1, kv_heads, k_head_size, block_size)]);
+ ASSERT_EQ(key_ptr[key_offset(2, 0, k, 4, kv_heads, k_head_size, block_size)],
+ key_cache_ref[key_offset(2, 0, k, 2, kv_heads, k_head_size, block_size)]);
+
+ // Unused middle block must stay untouched.
+ ASSERT_EQ(key_ptr[key_offset(1, 0, k, 4, kv_heads, k_head_size, block_size)],
+ key_cache_ref[key_offset(1, 0, k, 4, kv_heads, k_head_size, block_size)]);
+ }
+
+ for (size_t v = 0; v < v_head_size; v++) {
+ ASSERT_EQ(value_ptr[value_offset(0, 0, 3, v, kv_heads, v_head_size, block_size)],
+ value_cache_ref[value_offset(0, 0, 1, v, kv_heads, v_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_offset(2, 0, 4, v, kv_heads, v_head_size, block_size)],
+ value_cache_ref[value_offset(2, 0, 2, v, kv_heads, v_head_size, block_size)]);
+
+ ASSERT_EQ(value_ptr[value_offset(1, 0, 4, v, kv_heads, v_head_size, block_size)],
+ value_cache_ref[value_offset(1, 0, 4, v, kv_heads, v_head_size, block_size)]);
+ }
+}
+
+TEST(pa_kv_reorder_gpu, copy_between_blocks_single_sequence_compressed) {
+ auto& engine = get_test_engine();
+
+ constexpr size_t blocks_num = 2;
+ constexpr size_t kv_heads = 1;
+ constexpr size_t k_head_size = 4;
+ constexpr size_t v_head_size = 3;
+ constexpr size_t scales_zp_size = sizeof(ov::float16) * 2;
+ constexpr size_t adjusted_k_head_size = k_head_size + scales_zp_size;
+ constexpr size_t adjusted_v_head_size = v_head_size + scales_zp_size;
+ constexpr size_t block_size = cldnn::paged_attention::block_size;
+
+ auto key_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, adjusted_k_head_size, block_size}, data_types::i8, format::bfyx};
+ auto value_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, block_size, adjusted_v_head_size}, data_types::i8, format::bfyx};
+ auto block_indices_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_update_indices_layout = layout{ov::PartialShape{4}, data_types::i32, format::bfyx};
+ auto block_update_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+
+ auto key_cache_mem = engine.allocate_memory(key_cache_layout);
+ auto value_cache_mem = engine.allocate_memory(value_cache_layout);
+ auto block_indices_mem = engine.allocate_memory(block_indices_layout);
+ auto block_indices_begins_mem = engine.allocate_memory(block_indices_begins_layout);
+ auto block_update_indices_mem = engine.allocate_memory(block_update_indices_layout);
+ auto block_update_indices_begins_mem = engine.allocate_memory(block_update_indices_begins_layout);
+
+ std::vector<int8_t> key_cache_ref(key_cache_mem->count());
+ std::vector<int8_t> value_cache_ref(value_cache_mem->count());
+ for (size_t i = 0; i < key_cache_ref.size(); i++) {
+ key_cache_ref[i] = static_cast<int8_t>((static_cast<int>(i) % 101) - 50);
+ }
+ for (size_t i = 0; i < value_cache_ref.size(); i++) {
+ value_cache_ref[i] = static_cast<int8_t>((static_cast<int>(i) % 97) - 48);
+ }
+ set_values<int8_t>(key_cache_mem, key_cache_ref);
+ set_values<int8_t>(value_cache_mem, value_cache_ref);
+
+ set_values<int32_t>(block_indices_mem, {0, 1});
+ set_values<int32_t>(block_indices_begins_mem, {0, 2});
+ set_values<int32_t>(block_update_indices_mem, {
+ 0, 17,
+ 15, 16,
+ });
+ set_values<int32_t>(block_update_indices_begins_mem, {0, 2});
+
+ topology topo;
+ topo.add(mutable_data("key_cache", key_cache_mem));
+ topo.add(mutable_data("value_cache", value_cache_mem));
+ topo.add(input_layout("block_indices", block_indices_layout));
+ topo.add(input_layout("block_indices_begins", block_indices_begins_layout));
+ topo.add(input_layout("block_update_indices", block_update_indices_layout));
+ topo.add(input_layout("block_update_indices_begins", block_update_indices_begins_layout));
+
+ auto pa_reorder = pa_kv_reorder("pa_kv_reorder",
+ {input_info("key_cache"),
+ input_info("value_cache"),
+ input_info("block_indices"),
+ input_info("block_indices_begins"),
+ input_info("block_update_indices"),
+ input_info("block_update_indices_begins")});
+ pa_reorder.kv_heads_num = kv_heads;
+ pa_reorder.adjusted_k_head_size = adjusted_k_head_size;
+ pa_reorder.adjusted_paged_attention_block_size = block_size;
+ pa_reorder.adjusted_v_head_size = adjusted_v_head_size;
+ pa_reorder.cache_dt = data_types::i8;
+ pa_reorder.is_kv_compressed = true;
+ pa_reorder.scales_zp_size = scales_zp_size;
+ topo.add(pa_reorder);
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ auto network = get_network(engine, topo, config, get_test_stream_ptr(), false);
+
+ network->set_input_data("block_indices", block_indices_mem);
+ network->set_input_data("block_indices_begins", block_indices_begins_mem);
+ network->set_input_data("block_update_indices", block_update_indices_mem);
+ network->set_input_data("block_update_indices_begins", block_update_indices_begins_mem);
+ network->execute();
+ network->get_stream().finish();
+
+ cldnn::mem_lock<int8_t, mem_lock_type::read> key_ptr(key_cache_mem, network->get_stream());
+ cldnn::mem_lock<int8_t, mem_lock_type::read> value_ptr(value_cache_mem, network->get_stream());
+
+ for (size_t k = 0; k < k_head_size; k++) {
+ const auto src0 = key_cache_ref[key_offset(0, 0, k, 0, kv_heads, adjusted_k_head_size, block_size)];
+ const auto dst17 = key_ptr[key_offset(1, 0, k, 1, kv_heads, adjusted_k_head_size, block_size)];
+ ASSERT_EQ(dst17, src0);
+
+ const auto src15 = key_cache_ref[key_offset(0, 0, k, 15, kv_heads, adjusted_k_head_size, block_size)];
+ const auto dst16 = key_ptr[key_offset(1, 0, k, 0, kv_heads, adjusted_k_head_size, block_size)];
+ ASSERT_EQ(dst16, src15);
+ }
+
+ for (size_t v = 0; v < v_head_size; v++) {
+ const auto src0 = value_cache_ref[value_data_offset_compressed(0, 0, 0, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)];
+ const auto dst17 = value_ptr[value_data_offset_compressed(1, 0, 1, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)];
+ ASSERT_EQ(dst17, src0);
+
+ const auto src15 = value_cache_ref[value_data_offset_compressed(0, 0, 15, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)];
+ const auto dst16 = value_ptr[value_data_offset_compressed(1, 0, 0, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)];
+ ASSERT_EQ(dst16, src15);
+ }
+
+ for (size_t byte = 0; byte < sizeof(ov::float16); byte++) {
+ ASSERT_EQ(key_ptr[key_comp_byte_offset(1, 0, 1, byte, false, kv_heads, k_head_size, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_comp_byte_offset(0, 0, 0, byte, false, kv_heads, k_head_size, adjusted_k_head_size, block_size)]);
+ ASSERT_EQ(key_ptr[key_comp_byte_offset(1, 0, 1, byte, true, kv_heads, k_head_size, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_comp_byte_offset(0, 0, 0, byte, true, kv_heads, k_head_size, adjusted_k_head_size, block_size)]);
+
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(1, 0, 1, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 0, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(1, 0, 1, byte, true, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 0, byte, true, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+ }
+}
+
+TEST(pa_kv_reorder_gpu, copy_between_blocks_single_sequence_compressed_u4) {
+ run_copy_between_blocks_single_sequence_compressed_int4_test(data_types::u4);
+}
+
+TEST(pa_kv_reorder_gpu, copy_between_blocks_single_sequence_compressed_i4) {
+ run_copy_between_blocks_single_sequence_compressed_int4_test(data_types::i4);
+}
+
+TEST(pa_kv_reorder_gpu, updates_are_scoped_per_sequence_compressed) {
+ auto& engine = get_test_engine();
+
+ constexpr size_t blocks_num = 3;
+ constexpr size_t kv_heads = 1;
+ constexpr size_t k_head_size = 2;
+ constexpr size_t v_head_size = 2;
+ constexpr size_t scales_zp_size = sizeof(ov::float16) * 2;
+ constexpr size_t adjusted_k_head_size = k_head_size + scales_zp_size;
+ constexpr size_t adjusted_v_head_size = v_head_size + scales_zp_size;
+ constexpr size_t block_size = cldnn::paged_attention::block_size;
+
+ auto key_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, adjusted_k_head_size, block_size}, data_types::i8, format::bfyx};
+ auto value_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, block_size, adjusted_v_head_size}, data_types::i8, format::bfyx};
+ auto block_indices_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_indices_begins_layout = layout{ov::PartialShape{3}, data_types::i32, format::bfyx};
+ auto block_update_indices_layout = layout{ov::PartialShape{4}, data_types::i32, format::bfyx};
+ auto block_update_indices_begins_layout = layout{ov::PartialShape{3}, data_types::i32, format::bfyx};
+
+ auto key_cache_mem = engine.allocate_memory(key_cache_layout);
+ auto value_cache_mem = engine.allocate_memory(value_cache_layout);
+ auto block_indices_mem = engine.allocate_memory(block_indices_layout);
+ auto block_indices_begins_mem = engine.allocate_memory(block_indices_begins_layout);
+ auto block_update_indices_mem = engine.allocate_memory(block_update_indices_layout);
+ auto block_update_indices_begins_mem = engine.allocate_memory(block_update_indices_begins_layout);
+
+ std::vector<int8_t> key_cache_ref(key_cache_mem->count());
+ std::vector<int8_t> value_cache_ref(value_cache_mem->count());
+ for (size_t i = 0; i < key_cache_ref.size(); i++) {
+ key_cache_ref[i] = static_cast<int8_t>((static_cast<int>(i) % 89) - 44);
+ }
+ for (size_t i = 0; i < value_cache_ref.size(); i++) {
+ value_cache_ref[i] = static_cast<int8_t>((static_cast<int>(i) % 83) - 41);
+ }
+ set_values<int8_t>(key_cache_mem, key_cache_ref);
+ set_values<int8_t>(value_cache_mem, value_cache_ref);
+
+ set_values<int32_t>(block_indices_mem, {0, 2});
+ set_values<int32_t>(block_indices_begins_mem, {0, 1, 2});
+ set_values<int32_t>(block_update_indices_mem, {
+ 1, 3, // seq0: slot1 -> slot3 in block0
+ 2, 4, // seq1: slot2 -> slot4 in block2
+ });
+ set_values<int32_t>(block_update_indices_begins_mem, {0, 1, 2});
+
+ topology topo;
+ topo.add(mutable_data("key_cache", key_cache_mem));
+ topo.add(mutable_data("value_cache", value_cache_mem));
+ topo.add(input_layout("block_indices", block_indices_layout));
+ topo.add(input_layout("block_indices_begins", block_indices_begins_layout));
+ topo.add(input_layout("block_update_indices", block_update_indices_layout));
+ topo.add(input_layout("block_update_indices_begins", block_update_indices_begins_layout));
+
+ auto pa_reorder = pa_kv_reorder("pa_kv_reorder",
+ {input_info("key_cache"),
+ input_info("value_cache"),
+ input_info("block_indices"),
+ input_info("block_indices_begins"),
+ input_info("block_update_indices"),
+ input_info("block_update_indices_begins")});
+ pa_reorder.kv_heads_num = kv_heads;
+ pa_reorder.adjusted_k_head_size = adjusted_k_head_size;
+ pa_reorder.adjusted_paged_attention_block_size = block_size;
+ pa_reorder.adjusted_v_head_size = adjusted_v_head_size;
+ pa_reorder.cache_dt = data_types::i8;
+ pa_reorder.is_kv_compressed = true;
+ pa_reorder.scales_zp_size = scales_zp_size;
+ topo.add(pa_reorder);
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ auto network = get_network(engine, topo, config, get_test_stream_ptr(), false);
+
+ network->set_input_data("block_indices", block_indices_mem);
+ network->set_input_data("block_indices_begins", block_indices_begins_mem);
+ network->set_input_data("block_update_indices", block_update_indices_mem);
+ network->set_input_data("block_update_indices_begins", block_update_indices_begins_mem);
+ network->execute();
+ network->get_stream().finish();
+
+ cldnn::mem_lock<int8_t, mem_lock_type::read> key_ptr(key_cache_mem, network->get_stream());
+ cldnn::mem_lock<int8_t, mem_lock_type::read> value_ptr(value_cache_mem, network->get_stream());
+
+ for (size_t k = 0; k < k_head_size; k++) {
+ ASSERT_EQ(key_ptr[key_offset(0, 0, k, 3, kv_heads, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_offset(0, 0, k, 1, kv_heads, adjusted_k_head_size, block_size)]);
+ ASSERT_EQ(key_ptr[key_offset(2, 0, k, 4, kv_heads, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_offset(2, 0, k, 2, kv_heads, adjusted_k_head_size, block_size)]);
+
+ ASSERT_EQ(key_ptr[key_offset(1, 0, k, 4, kv_heads, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_offset(1, 0, k, 4, kv_heads, adjusted_k_head_size, block_size)]);
+ }
+
+ for (size_t v = 0; v < v_head_size; v++) {
+ ASSERT_EQ(value_ptr[value_data_offset_compressed(0, 0, 3, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_data_offset_compressed(0, 0, 1, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_data_offset_compressed(2, 0, 4, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_data_offset_compressed(2, 0, 2, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+
+ ASSERT_EQ(value_ptr[value_data_offset_compressed(1, 0, 4, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_data_offset_compressed(1, 0, 4, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+ }
+
+ for (size_t byte = 0; byte < sizeof(ov::float16); byte++) {
+ ASSERT_EQ(key_ptr[key_comp_byte_offset(0, 0, 3, byte, false, kv_heads, k_head_size, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_comp_byte_offset(0, 0, 1, byte, false, kv_heads, k_head_size, adjusted_k_head_size, block_size)]);
+ ASSERT_EQ(key_ptr[key_comp_byte_offset(0, 0, 3, byte, true, kv_heads, k_head_size, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_comp_byte_offset(0, 0, 1, byte, true, kv_heads, k_head_size, adjusted_k_head_size, block_size)]);
+
+ ASSERT_EQ(key_ptr[key_comp_byte_offset(2, 0, 4, byte, false, kv_heads, k_head_size, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_comp_byte_offset(2, 0, 2, byte, false, kv_heads, k_head_size, adjusted_k_head_size, block_size)]);
+ ASSERT_EQ(key_ptr[key_comp_byte_offset(2, 0, 4, byte, true, kv_heads, k_head_size, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_comp_byte_offset(2, 0, 2, byte, true, kv_heads, k_head_size, adjusted_k_head_size, block_size)]);
+
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(0, 0, 3, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 1, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(0, 0, 3, byte, true, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 1, byte, true, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(2, 0, 4, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(2, 0, 2, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(2, 0, 4, byte, true, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(2, 0, 2, byte, true, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+
+ ASSERT_EQ(key_ptr[key_comp_byte_offset(1, 0, 4, byte, false, kv_heads, k_head_size, adjusted_k_head_size, block_size)],
+ key_cache_ref[key_comp_byte_offset(1, 0, 4, byte, false, kv_heads, k_head_size, adjusted_k_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(1, 0, 4, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(1, 0, 4, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+ }
+}
+
+TEST(pa_kv_reorder_gpu, copy_between_blocks_single_sequence_compressed_key_by_channel) {
+ auto& engine = get_test_engine();
+
+ constexpr size_t blocks_num = 2;
+ constexpr size_t kv_heads = 1;
+ constexpr size_t k_head_size = 4;
+ constexpr size_t v_head_size = 3;
+ constexpr size_t scales_zp_size = sizeof(ov::float16) * 2;
+ constexpr size_t adjusted_paged_attention_block_size = cldnn::paged_attention::block_size + scales_zp_size;
+ constexpr size_t adjusted_v_head_size = v_head_size + scales_zp_size;
+ constexpr size_t block_size = cldnn::paged_attention::block_size;
+
+ auto key_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, k_head_size, adjusted_paged_attention_block_size}, data_types::i8, format::bfyx};
+ auto value_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, block_size, adjusted_v_head_size}, data_types::i8, format::bfyx};
+ auto block_indices_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_update_indices_layout = layout{ov::PartialShape{4}, data_types::i32, format::bfyx};
+ auto block_update_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+
+ auto key_cache_mem = engine.allocate_memory(key_cache_layout);
+ auto value_cache_mem = engine.allocate_memory(value_cache_layout);
+ auto block_indices_mem = engine.allocate_memory(block_indices_layout);
+ auto block_indices_begins_mem = engine.allocate_memory(block_indices_begins_layout);
+ auto block_update_indices_mem = engine.allocate_memory(block_update_indices_layout);
+ auto block_update_indices_begins_mem = engine.allocate_memory(block_update_indices_begins_layout);
+
+ std::vector<int8_t> key_cache_ref(key_cache_mem->count());
+ std::vector<int8_t> value_cache_ref(value_cache_mem->count());
+
+ for (size_t b = 0; b < blocks_num; b++) {
+ for (size_t h = 0; h < kv_heads; h++) {
+ for (size_t k = 0; k < k_head_size; k++) {
+ for (size_t t = 0; t < block_size; t++) {
+ key_cache_ref[key_offset(b, h, k, t, kv_heads, k_head_size, adjusted_paged_attention_block_size)] =
+ static_cast<int8_t>(static_cast<int>(10 * b + 3 * k + t) - 32);
+ }
+
+ const size_t comp_scale_byte_offset = key_offset(b, h, k, block_size, kv_heads, k_head_size, adjusted_paged_attention_block_size);
+ const size_t comp_zp_byte_offset = comp_scale_byte_offset + sizeof(ov::float16);
+ const ov::float16 scale_inv = ov::float16(1.0f);
+ const ov::float16 zp = ov::float16(0.0f);
+ std::memcpy(key_cache_ref.data() + comp_scale_byte_offset, &scale_inv, sizeof(ov::float16));
+ std::memcpy(key_cache_ref.data() + comp_zp_byte_offset, &zp, sizeof(ov::float16));
+ }
+ }
+ }
+
+ for (size_t b = 0; b < blocks_num; b++) {
+ for (size_t h = 0; h < kv_heads; h++) {
+ for (size_t t = 0; t < block_size; t++) {
+ for (size_t v = 0; v < v_head_size; v++) {
+ value_cache_ref[value_data_offset_compressed(b, h, t, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)] =
+ static_cast<int8_t>(static_cast<int>(13 * b + 5 * t + v) - 40);
+ }
+
+ const ov::float16 scale = ov::float16(0.25f * static_cast<float>(1 + ((b + t) % 3)));
+ const ov::float16 zp = ov::float16(static_cast<float>((static_cast<int>(t) % 5) - 2));
+ for (size_t byte = 0; byte < sizeof(ov::float16); byte++) {
+ const auto scale_off = value_comp_byte_offset(b,
+ h,
+ t,
+ byte,
+ false,
+ kv_heads,
+ v_head_size,
+ adjusted_v_head_size,
+ block_size);
+ const auto zp_off = value_comp_byte_offset(b,
+ h,
+ t,
+ byte,
+ true,
+ kv_heads,
+ v_head_size,
+ adjusted_v_head_size,
+ block_size);
+ value_cache_ref[scale_off] = reinterpret_cast<const int8_t*>(&scale)[byte];
+ value_cache_ref[zp_off] = reinterpret_cast<const int8_t*>(&zp)[byte];
+ }
+ }
+ }
+ }
+
+ set_values<int8_t>(key_cache_mem, key_cache_ref);
+ set_values<int8_t>(value_cache_mem, value_cache_ref);
+
+ set_values<int32_t>(block_indices_mem, {0, 1});
+ set_values<int32_t>(block_indices_begins_mem, {0, 2});
+ set_values<int32_t>(block_update_indices_mem, {
+ 0, 17,
+ 15, 16,
+ });
+ set_values<int32_t>(block_update_indices_begins_mem, {0, 2});
+
+ topology topo;
+ topo.add(mutable_data("key_cache", key_cache_mem));
+ topo.add(mutable_data("value_cache", value_cache_mem));
+ topo.add(input_layout("block_indices", block_indices_layout));
+ topo.add(input_layout("block_indices_begins", block_indices_begins_layout));
+ topo.add(input_layout("block_update_indices", block_update_indices_layout));
+ topo.add(input_layout("block_update_indices_begins", block_update_indices_begins_layout));
+
+ auto pa_reorder = pa_kv_reorder("pa_kv_reorder",
+ {input_info("key_cache"),
+ input_info("value_cache"),
+ input_info("block_indices"),
+ input_info("block_indices_begins"),
+ input_info("block_update_indices"),
+ input_info("block_update_indices_begins")});
+ pa_reorder.kv_heads_num = kv_heads;
+ pa_reorder.adjusted_k_head_size = k_head_size;
+ pa_reorder.adjusted_paged_attention_block_size = adjusted_paged_attention_block_size;
+ pa_reorder.adjusted_v_head_size = adjusted_v_head_size;
+ pa_reorder.cache_dt = data_types::i8;
+ pa_reorder.is_kv_compressed = true;
+ pa_reorder.is_key_by_channel = true;
+ pa_reorder.scales_zp_size = scales_zp_size;
+ topo.add(pa_reorder);
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ auto network = get_network(engine, topo, config, get_test_stream_ptr(), false);
+
+ network->set_input_data("block_indices", block_indices_mem);
+ network->set_input_data("block_indices_begins", block_indices_begins_mem);
+ network->set_input_data("block_update_indices", block_update_indices_mem);
+ network->set_input_data("block_update_indices_begins", block_update_indices_begins_mem);
+ network->execute();
+ network->get_stream().finish();
+
+ cldnn::mem_lock<int8_t, mem_lock_type::read> key_ptr(key_cache_mem, network->get_stream());
+ cldnn::mem_lock<int8_t, mem_lock_type::read> value_ptr(value_cache_mem, network->get_stream());
+
+ // In key-by-channel mode, key cache is re-quantized per channel on destination block.
+ // Validate by dequantized semantics instead of raw byte equality.
+ for (size_t k = 0; k < k_head_size; k++) {
+ const auto src0_q = key_cache_ref[key_offset(0, 0, k, 0, kv_heads, k_head_size, adjusted_paged_attention_block_size)];
+ const auto src15_q = key_cache_ref[key_offset(0, 0, k, 15, kv_heads, k_head_size, adjusted_paged_attention_block_size)];
+
+ const size_t comp_scale_byte_offset = key_offset(1, 0, k, block_size, kv_heads, k_head_size, adjusted_paged_attention_block_size);
+ const size_t comp_zp_byte_offset = comp_scale_byte_offset + sizeof(ov::float16);
+ const float dst_scale_inv = static_cast<float>(read_fp16_from_byte_buffer(key_ptr, comp_scale_byte_offset));
+ const float dst_zp = static_cast<float>(read_fp16_from_byte_buffer(key_ptr, comp_zp_byte_offset));
+
+ const float dst17_dequant = (static_cast<float>(key_ptr[key_offset(1, 0, k, 1, kv_heads, k_head_size, adjusted_paged_attention_block_size)]) - dst_zp) * dst_scale_inv;
+ const float dst16_dequant = (static_cast<float>(key_ptr[key_offset(1, 0, k, 0, kv_heads, k_head_size, adjusted_paged_attention_block_size)]) - dst_zp) * dst_scale_inv;
+
+ ASSERT_NEAR(dst17_dequant, static_cast<float>(src0_q), 1.0f);
+ ASSERT_NEAR(dst16_dequant, static_cast<float>(src15_q), 1.0f);
+ }
+
+ // Value cache remains per-token compressed copy behavior.
+ for (size_t v = 0; v < v_head_size; v++) {
+ const auto src0 = value_cache_ref[value_data_offset_compressed(0, 0, 0, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)];
+ const auto dst17 = value_ptr[value_data_offset_compressed(1, 0, 1, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)];
+ ASSERT_EQ(dst17, src0);
+
+ const auto src15 = value_cache_ref[value_data_offset_compressed(0, 0, 15, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)];
+ const auto dst16 = value_ptr[value_data_offset_compressed(1, 0, 0, v, kv_heads, v_head_size, adjusted_v_head_size, block_size)];
+ ASSERT_EQ(dst16, src15);
+ }
+
+ for (size_t byte = 0; byte < sizeof(ov::float16); byte++) {
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(1, 0, 1, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 0, byte, false, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(1, 0, 1, byte, true, kv_heads, v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 0, byte, true, kv_heads, v_head_size, adjusted_v_head_size, block_size)]);
+ }
+}
+
+TEST(pa_kv_reorder_gpu, copy_between_blocks_single_sequence_compressed_u4_key_by_channel) {
+ auto& engine = get_test_engine();
+
+ constexpr size_t blocks_num = 2;
+ constexpr size_t kv_heads = 1;
+ constexpr size_t k_head_size = 32;
+ constexpr size_t packed_k_head_size = k_head_size / 2;
+ constexpr size_t v_head_size = 16;
+ constexpr size_t subgroup_size = 16;
+ constexpr size_t packed_v_head_size = ((v_head_size / 2 + subgroup_size - 1) / subgroup_size) * subgroup_size;
+ constexpr size_t scales_zp_size = sizeof(ov::float16) * 4;
+ constexpr size_t adjusted_paged_attention_block_size = cldnn::paged_attention::block_size + scales_zp_size;
+ constexpr size_t adjusted_v_head_size = packed_v_head_size + scales_zp_size;
+ constexpr size_t block_size = cldnn::paged_attention::block_size;
+
+ auto key_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, packed_k_head_size, adjusted_paged_attention_block_size}, data_types::u8, format::bfyx};
+ auto value_cache_layout = layout{ov::PartialShape{blocks_num, kv_heads, block_size, adjusted_v_head_size}, data_types::u8, format::bfyx};
+ auto block_indices_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_update_indices_layout = layout{ov::PartialShape{4}, data_types::i32, format::bfyx};
+ auto block_update_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+
+ auto key_cache_mem = engine.allocate_memory(key_cache_layout);
+ auto value_cache_mem = engine.allocate_memory(value_cache_layout);
+ auto block_indices_mem = engine.allocate_memory(block_indices_layout);
+ auto block_indices_begins_mem = engine.allocate_memory(block_indices_begins_layout);
+ auto block_update_indices_mem = engine.allocate_memory(block_update_indices_layout);
+ auto block_update_indices_begins_mem = engine.allocate_memory(block_update_indices_begins_layout);
+
+ std::vector<uint8_t> key_cache_ref(key_cache_mem->count(), 0);
+ std::vector<uint8_t> value_cache_ref(value_cache_mem->count(), 0);
+
+ auto write_fp16_bytes = [](std::vector<uint8_t>& buffer, size_t byte_offset, ov::float16 value) {
+ const auto bits = value.to_bits();
+ buffer[byte_offset] = static_cast<uint8_t>(bits & 0xFF);
+ buffer[byte_offset + 1] = static_cast<uint8_t>((bits >> 8) & 0xFF);
+ };
+
+ // Fill key cache data and per-packed-dim comp params.
+ for (size_t b = 0; b < blocks_num; b++) {
+ for (size_t p = 0; p < packed_k_head_size; p++) {
+ const float s0 = 0.10f + 0.01f * static_cast<float>(p);
+ const float z0 = static_cast<float>((static_cast<int>(p) % 4) + 1);
+ const float s1 = 0.08f + 0.01f * static_cast<float>(p);
+ const float z1 = static_cast<float>((static_cast<int>(p) % 5) + 2);
+
+ const size_t comp_base = key_offset(b, 0, p, block_size, kv_heads, packed_k_head_size, adjusted_paged_attention_block_size);
+ write_fp16_bytes(key_cache_ref, comp_base + 0 * sizeof(ov::float16), ov::float16(s0));
+ write_fp16_bytes(key_cache_ref, comp_base + 1 * sizeof(ov::float16), ov::float16(z0));
+ write_fp16_bytes(key_cache_ref, comp_base + 2 * sizeof(ov::float16), ov::float16(s1));
+ write_fp16_bytes(key_cache_ref, comp_base + 3 * sizeof(ov::float16), ov::float16(z1));
+
+ for (size_t t = 0; t < block_size; t++) {
+ const uint8_t q0 = static_cast<uint8_t>((3 * t + p + b) & 0xF);
+ const uint8_t q1 = static_cast<uint8_t>((2 * t + 2 * p + 1 + b) & 0xF);
+ key_cache_ref[key_offset(b, 0, p, t, kv_heads, packed_k_head_size, adjusted_paged_attention_block_size)] =
+ static_cast<uint8_t>((q0 & 0xF) | ((q1 & 0xF) << 4));
+ }
+ }
+ }
+
+ // Fill value cache with packed BY_TOKEN style bytes and comp bytes.
+ for (size_t b = 0; b < blocks_num; b++) {
+ for (size_t t = 0; t < block_size; t++) {
+ for (size_t p = 0; p < packed_v_head_size; p++) {
+ value_cache_ref[value_data_offset_compressed(b, 0, t, p, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)] =
+ static_cast<uint8_t>((7 * t + 5 * p + b) % 251);
+ }
+
+ const ov::float16 scale = ov::float16(0.125f * static_cast<float>(1 + ((b + t) % 3)));
+ const ov::float16 zp = ov::float16(static_cast<float>((static_cast<int>(t) % 7) - 3));
+ for (size_t byte = 0; byte < sizeof(ov::float16); byte++) {
+ const auto scale_off = value_comp_byte_offset(b,
+ 0,
+ t,
+ byte,
+ false,
+ kv_heads,
+ packed_v_head_size,
+ adjusted_v_head_size,
+ block_size);
+ const auto zp_off = value_comp_byte_offset(b,
+ 0,
+ t,
+ byte,
+ true,
+ kv_heads,
+ packed_v_head_size,
+ adjusted_v_head_size,
+ block_size);
+ value_cache_ref[scale_off] = reinterpret_cast<const uint8_t*>(&scale)[byte];
+ value_cache_ref[zp_off] = reinterpret_cast<const uint8_t*>(&zp)[byte];
+ }
+ }
+ }
+
+ set_values<uint8_t>(key_cache_mem, key_cache_ref);
+ set_values<uint8_t>(value_cache_mem, value_cache_ref);
+
+ set_values<int32_t>(block_indices_mem, {0, 1});
+ set_values<int32_t>(block_indices_begins_mem, {0, 2});
+ set_values<int32_t>(block_update_indices_mem, {
+ 0, 17,
+ 15, 16,
+ });
+ set_values<int32_t>(block_update_indices_begins_mem, {0, 2});
+
+ topology topo;
+ topo.add(mutable_data("key_cache", key_cache_mem));
+ topo.add(mutable_data("value_cache", value_cache_mem));
+ topo.add(input_layout("block_indices", block_indices_layout));
+ topo.add(input_layout("block_indices_begins", block_indices_begins_layout));
+ topo.add(input_layout("block_update_indices", block_update_indices_layout));
+ topo.add(input_layout("block_update_indices_begins", block_update_indices_begins_layout));
+
+ auto pa_reorder = pa_kv_reorder("pa_kv_reorder",
+ {input_info("key_cache"),
+ input_info("value_cache"),
+ input_info("block_indices"),
+ input_info("block_indices_begins"),
+ input_info("block_update_indices"),
+ input_info("block_update_indices_begins")});
+ pa_reorder.kv_heads_num = kv_heads;
+ pa_reorder.adjusted_k_head_size = k_head_size;
+ pa_reorder.adjusted_paged_attention_block_size = adjusted_paged_attention_block_size;
+ pa_reorder.adjusted_v_head_size = v_head_size + scales_zp_size;
+ pa_reorder.cache_dt = data_types::u4;
+ pa_reorder.is_kv_compressed = true;
+ pa_reorder.is_key_by_channel = true;
+ pa_reorder.scales_zp_size = scales_zp_size;
+ topo.add(pa_reorder);
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ auto network = get_network(engine, topo, config, get_test_stream_ptr(), false);
+
+ network->set_input_data("block_indices", block_indices_mem);
+ network->set_input_data("block_indices_begins", block_indices_begins_mem);
+ network->set_input_data("block_update_indices", block_update_indices_mem);
+ network->set_input_data("block_update_indices_begins", block_update_indices_begins_mem);
+ network->execute();
+ network->get_stream().finish();
+
+ cldnn::mem_lock<uint8_t, mem_lock_type::read> key_ptr(key_cache_mem, network->get_stream());
+ cldnn::mem_lock<uint8_t, mem_lock_type::read> value_ptr(value_cache_mem, network->get_stream());
+
+ auto unpack_q_pair = [](uint8_t packed) {
+ return std::make_pair(static_cast<uint8_t>(packed & 0xF), static_cast<uint8_t>((packed >> 4) & 0xF));
+ };
+
+ auto read_comp_output = [&](const cldnn::mem_lock<uint8_t, mem_lock_type::read>& ptr, size_t block, size_t packed_k) {
+ const size_t comp_base = key_offset(block, 0, packed_k, block_size, kv_heads, packed_k_head_size, adjusted_paged_attention_block_size);
+ const float s0 = static_cast<float>(read_fp16_from_byte_buffer(ptr, comp_base + 0 * sizeof(ov::float16)));
+ const float z0 = static_cast<float>(read_fp16_from_byte_buffer(ptr, comp_base + 1 * sizeof(ov::float16)));
+ const float s1 = static_cast<float>(read_fp16_from_byte_buffer(ptr, comp_base + 2 * sizeof(ov::float16)));
+ const float z1 = static_cast<float>(read_fp16_from_byte_buffer(ptr, comp_base + 3 * sizeof(ov::float16)));
+ return std::make_tuple(s0, z0, s1, z1);
+ };
+
+ auto read_comp_ref = [&](const std::vector<uint8_t>& buffer, size_t block, size_t packed_k) {
+ const size_t comp_base = key_offset(block, 0, packed_k, block_size, kv_heads, packed_k_head_size, adjusted_paged_attention_block_size);
+ const float s0 = static_cast<float>(read_fp16_from_u8_vector(buffer, comp_base + 0 * sizeof(ov::float16)));
+ const float z0 = static_cast<float>(read_fp16_from_u8_vector(buffer, comp_base + 1 * sizeof(ov::float16)));
+ const float s1 = static_cast<float>(read_fp16_from_u8_vector(buffer, comp_base + 2 * sizeof(ov::float16)));
+ const float z1 = static_cast<float>(read_fp16_from_u8_vector(buffer, comp_base + 3 * sizeof(ov::float16)));
+ return std::make_tuple(s0, z0, s1, z1);
+ };
+
+ for (size_t p = 0; p < packed_k_head_size; p++) {
+ const uint8_t src_packed_0 = key_cache_ref[key_offset(0, 0, p, 0, kv_heads, packed_k_head_size, adjusted_paged_attention_block_size)];
+ const uint8_t src_packed_15 = key_cache_ref[key_offset(0, 0, p, 15, kv_heads, packed_k_head_size, adjusted_paged_attention_block_size)];
+ const auto [src_q0_0, src_q1_0] = unpack_q_pair(src_packed_0);
+ const auto [src_q0_15, src_q1_15] = unpack_q_pair(src_packed_15);
+
+ const auto [src_s0, src_z0, src_s1, src_z1] = read_comp_ref(key_cache_ref, 0, p);
+ const auto [dst_s0, dst_z0, dst_s1, dst_z1] = read_comp_output(key_ptr, 1, p);
+
+ const float src_val0_0 = (static_cast<float>(src_q0_0) - src_z0) * src_s0;
+ const float src_val1_0 = (static_cast<float>(src_q1_0) - src_z1) * src_s1;
+ const float src_val0_15 = (static_cast<float>(src_q0_15) - src_z0) * src_s0;
+ const float src_val1_15 = (static_cast<float>(src_q1_15) - src_z1) * src_s1;
+
+ const uint8_t dst_packed_17 = key_ptr[key_offset(1, 0, p, 1, kv_heads, packed_k_head_size, adjusted_paged_attention_block_size)];
+ const uint8_t dst_packed_16 = key_ptr[key_offset(1, 0, p, 0, kv_heads, packed_k_head_size, adjusted_paged_attention_block_size)];
+ const auto [dst_q0_17, dst_q1_17] = unpack_q_pair(dst_packed_17);
+ const auto [dst_q0_16, dst_q1_16] = unpack_q_pair(dst_packed_16);
+
+ const float dst_val0_17 = (static_cast<float>(dst_q0_17) - dst_z0) * dst_s0;
+ const float dst_val1_17 = (static_cast<float>(dst_q1_17) - dst_z1) * dst_s1;
+ const float dst_val0_16 = (static_cast<float>(dst_q0_16) - dst_z0) * dst_s0;
+ const float dst_val1_16 = (static_cast<float>(dst_q1_16) - dst_z1) * dst_s1;
+
+ ASSERT_NEAR(dst_val0_17, src_val0_0, 1.0f);
+ ASSERT_NEAR(dst_val1_17, src_val1_0, 1.0f);
+ ASSERT_NEAR(dst_val0_16, src_val0_15, 1.0f);
+ ASSERT_NEAR(dst_val1_16, src_val1_15, 1.0f);
+ }
+
+ for (size_t p = 0; p < packed_v_head_size; p++) {
+ const auto src0 = value_cache_ref[value_data_offset_compressed(0, 0, 0, p, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)];
+ const auto dst17 = value_ptr[value_data_offset_compressed(1, 0, 1, p, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)];
+ ASSERT_EQ(dst17, src0);
+
+ const auto src15 = value_cache_ref[value_data_offset_compressed(0, 0, 15, p, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)];
+ const auto dst16 = value_ptr[value_data_offset_compressed(1, 0, 0, p, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)];
+ ASSERT_EQ(dst16, src15);
+ }
+
+ for (size_t byte = 0; byte < sizeof(ov::float16); byte++) {
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(1, 0, 1, byte, false, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 0, byte, false, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)]);
+ ASSERT_EQ(value_ptr[value_comp_byte_offset(1, 0, 1, byte, true, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)],
+ value_cache_ref[value_comp_byte_offset(0, 0, 0, byte, true, kv_heads, packed_v_head_size, adjusted_v_head_size, block_size)]);
+ }
+}
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/pa_kv_reorder_fusion_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/pa_kv_reorder_fusion_test.cpp
new file mode 100644
index 00000000000..d2990ec9fdb
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/unit/transformations/pa_kv_reorder_fusion_test.cpp
@@ -0,0 +1,162 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "common_test_utils/ov_test_utils.hpp"
+
+#include "openvino/core/model.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/result.hpp"
+#include "openvino/op/scatter_update.hpp"
+
+#include "intel_gpu/op/pa_kv_reorder.hpp"
+#include "plugin/transformations/pa_kv_reorder_fusion.hpp"
+
+#include <memory>
+
+using namespace testing;
+using namespace ov::intel_gpu;
+
+namespace ov {
+namespace test {
+namespace intel_gpu {
+
+TEST_F(TransformationTestsF, PaKVReorderFusion_basic) {
+ disable_result_friendly_names_check();
+ {
+ auto key_cache = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{4, 2});
+ key_cache->set_friendly_name("key_cache.0_clone_for_k_update");
+ auto value_cache = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{4, 2});
+ value_cache->set_friendly_name("value_cache.0_clone_for_v_update");
+
+ auto block_indices = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_indices->set_friendly_name("block_indices");
+ auto block_indices_begins = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_indices_begins->set_friendly_name("block_indices_begins");
+
+ auto block_update_indices = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_update_indices->set_friendly_name("block_update_indices");
+ auto block_update_indices_begins = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_update_indices_begins->set_friendly_name("block_update_indices_begins");
+
+ auto gather_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});
+ auto scatter_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});
+
+ auto key_gather = std::make_shared<ov::op::v8::Gather>(key_cache, block_update_indices, gather_axis);
+ auto value_gather = std::make_shared<ov::op::v8::Gather>(value_cache, block_update_indices, gather_axis);
+
+ auto key_scatter = std::make_shared<ov::op::v3::ScatterUpdate>(key_cache, block_indices, key_gather, scatter_axis);
+ auto value_scatter = std::make_shared<ov::op::v3::ScatterUpdate>(value_cache, block_indices, value_gather, scatter_axis);
+
+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{key_scatter, value_scatter}, 0);
+ auto result = std::make_shared<ov::op::v0::Result>(concat);
+
+ model = std::make_shared<ov::Model>(ov::ResultVector{result},
+ ov::ParameterVector{key_cache,
+ value_cache,
+ block_indices,
+ block_indices_begins,
+ block_update_indices,
+ block_update_indices_begins});
+
+ manager.register_pass<PaKVReorderFusion>(false,
+ std::vector<size_t>{0, 1, 3, 2},
+ std::vector<size_t>{0, 1, 2, 3},
+ ov::element::f16,
+ ov::element::f16,
+ ov::element::f16);
+ }
+
+ {
+ auto key_cache = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{4, 2});
+ key_cache->set_friendly_name("key_cache.0_clone_for_k_update");
+ auto value_cache = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{4, 2});
+ value_cache->set_friendly_name("value_cache.0_clone_for_v_update");
+
+ auto block_indices = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_indices->set_friendly_name("block_indices");
+ auto block_indices_begins = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_indices_begins->set_friendly_name("block_indices_begins");
+
+ auto block_update_indices = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_update_indices->set_friendly_name("block_update_indices");
+ auto block_update_indices_begins = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_update_indices_begins->set_friendly_name("block_update_indices_begins");
+
+ auto pa_kv_reorder = std::make_shared<ov::intel_gpu::op::PA_KV_Reorder>(key_cache,
+ value_cache,
+ block_indices,
+ block_indices_begins,
+ block_update_indices,
+ block_update_indices_begins);
+ pa_kv_reorder->set_friendly_name("pa_kv_reorder_0");
+ auto result = std::make_shared<ov::op::v0::Result>(pa_kv_reorder);
+
+ model_ref = std::make_shared<ov::Model>(ov::ResultVector{result},
+ ov::ParameterVector{key_cache,
+ value_cache,
+ block_indices,
+ block_indices_begins,
+ block_update_indices,
+ block_update_indices_begins});
+ comparator.enable(FunctionsComparator::ATTRIBUTES);
+ }
+}
+
+TEST_F(TransformationTestsF, PaKVReorderFusion_skip_on_mismatched_block_indices) {
+ disable_result_friendly_names_check();
+ auto key_cache = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{4, 2});
+ key_cache->set_friendly_name("key_cache.0_clone_for_k_update");
+ auto value_cache = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{4, 2});
+ value_cache->set_friendly_name("value_cache.0_clone_for_v_update");
+
+ auto block_indices_k = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_indices_k->set_friendly_name("block_indices_k");
+ auto block_indices_v = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_indices_v->set_friendly_name("block_indices_v");
+
+ auto block_indices_begins = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_indices_begins->set_friendly_name("block_indices_begins");
+ auto block_update_indices = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_update_indices->set_friendly_name("block_update_indices");
+ auto block_update_indices_begins = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{2});
+ block_update_indices_begins->set_friendly_name("block_update_indices_begins");
+
+ auto gather_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});
+ auto scatter_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});
+
+ auto key_gather = std::make_shared<ov::op::v8::Gather>(key_cache, block_update_indices, gather_axis);
+ auto value_gather = std::make_shared<ov::op::v8::Gather>(value_cache, block_update_indices, gather_axis);
+
+ auto key_scatter = std::make_shared<ov::op::v3::ScatterUpdate>(key_cache, block_indices_k, key_gather, scatter_axis);
+ auto value_scatter = std::make_shared<ov::op::v3::ScatterUpdate>(value_cache, block_indices_v, value_gather, scatter_axis);
+
+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{key_scatter, value_scatter}, 0);
+ auto result = std::make_shared<ov::op::v0::Result>(concat);
+
+ model = std::make_shared<ov::Model>(ov::ResultVector{result},
+ ov::ParameterVector{key_cache,
+ value_cache,
+ block_indices_k,
+ block_indices_v,
+ block_indices_begins,
+ block_update_indices,
+ block_update_indices_begins});
+
+ manager.register_pass<PaKVReorderFusion>(false,
+ std::vector<size_t>{0, 1, 3, 2},
+ std::vector<size_t>{0, 1, 2, 3},
+ ov::element::f16,
+ ov::element::f16,
+ ov::element::f16);
+
+ model_ref = model->clone();
+ comparator.enable(FunctionsComparator::ATTRIBUTES);
+}
+
+} // namespace intel_gpu
+} // namespace test
+} // namespace ov