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
Keep xattention_threshold in FP32 to avoid boundary instability cause (#34732)
Wang Wangwang92ceda65d0a9
diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe1.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe1.hppindex 715da56c06a..84a9c3c39fc 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe1.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe1.hpp@@ -1,7 +1,7 @@// Copyright (C) 2018-2026 Intel Corporation// SPDX-License-Identifier: Apache-2.0//-+// #define CMPA_DEBUG_ALL_MASKED // Enable verbose cm_printf logging (opt-in; do not enable by default)#ifndef CM_HAS_LSC_UNTYPED_2D#if KV_CACHE_COMPRESSION@@ -286,6 +286,9 @@ void pa_lsc_u8(apply_causal_mask<1>(St);} else if (causal_left < 0) {St = -3.4e38f;+ } else if (causal_left < kv_step) {+ for (int p = causal_left; p < kv_step; p++)+ St[p] = -3.4e38f;}} else {if (causal_left == 0) {@@ -304,11 +307,10 @@ void pa_lsc_u8(}}causal_left -= kv_step;- } else {- int kv_tokens = kv_stop - kv_pos;- // LSC ensures no overflow-access, but mask off k-tails attn-score is still required- for(int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;}+ int kv_tokens = kv_stop - kv_pos;+ // LSC ensures no overflow-access, but mask off k-tails attn-score is still required+ for(int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;auto max_comp = online_softmax_update(St, cur_max, cur_sum);matrix<half, REG_N, REG_K> P;@@ -528,6 +530,10 @@ void pa_kernel_lsc_prefetch_f16(apply_causal_mask<1>(St);} else if (causal_left < 0) {St = -3.4e38f;+ } else if (causal_left < kv_step) {+ for (int p = causal_left; p < kv_step; p++)+ St[p] = -3.4e38f;+ }}} else {if (causal_left == 0) {@@ -546,12 +552,10 @@ void pa_kernel_lsc_prefetch_f16(}}causal_left -= kv_step;- } else {- int kv_tokens = kv_stop - kv_pos;- // LSC ensures no overflow-access, but mask off k-tails attn-score is still required- for(int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;}-+ int kv_tokens = kv_stop - kv_pos;+ // LSC ensures no overflow-access, but mask off k-tails attn-score is still required+ for(int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;// show(St);auto max_comp = online_softmax_update(St, cur_max, cur_sum);diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe2.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe2.hppindex 7d829e4f0be..33a129d9c2f 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe2.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe2.hpp@@ -338,13 +338,13 @@ void pa_lsc_u8(apply_causal_mask<1>(St);} else if (causal_left < 0) {St = -3.4e38f;+ } else if (causal_left < kv_step) {+ for (int p = causal_left; p < kv_step; p++) St[p] = -3.4e38f;}causal_left -= kv_step;- } else {- int kv_tokens = kv_stop - kv_pos;- for (int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;}-+ int kv_tokens = kv_stop - kv_pos;+ for (int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;auto max_comp = online_softmax_update(St, cur_max, cur_sum);matrix<half, REG_N, REG_K> P;@@ -529,13 +529,13 @@ void pa_lsc_u8(apply_causal_mask<1>(St);} else if (causal_left < 0) {St = -3.4e38f;+ } else if (causal_left < kv_step) {+ for (int p = causal_left; p < kv_step; p++) St[p] = -3.4e38f;}causal_left -= kv_step;- } else {- int kv_tokens = kv_stop - kv_pos;- for (int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;}-+ int kv_tokens = kv_stop - kv_pos;+ for (int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;auto max_comp = online_softmax_update(St, cur_max, cur_sum);matrix<half, REG_N, REG_K> P;@@ -746,14 +746,14 @@ void pa_kernel_lsc_prefetch_f16(apply_causal_mask<1>(St);} else if (causal_left < 0) {St = -3.4e38f;+ } else if (causal_left < kv_step) {+ for (int p = causal_left; p < kv_step; p++) St[p] = -3.4e38f;}causal_left -= kv_step;- } else {- int kv_tokens = kv_stop - kv_pos;- // LSC ensures no overflow-access, but mask off k-tails attn-score is still required- for(int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;}-+ int kv_tokens = kv_stop - kv_pos;+ // LSC ensures no overflow-access, but mask off k-tails attn-score is still required+ for(int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;//show(St);auto max_comp = online_softmax_update(St, cur_max, cur_sum);@@ -894,14 +894,14 @@ void pa_kernel_lsc_prefetch_f16(apply_causal_mask<1>(St);} else if (causal_left < 0) {St = -3.4e38f;+ } else if (causal_left < kv_step) {+ for (int p = causal_left; p < kv_step; p++) St[p] = -3.4e38f;}causal_left -= kv_step;- } else {- int kv_tokens = kv_stop - kv_pos;- // LSC ensures no overflow-access, but mask off k-tails attn-score is still required- for(int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;}-+ int kv_tokens = kv_stop - kv_pos;+ // LSC ensures no overflow-access, but mask off k-tails attn-score is still required+ for(int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;//show(St);auto max_comp = online_softmax_update(St, cur_max, cur_sum);diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.cpp b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.cppindex 14ba0c04bcd..8d8a3d0ae5b 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.cpp@@ -84,13 +84,26 @@ float get_xattn_thresh(const kernel_impl_params& params, const size_t seq_idx) {OPENVINO_THROW("XAttention threshold input is required at index ", static_cast<size_t>(PagedAttentionInputIdx::XATTENTION_THRESHOLD));}- mem_lock<float16, mem_lock_type::read> lock(it->second, *params.strm); // converted- if (seq_idx >= lock.size()) {- OPENVINO_THROW("XAttention threshold input index out of range: seq_idx=", seq_idx, ", input_size=", lock.size());+ const auto threshold_mem = it->second;+ const auto dt = threshold_mem->get_layout().data_type;++ if (dt == data_types::f16) {+ mem_lock<float16, mem_lock_type::read> lock(threshold_mem, *params.strm);+ if (seq_idx >= lock.size()) {+ OPENVINO_THROW("XAttention threshold input index out of range: seq_idx=", seq_idx, ", input_size=", lock.size());+ }+ return static_cast<float>(lock[seq_idx]);+ }++ if (dt == data_types::f32) {+ mem_lock<float, mem_lock_type::read> lock(threshold_mem, *params.strm);+ if (seq_idx >= lock.size()) {+ OPENVINO_THROW("XAttention threshold input index out of range: seq_idx=", seq_idx, ", input_size=", lock.size());+ }+ return lock[seq_idx];}- const auto thresh = static_cast<float>(lock[seq_idx]);- return thresh;+ OPENVINO_THROW("Unsupported xattention_threshold data type");}// Bypass xattn stages in the following conditions -diff --git a/src/plugins/intel_gpu/src/plugin/transformations/keep_xattention_threshold_precision.cpp b/src/plugins/intel_gpu/src/plugin/transformations/keep_xattention_threshold_precision.cppnew file mode 100644index 00000000000..4516f04a8b7--- /dev/null+++ b/src/plugins/intel_gpu/src/plugin/transformations/keep_xattention_threshold_precision.cpp@@ -0,0 +1,35 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "keep_xattention_threshold_precision.hpp"++#include "intel_gpu/primitives/paged_attention.hpp"+#include "openvino/op/paged_attention.hpp"+#include "openvino/op/util/precision_sensitive_attribute.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"++namespace ov::intel_gpu {++KeepXAttentionThresholdPrecision::KeepXAttentionThresholdPrecision() {+ auto pa_m = ov::pass::pattern::wrap_type<ov::op::PagedAttentionExtension>();++ ov::matcher_pass_callback callback = [=](ov::pass::pattern::Matcher& m) {+ auto pa = ov::as_type_ptr<ov::op::PagedAttentionExtension>(m.get_match_root());+ if (!pa)+ return false;++ if (transformation_callback(pa))+ return false;++ constexpr size_t thr_idx = cldnn::paged_attention::PagedAttentionInputIdx::XATTENTION_THRESHOLD;+ ov::mark_as_precision_sensitive(pa->input(thr_idx));+ return true;+ };++ auto matcher = std::make_shared<ov::pass::pattern::Matcher>(pa_m, "KeepXAttentionThresholdPrecision");+ register_matcher(matcher, callback);+}++} // namespace ov::intel_gpu+diff --git a/src/plugins/intel_gpu/src/plugin/transformations/keep_xattention_threshold_precision.hpp b/src/plugins/intel_gpu/src/plugin/transformations/keep_xattention_threshold_precision.hppnew file mode 100644index 00000000000..053c58bb53a--- /dev/null+++ b/src/plugins/intel_gpu/src/plugin/transformations/keep_xattention_threshold_precision.hpp@@ -0,0 +1,20 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "openvino/pass/matcher_pass.hpp"++namespace ov::intel_gpu {++// Keeps xattention threshold input in fp32 by marking it as precision-sensitive+// before ConvertPrecision pass. This avoids down-conversion to fp16 which can+// make thresholding unstable at certain boundary values.+class KeepXAttentionThresholdPrecision : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("ov::intel_gpu::KeepXAttentionThresholdPrecision");+ KeepXAttentionThresholdPrecision();+};++} // namespace ov::intel_gpudiff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cppindex a661bc13a17..9d0e6c293b9 100644--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp@@ -91,6 +91,7 @@#include "plugin/transformations/increase_position_ids_precision.hpp"#include "plugin/transformations/indirect_kv_cache.hpp"#include "plugin/transformations/keep_moe_3gemm_const_precision.hpp"+#include "plugin/transformations/keep_xattention_threshold_precision.hpp"#include "plugin/transformations/kv_cache_compression.hpp"#include "plugin/transformations/kv_cache_fusion.hpp"#include "plugin/transformations/lora_horizontal_fusion.hpp"@@ -590,6 +591,8 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {manager.register_pass<ov::pass::KeepDequantizationPrecision>(ov::element::TypeVector{ov::element::i32, ov::element::u32, ov::element::u16}, add_precision_sensitive_convert);+ // Keep xattention threshold in fp32 to avoid boundary issues caused by fp16 quantization.+ manager.register_pass<ov::intel_gpu::KeepXAttentionThresholdPrecision>();manager.register_pass<ov::pass::ConvertPrecision>(fp_convert_precision_map,empty_fuse_map,diff --git a/src/plugins/intel_gpu/tests/unit/transformations/keep_xattention_threshold_precision_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/keep_xattention_threshold_precision_test.cppnew file mode 100644index 00000000000..c49d53c9aa1--- /dev/null+++ b/src/plugins/intel_gpu/tests/unit/transformations/keep_xattention_threshold_precision_test.cpp@@ -0,0 +1,121 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "plugin/transformations/keep_xattention_threshold_precision.hpp"++#include <gtest/gtest.h>++#include "common_test_utils/ov_test_utils.hpp"+#include "intel_gpu/primitives/paged_attention.hpp"+#include "openvino/core/model.hpp"+#include "openvino/op/paged_attention.hpp"+#include "openvino/op/parameter.hpp"+#include "openvino/op/result.hpp"+#include "openvino/op/util/precision_sensitive_attribute.hpp"++using namespace testing;+using namespace ov::intel_gpu;++namespace {++using PAExt = ov::op::PagedAttentionExtension;++std::shared_ptr<ov::Model> make_pa_model_with_threshold_et(const ov::element::Type& thr_et) {+ // Build a minimal model containing PagedAttentionExtension with valid 28 inputs.+ // We keep shapes mostly dynamic but MUST satisfy rank/type checks in+ // ov::op::PagedAttentionExtension::validate_and_infer_types().++ const size_t thr_idx = cldnn::paged_attention::PagedAttentionInputIdx::XATTENTION_THRESHOLD;++ ov::ParameterVector params(28);+ // 0..4: query/key/value/key_cache/value_cache+ params[0] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape::dynamic(2));+ params[1] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape::dynamic(2));+ params[2] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape::dynamic(2));+ params[3] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape::dynamic());+ params[4] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape::dynamic());++ // 5..8: i32 vectors+ for (size_t i = 5; i <= 8; i++)+ params[i] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(1));++ // 9: scale scalar real+ params[9] = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape::dynamic(0));+ // 10: sliding_window scalar i32+ params[10] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(0));+ // 11: alibi_slopes rank1 real+ params[11] = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape::dynamic(1));+ // 12: max_context_len scalar i32+ params[12] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(0));+ // 13: score_aggregation_window rank0/1 i32+ params[13] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(0));++ // 14: rotated_block_indices rank1 i32+ params[14] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(1));+ // 15: rotation_deltas rank1/2 i32+ params[15] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(1));+ // 16: rotation_trig_lut rank1/2 f16/f32+ params[16] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape::dynamic(1));++ // 17: xattention_threshold rank1 f16/f32 (or other type in negative test)+ params[thr_idx] = std::make_shared<ov::op::v0::Parameter>(thr_et, ov::PartialShape::dynamic(1));++ // 18..19: scalars i32+ params[18] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(0));+ params[19] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(0));++ // 20: sinks rank1/4 any type (use f16)+ params[20] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape::dynamic(1));++ // 21: scalar i32+ params[21] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(0));+ // 22..25: vectors/matrices i32+ params[22] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(1));+ params[23] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(1));+ params[24] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(1));+ params[25] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(1));+ // 26..27: qq_bias inputs+ params[26] = std::make_shared<ov::op::v0::Parameter>(ov::element::u8, ov::PartialShape::dynamic(1));+ params[27] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape::dynamic(1));++ ov::OutputVector inputs;+ inputs.reserve(params.size());+ for (const auto& p : params)+ inputs.emplace_back(p);++ auto pa = std::make_shared<PAExt>(inputs);+ auto res = std::make_shared<ov::op::v0::Result>(pa);+ return std::make_shared<ov::Model>(ov::ResultVector{res}, params);+}++std::shared_ptr<PAExt> get_pa_ext(const std::shared_ptr<ov::Model>& model) {+ return ov::as_type_ptr<PAExt>(model->get_results().at(0)->input_value(0).get_node_shared_ptr());+}++std::shared_ptr<ov::Model> make_ref_pa_model_with_precision_sensitive_threshold(const ov::element::Type& thr_et) {+ auto model = make_pa_model_with_threshold_et(thr_et);+ const size_t thr_idx = cldnn::paged_attention::PagedAttentionInputIdx::XATTENTION_THRESHOLD;+ auto pa = get_pa_ext(model);+ OPENVINO_ASSERT(pa != nullptr);+ ov::mark_as_precision_sensitive(pa->input(thr_idx));+ return model;+}++} // namespace++TEST_F(TransformationTestsF, KeepXAttentionThresholdPrecisionMarksF32Threshold) {+ model = make_pa_model_with_threshold_et(ov::element::f32);+ model_ref = make_ref_pa_model_with_precision_sensitive_threshold(ov::element::f32);++ manager.register_pass<KeepXAttentionThresholdPrecision>();+}++TEST_F(TransformationTestsF, KeepXAttentionThresholdPrecisionMarksF16Threshold) {+ // PagedAttentionExtension validation allows only f16/f32 on xattention_threshold port,+ // so only the valid real-type cases can be covered here.+ model = make_pa_model_with_threshold_et(ov::element::f16);+ model_ref = make_ref_pa_model_with_precision_sensitive_threshold(ov::element::f16);++ manager.register_pass<KeepXAttentionThresholdPrecision>();+}