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
[ONNX] Enhance GroupQueryAttention to handle null inputs and improve interoperability (#36842)
liang1ebbf9cd898b
diff --git a/src/common/transformations/include/transformations/op_conversions/group_query_attention_decomposition.hpp b/src/common/transformations/include/transformations/op_conversions/group_query_attention_decomposition.hppindex 293e8c7d4b1..62bdfd66434 100644--- a/src/common/transformations/include/transformations/op_conversions/group_query_attention_decomposition.hpp+++ b/src/common/transformations/include/transformations/op_conversions/group_query_attention_decomposition.hpp@@ -53,13 +53,13 @@ private:// PER_CHANNEL -> [1, kv_num_heads, 1, head_size]; PER_TENSOR -> [1, 1, 1, 1].std::shared_ptr<ov::Node> make_kv_scale(const ov::Output<ov::Node>& scale,int64_t kv_num_heads,- const std::string& quant_type);+ ov::op::internal::GroupQueryAttentionQuantType quant_type);// Dequantize a quantized (i8/u8/f8e4m3) KV cache to compute_type: x = q * scale (symmetric, zero point = 0).std::shared_ptr<ov::Node> dequantize_kv(const ov::Output<ov::Node>& quantized,const ov::Output<ov::Node>& scale,int64_t kv_num_heads,int64_t kv_cache_bit_width,- const std::string& quant_type,+ ov::op::internal::GroupQueryAttentionQuantType quant_type,const ov::element::Type& compute_type);// Quantize current float KV tokens into the cache type: integer i8/u8 uses clamp(round(x / scale))// (round-half-to-even); f8e4m3 uses clamp(x / scale, +/-448) then Convert (no integer round).@@ -67,6 +67,6 @@ private:const ov::Output<ov::Node>& scale,int64_t kv_num_heads,int64_t kv_cache_bit_width,- const std::string& quant_type,+ ov::op::internal::GroupQueryAttentionQuantType quant_type,const ov::element::Type& cache_type);};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 087add928ab..51b7427bc47 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@@ -9,6 +9,7 @@#include "itt.hpp"#include "openvino/core/graph_util.hpp"#include "openvino/core/rt_info.hpp"+#include "openvino/core/validation_util.hpp"#include "openvino/decompositions/low_precision_dequantize.hpp"#include "openvino/op/add.hpp"#include "openvino/op/bitwise_and.hpp"@@ -56,7 +57,7 @@ namespace v8 = ov::op::v8;namespace v13 = ov::op::v13;namespace v15 = ov::op::v15;ov::pass::GroupQueryAttentionDecomposition::GroupQueryAttentionDecomposition() {- MATCHER_SCOPE(GroupQeuryAttentionDecomposition);+ MATCHER_SCOPE(GroupQueryAttentionDecomposition);auto pattern_node = ov::pass::pattern::wrap_type<ov::op::internal::GroupQueryAttention>();matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](Matcher& m) {@@ -79,6 +80,8 @@ ov::pass::GroupQueryAttentionDecomposition::GroupQueryAttentionDecomposition() {ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(std::shared_ptr<ov::op::internal::GroupQueryAttention> node) {+ using GQAInputs = ov::op::internal::GroupQueryAttentionInputs;+const auto num_heads = node->get_num_heads();const auto kv_num_heads = node->get_kv_num_heads();const auto scale = node->get_scale();@@ -88,31 +91,42 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(const auto smooth_softmax = node->get_smooth_softmax();// TODO: add softcap support- auto Q = node->input_value(0);- auto K = node->input_value(1);- auto V = node->input_value(2);- auto past_key = node->input_value(3);- auto past_value = node->input_value(4);- auto seqlens_k = node->input_value(5);- auto total_sequence_length = node->input_value(6);+ const auto has_input = [&](const GQAInputs input_pos) {+ const auto pos = static_cast<size_t>(input_pos);+ return (pos < node->get_input_size()) && !ov::util::is_empty_constant_tensor(node->input_value(pos));+ };++ const auto get_input = [&](const GQAInputs input_pos) -> ov::Output<ov::Node> {+ const auto original_pos = static_cast<size_t>(input_pos);+ const bool exists = has_input(input_pos);+ OPENVINO_ASSERT(exists, "Missing required GroupQueryAttention input at original position ", original_pos);+ return node->input_value(original_pos);+ };++ auto Q = get_input(GQAInputs::QUERY);+ auto K = get_input(GQAInputs::KEY);+ auto V = get_input(GQAInputs::VALUE);+ auto past_key = get_input(GQAInputs::PAST_KEY);+ auto past_value = get_input(GQAInputs::PAST_VALUE);+ auto seqlens_k = get_input(GQAInputs::SEQLENS_K);// Quantized KV cache (com.microsoft spec): past/present KV are i8/u8/f8e4m3 and are dequantized before the- // attention math and (re)quantized when appended to the cache. Scales live at inputs 12 (K) / 13 (V).+ // attention math and (re)quantized when appended to the cache. Scales live at ONNX K_SCALE / V_SCALE positions.const bool kv_quantized = node->is_kv_quantized();const auto kv_cache_bit_width = node->get_kv_cache_bit_width();const auto k_quant_type = node->get_k_quant_type();const auto v_quant_type = node->get_v_quant_type();const auto kv_cache_type = past_key.get_element_type();ov::Output<ov::Node> k_scale, v_scale;++ // Get k_scale and v_scale from their actual input indices.+ // Note: validate_and_infer_types() already verified these indices are valid when kv_quantized is true,+ // so we skip redundant bounds checks here.if (kv_quantized) {- k_scale = node->input_value(12);- v_scale = node->input_value(13);+ k_scale = get_input(GQAInputs::K_SCALE);+ v_scale = get_input(GQAInputs::V_SCALE);}- auto is_null = [](const ov::Output<ov::Node>& output) {- return output.get_node_shared_ptr()->description() == "NullNode";- };-// The length of all tokens (past + current) is `seqlens_k` + 1.// current = Q.shape[2], past = `seqlens_k` + 1 - current@@ -135,16 +149,19 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(const auto curr_seqlen_scalar = register_new_node<v0::Squeeze>(current_seqlen);if (do_rotary) {- auto cos_cache = node->input_value(7);- auto sin_cache = node->input_value(8);+ // Get cos_cache and sin_cache from their actual input indices (ONNX COS_CACHE and SIN_CACHE).+ // validate_and_infer_types() already verified these inputs exist and indices are valid when do_rotary is true.+ auto cos_cache = get_input(GQAInputs::COS_CACHE);+ auto sin_cache = get_input(GQAInputs::SIN_CACHE);ov::Output<ov::Node> position_ids =register_new_node<v4::Range>(zero_without_shape, curr_seqlen_scalar, one_without_shape, ov::element::i64);- if (node->get_input_size() > 9 && !is_null(node->input_value(9))) {+ // Check if position_ids is provided (optional input), using actual input index+ if (has_input(GQAInputs::POSITION_IDS)) {// Flatten position_ids to 1D so that Gather produces 2D [seqlen, head_size/2] output,// ensuring correct 4D shapes after Unsqueeze in rotaryEmbedding.const auto neg_one = register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1}));- position_ids = register_new_node<v1::Reshape>(node->input_value(9), neg_one, false);+ position_ids = register_new_node<v1::Reshape>(get_input(GQAInputs::POSITION_IDS), neg_one, false);} else {position_ids = register_new_node<v1::Add>(position_ids, past_seqlen);}@@ -305,8 +322,8 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(}ov::Output<ov::Node> external_bias;- if (node->get_input_size() > 10 && !is_null(node->input_value(10))) {- external_bias = node->input_value(10);+ if (has_input(GQAInputs::ATTENTION_BIAS)) {+ external_bias = get_input(GQAInputs::ATTENTION_BIAS);}const auto mask = make_attention_mask(curr_seqlen_scalar,concat_kv_len_scalar,@@ -321,11 +338,11 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(// this with its sink input: a [1, num_heads, 1, 1] tensor appended as one logit column, included in// the softmax, then sliced out. head_sink provides a per-head value; plain smooth_softmax uses 0.ov::Output<ov::Node> sink;- const bool has_head_sink = node->get_input_size() > 11 && !is_null(node->input_value(11));+ const bool has_head_sink = has_input(GQAInputs::HEAD_SINK);if (has_head_sink || smooth_softmax) {const auto sink_shape = register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{4}, {1, -1, 1, 1}));if (has_head_sink) {- auto head_sink = node->input_value(11);+ auto head_sink = get_input(GQAInputs::HEAD_SINK);if (head_sink.get_element_type() != T) {head_sink = register_new_node<v0::Convert>(head_sink, T);}@@ -558,22 +575,26 @@ std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::rotaryEmbereturn output.get_node_shared_ptr();}-std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::make_kv_scale(const ov::Output<ov::Node>& scale,- int64_t kv_num_heads,- const std::string& quant_type) {+std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::make_kv_scale(+ const ov::Output<ov::Node>& scale,+ int64_t kv_num_heads,+ ov::op::internal::GroupQueryAttentionQuantType quant_type) {// The KV cache is laid out as [batch, kv_num_heads, seq_len, head_size]. Reshape the flat scale so it// broadcasts along that layout. A fully static target shape is used (no -1 wildcard) so the result stays// static-shaped for plugins (e.g. NPU) that require static shapes. A fresh shape Constant is built per// call so no two GQA layers alias it.std::vector<int64_t> target_shape;- if (quant_type == "PER_CHANNEL") {+ if (quant_type == ov::op::internal::GroupQueryAttentionQuantType::PER_CHANNEL) {// Per-channel scale has kv_num_heads * head_size elements, head-major (scale[kv_head * head_size + ch]).// Reshape to [1, kv_num_heads, 1, head_size] to broadcast over batch and seq. head_size is derived from// the (static) scale length when known, otherwise falls back to a -1 wildcard.int64_t head_size = -1;const auto& scale_pshape = scale.get_partial_shape();- if (scale_pshape.rank().is_static() && scale_pshape.rank().get_length() == 1 && scale_pshape[0].is_static()) {- head_size = scale_pshape[0].get_length() / kv_num_heads;+ if (scale_pshape.is_static()) {+ // Element count alone determines head_size, regardless of rank/dim order (e.g. rank 1+ // [kv_num_heads*head_size], rank 2 [kv_num_heads,head_size], rank 4+ // [1,kv_num_heads,1,head_size] all resolve the same way).+ head_size = static_cast<int64_t>(ov::shape_size(scale_pshape.to_shape())) / kv_num_heads;}target_shape = {1, kv_num_heads, 1, head_size};} else {@@ -590,7 +611,7 @@ std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::dequantizeconst ov::Output<ov::Node>& scale,int64_t kv_num_heads,int64_t kv_cache_bit_width,- const std::string& quant_type,+ ov::op::internal::GroupQueryAttentionQuantType quant_type,const ov::element::Type& compute_type) {// Symmetric dequantization matching ONNX Runtime MLAS/CUDA QDQ. The actual Convert(->float) * scale// (optionally - zero_point) chain is built via the shared ov::decomposition::low_precision_dequantize@@ -632,12 +653,13 @@ std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::dequantizereturn dequant.get_node_shared_ptr();}-std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::quantize_kv(const ov::Output<ov::Node>& current,- const ov::Output<ov::Node>& scale,- int64_t kv_num_heads,- int64_t kv_cache_bit_width,- const std::string& quant_type,- const ov::element::Type& cache_type) {+std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::quantize_kv(+ const ov::Output<ov::Node>& current,+ const ov::Output<ov::Node>& scale,+ int64_t kv_num_heads,+ int64_t kv_cache_bit_width,+ ov::op::internal::GroupQueryAttentionQuantType quant_type,+ const ov::element::Type& cache_type) {// Symmetric quantize-on-write: q = clamp(round(x * inv_scale)). Rounding is round-half-to-even to match the// ONNX Runtime MLAS/CUDA reference (std::rintf). Clamp is applied before the narrowing Convert to avoid// overflow on out-of-range values. inv_scale mirrors MLAS SafeInvScale: a zero scale maps to 1.0 so the stepdiff --git a/src/common/transformations/tests/op_conversions/group_query_attention_decomposition_test.cpp b/src/common/transformations/tests/op_conversions/group_query_attention_decomposition_test.cppindex aa5de4bdf7b..98e6a3a5ca8 100644--- a/src/common/transformations/tests/op_conversions/group_query_attention_decomposition_test.cpp+++ b/src/common/transformations/tests/op_conversions/group_query_attention_decomposition_test.cpp@@ -38,30 +38,10 @@ constexpr int64_t NUM_HEADS = 2;constexpr int64_t KV_NUM_HEADS = 1;constexpr int64_t HEAD_SIZE = 16;-// Stand-in for an absent optional input. The decomposition detects missing inputs by node description-// ("NullNode"), matching the ONNX frontend's NullNode, so a filler must report the same description-// rather than be a real Parameter (which would be treated as a supplied position_ids / bias / etc.).-// Declared without the OPENVINO_OP macro so no export/visibility attributes are applied to this-// test-local type (the macro's RTTI attributes are rejected under -Werror on some toolchains).-class NullNode : public op::Op {-public:- static const ov::DiscreteTypeInfo& get_type_info_static() {- static const ov::DiscreteTypeInfo info{"NullNode", "test"};- return info;- }- const ov::DiscreteTypeInfo& get_type_info() const override {- return get_type_info_static();- }- std::string description() const override {- return "NullNode";- }- NullNode() {- set_output_size(1);- }- std::shared_ptr<Node> clone_with_new_inputs(const OutputVector&) const override {- return std::make_shared<NullNode>();- }-};+std::shared_ptr<op::v0::Constant> make_absent_optional_input() {+ // Optional ONNX inputs are represented as an empty tensor.+ return op::v0::Constant::create(element::dynamic, Shape{0}, {});+}// Chainable so each test case reads as a one-liner without C++20 designated initializers.struct GqaParams {@@ -132,7 +112,8 @@ struct GqaParams {std::shared_ptr<Model> make_gqa_model(const GqaParams& p) {const auto f32 = element::f32;const int64_t stored_head = p.kv_cache_bit_width == 4 ? HEAD_SIZE / 2 : HEAD_SIZE; // 4-bit packs 2/byte- const std::string quant = p.kv_cache_bit_width == 0 ? "NONE" : "PER_TENSOR";+ const auto quant = p.kv_cache_bit_width == 0 ? op::internal::GroupQueryAttentionQuantType::NONE+ : op::internal::GroupQueryAttentionQuantType::PER_TENSOR;OutputVector args;ParameterVector params;@@ -143,7 +124,7 @@ std::shared_ptr<Model> make_gqa_model(const GqaParams& p) {};auto pad_to = [&](size_t idx) {while (args.size() < idx)- args.push_back(std::make_shared<NullNode>()); // absent optional input+ args.push_back(make_absent_optional_input()); // absent optional input};// The internal op receives Q/K/V already transposed to [batch, heads, seq, head_size] (the ONNX FE@@ -214,8 +195,8 @@ std::shared_ptr<GroupQueryAttention> make_gqa_op(const Dimension& batch,/*do_rotary*/ false,/*rotary_interleaved*/ false,/*kv_cache_bit_width*/ 0,- "NONE",- "NONE",+ op::internal::GroupQueryAttentionQuantType::NONE,+ op::internal::GroupQueryAttentionQuantType::NONE,local_window_size,sliding_window_cache,/*smooth_softmax*/ false);diff --git a/src/core/dev_api/openvino/core/validation_util.hpp b/src/core/dev_api/openvino/core/validation_util.hppindex 6cef56e3070..4ca5105a6e1 100644--- a/src/core/dev_api/openvino/core/validation_util.hpp+++ b/src/core/dev_api/openvino/core/validation_util.hpp@@ -53,6 +53,12 @@ OPENVINO_API std::shared_ptr<op::v0::Constant> constantfold_subgraph(const Outpu/// \return Shared pointer to constant data or nullptr.OPENVINO_API std::shared_ptr<op::v0::Constant> get_constant_from_source(const Output<Node>& source);+/// \brief Checks whether the source is a Constant with shape {0}.+///+/// \param source Node output to check.+/// \return True if source is Constant and has shape {0}, otherwise false.+OPENVINO_API bool is_empty_constant_tensor(const Output<Node>& source);+/// \brief Make scalar tensor which stores maximum value of ov::element::Type./// \param et Element type to get its maximum./// \return Tensor with maximum value.diff --git a/src/core/dev_api/openvino/op/group_query_attention.hpp b/src/core/dev_api/openvino/op/group_query_attention.hppindex 477ab8c1f40..b1ae5f8c446 100644--- a/src/core/dev_api/openvino/op/group_query_attention.hpp+++ b/src/core/dev_api/openvino/op/group_query_attention.hpp@@ -3,12 +3,61 @@//#pragma once+#include <cstddef>#include <string>+#include <vector>#include "openvino/op/op.hpp"namespace ov::op::internal {+enum class GroupQueryAttentionInputs : size_t {+ QUERY = 0, // Q (mandatory)+ KEY = 1, // K (mandatory)+ VALUE = 2, // V (mandatory)+ PAST_KEY = 3, // KV cache key (mandatory)+ PAST_VALUE = 4, // KV cache value (mandatory)+ SEQLENS_K = 5, // Sequence lengths (mandatory)+ TOTAL_SEQUENCE_LENGTH = 6, // Total sequence length (mandatory)+ COS_CACHE = 7, // RoPE cos cache (optional, required if do_rotary=1)+ SIN_CACHE = 8, // RoPE sin cache (optional, required if do_rotary=1)+ POSITION_IDS = 9, // Position IDs (optional)+ ATTENTION_BIAS = 10, // Attention bias (optional)+ HEAD_SINK = 11, // Head sink (optional, required if smooth_softmax=1)+ K_SCALE = 12, // Quantization scale for K (optional, required if kv_cache_bit_width != 0)+ V_SCALE = 13, // Quantization scale for V (optional, required if kv_cache_bit_width != 0)+ // Positions 14-15 are reserved (QK-Norm, not supported)+};++enum class GroupQueryAttentionQuantType {+ NONE,+ PER_TENSOR,+ PER_CHANNEL,+};++OPENVINO_API+std::ostream& operator<<(std::ostream& s, const GroupQueryAttentionQuantType& quant_type);++} // namespace ov::op::internal++namespace ov {++template <>+class OPENVINO_API AttributeAdapter<op::internal::GroupQueryAttentionQuantType>+ : public EnumAttributeAdapterBase<op::internal::GroupQueryAttentionQuantType> {+public:+ AttributeAdapter(op::internal::GroupQueryAttentionQuantType& value)+ : EnumAttributeAdapterBase<op::internal::GroupQueryAttentionQuantType>(value) {}++ ~AttributeAdapter() override;++ OPENVINO_RTTI("AttributeAdapter<ov::op::internal::GroupQueryAttentionQuantType>");+};++} // namespace ov++namespace ov::op::internal {+// This is an experimental operation that is implemented in the plugins.class OPENVINO_API GroupQueryAttention : public Op {public:@@ -22,8 +71,8 @@ public:bool do_rotary,bool rotary_interleaved,int64_t kv_cache_bit_width = 0,- const std::string& k_quant_type = "NONE",- const std::string& v_quant_type = "NONE",+ GroupQueryAttentionQuantType k_quant_type = GroupQueryAttentionQuantType::NONE,+ GroupQueryAttentionQuantType v_quant_type = GroupQueryAttentionQuantType::NONE,int64_t local_window_size = -1,bool sliding_window_cache = false,bool smooth_softmax = false);@@ -49,10 +98,10 @@ public:int64_t get_kv_cache_bit_width() const {return m_kv_cache_bit_width;}- const std::string& get_k_quant_type() const {+ GroupQueryAttentionQuantType get_k_quant_type() const {return m_k_quant_type;}- const std::string& get_v_quant_type() const {+ GroupQueryAttentionQuantType get_v_quant_type() const {return m_v_quant_type;}// Mistral-style local (sliding-window) attention. -1 disables the window (pure causal); a value@@ -72,7 +121,7 @@ public:}// KV cache is quantized when a bit width is set and a K quantization scheme is selected.bool is_kv_quantized() const {- return m_kv_cache_bit_width != 0 && m_k_quant_type != "NONE";+ return m_kv_cache_bit_width != 0 && m_k_quant_type != GroupQueryAttentionQuantType::NONE;}private:@@ -82,8 +131,8 @@ private:bool m_do_rotary = false;bool m_rotary_interleaved = false;int64_t m_kv_cache_bit_width = 0;- std::string m_k_quant_type = "NONE";- std::string m_v_quant_type = "NONE";+ GroupQueryAttentionQuantType m_k_quant_type = GroupQueryAttentionQuantType::NONE;+ GroupQueryAttentionQuantType m_v_quant_type = GroupQueryAttentionQuantType::NONE;int64_t m_local_window_size = -1;bool m_sliding_window_cache = false;bool m_smooth_softmax = false;diff --git a/src/core/src/op/group_query_attention.cpp b/src/core/src/op/group_query_attention.cppindex 7a83b905026..45d88701f88 100644--- a/src/core/src/op/group_query_attention.cpp+++ b/src/core/src/op/group_query_attention.cpp@@ -4,7 +4,37 @@#include "openvino/op/group_query_attention.hpp"+#include <algorithm>+#include "itt.hpp"+#include "openvino/core/except.hpp"+#include "openvino/core/shape.hpp"+#include "openvino/core/validation_util.hpp"++namespace ov::op::internal {++std::ostream& operator<<(std::ostream& s, const GroupQueryAttentionQuantType& quant_type) {+ return s << as_string(quant_type);+}++} // namespace ov::op::internal++namespace ov {++template <>+OPENVINO_API EnumNames<op::internal::GroupQueryAttentionQuantType>&+EnumNames<op::internal::GroupQueryAttentionQuantType>::get() {+ static auto enum_names = EnumNames<op::internal::GroupQueryAttentionQuantType>(+ "op::internal::GroupQueryAttentionQuantType",+ {{"NONE", op::internal::GroupQueryAttentionQuantType::NONE},+ {"PER_TENSOR", op::internal::GroupQueryAttentionQuantType::PER_TENSOR},+ {"PER_CHANNEL", op::internal::GroupQueryAttentionQuantType::PER_CHANNEL}});+ return enum_names;+}++AttributeAdapter<op::internal::GroupQueryAttentionQuantType>::~AttributeAdapter() = default;++} // namespace ovnamespace ov::op::internal {@@ -15,8 +45,8 @@ GroupQueryAttention::GroupQueryAttention(const OutputVector& args,bool do_rotary,bool rotary_interleaved,int64_t kv_cache_bit_width,- const std::string& k_quant_type,- const std::string& v_quant_type,+ GroupQueryAttentionQuantType k_quant_type,+ GroupQueryAttentionQuantType v_quant_type,int64_t local_window_size,bool sliding_window_cache,bool smooth_softmax)@@ -47,27 +77,124 @@ void GroupQueryAttention::validate_and_infer_types() {// 3. Present_value tensor of shape [B, N, S, H].// Note: seqlens_k represents the number of 1's in the attention_mask minus 1.- const auto& q_shape = get_input_partial_shape(0);- const auto& batch_size = q_shape[0];- const auto& sequence_len = q_shape[2];- const auto& head_size = q_shape[3];- // present_key/present_value keep the past KV layout: kv head size may differ from the query head size- // when the cache is quantized (e.g. 4-bit values packed two per byte), so take it from past_key (input 3).- const auto& past_kv_shape = get_input_partial_shape(3);- auto kv_shape = PartialShape{batch_size, m_kv_num_heads, past_kv_shape[2], past_kv_shape[3]};- auto& output_kv_len = kv_shape[2];+ const auto input_name = [](GroupQueryAttentionInputs input) -> const char* {+ switch (input) {+ case GroupQueryAttentionInputs::QUERY:+ return "query";+ case GroupQueryAttentionInputs::KEY:+ return "key";+ case GroupQueryAttentionInputs::VALUE:+ return "value";+ case GroupQueryAttentionInputs::PAST_KEY:+ return "past_key";+ case GroupQueryAttentionInputs::PAST_VALUE:+ return "past_value";+ case GroupQueryAttentionInputs::SEQLENS_K:+ return "seqlens_k";+ case GroupQueryAttentionInputs::TOTAL_SEQUENCE_LENGTH:+ return "total_sequence_length";+ case GroupQueryAttentionInputs::COS_CACHE:+ return "cos_cache";+ case GroupQueryAttentionInputs::SIN_CACHE:+ return "sin_cache";+ case GroupQueryAttentionInputs::POSITION_IDS:+ return "position_ids";+ case GroupQueryAttentionInputs::ATTENTION_BIAS:+ return "attention_bias";+ case GroupQueryAttentionInputs::HEAD_SINK:+ return "head_sink";+ case GroupQueryAttentionInputs::K_SCALE:+ return "k_scale";+ case GroupQueryAttentionInputs::V_SCALE:+ return "v_scale";+ }+ return "unknown";+ };- // A windowed KV cache keeps the past buffer's own (capacity) sequence dimension: it rolls in place- // with front eviction instead of growing. Otherwise present = past + current.- if (!m_sliding_window_cache && (output_kv_len.is_dynamic() || sequence_len.is_dynamic())) {- output_kv_len += sequence_len;- }+ const auto check_input = [&](GroupQueryAttentionInputs input,+ std::initializer_list<Rank> allowed_ranks,+ const std::vector<element::Type>& allowed_types,+ bool required = true) {+ const auto pos = static_cast<size_t>(input);+ const bool present = (pos < get_input_size()) && !ov::util::is_empty_constant_tensor(input_value(pos));+ NODE_VALIDATION_CHECK(this,+ !required || present,+ "GroupQueryAttention requires ",+ input_name(input),+ " (ONNX input ",+ pos,+ ") to be present");++ if (!present) {+ return;+ }- // Query/activation (input 0) is always float; attention itself is computed in float precision.- const auto& q_type = get_input_element_type(0);+ const auto& pshape = get_input_partial_shape(pos);+ const auto& rank = pshape.rank();+ const bool rank_ok =+ rank.is_dynamic() || allowed_ranks.size() == 0 || ov::util::is_rank_compatible_any_of(rank, allowed_ranks);+ NODE_VALIDATION_CHECK(this,+ rank_ok,+ "GroupQueryAttention expects ",+ input_name(input),+ " rank to be one of the allowed values, got shape ",+ pshape);++ const auto& type = get_input_element_type(pos);+ const bool type_ok = type.is_dynamic() || allowed_types.size() == 0 ||+ std::find(allowed_types.begin(), allowed_types.end(), type) != allowed_types.end();+ NODE_VALIDATION_CHECK(this,+ type_ok,+ "GroupQueryAttention expects ",+ input_name(input),+ " element type to be one of the allowed values, got ",+ type);+ };++ const auto integral_types = []() {+ std::vector<element::Type> types;+ for (const auto& type_info : element::Type::get_known_types()) {+ if (type_info->is_integral_number()) {+ types.push_back(*type_info);+ }+ }+ return types;+ }();++ NODE_VALIDATION_CHECK(this, m_num_heads > 0, "GroupQueryAttention expects num_heads > 0, got: ", m_num_heads);NODE_VALIDATION_CHECK(this,- q_type == element::f32 || q_type == element::f16,- "GroupQueryAttention supports following query element types: {f32, f16}");+ m_kv_num_heads > 0,+ "GroupQueryAttention expects kv_num_heads > 0, got: ",+ m_kv_num_heads);++ // Base input checks in input_check-style form: required + rank/type whitelist.+ check_input(GroupQueryAttentionInputs::QUERY, {4}, {element::f16, element::f32});+ check_input(GroupQueryAttentionInputs::KEY, {4}, {element::f16, element::f32});+ check_input(GroupQueryAttentionInputs::VALUE, {4}, {element::f16, element::f32});+ check_input(GroupQueryAttentionInputs::PAST_KEY,+ {4},+ {element::f32, element::f16, element::i8, element::u8, element::f8e4m3});+ check_input(GroupQueryAttentionInputs::PAST_VALUE,+ {4},+ {element::f32, element::f16, element::i8, element::u8, element::f8e4m3});+ check_input(GroupQueryAttentionInputs::SEQLENS_K, {1, 2}, integral_types);+ check_input(GroupQueryAttentionInputs::TOTAL_SEQUENCE_LENGTH, {0, 1}, integral_types, false);++ if (m_do_rotary) {+ check_input(GroupQueryAttentionInputs::COS_CACHE, {2}, {}, true);+ check_input(GroupQueryAttentionInputs::SIN_CACHE, {2}, {}, true);+ check_input(GroupQueryAttentionInputs::POSITION_IDS, {1, 2}, integral_types, false);+ }+ check_input(GroupQueryAttentionInputs::ATTENTION_BIAS, {4}, {}, false);++ const auto q_shape = get_input_partial_shape(static_cast<size_t>(GroupQueryAttentionInputs::QUERY));+ const auto past_k_shape = get_input_partial_shape(static_cast<size_t>(GroupQueryAttentionInputs::PAST_KEY));++ const auto& q_type = get_input_element_type(static_cast<size_t>(GroupQueryAttentionInputs::QUERY));++ const auto& batch_size = q_shape[0];+ const auto& sequence_len = q_shape[2];+ const auto& head_size = q_shape[3];// The op is reachable directly from a loaded IR, so mirror the ONNX frontend's sliding-window// preconditions here as well: local_window_size must be -1 (disabled) or >= 1, and a windowed cache@@ -114,21 +241,22 @@ void GroupQueryAttention::validate_and_infer_types() {// The KV cache (past_key/past_value, input 3/4) may be quantized. present_key/present_value inherit the// cache element type so a quantized (i8/u8/f8e4m3) cache round-trips from past to present, matching the ONNX spec.- const auto& kv_cache_type = get_input_element_type(3);+ const auto& kv_cache_type = get_input_element_type(static_cast<size_t>(GroupQueryAttentionInputs::PAST_KEY));+ const auto& past_value_type = get_input_element_type(static_cast<size_t>(GroupQueryAttentionInputs::PAST_VALUE));NODE_VALIDATION_CHECK(this,- kv_cache_type == element::f32 || kv_cache_type == element::f16 ||- kv_cache_type == element::i8 || kv_cache_type == element::u8 ||- kv_cache_type == element::f8e4m3,- "GroupQueryAttention supports following KV cache element types: {f32, f16, i8, u8, f8e4m3}");-+ kv_cache_type.compatible(past_value_type),+ "GroupQueryAttention expects past_key and past_value element types to match, got ",+ kv_cache_type,+ " and ",+ past_value_type);if (is_kv_quantized()) {- // Quantized KV cache: i8 (8-bit), u8 (4-bit values packed two per byte), or f8e4m3 (8-bit float). Requires- // float dequant scales.NODE_VALIDATION_CHECK(this,kv_cache_type == element::i8 || kv_cache_type == element::u8 || kv_cache_type == element::f8e4m3,- "GroupQueryAttention with quantized KV cache requires an i8, u8, or f8e4m3 past/present "- "KV type");+ "GroupQueryAttention expects quantized KV cache element type to be one of ",+ "{i8, u8, f8e4m3}, got ",+ kv_cache_type);+ // Quantized KV cache: i8 (8-bit), u8 (4-bit values packed two per byte), or f8e4m3 (8-bit float).NODE_VALIDATION_CHECK(this,m_kv_cache_bit_width == 8 || m_kv_cache_bit_width == 4,"GroupQueryAttention supports kv_cache_bit_width of 8 or 4, got: ",@@ -136,16 +264,27 @@ void GroupQueryAttention::validate_and_infer_types() {NODE_VALIDATION_CHECK(this,m_k_quant_type == m_v_quant_type,"GroupQueryAttention requires matching k_quant_type and v_quant_type, got: ",- m_k_quant_type,+ as_string(m_k_quant_type)," and ",- m_v_quant_type);- NODE_VALIDATION_CHECK(this,- get_input_size() > 13,- "GroupQueryAttention with quantized KV cache requires k_scale (input 12) and "- "v_scale (input 13)");+ as_string(m_v_quant_type));NODE_VALIDATION_CHECK(this,- get_input_element_type(12) == element::f32 && get_input_element_type(13) == element::f32,- "GroupQueryAttention k_scale/v_scale must be f32");+ m_k_quant_type == GroupQueryAttentionQuantType::PER_TENSOR ||+ m_k_quant_type == GroupQueryAttentionQuantType::PER_CHANNEL,+ "GroupQueryAttention supports k/v quant types: {PER_TENSOR, PER_CHANNEL}, got: ",+ as_string(m_k_quant_type));++ check_input(GroupQueryAttentionInputs::K_SCALE, {0, 1, 2, 3, 4}, {element::f32, element::f16}, true);+ check_input(GroupQueryAttentionInputs::V_SCALE, {0, 1, 2, 3, 4}, {element::f32, element::f16}, true);+ }++ // present_key/present_value keep the past KV layout: kv head size may differ from query head size+ // when the cache is quantized (e.g. 4-bit values packed two per byte).+ auto kv_shape = PartialShape{batch_size, m_kv_num_heads, past_k_shape[2], past_k_shape[3]};+ auto& output_kv_len = kv_shape[2];+ // A windowed KV cache keeps the past buffer's own (capacity) sequence dimension: it rolls in place+ // with front eviction instead of growing. Otherwise present = past + current.+ if (!m_sliding_window_cache && (output_kv_len.is_dynamic() || sequence_len.is_dynamic())) {+ output_kv_len += sequence_len;}set_output_type(0, q_type, PartialShape{batch_size, sequence_len, head_size * m_num_heads});diff --git a/src/core/src/validation_util.cpp b/src/core/src/validation_util.cppindex 2c2081f04a4..b4acfc89a11 100644--- a/src/core/src/validation_util.cpp+++ b/src/core/src/validation_util.cpp@@ -164,6 +164,11 @@ std::shared_ptr<Constant> get_constant_from_source(const ov::Output<ov::Node>& s}}+bool is_empty_constant_tensor(const Output<Node>& source) {+ const auto constant = ov::as_type_ptr<Constant>(source.get_node_shared_ptr());+ return constant != nullptr && constant->get_shape() == Shape{0};+}+template <class T>Tensor make_tensor_of_max_value(const element::Type_t et) {Tensor t{et, Shape{}};diff --git a/src/core/tests/type_prop/group_query_attention.cpp b/src/core/tests/type_prop/group_query_attention.cppnew file mode 100644index 00000000000..16fc4e4a25f--- /dev/null+++ b/src/core/tests/type_prop/group_query_attention.cpp@@ -0,0 +1,259 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/op/group_query_attention.hpp"++#include <gtest/gtest.h>++#include <memory>+#include <vector>++#include "common_test_utils/test_assertions.hpp"+#include "openvino/core/type/element_type.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/parameter.hpp"++namespace ov {+namespace testing {+using ::testing::HasSubstr;++namespace {+ov::OutputVector make_valid_gqa_args(const element::Type& t = element::f32) {+ using ov::op::v0::Parameter;++ const auto query = std::make_shared<Parameter>(t, PartialShape{1, 6, 4, 8});+ const auto key = std::make_shared<Parameter>(t, PartialShape{1, 2, 4, 8});+ const auto value = std::make_shared<Parameter>(t, PartialShape{1, 2, 4, 8});++ const auto past_key = std::make_shared<Parameter>(t, PartialShape{1, 2, 5, 8});+ const auto past_value = std::make_shared<Parameter>(t, PartialShape{1, 2, 5, 8});++ const auto seqlens_k = std::make_shared<Parameter>(element::i32, PartialShape{1});+ const auto total_sequence_length = std::make_shared<Parameter>(element::i32, PartialShape{});++ return {query, key, value, past_key, past_value, seqlens_k, total_sequence_length};+}++ov::OutputVector make_valid_gqa_rotary_args(const element::Type& t = element::f32) {+ auto args = make_valid_gqa_args(t);+ const auto cos_cache = std::make_shared<op::v0::Parameter>(t, PartialShape{16, 4});+ const auto sin_cache = std::make_shared<op::v0::Parameter>(t, PartialShape{16, 4});+ args.push_back(cos_cache);+ args.push_back(sin_cache);+ return args;+}++// Fills optional inputs 7-11 with empty constants (treated as absent by has_input).+ov::OutputVector make_valid_gqa_quant_args(const element::Type& kv_type,+ int64_t kv_cache_bit_width,+ const element::Type& scale_type = element::f32) {+ using ov::op::v0::Constant;+ using ov::op::v0::Parameter;+ const auto empty = Constant::create(element::dynamic, Shape{0}, {});++ const auto query = std::make_shared<Parameter>(element::f32, PartialShape{1, 6, 4, 8});+ const auto key = std::make_shared<Parameter>(element::f32, PartialShape{1, 2, 4, 8});+ const auto value = std::make_shared<Parameter>(element::f32, PartialShape{1, 2, 4, 8});+ const auto past_key = std::make_shared<Parameter>(kv_type, PartialShape{1, 2, 5, 8});+ const auto past_value = std::make_shared<Parameter>(kv_type, PartialShape{1, 2, 5, 8});+ const auto seqlens_k = std::make_shared<Parameter>(element::i32, PartialShape{1});+ const auto total_sequence_length = std::make_shared<Parameter>(element::i32, PartialShape{});+ // positions 7-11: cos_cache, sin_cache, position_ids, attention_mask, head_sink (all absent)+ const auto k_scale = std::make_shared<Parameter>(scale_type, PartialShape{});+ const auto v_scale = std::make_shared<Parameter>(scale_type, PartialShape{});++ return {query,+ key,+ value,+ past_key,+ past_value,+ seqlens_k,+ total_sequence_length,+ empty,+ empty,+ empty,+ empty,+ empty,+ k_scale,+ v_scale};+}+} // namespace++TEST(type_prop, group_query_attention_gqa_output_shapes) {+ const auto args = make_valid_gqa_args();+ const auto op = std::make_shared<op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false);++ EXPECT_EQ(op->get_output_size(), 3);+ EXPECT_EQ(op->get_output_element_type(0), element::f32);+ EXPECT_EQ(op->get_output_element_type(1), element::f32);+ EXPECT_EQ(op->get_output_element_type(2), element::f32);+ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{1, 4, 48}));+ EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, 5, 8}));+ EXPECT_EQ(op->get_output_partial_shape(2), (PartialShape{1, 2, 5, 8}));+}++TEST(type_prop, group_query_attention_mha_output_shapes) {+ const auto query = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, 4, 8});+ const auto key = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, 4, 8});+ const auto value = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, 4, 8});+ const auto past_key = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, 5, 8});+ const auto past_value = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, 5, 8});+ const auto seqlens_k = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{1});+ const auto total_sequence_length = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{});++ const auto args = ov::OutputVector{query, key, value, past_key, past_value, seqlens_k, total_sequence_length};++ const auto op = std::make_shared<op::internal::GroupQueryAttention>(args, 2, 2, 1.0f, false, false);++ EXPECT_EQ(op->get_output_size(), 3);+ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{1, 4, 16}));+ EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, 5, 8}));+ EXPECT_EQ(op->get_output_partial_shape(2), (PartialShape{1, 2, 5, 8}));+}++TEST(type_prop, group_query_attention_dynamic_seq_len) {+ const auto query = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 6, -1, 8});+ const auto key = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, -1, 8});+ const auto value = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, -1, 8});+ const auto past_key = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, -1, 8});+ const auto past_value = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, -1, 8});+ const auto seqlens_k = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{1});+ const auto total_sequence_length = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{});++ const auto args = ov::OutputVector{query, key, value, past_key, past_value, seqlens_k, total_sequence_length};++ const auto op = std::make_shared<op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false);+ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{1, -1, 48}));+ EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, -1, 8}));+ EXPECT_EQ(op->get_output_partial_shape(2), (PartialShape{1, 2, -1, 8}));+}++TEST(type_prop, group_query_attention_dynamic_kv_len_accumulates) {+ const auto query = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 6, {1, 4}, 8});+ const auto key = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, {1, 4}, 8});+ const auto value = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, {1, 4}, 8});+ const auto past_key = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, {5, 9}, 8});+ const auto past_value = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, {5, 9}, 8});+ const auto seqlens_k = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{1});+ const auto total_sequence_length = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{});++ const auto args = ov::OutputVector{query, key, value, past_key, past_value, seqlens_k, total_sequence_length};++ const auto op = std::make_shared<op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false);++ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{1, {1, 4}, 48}));+ EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, {6, 13}, 8}));+ EXPECT_EQ(op->get_output_partial_shape(2), (PartialShape{1, 2, {6, 13}, 8}));+}++TEST(type_prop, group_query_attention_invalid_query_rank) {+ auto args = make_valid_gqa_args();+ args[0] = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 6, 8});++ OV_EXPECT_THROW(std::ignore = std::make_shared<op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false),+ ov::NodeValidationFailure,+ HasSubstr("query rank"));+}++TEST(type_prop, group_query_attention_do_rotary_requires_cos_sin) {+ const auto args = make_valid_gqa_args();++ OV_EXPECT_THROW(std::ignore = std::make_shared<op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, true, false),+ ov::NodeValidationFailure,+ HasSubstr("cos_cache"));+}++TEST(type_prop, group_query_attention_rotary_inputs_static_shapes) {+ const auto args = make_valid_gqa_rotary_args();+ const auto op = std::make_shared<op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, true, false);++ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{1, 4, 48}));+ EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, 5, 8}));+ EXPECT_EQ(op->get_output_partial_shape(2), (PartialShape{1, 2, 5, 8}));+}++TEST(type_prop, group_query_attention_quant_type_enum_names) {+ EXPECT_EQ(as_string(op::internal::GroupQueryAttentionQuantType::NONE), "NONE");+ EXPECT_EQ(as_string(op::internal::GroupQueryAttentionQuantType::PER_TENSOR), "PER_TENSOR");+ EXPECT_EQ(as_string(op::internal::GroupQueryAttentionQuantType::PER_CHANNEL), "PER_CHANNEL");+ EXPECT_EQ(as_enum<op::internal::GroupQueryAttentionQuantType>("PER_TENSOR"),+ op::internal::GroupQueryAttentionQuantType::PER_TENSOR);+}++// ---------- quantized KV cache ----------++TEST(type_prop, group_query_attention_kv_cache_int8_per_tensor) {+ const auto args = make_valid_gqa_quant_args(element::i8, 8);+ const auto quantize_type = op::internal::GroupQueryAttentionQuantType::PER_TENSOR;+ const auto op = std::make_shared<op::internal::GroupQueryAttention>(args,+ 6,+ 2,+ 1.0f,+ false,+ false,+ 8,+ quantize_type,+ quantize_type);++ EXPECT_EQ(op->get_output_element_type(0), element::f32);+ EXPECT_EQ(op->get_output_element_type(1), element::i8);+ EXPECT_EQ(op->get_output_element_type(2), element::i8);+ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{1, 4, 48}));+ EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, 5, 8}));+ EXPECT_EQ(op->get_output_partial_shape(2), (PartialShape{1, 2, 5, 8}));+}++TEST(type_prop, group_query_attention_kv_cache_uint4_not_supported) {+ // u4 is not in the allowed past_key/past_value type list; remove this when u4 support is added.+ const auto args = make_valid_gqa_quant_args(element::u4, 4);+ const auto quantize_type = op::internal::GroupQueryAttentionQuantType::PER_TENSOR;+ OV_EXPECT_THROW(+ std::ignore = std::make_shared<+ op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false, 4, quantize_type, quantize_type),+ ov::NodeValidationFailure,+ HasSubstr("past_key"));+}++TEST(type_prop, group_query_attention_kv_cache_mismatched_quant_types) {+ const auto args = make_valid_gqa_quant_args(element::i8, 8);+ OV_EXPECT_THROW(std::ignore = std::make_shared<op::internal::GroupQueryAttention>(+ args,+ 6,+ 2,+ 1.0f,+ false,+ false,+ 8,+ op::internal::GroupQueryAttentionQuantType::PER_TENSOR,+ op::internal::GroupQueryAttentionQuantType::PER_CHANNEL),+ ov::NodeValidationFailure,+ HasSubstr("matching k_quant_type and v_quant_type"));+}++TEST(type_prop, group_query_attention_kv_cache_mismatched_past_types) {+ auto args = make_valid_gqa_quant_args(element::i8, 8);+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_VALUE)] =+ std::make_shared<op::v0::Parameter>(element::u8, PartialShape{1, 2, 5, 8});+ const auto quantize_type = op::internal::GroupQueryAttentionQuantType::PER_TENSOR;++ OV_EXPECT_THROW(+ std::ignore = std::make_shared<+ op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false, 8, quantize_type, quantize_type),+ ov::NodeValidationFailure,+ HasSubstr("past_key and past_value element types to match"));+}++TEST(type_prop, group_query_attention_quantized_kv_requires_quantized_cache_type) {+ const auto args = make_valid_gqa_quant_args(element::f32, 8);+ const auto quantize_type = op::internal::GroupQueryAttentionQuantType::PER_TENSOR;++ OV_EXPECT_THROW(+ std::ignore = std::make_shared<+ op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false, 8, quantize_type, quantize_type),+ ov::NodeValidationFailure,+ HasSubstr("quantized KV cache element type"));+}++} // namespace testing+} // namespace ov\ No newline at end of filediff --git a/src/core/tests/type_prop/sources.cmake b/src/core/tests/type_prop/sources.cmakeindex 161b157edb3..380ccaffdda 100644--- a/src/core/tests/type_prop/sources.cmake+++ b/src/core/tests/type_prop/sources.cmake@@ -92,6 +92,7 @@ set(OV_CORE_TESTS_TYPE_PROP_SRCS${CMAKE_CURRENT_LIST_DIR}/group_convolution_backprop_data.cpp${CMAKE_CURRENT_LIST_DIR}/group_normalization.cpp${CMAKE_CURRENT_LIST_DIR}/grouped_matmul.cpp+ ${CMAKE_CURRENT_LIST_DIR}/group_query_attention.cpp${CMAKE_CURRENT_LIST_DIR}/gru_cell.cpp${CMAKE_CURRENT_LIST_DIR}/gru_sequence.cpp${CMAKE_CURRENT_LIST_DIR}/hard_sigmoid.cppdiff --git a/src/core/tests/validation_utils.cpp b/src/core/tests/validation_utils.cppindex 8adc801b2b7..9242fa8c78f 100644--- a/src/core/tests/validation_utils.cpp+++ b/src/core/tests/validation_utils.cpp@@ -65,6 +65,35 @@ TEST(get_constant_from_source, return_nullptr_for_empty_output) {ASSERT_EQ(res, nullptr);}+TEST(is_empty_constant_tensor, returns_true_for_empty_constant) {+ const auto empty = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{0}, {});+ ASSERT_TRUE(ov::util::is_empty_constant_tensor(empty));+}++TEST(is_empty_constant_tensor, returns_false_for_non_empty_constant) {+ const auto non_empty = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1}, {1.0f});+ ASSERT_FALSE(ov::util::is_empty_constant_tensor(non_empty));+}++TEST(is_empty_constant_tensor, returns_false_for_non_constant_node) {+ const auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1});+ ASSERT_FALSE(ov::util::is_empty_constant_tensor(param));+}++TEST(is_empty_constant_tensor, returns_false_for_scalar_constant) {+ const auto scalar = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {1.0f});+ ASSERT_FALSE(ov::util::is_empty_constant_tensor(scalar));+}++TEST(is_empty_constant_tensor, returns_false_for_multi_dim_zero_element_constant) {+ const auto multi_dim_empty = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{5, 1, 0, 5}, {});+ ASSERT_FALSE(ov::util::is_empty_constant_tensor(multi_dim_empty));+}++TEST(is_empty_constant_tensor, returns_false_for_empty_output) {+ ASSERT_FALSE(ov::util::is_empty_constant_tensor(ov::Output<ov::Node>()));+}+TEST(constantfold_subgraph, split) {std::vector<float> input{0, 1, 2, 3, 4, 5, 6, 7, 8};auto constant = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{input.size()}, input);diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cppindex a560f8b7599..73702853692 100644--- a/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cpp+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cpp@@ -11,6 +11,7 @@#include "exceptions.hpp"#include "openvino/frontend/exception.hpp"#include "openvino/op/concat.hpp"+#include "openvino/op/constant.hpp"#include "openvino/op/divide.hpp"#include "openvino/op/reshape.hpp"#include "openvino/op/shape_of.hpp"@@ -48,8 +49,22 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {const auto rotary_interleaved = node.get_attribute_value<int64_t>("rotary_interleaved", 0);// Quantized KV cache attributes (com.microsoft spec). Default to the unquantized (float KV) behavior.const auto kv_cache_bit_width = node.get_attribute_value<int64_t>("kv_cache_bit_width", 0);- const auto k_quant_type = node.get_attribute_value<std::string>("k_quant_type", "NONE");- const auto v_quant_type = node.get_attribute_value<std::string>("v_quant_type", "NONE");+ const auto parse_quant_type = [&](const std::string& quant_type_name) {+ using QuantType = ov::op::internal::GroupQueryAttentionQuantType;+ if (quant_type_name == "NONE") {+ return QuantType::NONE;+ }+ if (quant_type_name == "PER_TENSOR") {+ return QuantType::PER_TENSOR;+ }+ if (quant_type_name == "PER_CHANNEL") {+ return QuantType::PER_CHANNEL;+ }+ FRONT_END_GENERAL_CHECK(false, "GroupQueryAttention: unsupported quant type '", quant_type_name, "'.");+ return QuantType::NONE;+ };+ const auto k_quant_type = parse_quant_type(node.get_attribute_value<std::string>("k_quant_type", "NONE"));+ const auto v_quant_type = parse_quant_type(node.get_attribute_value<std::string>("v_quant_type", "NONE"));// Sliding-window / softcap / smooth-softmax attributes (com.microsoft spec). Default to no-op values,// matching the ONNX Runtime defaults (local_window_size = -1 disables the window).const auto local_window_size = node.get_attribute_value<int64_t>("local_window_size", -1);@@ -143,6 +158,11 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {const auto hidden_size_node = detail::get_dimensions(q_shape_node, {2});OutputVector ov_op_inputs;++ const auto make_empty_optional_input = []() {+ return v0::Constant::create(ov::element::dynamic, ov::Shape{0}, {})->output(0);+ };+if (ov::op::util::is_null(K) && ov::op::util::is_null(V)) {auto total_num_heads_node =v0::Constant::create(ov::element::i64, ov::Shape{1}, {num_heads + kv_num_heads + kv_num_heads});@@ -186,8 +206,18 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {ov_op_inputs.push_back(std::move(V));}- for (size_t i = ov_op_inputs.size(); i < onnx_op_inputs.size(); ++i) {- ov_op_inputs.push_back(onnx_op_inputs[i]);+ FRONT_END_OP_CONVERSION_CHECK(+ common::is_input_valid(onnx_op_inputs, 3) && common::is_input_valid(onnx_op_inputs, 4),+ "GroupQueryAttention: past_key (input 3) and past_value (input 4) must be provided as tensors");+ // Process optional inputs: use a zero-sized Constant placeholder for missing optional ONNX inputs.+ // Note: When the ONNX's input index changed, the corresponding index in the GroupQueryAttentionInputs enum must+ // also be updated and may need mapping the index manually.+ for (size_t i = ov_op_inputs.size(); i < inputs_count_max; ++i) {+ if (i < onnx_op_inputs.size() && !ov::op::util::is_null(onnx_op_inputs[i])) {+ ov_op_inputs.push_back(onnx_op_inputs[i]);+ } else {+ ov_op_inputs.push_back(make_empty_optional_input());+ }}return std::make_shared<internal::GroupQueryAttention>(ov_op_inputs,diff --git a/src/plugins/intel_gpu/tests/unit/transformations/keep_gqa_kv_scale_precision_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/keep_gqa_kv_scale_precision_test.cppindex fcc791d442c..260c80bb0d0 100644--- a/src/plugins/intel_gpu/tests/unit/transformations/keep_gqa_kv_scale_precision_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/transformations/keep_gqa_kv_scale_precision_test.cpp@@ -8,6 +8,7 @@#include "common_test_utils/ov_test_utils.hpp"#include "openvino/core/model.hpp"+#include "openvino/op/constant.hpp"#include "openvino/op/group_query_attention.hpp"#include "openvino/op/parameter.hpp"#include "openvino/op/result.hpp"@@ -20,6 +21,7 @@ using namespace ov::intel_gpu;namespace {using ov::op::internal::GroupQueryAttention;+using ov::op::internal::GroupQueryAttentionQuantType;// k_scale / v_scale are inputs 12 / 13 of GroupQueryAttention.constexpr size_t k_scale_idx = 12;@@ -32,20 +34,30 @@ constexpr size_t v_scale_idx = 13;std::shared_ptr<ov::Model> make_gqa_model(bool quantized) {const auto kv_et = quantized ? ov::element::i8 : ov::element::f16;- ov::ParameterVector params(14);- params[0] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{1, 2, 1, 16}); // query- params[1] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{1, 1, 1, 16}); // key- params[2] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{1, 1, 1, 16}); // value- params[3] = std::make_shared<ov::op::v0::Parameter>(kv_et, ov::PartialShape{1, 1, 8, 16}); // past_key- params[4] = std::make_shared<ov::op::v0::Parameter>(kv_et, ov::PartialShape{1, 1, 8, 16}); // past_value- params[5] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{1}); // seqlens_k- params[6] = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{1}); // total_sequence_length- for (size_t i = 7; i <= 11; ++i) // rotary / optional placeholders, unread by validate_and_infer_types- params[i] = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{1});- params[k_scale_idx] = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1}); // k_scale- params[v_scale_idx] = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1}); // v_scale-- ov::OutputVector args(params.begin(), params.end());+ ov::ParameterVector params;+ ov::OutputVector args(14);++ const auto add_param = [&](size_t idx, const ov::element::Type& et, const ov::PartialShape& pshape) {+ auto p = std::make_shared<ov::op::v0::Parameter>(et, pshape);+ params.push_back(p);+ args[idx] = p;+ };++ add_param(0, ov::element::f16, ov::PartialShape{1, 2, 1, 16}); // query+ add_param(1, ov::element::f16, ov::PartialShape{1, 1, 1, 16}); // key+ add_param(2, ov::element::f16, ov::PartialShape{1, 1, 1, 16}); // value+ add_param(3, kv_et, ov::PartialShape{1, 1, 8, 16}); // past_key+ add_param(4, kv_et, ov::PartialShape{1, 1, 8, 16}); // past_value+ add_param(5, ov::element::i32, ov::PartialShape{1}); // seqlens_k+ add_param(6, ov::element::i32, ov::PartialShape{1}); // total_sequence_length+ for (size_t i = 7; i <= 11; ++i) // empty Constants are treated as absent optional inputs by has_input()+ args[i] = ov::op::v0::Constant::create(ov::element::dynamic, ov::Shape{0}, {});+ add_param(k_scale_idx, ov::element::f32, ov::PartialShape{1}); // k_scale+ add_param(v_scale_idx, ov::element::f32, ov::PartialShape{1}); // v_scale++ // Keep this test compatible with both string-based and enum-based quant type APIs.+ const auto quant_type = quantized ? GroupQueryAttentionQuantType::PER_CHANNEL : GroupQueryAttentionQuantType::NONE;+auto gqa = std::make_shared<GroupQueryAttention>(args,/*num_heads*/ 2,/*kv_num_heads*/ 1,@@ -53,8 +65,8 @@ std::shared_ptr<ov::Model> make_gqa_model(bool quantized) {/*do_rotary*/ false,/*rotary_interleaved*/ false,/*kv_cache_bit_width*/ quantized ? 8 : 0,- /*k_quant_type*/ quantized ? "PER_CHANNEL" : "NONE",- /*v_quant_type*/ quantized ? "PER_CHANNEL" : "NONE");+ /*k_quant_type*/ quant_type,+ /*v_quant_type*/ quant_type);ov::ResultVector results{std::make_shared<ov::op::v0::Result>(gqa->output(0)),std::make_shared<ov::op::v0::Result>(gqa->output(1)),diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/decompose_gqa.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/decompose_gqa.cppindex 1eae5f40887..b5f01598c82 100644--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/decompose_gqa.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/decompose_gqa.cpp@@ -5,6 +5,7 @@#include "decompose_gqa.hpp"#include "openvino/core/graph_util.hpp"+#include "openvino/core/validation_util.hpp"#include "openvino/op/group_query_attention.hpp"#include "openvino/op/ops.hpp"#include "openvino/op/range.hpp"@@ -57,15 +58,24 @@ public:const auto rotary_interleaved = node->get_rotary_interleaved();// TODO: add softcap support- auto Q = node->input_value(0);- auto K = node->input_value(1);- auto V = node->input_value(2);- auto past_key = node->input_value(3);- auto past_value = node->input_value(4);- auto seqlens_k = node->input_value(5);- auto total_sequence_length = node->input_value(6);- auto cos_cache = node->input_value(7);- auto sin_cache = node->input_value(8);+ const auto has_input = [&](ov::op::internal::GroupQueryAttentionInputs input_pos) {+ const auto pos = static_cast<size_t>(input_pos);+ return pos < node->get_input_size() && !ov::util::is_empty_constant_tensor(node->input_value(pos));+ };++ const auto get_input = [&](ov::op::internal::GroupQueryAttentionInputs input_pos) -> ov::Output<ov::Node> {+ const auto original_pos = static_cast<int64_t>(input_pos);+ const bool exists = has_input(input_pos);+ OPENVINO_ASSERT(exists, "Missing required GroupQueryAttention input at original position ", original_pos);+ return node->input_value(static_cast<size_t>(original_pos));+ };++ auto Q = get_input(ov::op::internal::GroupQueryAttentionInputs::QUERY);+ auto K = get_input(ov::op::internal::GroupQueryAttentionInputs::KEY);+ auto V = get_input(ov::op::internal::GroupQueryAttentionInputs::VALUE);+ auto past_key = get_input(ov::op::internal::GroupQueryAttentionInputs::PAST_KEY);+ auto past_value = get_input(ov::op::internal::GroupQueryAttentionInputs::PAST_VALUE);+ auto seqlens_k = get_input(ov::op::internal::GroupQueryAttentionInputs::SEQLENS_K);// The length of all tokens (past + current) is `seqlens_k` + 1.// current = Q.shape[2], past = `seqlens_k` + 1 - current@@ -89,6 +99,8 @@ public:const auto curr_seqlen_scalar = register_new_node<v0::Squeeze>(current_seqlen);if (do_rotary) {+ auto cos_cache = get_input(ov::op::internal::GroupQueryAttentionInputs::COS_CACHE);+ auto sin_cache = get_input(ov::op::internal::GroupQueryAttentionInputs::SIN_CACHE);ov::Output<ov::Node> position_ids = register_new_node<v4::Range>(zero_without_shape,curr_seqlen_scalar,one_without_shape,diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cppindex 3c33c150970..709cd22cbc3 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp@@ -14,6 +14,7 @@#include "online/utils/utils.hpp" // getMetaDesc#include "openvino/core/parallel.hpp"#include "openvino/core/rt_info/weightless_caching_attributes.hpp"+#include "openvino/core/validation_util.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/slice.hpp"#include "openvino/op/util/op_types.hpp"@@ -1556,7 +1557,12 @@ void Partitioner::saveRepeatedConstants(const std::string& func_name) {return;}+ bool empty_constant = ov::util::is_empty_constant_tensor(proto_node);+bool all_identical = std::all_of(instances.begin(), instances.end(), [&](const CTPtr& other_node) -> bool {+ if (empty_constant) {+ return ov::util::is_empty_constant_tensor(other_node);+ }return (other_node->output(0).get_shape() == proto_node->output(0).get_shape()) &&values_are_the_same(proto_node, other_node);});