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 FE] Support quantized (int8/int4) KV cache in GroupQueryAttention (#36676)
Ankit Maheshkare33e707ae29e
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 5fcf8c848ee..3263aeae113 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@@ -31,4 +31,23 @@ private:ov::Output<ov::Node> cos,ov::Output<ov::Node> sin,bool interleaved);+ // Reshape a flat KV-cache dequant scale so it broadcasts against a [B, kv_num_heads, S, head_size] tensor:+ // 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);+ // Dequantize a quantized (i8/u8) 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,+ const ov::element::Type& compute_type);+ // Quantize current float KV tokens into the cache type: q = clamp(round(x / scale)) (round-half-to-even).+ std::shared_ptr<ov::Node> 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);};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 58a805b7826..d23a2418712 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,17 +9,26 @@#include "itt.hpp"#include "openvino/core/graph_util.hpp"#include "openvino/core/rt_info.hpp"+#include "openvino/decompositions/low_precision_dequantize.hpp"#include "openvino/op/add.hpp"+#include "openvino/op/bitwise_and.hpp"+#include "openvino/op/bitwise_left_shift.hpp"+#include "openvino/op/bitwise_or.hpp"+#include "openvino/op/bitwise_right_shift.hpp"#include "openvino/op/broadcast.hpp"+#include "openvino/op/clamp.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/convert.hpp"+#include "openvino/op/divide.hpp"+#include "openvino/op/equal.hpp"#include "openvino/op/gather.hpp"#include "openvino/op/greater.hpp"#include "openvino/op/greater_eq.hpp"#include "openvino/op/multiply.hpp"#include "openvino/op/range.hpp"#include "openvino/op/reshape.hpp"+#include "openvino/op/round.hpp"#include "openvino/op/scaled_dot_product_attention.hpp"#include "openvino/op/scatter_update.hpp"#include "openvino/op/select.hpp"@@ -38,8 +47,10 @@ namespace v0 = ov::op::v0;namespace v1 = ov::op::v1;namespace v3 = ov::op::v3;namespace v4 = ov::op::v4;+namespace v5 = ov::op::v5;namespace v8 = ov::op::v8;namespace v13 = ov::op::v13;+namespace v15 = ov::op::v15;ov::pass::GroupQueryAttentionDecomposition::GroupQueryAttentionDecomposition() {MATCHER_SCOPE(GroupQeuryAttentionDecomposition);auto pattern_node = ov::pass::pattern::wrap_type<ov::op::internal::GroupQueryAttention>();@@ -79,6 +90,19 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(auto seqlens_k = node->input_value(5);auto total_sequence_length = node->input_value(6);+ // Quantized KV cache (com.microsoft spec): past/present KV are i8/u8 and are dequantized before the+ // attention math and (re)quantized when appended to the cache. Scales live at inputs 12 (K) / 13 (V).+ 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;+ if (kv_quantized) {+ k_scale = node->input_value(12);+ v_scale = node->input_value(13);+ }+auto is_null = [](const ov::Output<ov::Node>& output) {return output.get_node_shared_ptr()->description() == "NullNode";};@@ -126,6 +150,14 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(}const auto is_static_input = K.get_partial_shape().is_static() && past_key.get_partial_shape().is_static();+ // Quantize-on-write: when the cache is quantized, quantize the (post-RoPE) current K/V into the cache type+ // before appending them, so the assembled present cache stays quantized and the past bytes are preserved+ // verbatim (no re-rounding of past tokens). Matches ONNX Runtime MLAS/CUDA semantics.+ if (kv_quantized) {+ K = quantize_kv(K, k_scale, kv_num_heads, kv_cache_bit_width, k_quant_type, kv_cache_type);+ V = quantize_kv(V, v_scale, kv_num_heads, kv_cache_bit_width, v_quant_type, kv_cache_type);+ }+if (is_static_input) {// static design for GQA (KV cache is static max length, valid KVs are left align)// inputs are:@@ -154,9 +186,17 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(V = construct_kv_cache(past_value, V);}+ // present_key/present_value are the assembled cache (quantized when kv_quantized), matching the op outputs.ov::Output<ov::Node> present_k = K;ov::Output<ov::Node> present_v = V;+ // Dequantize the assembled cache to the compute (float) type for the attention math. Everything downstream+ // (head broadcast, mask, SDPA) then operates in float exactly as in the non-quantized path.+ if (kv_quantized) {+ K = dequantize_kv(K, k_scale, kv_num_heads, kv_cache_bit_width, k_quant_type, T);+ V = dequantize_kv(V, v_scale, kv_num_heads, kv_cache_bit_width, v_quant_type, T);+ }+const auto concat_kv_len = get_dimensions(K.get_node_shared_ptr(), {2});const auto concat_kv_len_scalar = register_new_node<v0::Squeeze>(concat_kv_len);@@ -199,10 +239,10 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(const auto triu = register_new_node<v1::Greater>(hori_range, vert_range);const auto typed_zero = register_new_node(v0::Constant::create(T, ov::Shape{}, {0}));// cf. make_attention_mask@src\plugins\intel_gpu\tests\common\subgraphs_builders.hpp+ // Mask with the type's finite lowest(), not -inf: a fully-masked row would otherwise softmax to 0/0 = NaN.std::shared_ptr<ov::Node> minus_inf = nullptr;if (T == ov::element::f32)- minus_inf =- register_new_node(v0::Constant::create(T, ov::Shape{}, {-std::numeric_limits<float>::infinity()}));+ minus_inf = register_new_node(v0::Constant::create(T, ov::Shape{}, {std::numeric_limits<float>::lowest()}));else if (T == ov::element::f16)minus_inf =register_new_node(v0::Constant::create(T, ov::Shape{}, {std::numeric_limits<ov::float16>::lowest()}));@@ -308,3 +348,130 @@ 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) {+ // 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") {+ // 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;+ }+ target_shape = {1, kv_num_heads, 1, head_size};+ } else {+ // PER_TENSOR: a single scalar broadcast over the whole tensor.+ target_shape = {1, 1, 1, 1};+ }+ const auto shape_const =+ register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{target_shape.size()}, target_shape));+ return register_new_node<v1::Reshape>(scale, shape_const, false);+}++std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::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,+ 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+ // helper, so it produces the canonical dequantization pattern recognized by MarkDequantization / LPT.+ const auto scale_bcast = make_kv_scale(scale, kv_num_heads, quant_type);++ if (kv_cache_bit_width == 4) {+ // 4-bit cache is stored as u8 with two signed values packed per byte (even channel -> low nibble,+ // odd channel -> high nibble) biased by +8. Unpack the nibbles back into per-channel integers first,+ // then let low_precision_dequantize apply the (Convert - 8) * scale chain (zero_point = 8 removes the bias).+ const auto axis_last = register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1}));+ const auto mask_low =+ register_new_node(v0::Constant::create(quantized.get_element_type(), ov::Shape{}, {0x0F}));+ const auto shift_4 = register_new_node(v0::Constant::create(quantized.get_element_type(), ov::Shape{}, {4}));+ const auto low_nibble = register_new_node<v13::BitwiseAnd>(quantized, mask_low);+ const auto high_nibble = register_new_node<v15::BitwiseRightShift>(quantized, shift_4);+ // Interleave low/high nibbles back along the head_size axis: [.., packed] -> [.., packed, 2] -> [.., 2*packed].+ const auto low_u = register_new_node<v0::Unsqueeze>(low_nibble, axis_last);+ const auto high_u = register_new_node<v0::Unsqueeze>(high_nibble, axis_last);+ const auto interleaved = register_new_node<v0::Concat>(ov::OutputVector{low_u, high_u}, -1);+ const auto flat_shape = register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 0, 0, -1}));+ const auto unpacked = register_new_node<v1::Reshape>(interleaved, flat_shape, true);+ const auto zero_point = register_new_node(v0::Constant::create(quantized.get_element_type(), ov::Shape{}, {8}));+ ov::pass::NodeRegistry reg;+ auto dequant =+ ov::decomposition::low_precision_dequantize(reg, unpacked, scale_bcast, zero_point, {}, compute_type);+ for (const auto& node : reg.get()) {+ register_new_node(node);+ }+ return dequant.get_node_shared_ptr();+ }++ // 8-bit cache: symmetric (no zero point) Convert(->compute) * scale via the shared helper.+ ov::pass::NodeRegistry reg;+ auto dequant = ov::decomposition::low_precision_dequantize(reg, quantized, scale_bcast, {}, {}, compute_type);+ for (const auto& node : reg.get()) {+ register_new_node(node);+ }+ return 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) {+ // 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 step+ // degenerates to clamp(round(x)) instead of producing NaN (and multiplying by the reciprocal matches MLAS).+ const auto compute_type = current.get_element_type();+ const auto scale_bcast = make_kv_scale(scale, kv_num_heads, quant_type);+ ov::Output<ov::Node> scale_ct = scale_bcast;+ if (scale.get_element_type() != compute_type) {+ scale_ct = register_new_node<v0::Convert>(scale_bcast, compute_type);+ }+ const auto one = register_new_node(v0::Constant::create(compute_type, ov::Shape{}, {1}));+ const auto zero = register_new_node(v0::Constant::create(compute_type, ov::Shape{}, {0}));+ const auto is_zero_scale = register_new_node<v1::Equal>(scale_ct, zero);+ const auto safe_scale = register_new_node<v1::Select>(is_zero_scale, one, scale_ct);+ const auto inv_scale = register_new_node<v1::Divide>(one, safe_scale);+ const auto scaled = register_new_node<v1::Multiply>(current, inv_scale);+ const auto rounded = register_new_node<v5::Round>(scaled, v5::Round::RoundMode::HALF_TO_EVEN);++ if (kv_cache_bit_width == 4) {+ // Clamp to signed 4-bit range, add the +8 storage bias, then pack pairs of channels into u8 bytes.+ const auto clamped = register_new_node<v0::Clamp>(rounded, -8.0, 7.0);+ const auto bias = register_new_node(v0::Constant::create(compute_type, ov::Shape{}, {8}));+ const auto biased = register_new_node<v1::Add>(clamped, bias);+ const auto as_u8 = register_new_node<v0::Convert>(biased, cache_type);+ // Split the head_size axis into pairs: [.., 2*packed] -> [.., packed, 2] -> low/high nibbles.+ const auto pair_shape =+ register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{5}, {0, 0, 0, -1, 2}));+ const auto paired = register_new_node<v1::Reshape>(as_u8, pair_shape, true);+ const auto axis_last = register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1}));+ const auto split = register_new_node<v1::VariadicSplit>(+ paired,+ register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{}, {-1})),+ register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{2}, {1, 1})));+ const auto low = register_new_node<v0::Squeeze>(split->output(0), axis_last);+ const auto high = register_new_node<v0::Squeeze>(split->output(1), axis_last);+ const auto shift_4 = register_new_node(v0::Constant::create(cache_type, ov::Shape{}, {4}));+ const auto mask_low = register_new_node(v0::Constant::create(cache_type, ov::Shape{}, {0x0F}));+ const auto low_masked = register_new_node<v13::BitwiseAnd>(low, mask_low);+ const auto high_shifted = register_new_node<v15::BitwiseLeftShift>(high, shift_4);+ return register_new_node<v13::BitwiseOr>(low_masked, high_shifted);+ }++ // 8-bit cache: clamp to signed 8-bit range and narrow to the cache type.+ const auto clamped = register_new_node<v0::Clamp>(rounded, -128.0, 127.0);+ return register_new_node<v0::Convert>(clamped, cache_type);+}diff --git a/src/core/dev_api/openvino/op/group_query_attention.hpp b/src/core/dev_api/openvino/op/group_query_attention.hppindex a6133ce6d3f..4422d12ff58 100644--- a/src/core/dev_api/openvino/op/group_query_attention.hpp+++ b/src/core/dev_api/openvino/op/group_query_attention.hpp@@ -3,6 +3,8 @@//#pragma once+#include <string>+#include "openvino/op/op.hpp"namespace ov::op::internal {@@ -18,7 +20,10 @@ public:int64_t kv_num_heads,float scale,bool do_rotary,- bool rotary_interleaved);+ bool rotary_interleaved,+ int64_t kv_cache_bit_width = 0,+ const std::string& k_quant_type = "NONE",+ const std::string& v_quant_type = "NONE");void validate_and_infer_types() override;bool visit_attributes(AttributeVisitor& visitor) override;std::shared_ptr<ov::Node> clone_with_new_inputs(const ov::OutputVector& new_args) const override;@@ -38,6 +43,19 @@ public:bool get_rotary_interleaved() const {return m_rotary_interleaved;}+ int64_t get_kv_cache_bit_width() const {+ return m_kv_cache_bit_width;+ }+ const std::string& get_k_quant_type() const {+ return m_k_quant_type;+ }+ const std::string& get_v_quant_type() const {+ return m_v_quant_type;+ }+ // 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";+ }private:int64_t m_num_heads = 0;@@ -45,6 +63,9 @@ private:float m_scale = 0;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";};} // namespace ov::op::internaldiff --git a/src/core/src/op/group_query_attention.cpp b/src/core/src/op/group_query_attention.cppindex c340a39a949..17300caae19 100644--- a/src/core/src/op/group_query_attention.cpp+++ b/src/core/src/op/group_query_attention.cpp@@ -13,13 +13,19 @@ GroupQueryAttention::GroupQueryAttention(const OutputVector& args,int64_t kv_num_heads,float scale,bool do_rotary,- bool rotary_interleaved)+ bool rotary_interleaved,+ int64_t kv_cache_bit_width,+ const std::string& k_quant_type,+ const std::string& v_quant_type): Op(args),m_num_heads(num_heads),m_kv_num_heads(kv_num_heads),m_scale(scale),m_do_rotary(do_rotary),- m_rotary_interleaved(rotary_interleaved) {+ m_rotary_interleaved(rotary_interleaved),+ m_kv_cache_bit_width(kv_cache_bit_width),+ m_k_quant_type(k_quant_type),+ m_v_quant_type(v_quant_type) {constructor_validate_and_infer_types();}@@ -39,7 +45,10 @@ void GroupQueryAttention::validate_and_infer_types() {const auto& batch_size = q_shape[0];const auto& sequence_len = q_shape[2];const auto& head_size = q_shape[3];- auto kv_shape = PartialShape{batch_size, m_kv_num_heads, get_input_partial_shape(3)[2], head_size};+ // 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];if (output_kv_len.is_dynamic() || sequence_len.is_dynamic()) {@@ -47,24 +56,60 @@ void GroupQueryAttention::validate_and_infer_types() {output_kv_len += sequence_len;}- const auto& element_type = get_input_element_type(0);+ // Query/activation (input 0) is always float; attention itself is computed in float precision.+ const auto& q_type = get_input_element_type(0);NODE_VALIDATION_CHECK(this,- element_type == element::f32 || element_type == element::f16,- "GroupQueryAttention supports following element types: {f32, f16}");+ q_type == element::f32 || q_type == element::f16,+ "GroupQueryAttention supports following query element types: {f32, f16}");- set_output_type(0, element_type, PartialShape{batch_size, sequence_len, head_size * m_num_heads});+ // 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) cache round-trips from past to present, matching the ONNX spec.+ const auto& kv_cache_type = get_input_element_type(3);+ NODE_VALIDATION_CHECK(this,+ kv_cache_type == element::f32 || kv_cache_type == element::f16 ||+ kv_cache_type == element::i8 || kv_cache_type == element::u8,+ "GroupQueryAttention supports following KV cache element types: {f32, f16, i8, u8}");++ if (is_kv_quantized()) {+ // Quantized KV cache: i8 (8-bit) or u8 (4-bit values packed two per byte). Requires float dequant scales.+ NODE_VALIDATION_CHECK(this,+ kv_cache_type == element::i8 || kv_cache_type == element::u8,+ "GroupQueryAttention with quantized KV cache requires an i8 or u8 past/present KV type");+ 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: ",+ m_kv_cache_bit_width);+ 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,+ " 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)");+ 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");+ }++ set_output_type(0, q_type, PartialShape{batch_size, sequence_len, head_size * m_num_heads});for (auto&& port : {1, 2}) {- set_output_type(port, element_type, kv_shape);+ set_output_type(port, kv_cache_type, kv_shape);}}bool GroupQueryAttention::visit_attributes(AttributeVisitor& visitor) {OV_OP_SCOPE(GroupQueryAttention_visit_attributes);visitor.on_attribute("do_rotary", m_do_rotary);+ visitor.on_attribute("k_quant_type", m_k_quant_type);+ visitor.on_attribute("kv_cache_bit_width", m_kv_cache_bit_width);visitor.on_attribute("kv_num_heads", m_kv_num_heads);visitor.on_attribute("num_heads", m_num_heads);visitor.on_attribute("rotary_interleaved", m_rotary_interleaved);visitor.on_attribute("scale", m_scale);+ visitor.on_attribute("v_quant_type", m_v_quant_type);return true;}@@ -76,7 +121,10 @@ std::shared_ptr<ov::Node> GroupQueryAttention::clone_with_new_inputs(const ov::Om_kv_num_heads,m_scale,m_do_rotary,- m_rotary_interleaved);+ m_rotary_interleaved,+ m_kv_cache_bit_width,+ m_k_quant_type,+ m_v_quant_type);}} // namespace ov::op::internaldiff --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 5534f278694..c2677a4683b 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@@ -30,13 +30,14 @@ using ov::frontend::onnx::attention::get_dimensions;namespace opset_1 {ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {constexpr size_t inputs_count_min = 7; // Taken from ONNX spec- constexpr size_t inputs_count_max = 14; // Taken from ONNX spec+ constexpr size_t inputs_count_max = 16; // Taken from ONNX spec// Minimum required inputs basing on the spec and ONNX Runtime code: 7// 0: packed QKV (mandatory)// 3-4: possibly null (if unused)// 5: seqlens_k (mandatory)// 6: total_sequence_length (mandatory in the spec)+ // 12-13: k_scale/v_scale (required when the KV cache is quantized)common::default_op_checks(node, inputs_count_min, inputs_count_max);const auto onnx_op_inputs = node.get_ov_inputs();@@ -45,6 +46,18 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {const auto scale = node.get_attribute_value<float>("scale", 0.0f);const auto do_rotary = node.get_attribute_value<int64_t>("do_rotary", 0);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");++ // Reject spec inputs whose semantics are not implemented by the OpenVINO decomposition.+ FRONT_END_OP_CONVERSION_CHECK(!common::is_input_valid(onnx_op_inputs, 11),+ "GroupQueryAttention: head_sink input is not supported.");+ FRONT_END_OP_CONVERSION_CHECK(+ !common::is_input_valid(onnx_op_inputs, 14) && !common::is_input_valid(onnx_op_inputs, 15),+ "GroupQueryAttention: q_norm_weight/k_norm_weight (QK-Norm) inputs are not "+ "supported.");if (0 != do_rotary) {constexpr size_t cos_cache_index = 7;@@ -129,7 +142,10 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {kv_num_heads,scale,do_rotary,- rotary_interleaved)+ rotary_interleaved,+ kv_cache_bit_width,+ k_quant_type,+ v_quant_type)->outputs();}diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_i4kv_per_channel.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_i4kv_per_channel.prototxtnew file mode 100644index 00000000000..e0ff9f2c0fc--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_i4kv_per_channel.prototxt@@ -0,0 +1,241 @@+ir_version: 10+graph {+ node {+ input: "query"+ input: ""+ input: ""+ input: "past_key"+ input: "past_value"+ input: "seqlens_k"+ input: "total_sequence_length"+ input: ""+ input: ""+ input: ""+ input: ""+ input: ""+ input: "k_scale"+ input: "v_scale"+ output: "output"+ output: "present_key"+ output: "present_value"+ op_type: "GroupQueryAttention"+ attribute {+ name: "do_rotary"+ i: 0+ type: INT+ }+ attribute {+ name: "k_quant_type"+ s: "PER_CHANNEL"+ type: STRING+ }+ attribute {+ name: "kv_cache_bit_width"+ i: 4+ type: INT+ }+ attribute {+ name: "kv_num_heads"+ i: 1+ type: INT+ }+ attribute {+ name: "num_heads"+ i: 2+ type: INT+ }+ attribute {+ name: "v_quant_type"+ s: "PER_CHANNEL"+ type: STRING+ }+ domain: "com.microsoft"+ }+ name: "GQA_DynQuant"+ initializer {+ dims: 8+ data_type: 1+ float_data: 0.03088+ float_data: 0.05149+ float_data: 0.04601+ float_data: 0.0285+ float_data: 0.06849+ float_data: 0.02605+ float_data: 0.06375+ float_data: 0.02591+ name: "k_scale"+ }+ initializer {+ dims: 8+ data_type: 1+ float_data: 0.03712+ float_data: 0.04729+ float_data: 0.04498+ float_data: 0.06395+ float_data: 0.02376+ float_data: 0.04815+ float_data: 0.02656+ float_data: 0.0619+ name: "v_scale"+ }+ input {+ name: "query"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_param: "sequence_length"+ }+ dim {+ dim_value: 32+ }+ }+ }+ }+ }+ input {+ name: "past_key"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "past_sequence_length"+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ input {+ name: "past_value"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "past_sequence_length"+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ input {+ name: "seqlens_k"+ type {+ tensor_type {+ elem_type: 6+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ input {+ name: "total_sequence_length"+ type {+ tensor_type {+ elem_type: 6+ shape {+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_param: "sequence_length"+ }+ dim {+ dim_value: 16+ }+ }+ }+ }+ }+ output {+ name: "present_key"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "total_sequence_length"+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ output {+ name: "present_value"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "total_sequence_length"+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 21+}+opset_import {+ domain: "com.microsoft"+ version: 1+}diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_i4kv_per_tensor.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_i4kv_per_tensor.prototxtnew file mode 100644index 00000000000..817bb2a1ed0--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_i4kv_per_tensor.prototxt@@ -0,0 +1,227 @@+ir_version: 10+graph {+ node {+ input: "query"+ input: ""+ input: ""+ input: "past_key"+ input: "past_value"+ input: "seqlens_k"+ input: "total_sequence_length"+ input: ""+ input: ""+ input: ""+ input: ""+ input: ""+ input: "k_scale"+ input: "v_scale"+ output: "output"+ output: "present_key"+ output: "present_value"+ op_type: "GroupQueryAttention"+ attribute {+ name: "do_rotary"+ i: 0+ type: INT+ }+ attribute {+ name: "k_quant_type"+ s: "PER_TENSOR"+ type: STRING+ }+ attribute {+ name: "kv_cache_bit_width"+ i: 4+ type: INT+ }+ attribute {+ name: "kv_num_heads"+ i: 1+ type: INT+ }+ attribute {+ name: "num_heads"+ i: 2+ type: INT+ }+ attribute {+ name: "v_quant_type"+ s: "PER_TENSOR"+ type: STRING+ }+ domain: "com.microsoft"+ }+ name: "GQA_DynQuant"+ initializer {+ dims: 1+ data_type: 1+ float_data: 0.0488+ name: "k_scale"+ }+ initializer {+ dims: 1+ data_type: 1+ float_data: 0.05554+ name: "v_scale"+ }+ input {+ name: "query"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_param: "sequence_length"+ }+ dim {+ dim_value: 32+ }+ }+ }+ }+ }+ input {+ name: "past_key"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "past_sequence_length"+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ input {+ name: "past_value"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "past_sequence_length"+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ input {+ name: "seqlens_k"+ type {+ tensor_type {+ elem_type: 6+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ input {+ name: "total_sequence_length"+ type {+ tensor_type {+ elem_type: 6+ shape {+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_param: "sequence_length"+ }+ dim {+ dim_value: 16+ }+ }+ }+ }+ }+ output {+ name: "present_key"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "total_sequence_length"+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ output {+ name: "present_value"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "total_sequence_length"+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 21+}+opset_import {+ domain: "com.microsoft"+ version: 1+}diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_i8kv_per_channel.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_i8kv_per_channel.prototxtnew file mode 100644index 00000000000..9f5eaaa38d0--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_i8kv_per_channel.prototxt@@ -0,0 +1,241 @@+ir_version: 10+graph {+ node {+ input: "query"+ input: ""+ input: ""+ input: "past_key"+ input: "past_value"+ input: "seqlens_k"+ input: "total_sequence_length"+ input: ""+ input: ""+ input: ""+ input: ""+ input: ""+ input: "k_scale"+ input: "v_scale"+ output: "output"+ output: "present_key"+ output: "present_value"+ op_type: "GroupQueryAttention"+ attribute {+ name: "do_rotary"+ i: 0+ type: INT+ }+ attribute {+ name: "k_quant_type"+ s: "PER_CHANNEL"+ type: STRING+ }+ attribute {+ name: "kv_cache_bit_width"+ i: 8+ type: INT+ }+ attribute {+ name: "kv_num_heads"+ i: 1+ type: INT+ }+ attribute {+ name: "num_heads"+ i: 2+ type: INT+ }+ attribute {+ name: "v_quant_type"+ s: "PER_CHANNEL"+ type: STRING+ }+ domain: "com.microsoft"+ }+ name: "GQA_DynQuant"+ initializer {+ dims: 8+ data_type: 1+ float_data: 0.02677+ float_data: 0.06439+ float_data: 0.06663+ float_data: 0.04228+ float_data: 0.03941+ float_data: 0.03288+ float_data: 0.05287+ float_data: 0.04463+ name: "k_scale"+ }+ initializer {+ dims: 8+ data_type: 1+ float_data: 0.06821+ float_data: 0.06005+ float_data: 0.04276+ float_data: 0.06005+ float_data: 0.02209+ float_data: 0.05847+ float_data: 0.02016+ float_data: 0.03464+ name: "v_scale"+ }+ input {+ name: "query"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_param: "sequence_length"+ }+ dim {+ dim_value: 32+ }+ }+ }+ }+ }+ input {+ name: "past_key"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "past_sequence_length"+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+ input {+ name: "past_value"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "past_sequence_length"+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+ input {+ name: "seqlens_k"+ type {+ tensor_type {+ elem_type: 6+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ input {+ name: "total_sequence_length"+ type {+ tensor_type {+ elem_type: 6+ shape {+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_param: "sequence_length"+ }+ dim {+ dim_value: 16+ }+ }+ }+ }+ }+ output {+ name: "present_key"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "total_sequence_length"+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+ output {+ name: "present_value"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "total_sequence_length"+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 21+}+opset_import {+ domain: "com.microsoft"+ version: 1+}diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_i8kv_per_tensor.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_i8kv_per_tensor.prototxtnew file mode 100644index 00000000000..f151fe70f93--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_i8kv_per_tensor.prototxt@@ -0,0 +1,227 @@+ir_version: 10+graph {+ node {+ input: "query"+ input: ""+ input: ""+ input: "past_key"+ input: "past_value"+ input: "seqlens_k"+ input: "total_sequence_length"+ input: ""+ input: ""+ input: ""+ input: ""+ input: ""+ input: "k_scale"+ input: "v_scale"+ output: "output"+ output: "present_key"+ output: "present_value"+ op_type: "GroupQueryAttention"+ attribute {+ name: "do_rotary"+ i: 0+ type: INT+ }+ attribute {+ name: "k_quant_type"+ s: "PER_TENSOR"+ type: STRING+ }+ attribute {+ name: "kv_cache_bit_width"+ i: 8+ type: INT+ }+ attribute {+ name: "kv_num_heads"+ i: 1+ type: INT+ }+ attribute {+ name: "num_heads"+ i: 2+ type: INT+ }+ attribute {+ name: "v_quant_type"+ s: "PER_TENSOR"+ type: STRING+ }+ domain: "com.microsoft"+ }+ name: "GQA_DynQuant"+ initializer {+ dims: 1+ data_type: 1+ float_data: 0.06957+ name: "k_scale"+ }+ initializer {+ dims: 1+ data_type: 1+ float_data: 0.06868+ name: "v_scale"+ }+ input {+ name: "query"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_param: "sequence_length"+ }+ dim {+ dim_value: 32+ }+ }+ }+ }+ }+ input {+ name: "past_key"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "past_sequence_length"+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+ input {+ name: "past_value"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "past_sequence_length"+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+ input {+ name: "seqlens_k"+ type {+ tensor_type {+ elem_type: 6+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ input {+ name: "total_sequence_length"+ type {+ tensor_type {+ elem_type: 6+ shape {+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_param: "sequence_length"+ }+ dim {+ dim_value: 16+ }+ }+ }+ }+ }+ output {+ name: "present_key"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "total_sequence_length"+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+ output {+ name: "present_value"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_param: "batch_size"+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_param: "total_sequence_length"+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 21+}+opset_import {+ domain: "com.microsoft"+ version: 1+}diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_qk_norm.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_qk_norm.prototxtnew file mode 100644index 00000000000..036389c1920--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_qk_norm.prototxt@@ -0,0 +1,67 @@+ir_version: 10+graph {+ node {+ input: "query"+ input: ""+ input: ""+ input: "past_key"+ input: "past_value"+ input: "seqlens_k"+ input: "total_sequence_length"+ input: ""+ input: ""+ input: ""+ input: ""+ input: ""+ input: ""+ input: ""+ input: "q_norm_weight"+ input: "k_norm_weight"+ output: "output"+ output: "present_key"+ output: "present_value"+ name: "GroupQueryAttention_0"+ op_type: "GroupQueryAttention"+ attribute { name: "num_heads" i: 2 type: INT }+ attribute { name: "kv_num_heads" i: 1 type: INT }+ attribute { name: "do_rotary" i: 0 type: INT }+ domain: "com.microsoft"+ }+ name: "GQA_QKNorm_Graph"+ initializer {+ dims: 8+ data_type: 1+ name: "q_norm_weight"+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ }+ initializer {+ dims: 8+ data_type: 1+ name: "k_norm_weight"+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ float_data: 1.0+ }+ input { name: "query" type { tensor_type { elem_type: 1 shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 32 } } } } }+ input { name: "past_key" type { tensor_type { elem_type: 1 shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 8 } } } } }+ input { name: "past_value" type { tensor_type { elem_type: 1 shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 8 } } } } }+ input { name: "seqlens_k" type { tensor_type { elem_type: 6 shape { dim { dim_value: 1 } dim { dim_value: 1 } } } } }+ input { name: "total_sequence_length" type { tensor_type { elem_type: 6 shape { } } } }+ output { name: "output" type { tensor_type { elem_type: 1 shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 16 } } } } }+ output { name: "present_key" type { tensor_type { elem_type: 1 shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 8 } } } } }+ output { name: "present_value" type { tensor_type { elem_type: 1 shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 8 } } } } }+}+opset_import { version: 21 }+opset_import { domain: "com.microsoft" version: 1 }diff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cppindex 817eaa22bd6..6b4e67d4d8e 100644--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp@@ -3155,6 +3155,89 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_0_input_1_rotary) {test_case.run_with_tolerance_as_fp();}+// Regression test for the fully-masked query row. With a 2-token query but only 1 valid position+// (seqlens_k = 0), the causal mask fully masks the second query row. The attention mask must use the+// element type's finite lowest() (not -inf) so that row's softmax stays finite instead of 0/0 = NaN.+// The expected values below are finite; if the mask ever regresses to -inf this row becomes NaN and+// the comparison fails.+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_fully_masked_row_is_finite) {+ const auto model = convert_model("com.microsoft/gqa_rotary.onnx");++ std::vector<float> query = {+ -0.026000f, -0.055600f, 0.520900f, -0.628400f, 0.372700f, -0.855500f, -0.102900f, -0.117300f, 0.564100f,+ -0.006300f, -0.306600f, 0.686800f, 0.805500f, -0.344600f, 0.346000f, -0.224100f, 0.081200f, 0.128600f,+ -0.637700f, 0.032000f, -0.530900f, -0.494700f, -0.228900f, -0.992100f, -0.738200f, 0.115900f, 0.322100f,+ 0.426100f, -0.232000f, 0.348600f, 0.783900f, 0.589300f, -0.692000f, -0.873700f, 0.201400f, 0.622200f,+ -0.011900f, 0.476300f, 0.122500f, 0.112000f, 0.148300f, 0.110400f, -0.211700f, 0.922800f, 0.460100f,+ -0.279000f, -0.142600f, -0.520600f, 0.240200f, -0.713700f, -0.166600f, 0.373700f, 0.280100f, 0.286900f,+ -0.590400f, 0.382300f, -0.067200f, 0.662300f, -0.138200f, 0.839800f, 0.207600f, 0.373800f, -0.196300f,+ -0.203200f, -0.192900f, 0.500500f, -0.223800f, -0.058000f, 0.584500f, 0.257800f, -0.128900f, 0.142500f,+ -0.702300f, -0.754500f, 0.217200f, 1.377500f, 1.005400f, 1.259900f, 0.285200f, -0.263300f, 0.534100f,+ -0.597300f, -1.429800f, 0.212100f, 0.516800f, 0.352600f, 0.775100f, 0.382700f, 0.173200f, -0.526900f,+ -0.064700f, -0.590700f, -0.184400f, 0.951400f, 0.105100f, 0.188400f, -0.347000f, 1.301400f, -0.991600f,+ -0.463400f, 0.472900f, -0.375300f, -0.397100f, 0.254200f, 0.374200f, 0.341300f, -0.428100f, 0.376600f,+ -0.604900f, -0.458800f, -0.498400f, 0.163800f, -0.176100f, -0.475900f, 0.015700f, -0.511700f, -0.252400f,+ -0.156000f, -0.283900f, -0.503100f, -0.624000f, 0.549300f, -1.033800f, 0.148900f, -0.402500f, -0.353900f,+ -0.891500f, -0.015300f};+ std::vector<float> past_key = {};+ std::vector<float> past_value = {};+ std::vector<int> seqlens_k = {0}; // only 1 valid position => query row 1 is fully masked+ std::vector<int> total_sequence_length = {2};+ std::vector<float> cos_cache = {0.809800f,+ 0.337700f,+ 0.195600f,+ 0.342000f,+ 0.672800f,+ 0.283100f,+ 0.265700f,+ 0.881100f,+ 0.539100f,+ 0.743800f,+ 0.897800f,+ 0.274000f,+ 0.533400f,+ 0.133200f,+ 0.961900f,+ 0.812500f};+ std::vector<float> sin_cache = {0.651400f,+ 0.202300f,+ 0.970800f,+ 0.035500f,+ 0.423200f,+ 0.944400f,+ 0.319200f,+ 0.776900f,+ 0.975100f,+ 0.701900f,+ 0.302500f,+ 0.968700f,+ 0.395700f,+ 0.951300f,+ 0.129400f,+ 0.937100f};++ std::vector<float> expected_output = {+ 0.032050f, -0.594800f, -0.075450f, -0.069000f, 0.013850f, 0.065450f, -0.437150f, -0.060400f,+ -0.345600f, 0.605800f, -0.586000f, 0.494350f, -0.097450f, 0.009950f, -0.543900f, -0.109250f,+ 0.032050f, -0.594800f, -0.075450f, -0.069000f, 0.013850f, 0.065450f, -0.437150f, -0.060400f,+ -0.345600f, 0.605800f, -0.586000f, 0.494350f, -0.097450f, 0.009950f, -0.543900f, -0.109250f,+ 0.240200f, -0.713700f, -0.166600f, 0.373700f, 0.280100f, 0.286900f, -0.590400f, 0.382300f,+ -0.067200f, 0.662300f, -0.138200f, 0.839800f, 0.207600f, 0.373800f, -0.196300f, -0.203200f,+ 0.240200f, -0.713700f, -0.166600f, 0.373700f, 0.280100f, 0.286900f, -0.590400f, 0.382300f,+ -0.067200f, 0.662300f, -0.138200f, 0.839800f, 0.207600f, 0.373800f, -0.196300f, -0.203200f};++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{1, 2, 64}, query);+ test_case.add_input<float>(Shape{1, 1, 0, 16}, past_key);+ test_case.add_input<float>(Shape{1, 1, 0, 16}, past_value);+ test_case.add_input<int>(Shape{1, 1}, seqlens_k);+ test_case.add_input<int>(Shape{}, total_sequence_length);+ test_case.add_input<float>(Shape{2, 8}, cos_cache);+ test_case.add_input<float>(Shape{2, 8}, sin_cache);+ test_case.add_expected_output<float>(Shape{1, 2, 32}, expected_output);+ test_case.run_with_tolerance_as_fp(1e-3f);+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_0_input_1_rotary_fp16) {// Load the existing GQA rotary model and change float inputs to float16// to verify that neg_one constant type matches input element type in GQA decomposition@@ -4281,14 +4364,231 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_static_rotary) {test_case.run_with_tolerance_as_fp(1e-4f);}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_i8kv_per_channel) {+ const auto model = convert_model("com.microsoft/gqa_i8kv_per_channel.onnx");++ // num_heads=2, kv_num_heads=1, head_size=8, current=1, past=2 (dynamic past_sequence_length). Non-rotary.+ // KV cache is int8 with per-channel dequant scales embedded as initializers (k_scale/v_scale). The dynamic+ // past shape exercises the Slice+Concat KV-cache assembly (present grows to past+current = 3).+ std::vector<float> query = {+ 0.3054000f, 0.0820000f, -0.4166000f, -0.3312000f, -0.4215000f, -0.0355000f, 0.0633000f, -0.7157000f,+ 0.4254000f, 0.0148000f, -0.0655000f, -0.1313000f, 0.6356000f, -0.0755000f, -0.0727000f, 0.2872000f,+ -0.0003000f, 0.3129000f, 0.0348000f, 0.0265000f, 0.4802000f, -0.0247000f, -0.3604000f, -0.5037000f,+ 0.0227000f, -0.3865000f, 0.2994000f, 0.1113000f, -0.1243000f, -0.2616000f, -0.0292000f, 0.2368000f};+ std::vector<int8_t> past_key = {24, -23, 17, -27, 25, -22, 17, 18, 12, 13, 9, -18, 24, -4, -5, 0};+ std::vector<int8_t> past_value = {22, -5, -18, -28, -23, 9, 15, -26, 26, -20, -14, -25, 12, -2, -7, -19};+ std::vector<int> seqlens_k = {2};+ std::vector<int> total_sequence_length = {3};+ std::vector<float> expected_output = {1.01103735f,+ -0.632150471f,+ -0.295739084f,+ -0.919733584f,+ -0.0973050594f,+ 0.0105678858f,+ 0.0251368713f,+ -0.373427451f,+ 1.18633878f,+ -0.625254869f,+ -0.420571089f,+ -1.13011706f,+ -0.141064093f,+ 0.0992611125f,+ 0.0627523884f,+ -0.506907046f};+ std::vector<int8_t> expected_present_key = {24, -23, 17, -27, 25, -22, 17, 18, 12, 13, 9, -18,+ 24, -4, -5, 0, 0, 5, 1, 1, 12, -1, -7, -11};+ std::vector<int8_t> expected_present_value = {22, -5, -18, -28, -23, 9, 15, -26, 26, -20, -14, -25,+ 12, -2, -7, -19, 0, -6, 7, 2, -6, -4, -1, 7};++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{1, 1, 32}, query);+ test_case.add_input<int8_t>(Shape{1, 1, 2, 8}, past_key);+ test_case.add_input<int8_t>(Shape{1, 1, 2, 8}, past_value);+ test_case.add_input<int>(Shape{1, 1}, seqlens_k);+ test_case.add_input<int>(Shape{}, total_sequence_length);+ test_case.add_expected_output<float>(Shape{1, 1, 16}, expected_output);+ test_case.add_expected_output<int8_t>(Shape{1, 1, 3, 8}, expected_present_key);+ test_case.add_expected_output<int8_t>(Shape{1, 1, 3, 8}, expected_present_value);+ // Mixed float output + int8 present KV: run() compares floats by ULP tolerance and int8 exactly. The+ // present KV cache is checked bit-exact; the float output uses a relaxed ULP tolerance because a couple of+ // near-zero attention outputs differ across backends (CPU vs TEMPLATE) by a few low mantissa bits.+ test_case.run(11);+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_i8kv_per_tensor) {+ const auto model = convert_model("com.microsoft/gqa_i8kv_per_tensor.onnx");++ // Same tiny dynamic-past config as gqa_i8kv_per_channel but with a single per-tensor scale for K and V.+ std::vector<float> query = {+ -0.0954000f, -0.5184000f, 0.4668000f, 0.5252000f, 0.6202000f, -0.4286000f, 0.3644000f, 0.2827000f,+ -0.2593000f, -0.1152000f, 0.2258000f, -0.1652000f, -0.6331000f, -0.0576000f, -0.3862000f, -0.1533000f,+ -0.0389000f, -0.0381000f, -0.2434000f, 0.7596000f, -0.0479000f, -0.0554000f, -0.5949000f, 0.2861000f,+ -0.3534000f, 0.5613000f, 0.2467000f, -0.1424000f, 0.0029000f, -0.8551000f, 0.0904000f, 0.4609000f};+ std::vector<int8_t> past_key = {15, -24, 10, 25, 25, -30, 6, 20, 25, -16, -8, 1, 12, 0, 9, 28};+ std::vector<int8_t> past_value = {-21, 16, 0, 4, -6, -1, -19, 18, 10, -30, 4, 22, -26, 12, 28, -17};+ std::vector<int> seqlens_k = {2};+ std::vector<int> total_sequence_length = {3};+ std::vector<float> expected_output = {-0.823096991f,+ 0.329842567f,+ 0.0983065218f,+ 0.488791496f,+ -0.656279087f,+ 0.0225083288f,+ -0.406664789f,+ 0.604365826f,+ -0.358047426f,+ 0.000576359977f,+ 0.202674463f,+ 0.408363998f,+ -0.582195282f,+ -0.188379109f,+ 0.200814918f,+ 0.241229594f};+ std::vector<int8_t> expected_present_key = {15, -24, 10, 25, 25, -30, 6, 20, 25, -16, -8, 1,+ 12, 0, 9, 28, -1, -1, -3, 11, -1, -1, -9, 4};+ std::vector<int8_t> expected_present_value = {-21, 16, 0, 4, -6, -1, -19, 18, 10, -30, 4, 22,+ -26, 12, 28, -17, -5, 8, 4, -2, 0, -12, 1, 7};++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{1, 1, 32}, query);+ test_case.add_input<int8_t>(Shape{1, 1, 2, 8}, past_key);+ test_case.add_input<int8_t>(Shape{1, 1, 2, 8}, past_value);+ test_case.add_input<int>(Shape{1, 1}, seqlens_k);+ test_case.add_input<int>(Shape{}, total_sequence_length);+ test_case.add_expected_output<float>(Shape{1, 1, 16}, expected_output);+ test_case.add_expected_output<int8_t>(Shape{1, 1, 3, 8}, expected_present_key);+ test_case.add_expected_output<int8_t>(Shape{1, 1, 3, 8}, expected_present_value);+ // Mixed float output + int8 present KV: run() compares floats by ULP tolerance and int8 exactly. The+ // present KV cache is checked bit-exact; the float output uses a relaxed ULP tolerance because a couple of+ // near-zero attention outputs differ across backends (CPU vs TEMPLATE) by a few low mantissa bits.+ test_case.run(11);+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_i4kv_per_channel) {+ const auto model = convert_model("com.microsoft/gqa_i4kv_per_channel.onnx");++ // Same tiny dynamic-past config as the int8 tests but with a 4-bit KV cache: past/present KV are uint8 with+ // two signed 4-bit values packed per byte (head_size 8 -> packed dim 4), +8 storage bias, per-channel scales.+ std::vector<float> query = {+ -0.3970000f, -0.3313000f, -0.1520000f, -0.4047000f, 0.7195000f, 0.6105000f, 0.5133000f, -0.4848000f,+ 0.6007000f, -0.1116000f, -0.6246000f, 0.3679000f, -0.1469000f, 0.2853000f, 0.6529000f, 0.2068000f,+ 0.4573000f, -0.4164000f, -0.2704000f, -0.0339000f, 0.0057000f, 0.0248000f, 0.2101000f, -0.2351000f,+ -0.0529000f, 0.1247000f, 0.0053000f, 0.1374000f, -0.2304000f, -0.5744000f, -0.0142000f, 0.5319000f};+ std::vector<uint8_t> past_key = {226, 254, 33, 147, 184, 179, 180, 95};+ std::vector<uint8_t> past_value = {77, 81, 103, 126, 224, 84, 47, 39};+ std::vector<int> seqlens_k = {2};+ std::vector<int> total_sequence_length = {3};+ std::vector<float> expected_output = {-0.0702551f,+ 0.1050181f,+ -0.1460064f,+ -0.0686645f,+ -0.0208528f,+ -0.2754714f,+ 0.0222163f,+ 0.0198129f,+ -0.0737319f,+ 0.1057612f,+ -0.1510011f,+ -0.0762182f,+ -0.0134817f,+ -0.2721340f,+ 0.0232418f,+ 0.0025098f};+ std::vector<uint8_t> expected_present_key = {226, 254, 33, 147, 184, 179, 180, 95, 15, 114, 152, 11};+ std::vector<uint8_t> expected_present_value = {77, 81, 103, 126, 224, 84, 47, 39, 183, 168, 0, 247};++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{1, 1, 32}, query);+ test_case.add_input<uint8_t>(Shape{1, 1, 2, 4}, past_key);+ test_case.add_input<uint8_t>(Shape{1, 1, 2, 4}, past_value);+ test_case.add_input<int>(Shape{1, 1}, seqlens_k);+ test_case.add_input<int>(Shape{}, total_sequence_length);+ test_case.add_expected_output<float>(Shape{1, 1, 16}, expected_output);+ test_case.add_expected_output<uint8_t>(Shape{1, 1, 3, 4}, expected_present_key);+ test_case.add_expected_output<uint8_t>(Shape{1, 1, 3, 4}, expected_present_value);+ // Mixed float output + uint8 packed present KV: run() compares floats by ULP tolerance and u8 exactly.+ // A few extra tolerance bits absorb the 7-digit precision of the small expected float literals.+ test_case.run(8);+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_i4kv_per_tensor) {+ const auto model = convert_model("com.microsoft/gqa_i4kv_per_tensor.onnx");++ // 4-bit KV cache with a single per-tensor scale for K and V (dynamic past).+ std::vector<float> query = {+ 0.4508000f, -0.5269000f, 0.4693000f, -0.2170000f, 0.3558000f, 0.1264000f, -0.0002000f, -0.3639000f,+ 0.6855000f, 0.6029000f, 0.1017000f, -0.0686000f, -0.0005000f, -0.3225000f, 0.7487000f, -0.4704000f,+ 0.3172000f, -0.0167000f, 0.4925000f, 0.0029000f, 0.6619000f, 0.4461000f, -0.1889000f, 0.5623000f,+ -0.5318000f, 0.5412000f, 0.6026000f, 0.3187000f, -0.0172000f, 0.6877000f, 0.4319000f, 0.0561000f};+ std::vector<uint8_t> past_key = {208, 146, 243, 230, 6, 46, 248, 39};+ std::vector<uint8_t> past_value = {19, 117, 129, 234, 146, 27, 198, 71};+ std::vector<int> seqlens_k = {2};+ std::vector<int> total_sequence_length = {3};+ std::vector<float> expected_output = {-0.3590473f,+ 0.0615028f,+ 0.1610674f,+ -0.0423852f,+ -0.1424399f,+ 0.2251414f,+ 0.1477317f,+ 0.0210057f,+ -0.3548365f,+ 0.0335620f,+ 0.1403990f,+ -0.0314840f,+ -0.1588975f,+ 0.2111710f,+ 0.1523205f,+ 0.0487500f};+ std::vector<uint8_t> expected_present_key = {208, 146, 243, 230, 6, 46, 248, 39, 143, 143, 255, 244};+ std::vector<uint8_t> expected_present_value = {19, 117, 129, 234, 146, 27, 198, 71, 240, 239, 248, 159};++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{1, 1, 32}, query);+ test_case.add_input<uint8_t>(Shape{1, 1, 2, 4}, past_key);+ test_case.add_input<uint8_t>(Shape{1, 1, 2, 4}, past_value);+ test_case.add_input<int>(Shape{1, 1}, seqlens_k);+ test_case.add_input<int>(Shape{}, total_sequence_length);+ test_case.add_expected_output<float>(Shape{1, 1, 16}, expected_output);+ test_case.add_expected_output<uint8_t>(Shape{1, 1, 3, 4}, expected_present_key);+ test_case.add_expected_output<uint8_t>(Shape{1, 1, 3, 4}, expected_present_value);+ // Mixed float output + uint8 packed present KV: run() compares floats by ULP tolerance and u8 exactly.+ // A few extra tolerance bits absorb the 7-digit precision of the small expected float literals.+ test_case.run(8);+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_i8kv_present_type_is_int8) {+ // Type-inference check: with a quantized KV cache the present_key/present_value outputs must be i8,+ // while the attention output stays float.+ const auto model = convert_model("com.microsoft/gqa_i8kv_per_channel.onnx");+ ASSERT_EQ(model->get_results().size(), 3);+ EXPECT_EQ(model->outputs()[0].get_element_type(), element::f32);+ EXPECT_EQ(model->outputs()[1].get_element_type(), element::i8);+ EXPECT_EQ(model->outputs()[2].get_element_type(), element::i8);+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_qk_norm_unsupported_throws) {+ // q_norm_weight/k_norm_weight (QK-Norm) inputs are not implemented and must be rejected.+ try {+ convert_model("com.microsoft/gqa_qk_norm.onnx");+ FAIL() << "ONNX Importer did not reject unsupported QK-Norm inputs for GroupQueryAttention";+ } catch (const ::ov::frontend::OpConversionFailure& e) {+ EXPECT_THAT(e.what(), testing::HasSubstr("QK-Norm"));+ } catch (const std::exception& e) {+ EXPECT_THAT(e.what(), testing::HasSubstr("QK-Norm"));+ } catch (...) {+ FAIL() << "Unexpected exception type thrown";+ }+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_insufficient_inputs_throws) {try {convert_model("com.microsoft/gqa_oob.onnx");FAIL() << "ONNX Importer did not detect insufficient inputs for GroupQueryAttention";} catch (const ::ov::frontend::OpConversionFailure& e) {- EXPECT_THAT(e.what(), testing::AllOf(testing::HasSubstr("expected"), testing::HasSubstr(" 7 to 14 inputs")));+ EXPECT_THAT(e.what(), testing::AllOf(testing::HasSubstr("expected"), testing::HasSubstr(" 7 to 16 inputs")));} catch (const std::exception& e) {- EXPECT_THAT(e.what(), testing::AllOf(testing::HasSubstr("expected"), testing::HasSubstr(" 7 to 14 inputs")));+ EXPECT_THAT(e.what(), testing::AllOf(testing::HasSubstr("expected"), testing::HasSubstr(" 7 to 16 inputs")));} catch (...) {FAIL() << "Unexpected exception type thrown";}diff --git a/src/frontends/onnx/tests/runtime/ie/unit_test.manifest b/src/frontends/onnx/tests/runtime/ie/unit_test.manifestindex 4ee2eaec07f..4a727acdb3a 100644--- a/src/frontends/onnx/tests/runtime/ie/unit_test.manifest+++ b/src/frontends/onnx/tests/runtime/ie/unit_test.manifest@@ -359,6 +359,14 @@ IE_GPU.onnx_model_gather_elements_int32_axis_0IE_GPU.onnx_model_gather_elements_int8_axis_1IE_GPU.onnx_model_gather_elements_float_3D_axis_2+# GroupQueryAttention with a quantized KV cache appends tokens via ScatterUpdate on i8/u8+# data, for which the GPU plugin has no layout ("No layout format available for scatterupdate+# ... data_type: i8"). The numerics are covered on CPU/INTERPRETER; GPU is skipped here.+IE_GPU.onnx_model_gqa_i8kv_per_channel+IE_GPU.onnx_model_gqa_i8kv_per_tensor+IE_GPU.onnx_model_gqa_i4kv_per_channel+IE_GPU.onnx_model_gqa_i4kv_per_tensor+IE_CPU/ElemTypesTests/1.onnx_test_add_abc_set_precission# RuntimeError: Unsupported dynamic ops: v4::Interpolate - Ticket: 50691