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] Fix GroupQueryAttention sliding-window-cache present-cache corruption under dynamic shapes (#37659)
Ankit Maheshkard99f9d7139db
diff --git a/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp b/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cppindex 571b91164e4..c08cd79cd3f 100644--- a/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp+++ b/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp@@ -423,7 +423,10 @@ std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::windowed_cconst auto reclaimed = register_new_node<v1::Multiply>(blocks, gap);const auto evicted = register_new_node<v1::Subtract>(seqlen_scalar, reclaimed);const auto overflowed = register_new_node<v1::Greater>(seqlen_scalar, capacity_scalar);- return register_new_node<v1::Select>(overflowed, evicted, seqlen_scalar);+ const auto end = register_new_node<v1::Select>(overflowed, evicted, seqlen_scalar);+ // Data-dependent index (feeds Slice/Gather/ScatterUpdate bounds); GPU protects it from fusion.+ end->get_rt_info()["gpu_shape_of_subgraph_root"] = true;+ return end;}std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::make_attention_mask(diff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cppindex accf738b543..00c44482e43 100644--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp@@ -5998,20 +5998,6 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache) {// so end_after=3 and the in-place kept=end_after-S=-3 is invalid. Dynamic query seqlen routes into the// staging branch (a static S>1 is rejected at import). Reference is a NumPy port of the ORT windowed attention.OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache_staging) {- // On GPU the present cache comes back zeroed (attention output is correct); verified numerically exact on- // CPU/INTERPRETER. The failure only reproduces in the full staging graph - the isolated present-write- // subgraph (windowed_cache_end + Range + Slice + ScatterUpdate) lowers correctly on GPU on its own - so it- // is a GPU-plugin issue in the assembled dynamic graph, not a decomposition bug. Because the staging branch- // is chosen from the query's declared shape rather than its runtime length, a DYNAMIC-shape decode step- // (S==1 every call - see onnx_model_gqa_sliding_window_cache_bias below) hits this too, not only a genuine- // multi-token overflow: this regresses vs. the pre-staging in-place path, which GPU lowered correctly even- // under a dynamic shape (onnx_model_gqa_sliding_window_cache_bias_static exercises that same math via the- // statically-shaped decode path and passes on GPU). Parking this for the GPU plugin team / a follow-up PR.- if (s_device == ov::test::utils::DEVICE_GPU) {- GTEST_SKIP() << "GPU zeroes the staging present cache in the full graph (incl. plain dynamic-shape "- "decode, not just multi-token overflow); verified correct on CPU/INTERPRETER. Parking "- "this for the GPU plugin team / a follow-up PR.";- }auto model = convert_model("com.microsoft/gqa_sliding_window_cache.onnx");// Query seqlen stays dynamic (static S>1 is rejected); the concrete S=6 is supplied at inference.model->reshape({{"query", ov::PartialShape{1, -1, 64}},@@ -6125,13 +6111,6 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache_staging) {// the FE conversion-time path directly. The decomposition picks the staging vs. in-place branch from the// runtime past/total length, not static-ness of S, so results must be byte-identical to the dynamic version.OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache_static_staging) {- // Same pre-existing GPU present-cache-zeroing bug as onnx_model_gqa_sliding_window_cache_staging above- // (this takes the identical staging branch) - fixed on the gqa_fixes branch / GPU PR #37659, not yet- // present here. Drop this skip once that fix lands on this branch.- if (s_device == ov::test::utils::DEVICE_GPU) {- GTEST_SKIP() << "GPU zeroes the staging present cache in the full graph; verified correct on "- "CPU/INTERPRETER. Fixed on gqa_fixes / GPU PR #37659, not yet on this branch.";- }// INTERPRETER returns present_key/present_value with shape [0] instead of [1,1,4,16] on this fully-static// staging graph, while CPU matches expected values exactly - a template-backend constant-folding quirk in// this backend, not a decomposition bug. Needs follow-up; not blocking.@@ -6251,10 +6230,6 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache_static_stagin// C=4 / W=2 crossing-eviction step as above; verifies the quant round-trip is preserved through staging.// Reference from a NumPy port of the ORT windowed attention with matching symmetric i8 quant (scale 0.05).OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache_staging_i8) {- if (s_device == ov::test::utils::DEVICE_GPU) {- GTEST_SKIP() << "GPU zeroes the staging present cache in the full graph; verified correct on "- "CPU/INTERPRETER. Parking this for the GPU plugin team / a follow-up PR.";- }auto model = convert_model("com.microsoft/gqa_i8kv_swc.onnx");model->reshape({{"query", ov::PartialShape{1, -1, 64}},{"past_key", ov::PartialShape{1, 1, 4, 16}},@@ -6360,12 +6335,6 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache_staging_i8) {// evicted, resident=2, so the bias column offset is 3 (nonzero) - the case that would misalign without the// fix. Reference from a NumPy port of the ORT windowed attention with the bias added on top of the mask.OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache_bias) {- if (s_device == ov::test::utils::DEVICE_GPU) {- GTEST_SKIP() << "GPU zeroes the staging present cache for this dynamic-shape decode step (S==1, not "- "an overflow - see onnx_model_gqa_sliding_window_cache_bias_static for the equivalent "- "statically-shaped decode, which passes on GPU); verified correct on CPU/INTERPRETER. "- "Parking this for the GPU plugin team / a follow-up PR.";- }auto model = convert_model("com.microsoft/gqa_swc_bias.onnx");model->reshape({{"query", ov::PartialShape{1, -1, 64}},{"past_key", ov::PartialShape{1, 1, 4, 16}},@@ -6550,10 +6519,6 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache_bias_static)// bias is sliced from offset 3. Covers the bias-alignment offset on the staging branch (distinct from the// decode branch above). Reference from a NumPy port of the ORT windowed attention.OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_sliding_window_cache_bias_staging) {- if (s_device == ov::test::utils::DEVICE_GPU) {- GTEST_SKIP() << "GPU zeroes the staging present cache in the full graph; verified correct on "- "CPU/INTERPRETER. Parking this for the GPU plugin team / a follow-up PR.";- }auto model = convert_model("com.microsoft/gqa_swc_bias.onnx");model->reshape({{"query", ov::PartialShape{1, -1, 64}},{"past_key", ov::PartialShape{1, 1, 4, 16}},diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/primitive.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/primitive.hppindex 50018300e74..380053bb65e 100644--- a/src/plugins/intel_gpu/include/intel_gpu/primitives/primitive.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/primitive.hpp@@ -219,6 +219,9 @@ public:/// @brief Type name of original ov operation.std::string origin_op_type_name;+ /// @brief Set via "gpu_shape_of_subgraph_root" rt_info; protected from fusion like shape_of.+ bool is_shape_of_subgraph_root = false;+/// @brief Requested output padding.std::vector<padding> output_paddings;@@ -258,6 +261,7 @@ public:}ob << input;ob << num_outputs;+ ob << is_shape_of_subgraph_root;}virtual void load(BinaryInputBuffer& ib) {@@ -284,6 +288,7 @@ public:}ib >> input;ib >> num_outputs;+ ib >> is_shape_of_subgraph_root;}virtual padding get_output_padding(size_t idx) const {diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/mark_shape_of_subgraphs.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/mark_shape_of_subgraphs.cppindex 5c4061e947e..6fd31a5baf0 100644--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/mark_shape_of_subgraphs.cpp+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/mark_shape_of_subgraphs.cpp@@ -25,6 +25,11 @@ static bool is_shape_of_subgraph_root(program_node& node) {return true;}+ // Marked via "gpu_shape_of_subgraph_root" rt_info (e.g. GQA's windowed-cache eviction bound).+ if (node.get_primitive()->is_shape_of_subgraph_root) {+ return true;+ }+// Allow input_layout to be the root of the shape_of subgraph if it's 'max_context_len'// input of PagedAttention, which can be used as a shape calculation flow source in some// models like Qwen and Qwen2diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cppindex 3eaff8955b5..c0300efdf85 100644--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp@@ -365,6 +365,14 @@ void primitive_inst::update_shape() {}}+ // Unlike shape_of, this root's dependencies are ordinary data-typed scalars whose layout never+ // changes between iterations (only their value does), so the layout diff above can never observe+ // a change here. Treat every run as changed: it is the only safe signal available, and this node+ // is a cheap scalar computation, so always recomputing it is not a meaningful perf concern.+ if (get_node().get_primitive()->is_shape_of_subgraph_root) {+ input_shape_changed = true;+ }+set_flag(ExecutionFlags::SHAPE_CHANGED, input_shape_changed);// We assume that tensor ranks are static, thus shape_of doesn't need to update anything even if input shape is dynamicdiff --git a/src/plugins/intel_gpu/src/graph/program_node.cpp b/src/plugins/intel_gpu/src/graph/program_node.cppindex fadde7d43c3..17a94e8ebfc 100644--- a/src/plugins/intel_gpu/src/graph/program_node.cpp+++ b/src/plugins/intel_gpu/src/graph/program_node.cpp@@ -671,7 +671,8 @@ void program_node::select_preferred_formats(impl_types impl_type) {}void program_node::add_dependant_shape_of_node(const program_node* node) {- OPENVINO_ASSERT(node->is_type<shape_of>() || node->is_type<input_layout>(), "[GPU] Expected node type is shape_of");+ OPENVINO_ASSERT(node->is_type<shape_of>() || node->is_type<input_layout>() || node->get_primitive()->is_shape_of_subgraph_root,+ "[GPU] Expected node type is shape_of");dependant_shape_of_nodes.insert(node);}diff --git a/src/plugins/intel_gpu/src/plugin/program_builder.cpp b/src/plugins/intel_gpu/src/plugin/program_builder.cppindex 79d3e1e236b..ab9a147a98e 100644--- a/src/plugins/intel_gpu/src/plugin/program_builder.cpp+++ b/src/plugins/intel_gpu/src/plugin/program_builder.cpp@@ -291,6 +291,7 @@ void ProgramBuilder::add_primitive(const ov::Node& op, std::shared_ptr<cldnn::prprim->origin_op_name = op.get_friendly_name();prim->origin_op_type_name = op.get_type_name();+ prim->is_shape_of_subgraph_root = op.get_rt_info().count("gpu_shape_of_subgraph_root") != 0;if (this->m_config.get_enable_weightless()) {if (auto* data_prim = dynamic_cast<cldnn::data*>(prim.get())) {