OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Enable sdpa_micro in PA MIXED stage with token_type_ids (#37616)
Jade Cho388ff20ee67e
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cppindex b6e517d174b..622e5127094 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cpp@@ -1382,11 +1382,15 @@ public:return false;}+ // MIXED may use micro SDPA regardless of token_type_ids: bidirectional masking is implemented+ // only in the PREFILL kernels, and in MIXED neither micro SDPA nor paged_attention_opt.cl consumes token_type_ids.+ // TODO: implement bidirectional attention for MIXED with token_type_idsbool can_use_micro_sdpa_for(const kernel_impl_params& params, const PagedAttentionStage& stage) const {- if (!supports_micro_sdpa(params) || !valid_micro_stage(stage))- return false;- const auto desc = params.typed_desc<paged_attention>();- return !desc->has_token_type_ids || stage == PagedAttentionStage::PREFILL;+ const auto can_use_micro_sdpa = supports_micro_sdpa(params) && valid_micro_stage(stage);+ GPU_DEBUG_TRACE_DETAIL << "can_use_micro_sdpa_for: stage = " << static_cast<size_t>(stage)+ << ", token_type_ids = " << params.get_input_layout(PagedAttentionInputIdx::TOKEN_TYPE_IDS).to_short_string()+ << ", can_use_micro_sdpa = " << can_use_micro_sdpa << std::endl;+ return can_use_micro_sdpa;}bool supports_micro_sdpa(const kernel_impl_params& params) const {diff --git a/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp b/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cppindex 94c2717772f..60e9ecf7293 100644--- a/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp@@ -353,7 +353,7 @@ TEST(update_shape_test, max_context_len_shapeof_subgraph) {ASSERT_EQ(broadcast_shape, ov::Shape{8});}-TEST(update_shape_test, paged_attention_mixed_stage_token_type_ids_disables_micro_layout) {+TEST(update_shape_test, paged_attention_mixed_stage_token_type_ids_buffer_layout) {tests::random_generator rg(GET_SUITE_NAME);auto& engine = get_test_engine();@@ -570,6 +570,10 @@ TEST(update_shape_test, paged_attention_mixed_stage_token_type_ids_disables_micrauto pa_inst = network.get_primitive("paged_attention");const auto& intermediate_mems = pa_inst->get_intermediates_memories();- ASSERT_EQ(intermediate_mems.size(), 7u);++ // Allocation-time and execution-time micro/non-micro decisions must agree; a mismatch shows up+ // as the wrong buffer count. token_type_ids no longer forces the non-micro path in MIXED.+ const bool micro_layout = engine.get_device_info().supports_immad;+ ASSERT_EQ(intermediate_mems.size(), micro_layout ? 4u : 7u);}} // update_shape_testdiff --git a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_token_type_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_token_type_gpu_test.cppindex cf2133d2b8e..268e16441f5 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_token_type_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_token_type_gpu_test.cpp@@ -151,4 +151,122 @@ INSTANTIATE_TEST_SUITE_P(smoke_paged_attention_token_type_micro_sdpa_prefill,paged_attention_token_type_micro_sdpa_prefill_test,::testing::ValuesIn(make_micro_sdpa_prefill_test_params()),get_micro_sdpa_prefill_test_name);++// Micro SDPA in the MIXED stage while token_type_ids is present. MIXED used to fall back to+// paged_attention_opt__multi_tokens, which carries no token_type_ids handling of its own, so the+// fallback only ever cost performance.+//+// The PREFILL golden data is replayed as a chunked prefill:+//+// |<----- past_len ----->|<---- num_tokens ---->|+// k, v | preloaded into cache | passed as input |+// q | - | passed as input |+// golden | - | compared |+//+// past_len is picked so the query chunk holds text tokens only. A bidirectional pair needs both the+// query and the key to be image tokens, so a text-only chunk is masked purely causally - which is+// what both MIXED kernels produce, neither of them implementing token_type_ids.+class paged_attention_token_type_micro_sdpa_mixed_test : public paged_attention_token_type_test {+public:+ void apply_mixed_test_data(PagedAttentionManager& pam, const paged_attention_token_type_test_params& p, const test::TestData& data) {+ ASSERT_EQ(p.subsequences.size(), 1);++ const size_t past_len = static_cast<size_t>(p.subsequences[0].past_len);+ const size_t num_tokens = static_cast<size_t>(p.subsequences[0].num_tokens);+ const size_t seq_len = data.tokenTypes.size();+ const size_t hidden_dim = static_cast<size_t>(p.num_heads) * static_cast<size_t>(p.k_head_size);++ ASSERT_GT(past_len, 0u);+ ASSERT_EQ(past_len + num_tokens, seq_len);+ ASSERT_EQ(data.qData.size(), seq_len * hidden_dim);+ ASSERT_EQ(data.kData.size(), seq_len * hidden_dim);+ ASSERT_EQ(data.vData.size(), seq_len * hidden_dim);+ ASSERT_EQ(data.expectedOutput.size(), seq_len * hidden_dim);++ // Key/Value cover the whole sequence: PagedAttentionManager copies the leading past_len+ // tokens into the KV cache and submits the rest through the key/value inputs.+ pam.key_data = {to_float16(data.kData)};+ pam.value_data = {to_float16(data.vData)};++ // Query only covers the scheduled chunk.+ const auto query_data = to_float16(data.qData);+ pam.query_data = {std::vector<ov::float16>(query_data.begin() + past_len * hidden_dim, query_data.end())};++ pam.token_type_ids.assign(data.tokenTypes.begin(), data.tokenTypes.end());+ }+};++TEST_P(paged_attention_token_type_micro_sdpa_mixed_test, mixed_stage) {+ const auto& device_info = tests::get_test_engine().get_device_info();+ if (!device_info.supports_immad || device_info.arch < cldnn::gpu_arch::xe_hpg)+ GTEST_SKIP() << "Micro SDPA requires DPAS/XMX support";+ if (device_info.arch == cldnn::gpu_arch::xe3p)+ GTEST_SKIP() << "Micro SDPA is disabled on xe3p";++ auto p = GetParam();++ ASSERT_TRUE(this->pam.has_value());+ auto& pam = *this->pam;++ apply_mixed_test_data(pam, p, p.token_type_test_data);+ auto result = run_gpu_inference(pam, p);++ auto pa_inst = result.network->get_primitive("paged_attention");+ ASSERT_NE(pa_inst, nullptr);+ auto* impl = pa_inst->get_impl();+ ASSERT_NE(impl, nullptr);+ const auto kernel_entries = impl->get_kernels_dump_info(*pa_inst->get_impl_params()).get_entries();+ EXPECT_NE(kernel_entries.find("sdpa_micro"), std::string::npos) << "Expected micro SDPA kernel for MIXED with token_type_ids, got: " << kernel_entries;+ EXPECT_EQ(kernel_entries.find("paged_attention_opt__multi_tokens"), std::string::npos) << "MIXED fell back to the partition kernel: " << kernel_entries;++ // The golden data covers the whole sequence, but only the scheduled chunk is produced here.+ const size_t hidden_dim = static_cast<size_t>(p.num_heads) * static_cast<size_t>(p.v_head_size);+ const size_t cached_values = static_cast<size_t>(p.subsequences[0].past_len) * hidden_dim;+ const auto& golden_output = p.token_type_test_data.expectedOutput;+ const std::vector<float> expected_output(golden_output.begin() + cached_values, golden_output.end());++ cldnn::memory::ptr output_data_mem = result.outputs.at("output_data").get_memory();+ compare_token_type_output(output_data_mem, expected_output);+}++// Picks a split that leaves only text tokens in the scheduled chunk, and returns 0 when no usable+// split exists - either every token is an image token, or the tail would be short enough to be+// classified as GENERATE instead of MIXED.+static int find_text_only_mixed_split(const test::TestData& data) {+ const int seq_len = static_cast<int>(data.tokenTypes.size());+ int last_image_token = -1;+ for (int i = 0; i < seq_len; i++) {+ if (data.tokenTypes[i] == 1)+ last_image_token = i;+ }++ const int past_len = std::max(last_image_token + 1, seq_len / 2);+ const bool usable = past_len > 0 && seq_len - past_len >= 2;+ return usable ? past_len : 0;+}++static std::vector<paged_attention_token_type_test_params> make_micro_sdpa_mixed_test_params() {+ std::vector<paged_attention_token_type_test_params> params;+ for (const auto& data : test::PagedAttentionTokenTypeTestData::GetTestData()) {+ const int past_len = find_text_only_mixed_split(data);+ if (past_len == 0)+ continue;++ auto p = make_token_type_test_param(data, ENABLE_FA_V2);+ p.subsequences = {{static_cast<int>(data.tokenTypes.size()) - past_len, past_len}};+ params.push_back(p);+ }+ return params;+}++static std::string get_micro_sdpa_mixed_test_name(const testing::TestParamInfo<paged_attention_token_type_test_params>& obj) {+ const auto& p = obj.param;+ return p.token_type_test_data.name + "_SW" + std::to_string(p.sliding_window_size) + "_Past" + std::to_string(p.subsequences[0].past_len) ++ "_MicroSDPA_Mixed";+}++INSTANTIATE_TEST_SUITE_P(smoke_paged_attention_token_type_micro_sdpa_mixed,+ paged_attention_token_type_micro_sdpa_mixed_test,+ ::testing::ValuesIn(make_micro_sdpa_mixed_test_params()),+ get_micro_sdpa_mixed_test_name);#endif