Skip to content

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

[PA ref] Add support for bidirectional image attention (#36027)

Przemyslaw Wysocki86048d9f3782
diff --git a/src/core/reference/include/openvino/reference/paged_attention.hpp b/src/core/reference/include/openvino/reference/paged_attention.hpp
index 84474949122..2b7304da829 100644
--- a/src/core/reference/include/openvino/reference/paged_attention.hpp
+++ b/src/core/reference/include/openvino/reference/paged_attention.hpp
@@ -191,6 +191,8 @@ void paged_attention(std::uintptr_t node_key,
const int32_t* adaptive_rkv_evictable_sizes,
const int32_t* adaptive_rkv_diversity_block_set_indices,
const int32_t* adaptive_rkv_diversity_block_set_indices_begins,
+ const int32_t* token_type_ids,
+ std::size_t token_type_ids_count,
const ov::Shape& query_shape,
const ov::Shape& key_shape,
const ov::Shape& value_shape,
@@ -268,6 +270,31 @@ void paged_attention(std::uintptr_t node_key,
// Parse token-to-sequence partition
const auto seq_begins = detail::parse_subsequence_ranges(subsequence_begins, subseq_count, seq_count, batch_tokens);
+ // Bidirectional image attention: precompute image group boundaries from token_type_ids.
+ // For each image token, record the inclusive begin and exclusive end of its contiguous group.
+ const bool has_token_types = (token_type_ids != nullptr && token_type_ids_count > 0);
+ std::vector<int32_t> image_group_begin(batch_tokens, -1);
+ std::vector<int32_t> image_group_end(batch_tokens, -1);
+ if (has_token_types) {
+ for (std::size_t s = 0; s < seq_count; ++s) {
+ auto se = seq_begins[s + 1];
+ for (auto i = seq_begins[s]; i < se;) {
+ if (token_type_ids[i] != 1) {
+ ++i;
+ continue;
+ }
+ auto grp_begin = i;
+ while (i < se && token_type_ids[i] == 1) {
+ ++i;
+ }
+ for (auto j = grp_begin; j < i; ++j) {
+ image_group_begin[j] = static_cast<int32_t>(grp_begin);
+ image_group_end[j] = static_cast<int32_t>(i);
+ }
+ }
+ }
+ }
+
// Prepare mapping for rotated blocks (block_id -> rotated_index)
std::unordered_map<int32_t, int32_t> rotated_map;
rotated_map.reserve(rotated_block_count);
@@ -342,6 +369,8 @@ void paged_attention(std::uintptr_t node_key,
const std::int32_t past = past_lens ? past_lens[s] : 0;
OPENVINO_ASSERT(past >= 0, "PagedAttention reference: negative past_lens");
const std::size_t new_len = t_end - t_begin;
+ const int32_t t_begin_i = static_cast<int32_t>(t_begin);
+ const int32_t new_len_i = static_cast<int32_t>(new_len);
// Score aggregation window: [] broadcasts to all sequences, [B_seq] is per-sequence.
// Negative = unbounded, 0 = disabled.
@@ -511,27 +540,40 @@ void paged_attention(std::uintptr_t node_key,
// Base offset for out_scores for this sequence (concatenation order is sequence order)
const std::size_t score_base = score_prefix[s];
+ // Populate cache with all new tokens before computing attention.
+ // This ordering is required for bidirectional image groups (token_type_ids)
+ // which attend to future positions within the same group.
+ for (std::size_t i = 0; i < new_len; ++i) {
+ cache_manager->write_token_kv<T>(node_key,
+ s,
+ past + static_cast<std::int32_t>(i),
+ key + (t_begin + i) * key_features,
+ value + (t_begin + i) * value_features);
+ }
+
for (std::size_t i = 0; i < new_len; ++i) {
const std::size_t token = t_begin + i;
const std::int32_t qpos = past + static_cast<std::int32_t>(i);
- // Append this token's KV into the cache
- const T* krow = key + token * key_features;
- const T* vrow = value + token * value_features;
- cache_manager->write_token_kv<T>(node_key, s, qpos, krow, vrow);
-
// Determine attention window
- std::int32_t start = 0;
- if (max_context_i > 0) {
- start = std::max(start, qpos + 1 - max_context_i);
- }
- if (sliding_window_i > 0) {
- start = std::max(start, qpos + 1 - sliding_window_i);
+ int32_t ncausal = qpos + 1;
+ const int32_t causal_pos = ncausal;
+ const bool is_image = has_token_types && token_type_ids[token] == 1;
+
+ if (is_image) {
+ ncausal = std::min(past + (image_group_end[token] - t_begin_i), past + new_len_i);
}
- if (start < 0) {
+
+ int32_t start = 0;
+ if (max_context_i > 0)
+ start = std::max(start, causal_pos - max_context_i);
+ if (sliding_window_i > 0)
+ start = std::max(start, causal_pos - sliding_window_i);
+ if (is_image)
+ start = std::min(start, past + (image_group_begin[token] - t_begin_i));
+ if (start < 0)
start = 0;
- }
- const std::int32_t end = qpos;
+ const std::int32_t end = ncausal - 1;
const std::size_t ctx_len = (end >= start) ? static_cast<std::size_t>(end - start + 1) : 0;
if (ctx_len == 0) {
// No context (shouldn't happen for causal), output zeros
diff --git a/src/core/tests/reference/paged_cache_manager.cpp b/src/core/tests/reference/paged_cache_manager.cpp
index 3ecfd3bad0e..09aa5d89b1f 100644
--- a/src/core/tests/reference/paged_cache_manager.cpp
+++ b/src/core/tests/reference/paged_cache_manager.cpp
@@ -579,6 +579,8 @@ TEST(PagedCacheManagerTest, ReferenceKernelEvictionNoCorruption) {
nullptr,
nullptr,
nullptr,
+ nullptr,
+ 0,
{step1_tokens, q_features},
{step1_tokens, kv_features},
{step1_tokens, kv_features},
@@ -651,6 +653,8 @@ TEST(PagedCacheManagerTest, ReferenceKernelEvictionNoCorruption) {
nullptr,
nullptr,
nullptr,
+ nullptr,
+ 0,
{step2_tokens, q_features},
{step2_tokens, kv_features},
{step2_tokens, kv_features},
@@ -1466,6 +1470,8 @@ TEST(PagedCacheManagerTest, PoolKernelChangesEvictionVictimViaPA) {
nullptr, // adaptive_rkv_evictable_sizes
nullptr, // adaptive_rkv_diversity_block_set_indices
nullptr, // adaptive_rkv_diversity_block_set_indices_begins
+ nullptr, // token_type_ids
+ 0, // token_type_ids_count
{tokens, q_features},
{tokens, kv_features},
{tokens, kv_features},
@@ -1532,6 +1538,8 @@ TEST(PagedCacheManagerTest, PoolKernelChangesEvictionVictimViaPA) {
nullptr,
nullptr,
nullptr,
+ nullptr,
+ 0,
{1, q_features},
{1, kv_features},
{1, kv_features},
@@ -1565,3 +1573,394 @@ TEST(PagedCacheManagerTest, PoolKernelChangesEvictionVictimViaPA) {
EXPECT_FALSE(a1 && a2) << "pk=1: one of seq 1/2 should have lost its front block";
EXPECT_FALSE(b1 && b2) << "pk=7: one of seq 1/2 should have lost its front block";
}
+
+// ===================================================================
+// Bidirectional image attention via token_type_ids
+// ===================================================================
+
+// Helper: run paged_attention with given token_type_ids and sliding_window, return output
+static std::vector<float> run_pa_with_token_types(const std::vector<float>& q,
+ const std::vector<float>& k,
+ const std::vector<float>& v,
+ std::size_t num_tokens,
+ std::size_t q_heads,
+ std::size_t kv_heads,
+ std::size_t head_size,
+ const std::vector<int32_t>& token_type_ids,
+ int32_t sliding_window_val = 0) {
+ const std::size_t q_features = q_heads * head_size;
+ const std::size_t kv_features = kv_heads * head_size;
+ const std::size_t block_size = 4;
+ const std::size_t num_blocks = 8;
+
+ ov::Shape cache_shape = {num_blocks, kv_heads, block_size, head_size};
+ std::vector<float> key_cache(num_blocks * kv_heads * block_size * head_size, 0.f);
+ std::vector<float> val_cache(num_blocks * kv_heads * block_size * head_size, 0.f);
+
+ auto mgr = std::make_unique<PagedCacheManager>(ov::element::f32, EvictionPolicy::FIFO, 0);
+ constexpr std::uintptr_t nk = 200;
+
+ std::int32_t past = 0;
+ mgr->ensure_operator(nk,
+ key_cache.data(),
+ val_cache.data(),
+ cache_shape,
+ cache_shape,
+ nullptr,
+ 0,
+ nullptr,
+ 0,
+ &past,
+ 1);
+
+ std::int32_t subseq[2] = {0, static_cast<int32_t>(num_tokens)};
+ // scale=1.0 so softmax input = raw dot product, keeps expected values simple
+ float scale_val = 1.0f;
+ std::int32_t score_window = 0;
+ std::vector<int32_t> block_idx(num_blocks);
+ std::iota(block_idx.begin(), block_idx.end(), 0);
+ std::vector<int32_t> block_begins = {0, static_cast<int32_t>(num_blocks)};
+
+ const int32_t* sliding_ptr = (sliding_window_val > 0) ? &sliding_window_val : nullptr;
+ const int32_t* token_types_ptr = token_type_ids.empty() ? nullptr : token_type_ids.data();
+ std::size_t token_types_count = token_type_ids.size();
+
+ std::vector<float> out(num_tokens * q_features, -999.f);
+
+ ov::reference::paged_attention<float>(nk,
+ mgr.get(),
+ out.data(),
+ nullptr,
+ nullptr,
+ q.data(),
+ k.data(),
+ v.data(),
+ key_cache.data(),
+ val_cache.data(),
+ &past,
+ subseq,
+ block_idx.data(),
+ block_idx.size(),
+ block_begins.data(),
+ block_begins.size(),
+ &scale_val,
+ ov::element::f32,
+ sliding_ptr,
+ nullptr,
+ ov::element::Type{},
+ {},
+ nullptr,
+ &score_window,
+ 1,
+ nullptr,
+ 0,
+ nullptr,
+ {},
+ nullptr,
+ ov::element::Type{},
+ {},
+ nullptr,
+ ov::element::Type{},
+ nullptr,
+ nullptr,
+ nullptr,
+ ov::element::Type{},
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ token_types_ptr,
+ token_types_count,
+ {num_tokens, q_features},
+ {num_tokens, kv_features},
+ {num_tokens, kv_features},
+ cache_shape,
+ cache_shape,
+ {1},
+ {2});
+ return out;
+}
+
+// Bidirectional attention: image tokens attend to the full image group.
+// With uniform Q=[1,0,0,0], K=[1,0,0,0] (all dots=1), scale=1.0, softmax produces
+// uniform weights 1/N over all N attended positions. Output = mean(V[attended]).
+TEST(PagedAttentionRefTest, BidirectionalImageAttention_GroupTokensSameOutput) {
+ // 6 tokens: [text, image, image, image, text, text]
+ // Image tokens 1,2,3 form a group -> each attends to positions {0,1,2,3}.
+ // Text token 0 attends to {0}, text token 4 attends to {0..4}, text token 5 to {0..5}.
+ const std::size_t num_tokens = 6;
+ const std::size_t q_heads = 2;
+ const std::size_t kv_heads = 2;
+ const std::size_t head_size = 4;
+ const std::size_t q_features = q_heads * head_size;
+ const std::size_t kv_features = kv_heads * head_size;
+
+ std::vector<float> q(num_tokens * q_features, 0.f);
+ std::vector<float> k(num_tokens * kv_features, 0.f);
+ std::vector<float> v(num_tokens * kv_features, 0.f);
+
+ for (std::size_t i = 0; i < num_tokens; i++) {
+ q[i * q_features] = 1.0f;
+ k[i * kv_features] = 1.0f;
+ // First value feature = position+1, rest zeros
+ v[i * kv_features] = static_cast<float>(i + 1);
+ }
+
+ std::vector<int32_t> token_types = {0, 1, 1, 1, 0, 0};
+
+ auto out = run_pa_with_token_types(q, k, v, num_tokens, q_heads, kv_heads, head_size, token_types);
+
+ // All outputs should be finite
+ for (std::size_t i = 0; i < out.size(); i++) {
+ EXPECT_TRUE(std::isfinite(out[i])) << "output[" << i << "] is not finite";
+ }
+
+ // Image tokens 1,2,3: all attend to {0,1,2,3}, uniform weight -> mean = (1+2+3+4)/4 = 2.5
+ const float img_expected = 2.5f;
+ EXPECT_NEAR(out[1 * q_features], img_expected, 0.01f);
+ EXPECT_NEAR(out[2 * q_features], img_expected, 0.01f);
+ EXPECT_NEAR(out[3 * q_features], img_expected, 0.01f);
+
+ // Text token 0: attends to {0} -> output = v[0] = 1.0
+ EXPECT_NEAR(out[0 * q_features], 1.0f, 0.01f);
+
+ // Text token 4: causal, attends to {0,1,2,3,4} -> mean = (1+2+3+4+5)/5 = 3.0
+ EXPECT_NEAR(out[4 * q_features], 3.0f, 0.01f);
+
+ // Text token 5: causal, attends to {0..5} -> mean = (1+2+3+4+5+6)/6 = 3.5
+ EXPECT_NEAR(out[5 * q_features], 3.5f, 0.01f);
+}
+
+// Text tokens remain strictly causal even when token_type_ids is present.
+TEST(PagedAttentionRefTest, BidirectionalImageAttention_TextTokensCausal) {
+ // 5 tokens: [image, image, text, text, text]
+ // Image group: {0,1}. Text tokens: {2,3,4}.
+ const std::size_t num_tokens = 5;
+ const std::size_t q_heads = 1;
+ const std::size_t kv_heads = 1;
+ const std::size_t head_size = 4;
+ const std::size_t q_features = q_heads * head_size;
+ const std::size_t kv_features = kv_heads * head_size;
+
+ std::vector<float> q(num_tokens * q_features, 0.f);
+ std::vector<float> k(num_tokens * kv_features, 0.f);
+ std::vector<float> v(num_tokens * kv_features, 0.f);
+
+ for (std::size_t i = 0; i < num_tokens; i++) {
+ q[i * q_features] = 1.0f;
+ k[i * kv_features] = 1.0f;
+ v[i * kv_features] = static_cast<float>(i + 1);
+ }
+
+ std::vector<int32_t> token_types = {1, 1, 0, 0, 0};
+
+ auto out = run_pa_with_token_types(q, k, v, num_tokens, q_heads, kv_heads, head_size, token_types);
+
+ for (std::size_t i = 0; i < out.size(); i++) {
+ EXPECT_TRUE(std::isfinite(out[i])) << "output[" << i << "] is not finite";
+ }
+
+ // Image tokens 0,1: attend to {0,1} -> mean = (1+2)/2 = 1.5
+ EXPECT_NEAR(out[0 * q_features], 1.5f, 0.01f);
+ EXPECT_NEAR(out[1 * q_features], 1.5f, 0.01f);
+
+ // Text token 2: causal, attends to {0,1,2} -> mean = (1+2+3)/3 = 2.0
+ EXPECT_NEAR(out[2 * q_features], 2.0f, 0.01f);
+
+ // Text token 3: causal, attends to {0,1,2,3} -> mean = (1+2+3+4)/4 = 2.5
+ EXPECT_NEAR(out[3 * q_features], 2.5f, 0.01f);
+
+ // Text token 4: causal, attends to {0,1,2,3,4} -> mean = (1+2+3+4+5)/5 = 3.0
+ EXPECT_NEAR(out[4 * q_features], 3.0f, 0.01f);
+}
+
+// The window is applied relative to the extended ncausal, not the default causal position.
+TEST(PagedAttentionRefTest, BidirectionalImageAttention_SlidingWindow) {
+ // 8 tokens: [text, text, image, image, image, image, text, text]
+ // Image group: tokens 2,3,4,5 -> group end = 6 -> ncausal = 6.
+ // Sliding window = 3 -> start = max(0, 6-3) = 3, end = 5.
+ // All image tokens see KV positions {3,4,5}: uniform weight -> mean(v[3..5]).
+ const std::size_t num_tokens = 8;
+ const std::size_t q_heads = 1;
+ const std::size_t kv_heads = 1;
+ const std::size_t head_size = 4;
+ const std::size_t q_features = q_heads * head_size;
+ const std::size_t kv_features = kv_heads * head_size;
+
+ std::vector<float> q(num_tokens * q_features, 0.f);
+ std::vector<float> k(num_tokens * kv_features, 0.f);
+ std::vector<float> v(num_tokens * kv_features, 0.f);
+
+ for (std::size_t i = 0; i < num_tokens; i++) {
+ q[i * q_features] = 1.0f;
+ k[i * kv_features] = 1.0f;
+ v[i * kv_features] = static_cast<float>(i + 1) * 10.0f;
+ }
+
+ std::vector<int32_t> token_types = {0, 0, 1, 1, 1, 1, 0, 0};
+ int32_t sliding_window = 3;
+
+ auto out = run_pa_with_token_types(q, k, v, num_tokens, q_heads, kv_heads, head_size, token_types, sliding_window);
+
+ for (std::size_t i = 0; i < out.size(); i++) {
+ EXPECT_TRUE(std::isfinite(out[i])) << "output[" << i << "] is not finite";
+ }
+
+ // Sliding window start uses original causal_pos, pulled back to image_group_begin.
+ // Token 2: causal_pos=3, sw_start=0, group_begin=2, start=min(0,2)=0, ncausal=6. Sees {0..5}.
+ EXPECT_NEAR(out[2 * q_features], (10 + 20 + 30 + 40 + 50 + 60) / 6.f, 0.01f); // 35.0
+ // Token 3: causal_pos=4, sw_start=1, group_begin=2, start=min(1,2)=1, ncausal=6. Sees {1..5}.
+ EXPECT_NEAR(out[3 * q_features], (20 + 30 + 40 + 50 + 60) / 5.f, 0.01f); // 40.0
+ // Token 4: causal_pos=5, sw_start=2, group_begin=2, start=min(2,2)=2, ncausal=6. Sees {2..5}.
+ EXPECT_NEAR(out[4 * q_features], (30 + 40 + 50 + 60) / 4.f, 0.01f); // 45.0
+ // Token 5: causal_pos=6, sw_start=3, group_begin=2, start=min(3,2)=2, ncausal=6. Sees {2..5}.
+ EXPECT_NEAR(out[5 * q_features], (30 + 40 + 50 + 60) / 4.f, 0.01f); // 45.0
+
+ // Text token 0: causal_pos=1, SW=3 -> start=0. Sees {0}. Out = 10.
+ EXPECT_NEAR(out[0 * q_features], 10.0f, 0.01f);
+ // Text token 1: causal_pos=2, SW=3 -> start=0. Sees {0,1}. Out = 15.
+ EXPECT_NEAR(out[1 * q_features], 15.0f, 0.01f);
+ // Text token 6: causal_pos=7, SW=3 -> start=4. Sees {4,5,6}. Out = 60.
+ EXPECT_NEAR(out[6 * q_features], 60.0f, 0.01f);
+ // Text token 7: causal_pos=8, SW=3 -> start=5. Sees {5,6,7}. Out = 70.
+ EXPECT_NEAR(out[7 * q_features], 70.0f, 0.01f);
+}
+
+// Multiple image groups are handled independently.
+TEST(PagedAttentionRefTest, BidirectionalImageAttention_MultipleGroups) {
+ // 8 tokens: [image, image, text, image, image, image, text, text]
+ // Group 1: tokens {0,1} -> ncausal=2 for both. Group 2: tokens {3,4,5} -> ncausal=6.
+ const std::size_t num_tokens = 8;
+ const std::size_t q_heads = 1;
+ const std::size_t kv_heads = 1;
+ const std::size_t head_size = 4;
+ const std::size_t q_features = q_heads * head_size;
+ const std::size_t kv_features = kv_heads * head_size;
+
+ std::vector<float> q(num_tokens * q_features, 0.f);
+ std::vector<float> k(num_tokens * kv_features, 0.f);
+ std::vector<float> v(num_tokens * kv_features, 0.f);
+
+ for (std::size_t i = 0; i < num_tokens; i++) {
+ q[i * q_features] = 1.0f;
+ k[i * kv_features] = 1.0f;
+ v[i * kv_features] = static_cast<float>(i + 1);
+ }
+
+ std::vector<int32_t> token_types = {1, 1, 0, 1, 1, 1, 0, 0};
+
+ auto out = run_pa_with_token_types(q, k, v, num_tokens, q_heads, kv_heads, head_size, token_types);
+
+ for (std::size_t i = 0; i < out.size(); i++) {
+ EXPECT_TRUE(std::isfinite(out[i])) << "output[" << i << "] is not finite";
+ }
+
+ // Group 1 (tokens 0,1): both attend to {0,1} -> mean = (1+2)/2 = 1.5
+ EXPECT_NEAR(out[0 * q_features], 1.5f, 0.01f);
+ EXPECT_NEAR(out[1 * q_features], 1.5f, 0.01f);
+
+ // Text token 2: causal, attends to {0,1,2} -> mean = (1+2+3)/3 = 2.0
+ EXPECT_NEAR(out[2 * q_features], 2.0f, 0.01f);
+
+ // Group 2 (tokens 3,4,5): all attend to {0,1,2,3,4,5} -> mean = (1+2+3+4+5+6)/6 = 3.5
+ EXPECT_NEAR(out[3 * q_features], 3.5f, 0.01f);
+ EXPECT_NEAR(out[4 * q_features], 3.5f, 0.01f);
+ EXPECT_NEAR(out[5 * q_features], 3.5f, 0.01f);
+
+ // Text token 6: causal, attends to {0..6} -> mean = (1+2+3+4+5+6+7)/7 = 4.0
+ EXPECT_NEAR(out[6 * q_features], 4.0f, 0.01f);
+
+ // Text token 7: causal, attends to {0..7} -> mean = (1+..+8)/8 = 4.5
+ EXPECT_NEAR(out[7 * q_features], 4.5f, 0.01f);
+}
+
+// All-text token_type_ids is equivalent to standard causal behavior
+TEST(PagedAttentionRefTest, BidirectionalImageAttention_AllTextCausal) {
+ // 4 tokens, all text. Each causal: token i attends to {0..i}.
+ const std::size_t num_tokens = 4;
+ const std::size_t q_heads = 1;
+ const std::size_t kv_heads = 1;
+ const std::size_t head_size = 4;
+ const std::size_t q_features = q_heads * head_size;
+ const std::size_t kv_features = kv_heads * head_size;
+
+ std::vector<float> q(num_tokens * q_features, 0.f);
+ std::vector<float> k(num_tokens * kv_features, 0.f);
+ std::vector<float> v(num_tokens * kv_features, 0.f);
+
+ for (std::size_t i = 0; i < num_tokens; i++) {
+ q[i * q_features] = 1.0f;
+ k[i * kv_features] = 1.0f;
+ v[i * kv_features] = static_cast<float>(i + 1);
+ }
+
+ std::vector<int32_t> all_text = {0, 0, 0, 0};
+ auto out = run_pa_with_token_types(q, k, v, num_tokens, q_heads, kv_heads, head_size, all_text);
+
+ for (std::size_t i = 0; i < out.size(); i++) {
+ EXPECT_TRUE(std::isfinite(out[i])) << "output[" << i << "] is not finite";
+ }
+
+ // Token 0: sees {0} -> 1.0
+ EXPECT_NEAR(out[0 * q_features], 1.0f, 0.01f);
+ // Token 1: sees {0,1} -> (1+2)/2 = 1.5
+ EXPECT_NEAR(out[1 * q_features], 1.5f, 0.01f);
+ // Token 2: sees {0,1,2} -> (1+2+3)/3 = 2.0
+ EXPECT_NEAR(out[2 * q_features], 2.0f, 0.01f);
+ // Token 3: sees {0,1,2,3} -> (1+2+3+4)/4 = 2.5
+ EXPECT_NEAR(out[3 * q_features], 2.5f, 0.01f);
+}
+
+// Sliding window smaller than the group: group_begin pullback ensures later image tokens
+// still see the group beginning even when the sliding window would clip it away.
+TEST(PagedAttentionRefTest, BidirectionalImageAttention_GroupBeginPullback) {
+ // 6 tokens: [text, image, image, image, image, text]
+ // Image group: tokens {1,2,3,4}, sliding_window = 2.
+ const std::size_t num_tokens = 6;
+ const std::size_t q_heads = 1;
+ const std::size_t kv_heads = 1;
+ const std::size_t head_size = 4;
+ const std::size_t q_features = q_heads * head_size;
+ const std::size_t kv_features = kv_heads * head_size;
+
+ std::vector<float> q(num_tokens * q_features, 0.f);
+ std::vector<float> k(num_tokens * kv_features, 0.f);
+ std::vector<float> v(num_tokens * kv_features, 0.f);
+
+ for (std::size_t i = 0; i < num_tokens; i++) {
+ q[i * q_features] = 1.0f;
+ k[i * kv_features] = 1.0f;
+ v[i * kv_features] = static_cast<float>(i + 1) * 10.0f;
+ }
+
+ std::vector<int32_t> token_types = {0, 1, 1, 1, 1, 0};
+ int32_t sliding_window = 2;
+
+ auto out = run_pa_with_token_types(q, k, v, num_tokens, q_heads, kv_heads, head_size, token_types, sliding_window);
+
+ for (std::size_t i = 0; i < out.size(); i++) {
+ EXPECT_TRUE(std::isfinite(out[i])) << "output[" << i << "] is not finite";
+ }
+
+ // Token 0: text, causal_pos=1, SW=2, start=0. Sees {0}. Out = 10.
+ EXPECT_NEAR(out[0 * q_features], 10.0f, 0.01f);
+
+ // Token 1: causal_pos=2, sw_start=0, group_begin=1, start=min(0,1)=0, ncausal=5.
+ // Sees {0,1,2,3,4}. Mean = (10+20+30+40+50)/5 = 30.
+ EXPECT_NEAR(out[1 * q_features], 30.0f, 0.01f);
+
+ // Token 2: causal_pos=3, sw_start=1, group_begin=1, start=min(1,1)=1, ncausal=5.
+ // Sees {1,2,3,4}. Mean = (20+30+40+50)/4 = 35.
+ EXPECT_NEAR(out[2 * q_features], 35.0f, 0.01f);
+
+ // Token 3: causal_pos=4, sw_start=2, group_begin=1, start=min(2,1)=1, ncausal=5.
+ // Sees {1,2,3,4}. Mean = (20+30+40+50)/4 = 35.
+ EXPECT_NEAR(out[3 * q_features], 35.0f, 0.01f);
+
+ // Token 4: causal_pos=5, sw_start=3, group_begin=1, start=min(3,1)=1, ncausal=5.
+ // Sees {1,2,3,4}. Mean = (20+30+40+50)/4 = 35.
+ EXPECT_NEAR(out[4 * q_features], 35.0f, 0.01f);
+
+ // Token 5: text, causal_pos=6, SW=2, start=4. Sees {4,5}. Mean = (50+60)/2 = 55.
+ EXPECT_NEAR(out[5 * q_features], 55.0f, 0.01f);
+}
diff --git a/src/plugins/template/backend/ops/paged_attention.cpp b/src/plugins/template/backend/ops/paged_attention.cpp
index 338690bcb12..dfeb01bc287 100644
--- a/src/plugins/template/backend/ops/paged_attention.cpp
+++ b/src/plugins/template/backend/ops/paged_attention.cpp
@@ -65,6 +65,8 @@ bool evaluate(const ov::op::PagedAttentionExtension* pa_op,
const int32_t* arkv_evict_ptr = inputs[22].get_size() > 0 ? inputs[22].data<int32_t>() : nullptr;
const int32_t* arkv_indices_ptr = inputs[23].get_size() > 0 ? inputs[23].data<int32_t>() : nullptr;
const int32_t* arkv_begins_ptr = inputs[24].get_size() > 0 ? inputs[24].data<int32_t>() : nullptr;
+ const int32_t* token_type_ids_ptr = inputs[25].get_size() > 0 ? inputs[25].data<int32_t>() : nullptr;
+ const std::size_t token_type_ids_count = inputs[25].get_size();
ov::reference::paged_attention<T>(node_key,
cache_manager,
@@ -108,6 +110,8 @@ bool evaluate(const ov::op::PagedAttentionExtension* pa_op,
arkv_evict_ptr, // adaptive_rkv_evictable_sizes
arkv_indices_ptr, // adaptive_rkv_diversity_block_set_indices
arkv_begins_ptr, // adaptive_rkv_diversity_block_set_indices_begins
+ token_type_ids_ptr, // token_type_ids
+ token_type_ids_count, // token_type_ids_count
inputs[0].get_shape(),
inputs[1].get_shape(),
inputs[2].get_shape(),