Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[ONNX FE] Convert ONNX Attention op directly to SDPA instead of using function expansion (#34184)

Evgeny Kotov83255c468e77
diff --git a/src/frontends/onnx/docs/supported_ops.md b/src/frontends/onnx/docs/supported_ops.md
index 9c546ed48cf..d0f6fda8d22 100644
--- a/src/frontends/onnx/docs/supported_ops.md
+++ b/src/frontends/onnx/docs/supported_ops.md
@@ -20,6 +20,7 @@ OpenVINO provides support for operations of Default Opset (empty in table below)
| |Asinh |9 |22, 9 | |
| |Atan |7 |22, 7 | |
| |Atanh |9 |22, 9 | |
+| |Attention |23 |23 | |
| |AveragePool |1 |22, 19, 11, 10, 7, 1 | |
| |BatchNormalization |14, 7, 1 |15, 14, 9, 7, 6, 1 | |
| |Bernoulli | |22, 15 | |
diff --git a/src/frontends/onnx/frontend/src/core/transform.hpp b/src/frontends/onnx/frontend/src/core/transform.hpp
index 028cd4bcb7b..0ecde83786d 100644
--- a/src/frontends/onnx/frontend/src/core/transform.hpp
+++ b/src/frontends/onnx/frontend/src/core/transform.hpp
@@ -13,7 +13,7 @@ namespace transform {
using ::ONNX_NAMESPACE::ModelProto;
-static const std::vector<std::string> onnx_functions_to_expand = {"Bernoulli", "Attention"};
+static const std::vector<std::string> onnx_functions_to_expand = {"Bernoulli"};
/// \brief Replace nodes with expanded body of ONNX functions
///
diff --git a/src/frontends/onnx/frontend/src/op/attention.cpp b/src/frontends/onnx/frontend/src/op/attention.cpp
new file mode 100644
index 00000000000..db75a444f1b
--- /dev/null
+++ b/src/frontends/onnx/frontend/src/op/attention.cpp
@@ -0,0 +1,455 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "utils/attention.hpp"
+
+#include "core/null_node.hpp"
+#include "core/operator_set.hpp"
+#include "exceptions.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/divide.hpp"
+#include "openvino/op/greater_eq.hpp"
+#include "openvino/op/matmul.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/range.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/scaled_dot_product_attention.hpp"
+#include "openvino/op/select.hpp"
+#include "openvino/op/shape_of.hpp"
+#include "openvino/op/softmax.hpp"
+#include "openvino/op/sqrt.hpp"
+#include "openvino/op/squeeze.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/op/tanh.hpp"
+#include "openvino/op/tile.hpp"
+#include "openvino/op/transpose.hpp"
+#include "openvino/op/unsqueeze.hpp"
+#include "utils/common.hpp"
+
+using namespace ov::op;
+
+namespace ov {
+namespace frontend {
+namespace onnx {
+namespace ai_onnx {
+namespace detail {
+namespace {
+
+using ov::frontend::onnx::attention::get_dimensions;
+
+// Reshape 3D input (batch, seq, num_heads * head_size) to 4D (batch, num_heads, seq, head_size)
+// Uses direct reshape matching the ONNX Attention spec (no transpose):
+// the hidden dimension is split as [num_heads, seq, head_size] in row-major order.
+ov::Output<ov::Node> reshape_3d_to_4d(const ov::Output<ov::Node>& input, int64_t num_heads) {
+ auto input_shape = std::make_shared<v3::ShapeOf>(input);
+ auto batch = get_dimensions(input_shape, {0});
+ auto seq = get_dimensions(input_shape, {1});
+ auto num_heads_node = v0::Constant::create(ov::element::i64, ov::Shape{1}, {num_heads});
+ auto hidden = get_dimensions(input_shape, {2});
+ auto head_size = std::make_shared<v1::Divide>(hidden, num_heads_node);
+ // Direct reshape to (batch, num_heads, seq, head_size)
+ auto new_shape = std::make_shared<v0::Concat>(ov::NodeVector{batch, num_heads_node, seq, head_size}, 0);
+ return std::make_shared<v1::Reshape>(input, new_shape, false);
+}
+
+// Reshape 4D output (batch, num_heads, seq, head_size) back to 3D (batch, seq, num_heads * head_size)
+ov::Output<ov::Node> reshape_4d_to_3d(const ov::Output<ov::Node>& output) {
+ // Transpose from (batch, num_heads, seq, head_size) to (batch, seq, num_heads, head_size)
+ auto perm = v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});
+ auto transposed = std::make_shared<v1::Transpose>(output, perm);
+ // Reshape to (batch, seq, num_heads * head_size)
+ auto reshape_pattern = v0::Constant::create(ov::element::i32, ov::Shape{3}, {0, 0, -1});
+ return std::make_shared<v1::Reshape>(transposed, reshape_pattern, true);
+}
+
+// Repeat K/V heads for GQA: (B, kv_h, seq, h) -> (B, kv_h * n_rep, seq, h)
+// Uses Tile to match ONNX spec's np.tile(K, [1, n_rep, 1, 1]) behavior.
+// n_rep = q_num_heads / kv_num_heads
+ov::Output<ov::Node> repeat_kv(const ov::Output<ov::Node>& input, int64_t n_rep) {
+ if (n_rep == 1) {
+ return input;
+ }
+ auto repeats = v0::Constant::create(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{1, n_rep, 1, 1});
+ return std::make_shared<v0::Tile>(input, repeats);
+}
+
+// Convert boolean mask to float additive mask: true -> 0.0, false -> -10000.0
+ov::Output<ov::Node> convert_boolean_mask(const ov::Output<ov::Node>& mask, const ov::element::Type& type) {
+ auto zero = v0::Constant::create(type, ov::Shape{}, {0.0f});
+ auto neg_large = v0::Constant::create(type, ov::Shape{}, {-10000.0f});
+ return std::make_shared<v1::Select>(mask, zero, neg_large);
+}
+
+// Build additive causal mask of shape (seq_q, seq_kv): 0 for allowed, -10000 for masked.
+// When use_offset=true, accounts for KV cache offset so that query position i attends to
+// key positions j where j <= i + (seq_kv - seq_q). Use this for KV cache scenarios.
+// When use_offset=false, builds a simple lower-triangular mask matching np.tril(k=0),
+// where query position i attends to key positions j where j <= i.
+ov::Output<ov::Node> build_causal_mask(const ov::Output<ov::Node>& Q, const ov::Output<ov::Node>& K, bool use_offset) {
+ auto q_shape = std::make_shared<v3::ShapeOf>(Q);
+ auto k_shape = std::make_shared<v3::ShapeOf>(K);
+ // Q is 4D: (B, heads, seq_q, head_size), K is 4D: (B, heads, seq_kv, head_size)
+ auto seq_q = get_dimensions(q_shape, {2});
+ auto seq_kv = get_dimensions(k_shape, {2});
+
+ auto zero = v0::Constant::create(ov::element::i64, ov::Shape{}, {0});
+ auto one = v0::Constant::create(ov::element::i64, ov::Shape{}, {1});
+
+ auto seq_q_scalar = std::make_shared<v0::Squeeze>(seq_q, zero);
+ auto seq_kv_scalar = std::make_shared<v0::Squeeze>(seq_kv, zero);
+
+ // Column indices: [0, 1, ..., seq_kv-1]
+ auto col_indices = std::make_shared<v4::Range>(zero, seq_kv_scalar, one, ov::element::i64);
+
+ std::shared_ptr<ov::Node> row_indices;
+ if (use_offset) {
+ // Row indices adjusted for KV cache offset: [offset, offset+1, ..., offset+seq_q-1]
+ auto offset = std::make_shared<v1::Subtract>(seq_kv_scalar, seq_q_scalar);
+ auto end = std::make_shared<v1::Add>(offset, seq_q_scalar);
+ row_indices = std::make_shared<v4::Range>(offset, end, one, ov::element::i64);
+ } else {
+ // Row indices without offset: [0, 1, ..., seq_q-1] (matches np.tril(k=0))
+ row_indices = std::make_shared<v4::Range>(zero, seq_q_scalar, one, ov::element::i64);
+ }
+
+ // Unsqueeze rows to (seq_q, 1) for broadcasting with (seq_kv,)
+ auto axis = v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});
+ auto rows_2d = std::make_shared<v0::Unsqueeze>(row_indices, axis);
+
+ // Lower-triangular: position (i, j) allowed when adjusted_row[i] >= col[j]
+ auto is_allowed = std::make_shared<v1::GreaterEqual>(rows_2d, col_indices);
+
+ // Convert boolean to additive float mask: true -> 0.0, false -> -10000.0
+ return convert_boolean_mask(is_allowed, Q.get_element_type());
+}
+
+// Build SDPA-based attention (primary fast path)
+ov::Output<ov::Node> build_sdpa(const ov::Output<ov::Node>& Q,
+ const ov::Output<ov::Node>& K,
+ const ov::Output<ov::Node>& V,
+ bool has_mask,
+ const ov::Output<ov::Node>& attn_mask,
+ float scale_attr,
+ bool is_causal) {
+ ov::OutputVector inputs{Q, K, V};
+ if (has_mask) {
+ inputs.push_back(attn_mask);
+ }
+ if (scale_attr != 0.0f) {
+ if (!has_mask) {
+ // SDPA interprets inputs positionally (index 3 = mask, index 4 = scale),
+ // so a zero mask placeholder is needed when only scale is provided
+ inputs.push_back(v0::Constant::create(Q.get_element_type(), ov::Shape{}, {0.0f}));
+ }
+ inputs.push_back(v0::Constant::create(Q.get_element_type(), ov::Shape{}, {scale_attr}));
+ }
+ return std::make_shared<v13::ScaledDotProductAttention>(inputs, is_causal)->output(0);
+}
+
+// Build manual attention decomposition (for softcap or qk_matmul_output)
+// Returns {Y, qk_matmul_output_or_null}
+ov::OutputVector build_manual_attention(const ov::Output<ov::Node>& Q,
+ const ov::Output<ov::Node>& K,
+ const ov::Output<ov::Node>& V,
+ bool has_mask,
+ const ov::Output<ov::Node>& attn_mask,
+ float scale_attr,
+ float softcap,
+ bool is_causal,
+ int64_t qk_matmul_output_mode,
+ bool needs_qk_output) {
+ // 1. Q @ K^T
+ auto qk = std::make_shared<v0::MatMul>(Q, K, false, true);
+
+ // 2. Apply scale
+ std::shared_ptr<ov::Node> scaled_qk;
+ if (scale_attr != 0.0f) {
+ auto scale_node = v0::Constant::create(Q.get_element_type(), ov::Shape{}, {scale_attr});
+ scaled_qk = std::make_shared<v1::Multiply>(qk, scale_node);
+ } else {
+ // Default scale: 1/sqrt(head_size). Q is always 4D here: (B, heads, seq, head_size)
+ auto q_shape = std::make_shared<v3::ShapeOf>(Q);
+ auto head_size = get_dimensions(q_shape, {3});
+ auto head_size_f = std::make_shared<v0::Convert>(head_size, Q.get_element_type());
+ auto sqrt_head = std::make_shared<v0::Sqrt>(head_size_f);
+ scaled_qk = std::make_shared<v1::Divide>(qk, sqrt_head);
+ }
+
+ // 3. Apply attention mask and causal mask
+ std::shared_ptr<ov::Node> masked = scaled_qk;
+ if (has_mask) {
+ masked = std::make_shared<v1::Add>(scaled_qk, attn_mask);
+ }
+ if (is_causal) {
+ auto causal_mask = build_causal_mask(Q, K, false);
+ masked = std::make_shared<v1::Add>(masked, causal_mask);
+ }
+
+ // Capture qk_matmul_output at mode 0 (raw QK) or mode 1 (after mask)
+ ov::Output<ov::Node> qk_debug_output;
+ if (needs_qk_output && qk_matmul_output_mode == 0) {
+ qk_debug_output = scaled_qk->output(0);
+ } else if (needs_qk_output && qk_matmul_output_mode == 1) {
+ qk_debug_output = masked->output(0);
+ }
+
+ // 4. Apply softcap: softcap * tanh(scores / softcap)
+ std::shared_ptr<ov::Node> capped = masked;
+ if (softcap > 0.0f) {
+ auto cap = v0::Constant::create(Q.get_element_type(), ov::Shape{}, {softcap});
+ auto divided = std::make_shared<v1::Divide>(masked, cap);
+ auto tanh_out = std::make_shared<v0::Tanh>(divided);
+ capped = std::make_shared<v1::Multiply>(tanh_out, cap);
+ }
+
+ // Capture at mode 2 (after softcap)
+ if (needs_qk_output && qk_matmul_output_mode == 2) {
+ qk_debug_output = capped->output(0);
+ }
+
+ // 5. Softmax
+ auto softmax_out = std::make_shared<v8::Softmax>(capped, -1);
+
+ // Capture at mode 3 (after softmax)
+ if (needs_qk_output && qk_matmul_output_mode == 3) {
+ qk_debug_output = softmax_out->output(0);
+ }
+
+ // 6. softmax @ V
+ auto output = std::make_shared<v0::MatMul>(softmax_out, V);
+
+ ov::OutputVector results;
+ results.push_back(output->output(0));
+ if (needs_qk_output && qk_debug_output.get_node()) {
+ results.push_back(qk_debug_output);
+ } else {
+ results.push_back(ov::Output<ov::Node>{});
+ }
+ return results;
+}
+
+} // namespace
+} // namespace detail
+
+namespace opset_23 {
+ov::OutputVector attention(const ov::frontend::onnx::Node& node) {
+ auto inputs = node.get_ov_inputs();
+ const auto num_inputs = inputs.size();
+ CHECK_VALID_NODE(node, num_inputs >= 3, "Attention expects at least 3 inputs, got: ", num_inputs);
+
+ // Required inputs
+ auto Q = inputs[0];
+ auto K = inputs[1];
+ auto V = inputs[2];
+
+ // Optional inputs
+ bool has_attn_mask = common::is_input_valid(node, 3);
+ bool has_past_key = common::is_input_valid(node, 4);
+ bool has_past_value = common::is_input_valid(node, 5);
+
+ CHECK_VALID_NODE(node,
+ has_past_key == has_past_value,
+ "past_key and past_value must be both present or both absent");
+
+ // Attributes
+ bool is_causal = static_cast<bool>(node.get_attribute_value<int64_t>("is_causal", 0));
+ float scale_attr = node.get_attribute_value<float>("scale", 0.0f);
+ float softcap = node.get_attribute_value<float>("softcap", 0.0f);
+ int64_t qk_matmul_output_mode = node.get_attribute_value<int64_t>("qk_matmul_output_mode", 0);
+ int64_t q_num_heads = node.get_attribute_value<int64_t>("q_num_heads", 0);
+ int64_t kv_num_heads = node.get_attribute_value<int64_t>("kv_num_heads", 0);
+
+ CHECK_VALID_NODE(node, softcap >= 0.0f, "softcap must be non-negative, got: ", softcap);
+ CHECK_VALID_NODE(node,
+ qk_matmul_output_mode >= 0 && qk_matmul_output_mode <= 3,
+ "qk_matmul_output_mode must be 0, 1, 2, or 3, got: ",
+ qk_matmul_output_mode);
+
+ // Determine number of requested outputs
+ size_t num_outputs = node.get_outputs_size();
+ const auto& output_names = node.get_output_names();
+ bool needs_qk_output = output_names.size() > 3 && !output_names[3].get().empty();
+
+ // Determine if inputs are 3D and need reshaping
+ auto q_rank = Q.get_partial_shape().rank();
+ auto k_rank = K.get_partial_shape().rank();
+ auto v_rank = V.get_partial_shape().rank();
+ bool q_is_3d = false;
+ bool kv_is_3d = false;
+
+ if (q_rank.is_static()) {
+ q_is_3d = (q_rank.get_length() == 3);
+ CHECK_VALID_NODE(node,
+ q_is_3d || q_rank.get_length() == 4,
+ "Q input rank must be 3 or 4, got: ",
+ q_rank.get_length());
+ }
+ if (k_rank.is_static()) {
+ kv_is_3d = (k_rank.get_length() == 3);
+ CHECK_VALID_NODE(node,
+ kv_is_3d || k_rank.get_length() == 4,
+ "K input rank must be 3 or 4, got: ",
+ k_rank.get_length());
+ }
+ if (v_rank.is_static()) {
+ CHECK_VALID_NODE(node,
+ v_rank.get_length() == 3 || v_rank.get_length() == 4,
+ "V input rank must be 3 or 4, got: ",
+ v_rank.get_length());
+ }
+
+ // Reshape 3D inputs to 4D
+ if (q_is_3d) {
+ CHECK_VALID_NODE(node, q_num_heads > 0, "q_num_heads attribute is required for 3D Q input");
+ Q = detail::reshape_3d_to_4d(Q, q_num_heads);
+ }
+ if (kv_is_3d) {
+ CHECK_VALID_NODE(node, kv_num_heads > 0, "kv_num_heads attribute is required for 3D K/V inputs");
+ K = detail::reshape_3d_to_4d(K, kv_num_heads);
+ V = detail::reshape_3d_to_4d(V, kv_num_heads);
+ }
+
+ // Handle KV cache: concatenate past K/V with current K/V along sequence dim (axis=2)
+ if (has_past_key) {
+ K = std::make_shared<v0::Concat>(ov::OutputVector{inputs[4], K}, 2);
+ V = std::make_shared<v0::Concat>(ov::OutputVector{inputs[5], V}, 2);
+ }
+
+ // present_key and present_value are the K and V after concatenation (before head expansion)
+ auto present_key = K;
+ auto present_value = V;
+
+ // Handle GQA: expand K/V heads to match Q heads
+ // Case 1: head counts from explicit attributes (3D inputs)
+ if (q_num_heads > 0 && kv_num_heads > 0 && q_num_heads != kv_num_heads) {
+ CHECK_VALID_NODE(node,
+ q_num_heads % kv_num_heads == 0,
+ "q_num_heads must be divisible by kv_num_heads for GQA. q_num_heads=",
+ q_num_heads,
+ ", kv_num_heads=",
+ kv_num_heads);
+ int64_t n_rep = q_num_heads / kv_num_heads;
+ K = detail::repeat_kv(K, n_rep);
+ V = detail::repeat_kv(V, n_rep);
+ }
+ // Case 2: head counts from shapes (4D inputs without attributes)
+ else {
+ auto q_pshape = Q.get_partial_shape();
+ auto k_pshape = K.get_partial_shape();
+ if (q_pshape.rank().is_static() && q_pshape.rank().get_length() == 4 && k_pshape.rank().is_static() &&
+ k_pshape.rank().get_length() == 4 && q_pshape[1].is_static() && k_pshape[1].is_static()) {
+ auto q_heads = q_pshape[1].get_length();
+ auto kv_heads = k_pshape[1].get_length();
+ if (q_heads != kv_heads) {
+ CHECK_VALID_NODE(node,
+ q_heads % kv_heads == 0,
+ "q_heads must be divisible by kv_heads for GQA. q_heads=",
+ q_heads,
+ ", kv_heads=",
+ kv_heads);
+ int64_t n_rep = static_cast<int64_t>(q_heads / kv_heads);
+ K = detail::repeat_kv(K, n_rep);
+ V = detail::repeat_kv(V, n_rep);
+ }
+ }
+ }
+
+ // Prepare attention mask
+ ov::Output<ov::Node> attn_mask;
+ if (has_attn_mask) {
+ attn_mask = inputs[3];
+ if (attn_mask.get_element_type() == ov::element::boolean) {
+ // For manual path, convert boolean to float; for SDPA path, SDPA handles boolean natively
+ if (softcap > 0.0f || needs_qk_output) {
+ attn_mask = detail::convert_boolean_mask(attn_mask, Q.get_element_type());
+ }
+ }
+ }
+
+ // Build an explicit causal mask instead of relying on SDPA's internal is_causal flag.
+ // SDPA's internal mask uses offset-based semantics (ncausal = kv_len - q_len + m + 1)
+ // which doesn't match the ONNX spec's np.tril(k=0) for non-square attention matrices
+ // (seq_q != seq_kv). For KV cache scenarios, use offset to account for past sequence.
+ if (is_causal) {
+ auto causal_mask = detail::build_causal_mask(Q, K, has_past_key);
+ if (has_attn_mask) {
+ attn_mask = std::make_shared<v1::Add>(attn_mask, causal_mask);
+ } else {
+ attn_mask = causal_mask;
+ }
+ has_attn_mask = true;
+ is_causal = false;
+ }
+
+ // Choose execution path
+ ov::Output<ov::Node> Y;
+ ov::Output<ov::Node> qk_debug_output;
+
+ if (softcap > 0.0f || needs_qk_output) {
+ // Manual decomposition path (softcap or debug output)
+ auto results = detail::build_manual_attention(Q,
+ K,
+ V,
+ has_attn_mask,
+ attn_mask,
+ scale_attr,
+ softcap,
+ is_causal,
+ qk_matmul_output_mode,
+ needs_qk_output);
+ Y = results[0];
+ if (needs_qk_output && results.size() > 1 && results[1].get_node()) {
+ qk_debug_output = results[1];
+ }
+ } else {
+ // SDPA path (primary fast path)
+ Y = detail::build_sdpa(Q, K, V, has_attn_mask, attn_mask, scale_attr, is_causal);
+ }
+
+ // Reshape output back to 3D if Q was 3D
+ if (q_is_3d) {
+ Y = detail::reshape_4d_to_3d(Y);
+ }
+
+ // Build output vector.
+ // Output names from the ONNX graph determine which outputs are actually requested.
+ // Empty names indicate unused optional outputs — push NullNode for those to avoid
+ // creating shared input/output parameters that confuse port resolution.
+ ov::OutputVector results;
+ results.push_back(Y);
+
+ if (num_outputs > 1) {
+ if (!output_names[1].get().empty()) {
+ results.push_back(present_key);
+ } else {
+ results.push_back(std::make_shared<NullNode>()->output(0));
+ }
+ }
+ if (num_outputs > 2) {
+ if (!output_names[2].get().empty()) {
+ results.push_back(present_value);
+ } else {
+ results.push_back(std::make_shared<NullNode>()->output(0));
+ }
+ }
+ if (num_outputs > 3) {
+ if (qk_debug_output.get_node() && !output_names[3].get().empty()) {
+ results.push_back(qk_debug_output);
+ } else {
+ results.push_back(std::make_shared<NullNode>()->output(0));
+ }
+ }
+
+ return results;
+}
+ONNX_OP("Attention", OPSET_SINCE(1), ai_onnx::opset_23::attention);
+} // namespace opset_23
+
+} // namespace ai_onnx
+} // namespace onnx
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/attention.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/attention.cpp
index 18d45a2ffdf..94295436a19 100644
--- a/src/frontends/onnx/frontend/src/op/com.microsoft/attention.cpp
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/attention.cpp
@@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include "utils/attention.hpp"
+
#include "core/null_node.hpp"
#include "core/operator_set.hpp"
#include "openvino/frontend/exception.hpp"
@@ -119,15 +121,7 @@ ONNX_OP("Attention", OPSET_SINCE(1), com_microsoft::opset_1::attention, MICROSOF
namespace detail {
namespace {
-std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<v3::ShapeOf>& shape, const std::vector<int>& dims) {
- static const auto zero = v0::Constant::create(ov::element::i32, ov::Shape{}, {0});
- const auto dims_const = v0::Constant::create(ov::element::i32, ov::Shape{dims.size()}, dims);
- return std::make_shared<v8::Gather>(shape, dims_const, zero);
-}
-
-std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<ov::Node>& node, const std::vector<int>& dims) {
- return get_dimensions(std::make_shared<v3::ShapeOf>(node), dims);
-}
+using ov::frontend::onnx::attention::get_dimensions;
std::shared_ptr<ov::Node> get_hidden_size(const std::shared_ptr<v3::ShapeOf>& node_shape) {
// node has shape (batch_size, sequence_length, 3 * hidden_size)
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.cpp
index 26ba01a741c..cf839ffcef3 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,10 +11,10 @@
#include "exceptions.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/divide.hpp"
-#include "openvino/op/gather.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/transpose.hpp"
+#include "utils/attention.hpp"
#include "utils/common.hpp"
#include "utils/split.hpp"
@@ -23,9 +23,7 @@ using namespace ov::op;
namespace ov::frontend::onnx::com_microsoft {
namespace detail {
-namespace {
-std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<v3::ShapeOf>& shape, const std::vector<int>& dims);
-} // namespace
+using ov::frontend::onnx::attention::get_dimensions;
} // namespace detail
namespace opset_1 {
@@ -107,14 +105,4 @@ ONNX_OP("GroupQueryAttention", OPSET_SINCE(1), com_microsoft::opset_1::group_que
} // namespace opset_1
-namespace detail {
-namespace {
-std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<v3::ShapeOf>& shape, const std::vector<int>& dims) {
- static const auto zero = v0::Constant::create(ov::element::i32, ov::Shape{}, {0});
- const auto dims_const = v0::Constant::create(ov::element::i32, ov::Shape{dims.size()}, dims);
- return std::make_shared<v8::Gather>(shape, dims_const, zero);
-}
-} // namespace
-} // namespace detail
-
} // namespace ov::frontend::onnx::com_microsoft
diff --git a/src/frontends/onnx/frontend/src/utils/attention.cpp b/src/frontends/onnx/frontend/src/utils/attention.cpp
new file mode 100644
index 00000000000..4164c1f7714
--- /dev/null
+++ b/src/frontends/onnx/frontend/src/utils/attention.cpp
@@ -0,0 +1,30 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "utils/attention.hpp"
+
+#include "openvino/op/constant.hpp"
+#include "openvino/op/gather.hpp"
+
+using namespace ov::op;
+
+namespace ov {
+namespace frontend {
+namespace onnx {
+namespace attention {
+
+std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<v3::ShapeOf>& shape, const std::vector<int>& dims) {
+ static const auto zero = v0::Constant::create(ov::element::i32, ov::Shape{}, {0});
+ const auto dims_const = v0::Constant::create(ov::element::i32, ov::Shape{dims.size()}, dims);
+ return std::make_shared<v8::Gather>(shape, dims_const, zero);
+}
+
+std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<ov::Node>& node, const std::vector<int>& dims) {
+ return get_dimensions(std::make_shared<v3::ShapeOf>(node), dims);
+}
+
+} // namespace attention
+} // namespace onnx
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/onnx/frontend/src/utils/attention.hpp b/src/frontends/onnx/frontend/src/utils/attention.hpp
new file mode 100644
index 00000000000..32d9dca2509
--- /dev/null
+++ b/src/frontends/onnx/frontend/src/utils/attention.hpp
@@ -0,0 +1,31 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#pragma once
+
+#include <memory>
+#include <vector>
+
+#include "openvino/core/node.hpp"
+#include "openvino/op/shape_of.hpp"
+
+namespace ov {
+namespace frontend {
+namespace onnx {
+namespace attention {
+
+/// \brief Extracts specific dimensions from a ShapeOf node using Gather.
+///
+/// \param shape A ShapeOf node whose output contains the tensor's shape.
+/// \param dims Indices of the dimensions to extract.
+/// \return A Gather node producing the selected dimension values.
+std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<ov::op::v3::ShapeOf>& shape,
+ const std::vector<int>& dims);
+
+/// \brief Convenience overload: computes ShapeOf for the given node, then extracts dimensions.
+std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<ov::Node>& node, const std::vector<int>& dims);
+
+} // namespace attention
+} // namespace onnx
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/onnx/tests/models/attention_batch2.prototxt b/src/frontends/onnx/tests/models/attention_batch2.prototxt
new file mode 100644
index 00000000000..202fd2a660b
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_batch2.prototxt
@@ -0,0 +1,34 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ }
+ name: "attention_batch2"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 2 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 2 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 2 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 2 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_causal.prototxt b/src/frontends/onnx/tests/models/attention_causal.prototxt
new file mode 100644
index 00000000000..16524f8b29e
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_causal.prototxt
@@ -0,0 +1,35 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "is_causal" i: 1 type: INT }
+ }
+ name: "attention_causal"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_causal_softcap.prototxt b/src/frontends/onnx/tests/models/attention_causal_softcap.prototxt
new file mode 100644
index 00000000000..57163d90456
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_causal_softcap.prototxt
@@ -0,0 +1,36 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "is_causal" i: 1 type: INT }
+ attribute { name: "softcap" f: 2.0 type: FLOAT }
+ }
+ name: "attention_causal_softcap"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_custom_scale.prototxt b/src/frontends/onnx/tests/models/attention_custom_scale.prototxt
new file mode 100644
index 00000000000..b78a442c22d
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_custom_scale.prototxt
@@ -0,0 +1,35 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "scale" f: 0.5 type: FLOAT }
+ }
+ name: "attention_custom_scale"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_diff_seq_len.prototxt b/src/frontends/onnx/tests/models/attention_diff_seq_len.prototxt
new file mode 100644
index 00000000000..65330a2e949
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_diff_seq_len.prototxt
@@ -0,0 +1,34 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ }
+ name: "attention_diff_seq_len"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 2 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 6 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 6 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 2 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_gqa_3d.prototxt b/src/frontends/onnx/tests/models/attention_gqa_3d.prototxt
new file mode 100644
index 00000000000..1216eb93cdd
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_gqa_3d.prototxt
@@ -0,0 +1,36 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "q_num_heads" i: 4 type: INT }
+ attribute { name: "kv_num_heads" i: 2 type: INT }
+ }
+ name: "attention_gqa_3d"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 3 } dim { dim_value: 32 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 3 } dim { dim_value: 16 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 3 } dim { dim_value: 16 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 3 } dim { dim_value: 32 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_gqa_4d.prototxt b/src/frontends/onnx/tests/models/attention_gqa_4d.prototxt
new file mode 100644
index 00000000000..f958c903a76
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_gqa_4d.prototxt
@@ -0,0 +1,36 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "q_num_heads" i: 4 type: INT }
+ attribute { name: "kv_num_heads" i: 2 type: INT }
+ }
+ name: "attention_gqa_4d"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_gqa_causal.prototxt b/src/frontends/onnx/tests/models/attention_gqa_causal.prototxt
new file mode 100644
index 00000000000..6033b2013f6
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_gqa_causal.prototxt
@@ -0,0 +1,37 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "q_num_heads" i: 4 type: INT }
+ attribute { name: "kv_num_heads" i: 2 type: INT }
+ attribute { name: "is_causal" i: 1 type: INT }
+ }
+ name: "attention_gqa_causal"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_kv_cache.prototxt b/src/frontends/onnx/tests/models/attention_kv_cache.prototxt
new file mode 100644
index 00000000000..5be3dd22c47
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_kv_cache.prototxt
@@ -0,0 +1,59 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ input: ""
+ input: "past_key"
+ input: "past_value"
+ output: "Y"
+ output: "present_key"
+ output: "present_value"
+ name: "Attention_1"
+ op_type: "Attention"
+ }
+ name: "attention_kv_cache"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "past_key"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 3 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "past_value"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 3 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "present_key"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "present_value"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_kv_cache_causal.prototxt b/src/frontends/onnx/tests/models/attention_kv_cache_causal.prototxt
new file mode 100644
index 00000000000..8d7b8da5aab
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_kv_cache_causal.prototxt
@@ -0,0 +1,60 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ input: ""
+ input: "past_key"
+ input: "past_value"
+ output: "Y"
+ output: "present_key"
+ output: "present_value"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "is_causal" i: 1 type: INT }
+ }
+ name: "attention_kv_cache_causal"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "past_key"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 3 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "past_value"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 3 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "present_key"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "present_value"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_kv_cache_gqa.prototxt b/src/frontends/onnx/tests/models/attention_kv_cache_gqa.prototxt
new file mode 100644
index 00000000000..a24281ffbd9
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_kv_cache_gqa.prototxt
@@ -0,0 +1,61 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ input: ""
+ input: "past_key"
+ input: "past_value"
+ output: "Y"
+ output: "present_key"
+ output: "present_value"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "q_num_heads" i: 4 type: INT }
+ attribute { name: "kv_num_heads" i: 2 type: INT }
+ }
+ name: "attention_kv_cache_gqa"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "past_key"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 3 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "past_value"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 3 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 1 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "present_key"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "present_value"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_mask_bool.prototxt b/src/frontends/onnx/tests/models/attention_mask_bool.prototxt
new file mode 100644
index 00000000000..9376f1a1015
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_mask_bool.prototxt
@@ -0,0 +1,40 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ input: "attn_mask"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ }
+ name: "attention_mask_bool"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "attn_mask"
+ type { tensor_type { elem_type: 9
+ shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_mask_float.prototxt b/src/frontends/onnx/tests/models/attention_mask_float.prototxt
new file mode 100644
index 00000000000..d8410f1fb65
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_mask_float.prototxt
@@ -0,0 +1,40 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ input: "attn_mask"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ }
+ name: "attention_mask_float"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "attn_mask"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_mha_3d.prototxt b/src/frontends/onnx/tests/models/attention_mha_3d.prototxt
new file mode 100644
index 00000000000..a043471c2f1
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_mha_3d.prototxt
@@ -0,0 +1,36 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "q_num_heads" i: 2 type: INT }
+ attribute { name: "kv_num_heads" i: 2 type: INT }
+ }
+ name: "attention_mha_3d"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 6 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 6 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 6 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 6 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_mha_4d.prototxt b/src/frontends/onnx/tests/models/attention_mha_4d.prototxt
new file mode 100644
index 00000000000..f8461dcaa04
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_mha_4d.prototxt
@@ -0,0 +1,34 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ }
+ name: "attention_mha_4d"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_mqa_3d.prototxt b/src/frontends/onnx/tests/models/attention_mqa_3d.prototxt
new file mode 100644
index 00000000000..be8f7d9d0c7
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_mqa_3d.prototxt
@@ -0,0 +1,36 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "q_num_heads" i: 4 type: INT }
+ attribute { name: "kv_num_heads" i: 1 type: INT }
+ }
+ name: "attention_mqa_3d"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 3 } dim { dim_value: 32 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 3 } dim { dim_value: 8 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 3 } dim { dim_value: 8 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 3 } dim { dim_value: 32 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_mqa_4d.prototxt b/src/frontends/onnx/tests/models/attention_mqa_4d.prototxt
new file mode 100644
index 00000000000..9b99f2f4175
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_mqa_4d.prototxt
@@ -0,0 +1,36 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "q_num_heads" i: 4 type: INT }
+ attribute { name: "kv_num_heads" i: 1 type: INT }
+ }
+ name: "attention_mqa_4d"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_present_no_past.prototxt b/src/frontends/onnx/tests/models/attention_present_no_past.prototxt
new file mode 100644
index 00000000000..adf935d4617
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_present_no_past.prototxt
@@ -0,0 +1,46 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ output: "present_key"
+ output: "present_value"
+ name: "Attention_1"
+ op_type: "Attention"
+ }
+ name: "attention_present_no_past"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "present_key"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "present_value"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_softcap.prototxt b/src/frontends/onnx/tests/models/attention_softcap.prototxt
new file mode 100644
index 00000000000..a9de364a51a
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_softcap.prototxt
@@ -0,0 +1,35 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "softcap" f: 2.0 type: FLOAT }
+ }
+ name: "attention_softcap"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_softcap_mask_bool.prototxt b/src/frontends/onnx/tests/models/attention_softcap_mask_bool.prototxt
new file mode 100644
index 00000000000..690de1586ee
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_softcap_mask_bool.prototxt
@@ -0,0 +1,41 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ input: "attn_mask"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "softcap" f: 2.0 type: FLOAT }
+ }
+ name: "attention_softcap_mask_bool"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "attn_mask"
+ type { tensor_type { elem_type: 9
+ shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/models/attention_softcap_mask_float.prototxt b/src/frontends/onnx/tests/models/attention_softcap_mask_float.prototxt
new file mode 100644
index 00000000000..bae33133b90
--- /dev/null
+++ b/src/frontends/onnx/tests/models/attention_softcap_mask_float.prototxt
@@ -0,0 +1,41 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "Q"
+ input: "K"
+ input: "V"
+ input: "attn_mask"
+ output: "Y"
+ name: "Attention_1"
+ op_type: "Attention"
+ attribute { name: "softcap" f: 2.0 type: FLOAT }
+ }
+ name: "attention_softcap_mask_float"
+ input {
+ name: "Q"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "K"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "V"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+ input {
+ name: "attn_mask"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 1 } dim { dim_value: 4 } dim { dim_value: 4 } } } }
+ }
+ output {
+ name: "Y"
+ type { tensor_type { elem_type: 1
+ shape { dim { dim_value: 1 } dim { dim_value: 2 } dim { dim_value: 4 } dim { dim_value: 3 } } } }
+ }
+}
+opset_import { version: 23 }
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index be89b28dae2..67a3dc71323 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -7362,3 +7362,848 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_layer_normalization_biased) {
test_case.run_with_tolerance_as_fp(0.0001f);
}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_mha_4d) {
+ auto model = convert_model("attention_mha_4d.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3)
+ test_case.add_input<float>({0.496714f, -0.138264f, 0.647689f, 1.523030f, -0.234153f, -0.234137f,
+ 1.579213f, 0.767435f, -0.469474f, 0.542560f, -0.463418f, -0.465730f,
+ 0.241962f, -1.913280f, -1.724918f, -0.562288f, -1.012831f, 0.314247f,
+ -0.908024f, -1.412304f, 1.465649f, -0.225776f, 0.067528f, -1.424748f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.544383f, 0.110923f, -1.150994f, 0.375698f, -0.600639f, -0.291694f,
+ -0.601707f, 1.852278f, -0.013497f, -1.057711f, 0.822545f, -1.220844f,
+ 0.208864f, -1.959670f, -1.328186f, 0.196861f, 0.738467f, 0.171368f,
+ -0.115648f, -0.301104f, -1.478522f, -0.719844f, -0.460639f, 1.057122f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({0.343618f, -1.763040f, 0.324084f, -0.385082f, -0.676922f, 0.611676f,
+ 1.031000f, 0.931280f, -0.839218f, -0.309212f, 0.331263f, 0.975545f,
+ -0.479174f, -0.185659f, -1.106335f, -1.196207f, 0.812526f, 1.356240f,
+ -0.072010f, 1.003533f, 0.361636f, -0.645120f, 0.361396f, 1.538037f});
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {0.143204f, -0.312989f, 0.232224f, -0.004593f, -0.555314f, 0.383667f, 0.235821f, -0.206320f,
+ 0.154587f, 0.028000f, -0.570602f, 0.413158f, -0.428041f, 0.006260f, -0.832314f, -0.546679f,
+ 0.298202f, 0.292189f, -0.625760f, 0.324462f, 0.962753f, -0.387417f, 0.509615f, 0.017205f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_mha_3d) {
+ auto model = convert_model("attention_mha_3d.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 4, 6)
+ test_case.add_input<float>({-0.035826f, 1.564644f, -2.619745f, 0.821903f, 0.087047f, -0.299007f,
+ 0.091761f, -1.987569f, -0.219672f, 0.357113f, 1.477894f, -0.518270f,
+ -0.808494f, -0.501757f, 0.915402f, 0.328751f, -0.529760f, 0.513267f,
+ 0.097078f, 0.968645f, -0.702053f, -0.327662f, -0.392108f, -1.463515f});
+ // K: (1, 4, 6)
+ test_case.add_input<float>({0.296120f, 0.261055f, 0.005113f, -0.234587f, -1.415371f, -0.420645f,
+ -0.342715f, -0.802277f, -0.161286f, 0.404051f, 1.886186f, 0.174578f,
+ 0.257550f, -0.074446f, -1.918771f, -0.026514f, 0.060230f, 2.463242f,
+ -0.192361f, 0.301547f, -0.034712f, -1.168678f, 1.142823f, 0.751933f});
+ // V: (1, 4, 6)
+ test_case.add_input<float>({0.791032f, -0.909387f, 1.402794f, -1.401851f, 0.586857f, 2.190456f,
+ -0.990536f, -0.566298f, 0.099651f, -0.503476f, -1.550663f, 0.068563f,
+ -1.062304f, 0.473592f, -0.919424f, 1.549934f, -0.783253f, -0.322062f,
+ 0.813517f, -1.230864f, 0.227460f, 1.307143f, -1.607483f, 0.184634f});
+ test_case.add_expected_output<float>(
+ Shape{1, 4, 6},
+ {-0.376205f, -1.164383f, 0.494832f, 1.251880f, -1.010314f, -0.134396f, -0.433258f, -0.718601f,
+ 0.898995f, 0.979683f, -0.832299f, -0.209895f, -1.080272f, 0.093997f, 1.487117f, 0.203439f,
+ -0.572602f, -0.311992f, -0.340959f, -1.273095f, 0.406927f, -0.541346f, 0.026989f, -0.646756f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_gqa_4d) {
+ auto model = convert_model("attention_gqa_4d.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 4, 4, 3)
+ test_case.add_input<float>({0.259883f, 0.781823f, -1.236951f, -1.320457f, 0.521942f, 0.296985f, 0.250493f,
+ 0.346448f, -0.680025f, 0.232254f, 0.293072f, -0.714351f, 1.865775f, 0.473833f,
+ -1.191303f, 0.656554f, -0.974682f, 0.787085f, 1.158596f, -0.820682f, 0.963376f,
+ 0.412781f, 0.822060f, 1.896793f, -0.245388f, -0.753736f, -0.889514f, -0.815810f,
+ -0.077102f, 0.341152f, 0.276691f, 0.827183f, 0.013002f, 1.453534f, -0.264657f,
+ 2.720169f, 0.625667f, -0.857158f, -1.070892f, 0.482472f, -0.223463f, 0.714001f,
+ 0.473238f, -0.072829f, -0.846794f, -1.514847f, -0.446515f, 0.856399f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({0.214094f, -1.245739f, 0.173181f, 0.385317f, -0.883857f, 0.153725f,
+ 0.058209f, -1.142970f, 0.357787f, 0.560785f, 1.083051f, 1.053802f,
+ -1.377669f, -0.937825f, 0.515035f, 0.513786f, 0.515048f, 3.852731f,
+ 0.570890f, 1.135566f, 0.954002f, 0.651391f, -0.315269f, 0.758969f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.772825f, -0.236819f, -0.485364f, 0.081874f, 2.314659f, -1.867265f,
+ 0.686260f, -1.612716f, -0.471932f, 1.088951f, 0.064280f, -1.077745f,
+ -0.715304f, 0.679598f, -0.730367f, 0.216459f, 0.045572f, -0.651600f,
+ 2.143944f, 0.633919f, -2.025143f, 0.186454f, -0.661786f, 0.852433f});
+ test_case.add_expected_output<float>(
+ Shape{1, 4, 4, 3},
+ {0.357977f, 0.265504f, -1.042098f, 0.420300f, 0.040855f, -0.967495f, 0.294989f, 0.216078f,
+ -1.011940f, 0.274893f, 0.215012f, -1.007991f, 1.105739f, 0.072415f, -0.710253f, 0.288786f,
+ 0.032542f, -0.470010f, 0.351593f, 0.006968f, -0.509201f, 0.309487f, 0.065849f, -0.691483f,
+ 0.049388f, 0.099175f, -0.919799f, 0.268812f, 0.031392f, -0.937286f, 0.548290f, 0.176453f,
+ -1.039254f, 0.600669f, 0.085532f, -1.012268f, 0.247689f, 0.055143f, -0.270079f, 0.423475f,
+ 0.074996f, -0.605837f, 0.572569f, 0.121736f, -0.539797f, -0.127635f, 0.346899f, -0.659494f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_mqa_4d) {
+ auto model = convert_model("attention_mqa_4d.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 4, 4, 3)
+ test_case.add_input<float>({-0.792521f, -0.114736f, 0.504987f, 0.865755f, -1.200296f, -0.334501f, -0.474945f,
+ -0.653329f, 1.765454f, 0.404982f, -1.260884f, 0.917862f, 2.122156f, 1.032465f,
+ -1.519370f, -0.484234f, 1.266911f, -0.707669f, 0.443819f, 0.774634f, -0.926930f,
+ -0.059525f, -3.241267f, -1.024388f, -0.252568f, -1.247783f, 1.632411f, -1.430141f,
+ -0.440044f, 0.130741f, 1.441273f, -1.435862f, 1.163164f, 0.010233f, -0.981509f,
+ 0.462103f, 0.199060f, -0.600217f, 0.069802f, -0.385314f, 0.113517f, 0.662131f,
+ 1.586017f, -1.237815f, 2.133033f, -1.952088f, -0.151785f, 0.588317f});
+ // K: (1, 1, 4, 3)
+ test_case.add_input<float>({0.280992f,
+ -0.622699f,
+ -0.208122f,
+ -0.493001f,
+ -0.589365f,
+ 0.849602f,
+ 0.357015f,
+ -0.692910f,
+ 0.899600f,
+ 0.307300f,
+ 0.812862f,
+ 0.629629f});
+ // V: (1, 1, 4, 3)
+ test_case.add_input<float>({-0.828995f,
+ -0.560181f,
+ 0.747294f,
+ 0.610370f,
+ -0.020902f,
+ 0.117327f,
+ 1.277665f,
+ -0.591571f,
+ 0.547097f,
+ -0.202193f,
+ -0.217681f,
+ 1.098777f});
+ test_case.add_expected_output<float>(
+ Shape{1, 4, 4, 3},
+ {0.337986f, -0.303017f, 0.547312f, 0.205010f, -0.423954f, 0.599464f, 0.558864f, -0.305937f,
+ 0.481555f, 0.487686f, -0.377153f, 0.524956f, -0.169225f, -0.387052f, 0.832382f, 0.005277f,
+ -0.308193f, 0.762964f, -0.027680f, -0.357686f, 0.754208f, 0.108195f, -0.432638f, 0.547678f,
+ 0.597206f, -0.330012f, 0.463645f, 0.332367f, -0.286605f, 0.489778f, 0.550417f, -0.424551f,
+ 0.558099f, 0.385289f, -0.361417f, 0.535184f, 0.273678f, -0.371122f, 0.586114f, 0.322549f,
+ -0.312850f, 0.592683f, 0.691556f, -0.417891f, 0.548147f, 0.397891f, -0.242785f, 0.454272f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_gqa_3d) {
+ auto model = convert_model("attention_gqa_3d.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 3, 32)
+ test_case.add_input<float>(
+ {0.496714f, -0.138264f, 0.647689f, 1.523030f, -0.234153f, -0.234137f, 1.579213f, 0.767435f, -0.469474f,
+ 0.542560f, -0.463418f, -0.465730f, 0.241962f, -1.913280f, -1.724918f, -0.562288f, -1.012831f, 0.314247f,
+ -0.908024f, -1.412304f, 1.465649f, -0.225776f, 0.067528f, -1.424748f, -0.544383f, 0.110923f, -1.150994f,
+ 0.375698f, -0.600639f, -0.291694f, -0.601707f, 1.852278f, -0.013497f, -1.057711f, 0.822545f, -1.220844f,
+ 0.208864f, -1.959670f, -1.328186f, 0.196861f, 0.738467f, 0.171368f, -0.115648f, -0.301104f, -1.478522f,
+ -0.719844f, -0.460639f, 1.057122f, 0.343618f, -1.763040f, 0.324084f, -0.385082f, -0.676922f, 0.611676f,
+ 1.031000f, 0.931280f, -0.839218f, -0.309212f, 0.331263f, 0.975545f, -0.479174f, -0.185659f, -1.106335f,
+ -1.196207f, 0.812526f, 1.356240f, -0.072010f, 1.003533f, 0.361636f, -0.645120f, 0.361396f, 1.538037f,
+ -0.035826f, 1.564644f, -2.619745f, 0.821903f, 0.087047f, -0.299007f, 0.091761f, -1.987569f, -0.219672f,
+ 0.357113f, 1.477894f, -0.518270f, -0.808494f, -0.501757f, 0.915402f, 0.328751f, -0.529760f, 0.513267f,
+ 0.097078f, 0.968645f, -0.702053f, -0.327662f, -0.392108f, -1.463515f});
+ // K: (1, 3, 16)
+ test_case.add_input<float>({0.296120f, 0.261055f, 0.005113f, -0.234587f, -1.415371f, -0.420645f, -0.342715f,
+ -0.802277f, -0.161286f, 0.404051f, 1.886186f, 0.174578f, 0.257550f, -0.074446f,
+ -1.918771f, -0.026514f, 0.060230f, 2.463242f, -0.192361f, 0.301547f, -0.034712f,
+ -1.168678f, 1.142823f, 0.751933f, 0.791032f, -0.909387f, 1.402794f, -1.401851f,
+ 0.586857f, 2.190456f, -0.990536f, -0.566298f, 0.099651f, -0.503476f, -1.550663f,
+ 0.068563f, -1.062304f, 0.473592f, -0.919424f, 1.549934f, -0.783253f, -0.322062f,
+ 0.813517f, -1.230864f, 0.227460f, 1.307143f, -1.607483f, 0.184634f});
+ // V: (1, 3, 16)
+ test_case.add_input<float>({0.259883f, 0.781823f, -1.236951f, -1.320457f, 0.521942f, 0.296985f, 0.250493f,
+ 0.346448f, -0.680025f, 0.232254f, 0.293072f, -0.714351f, 1.865775f, 0.473833f,
+ -1.191303f, 0.656554f, -0.974682f, 0.787085f, 1.158596f, -0.820682f, 0.963376f,
+ 0.412781f, 0.822060f, 1.896793f, -0.245388f, -0.753736f, -0.889514f, -0.815810f,
+ -0.077102f, 0.341152f, 0.276691f, 0.827183f, 0.013002f, 1.453534f, -0.264657f,
+ 2.720169f, 0.625667f, -0.857158f, -1.070892f, 0.482472f, -0.223463f, 0.714001f,
+ 0.473238f, -0.072829f, -0.846794f, -1.514847f, -0.446515f, 0.856399f});
+ test_case.add_expected_output<float>(
+ Shape{1, 3, 32},
+ {-0.704795f, 0.707981f, 0.593540f, -0.898167f, 1.008786f, 0.399956f, 0.432802f, 1.435332f, -0.019993f,
+ 1.311787f, -0.201133f, 2.317111f, 0.445180f, -0.896102f, -0.965436f, 0.532888f, -0.251821f, 0.630770f,
+ -0.325360f, -1.050858f, 0.983863f, 0.369530f, -0.032157f, 0.748155f, -0.048911f, 1.124254f, -0.210208f,
+ 1.941960f, 0.327995f, -0.843507f, -0.842911f, 0.574183f, -0.474955f, 0.530429f, 0.053854f, -0.919247f,
+ 1.248044f, 0.406687f, -0.272292f, 0.869132f, -0.184070f, 0.404185f, -0.092606f, 0.242662f, -0.321041f,
+ -0.812016f, -0.344628f, 0.774335f, -0.372639f, 0.473363f, -0.174731f, -0.937587f, 1.314551f, 0.406163f,
+ -0.511668f, 0.651625f, -0.183792f, 0.282770f, -0.218555f, 0.203643f, -0.240109f, -0.643703f, -0.287215f,
+ 0.768312f, -0.479064f, 0.635002f, 0.117315f, -0.960196f, 1.059176f, 0.390321f, 0.084115f, 1.038653f,
+ -0.054916f, 1.087806f, -0.209541f, 1.864920f, 0.302044f, -0.836003f, -0.818654f, 0.582872f, -0.866851f,
+ 0.733830f, 0.921344f, -0.842817f, 1.020930f, 0.411125f, 0.593132f, 1.678388f, -0.154474f, 0.530392f,
+ -0.150549f, 0.603883f, -0.158533f, -0.775794f, -0.438776f, 0.729069f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_causal) {
+ auto model = convert_model("attention_causal.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3)
+ test_case.add_input<float>({0.120296f, 0.514439f, 0.711615f, -1.124642f, -1.534114f, 1.277677f,
+ 0.332314f, -0.748487f, 1.551152f, 0.115675f, 1.179297f, 0.067518f,
+ 2.060748f, 1.755341f, -0.248964f, 0.971571f, 0.645376f, 1.368632f,
+ -0.964923f, 0.686051f, 1.058424f, -1.758739f, -1.183259f, -2.039232f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.269407f, 0.717542f, 1.502357f, 0.074095f, 1.628615f, -1.380101f,
+ -1.703382f, -0.055548f, 0.384065f, -0.032695f, -2.067442f, -0.089120f,
+ -1.304469f, 0.669673f, 0.366598f, -0.939880f, -0.513867f, -1.059214f,
+ -0.062679f, 0.955142f, -0.985726f, 0.504046f, -0.530258f, -0.792873f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.107030f, -1.035242f, -0.553649f, -1.197878f, 1.964725f, 0.035264f,
+ -0.699726f, 0.213980f, -0.112328f, -0.220970f, 0.614167f, 0.757508f,
+ -0.530501f, -0.575818f, -0.275052f, -2.301921f, -1.515191f, 1.366874f,
+ 1.644968f, -0.249036f, 0.576557f, 0.311250f, 3.078881f, 1.119575f});
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {-0.107030f, -1.035242f, -0.553649f, -0.151585f, -0.912711f, -0.529596f, -0.308122f, -0.584946f,
+ -0.412347f, -0.755049f, 0.745656f, -0.131881f, -0.530501f, -0.575818f, -0.275052f, -0.891396f,
+ -0.767198f, 0.059461f, -0.424143f, -0.658454f, 0.108633f, -1.459665f, -0.765383f, 1.122714f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_mask_float) {
+ auto model = convert_model("attention_mask_float.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.127918f, -0.955540f, -1.606446f, 0.203464f, -0.756351f, -1.422254f,
+ -0.646573f, -1.081548f, 1.687142f, 0.881640f, -0.007973f, 1.479944f,
+ 0.077368f, -0.861284f, 1.523124f, 0.538910f, -1.037246f, -0.190339f,
+ -0.875618f, -1.382800f, 0.926178f, 1.909417f, -1.398568f, 0.562969f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.650643f, -0.487125f, -0.592394f, -0.863991f, 0.048522f, -0.830950f,
+ 0.270457f, -0.050238f, -0.238948f, -0.907564f, -0.576771f, 0.755391f,
+ 0.500917f, -0.977555f, 0.099332f, 0.751387f, -1.669405f, 0.543360f,
+ -0.662624f, 0.570599f, -0.763259f, -1.804882f, -1.627542f, 0.048085f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({0.259723f, -0.904317f, 0.638592f, -1.661520f, -0.066080f, -1.211016f,
+ -0.651836f, 0.047399f, -0.860413f, -0.384556f, 1.006293f, -0.576892f,
+ 0.835692f, -1.129707f, 0.529804f, 1.441569f, -2.471644f, -0.796895f,
+ 0.577072f, -0.203045f, 0.371146f, -0.603985f, 0.086590f, -0.155677f});
+ // attn_mask: (1, 1, 4, 4) — lower triangular float mask
+ test_case.add_input<float>({0.0f,
+ -10000.0f,
+ -10000.0f,
+ -10000.0f,
+ 0.0f,
+ 0.0f,
+ -10000.0f,
+ -10000.0f,
+ 0.0f,
+ 0.0f,
+ 0.0f,
+ -10000.0f,
+ 0.0f,
+ 0.0f,
+ 0.0f,
+ 0.0f});
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {0.259723f, -0.904317f, 0.638592f, -0.670611f, -0.498413f, -0.257053f, -0.529244f, -0.382512f,
+ -0.320152f, -0.514261f, 0.291701f, -0.555729f, 0.835692f, -1.129707f, 0.529804f, 1.204720f,
+ -1.947056f, -0.278264f, 1.169140f, -1.832562f, -0.273470f, 1.201381f, -2.001133f, -0.412585f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_mask_bool) {
+ auto model = convert_model("attention_mask_bool.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3)
+ test_case.add_input<float>({1.167782f, 0.254421f, 0.337603f, -0.411877f, -0.487606f, -0.432558f,
+ 0.394452f, -0.420984f, 0.289775f, 2.075401f, 0.871125f, -0.326024f,
+ 1.201214f, -0.408075f, -2.038125f, -1.008086f, -1.870792f, -0.351513f,
+ 0.018418f, 1.676437f, 0.326927f, -0.219101f, 0.829406f, -2.211135f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({0.235615f, 0.770865f, -1.478586f, 1.143754f, 0.338496f, -0.415288f,
+ 0.632782f, 2.270693f, 0.181866f, 0.248221f, -0.459361f, -0.849844f,
+ 0.830336f, -0.856084f, 0.071566f, -0.477657f, 0.478980f, 0.333662f,
+ 1.037540f, -0.510016f, -0.269875f, -0.978764f, -0.444293f, 0.377301f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({0.756989f, -0.922165f, 0.869606f, 1.355638f, 0.413435f, 1.876796f,
+ -0.773789f, -1.244655f, -1.778720f, 1.496044f, 0.654366f, -0.055585f,
+ 0.279969f, -1.125489f, 2.445752f, 0.129221f, 0.109395f, 0.725767f,
+ 0.481009f, 0.223884f, -0.790474f, 0.471468f, 1.882025f, 1.345420f});
+ // attn_mask: (1, 1, 4, 4) bool — lower triangular
+ test_case.add_input<bool>(
+ {true, false, false, false, true, true, false, false, true, true, true, false, true, true, true, true});
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {0.756989f, -0.922165f, 0.869606f, 1.003041f, -0.373216f, 1.283574f, 0.603106f, -0.411673f,
+ 0.590585f, 0.369631f, -0.489830f, 0.009988f, 0.279969f, -1.125489f, 2.445752f, 0.231076f,
+ -0.724973f, 1.887900f, 0.229914f, -0.068798f, 0.681135f, 0.338080f, 0.258214f, 0.664186f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_custom_scale) {
+ auto model = convert_model("attention_custom_scale.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3)
+ test_case.add_input<float>({1.593187f, -0.511216f, -0.989605f, -0.125787f, 0.055725f, 1.094192f,
+ -1.692465f, 1.529550f, -0.158008f, -0.426881f, -1.012104f, -1.654857f,
+ 0.823171f, 0.073318f, -1.289961f, -1.295079f, -0.335785f, 1.669021f,
+ -0.259591f, -1.503143f, -0.245743f, -0.272724f, -2.696887f, -0.054295f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.230935f, 0.696206f, 1.848956f, 1.126565f, -0.268889f, -1.106526f,
+ 2.573360f, 0.059218f, 0.013929f, -0.024125f, 0.198085f, -0.144360f,
+ -0.573662f, -0.546859f, -0.032753f, -0.543425f, -0.712846f, 0.106430f,
+ -0.254977f, 1.503993f, -2.650970f, 1.091507f, 1.246085f, -2.073390f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.342688f, -0.371441f, -1.407512f, -0.777817f, -1.110576f, 1.752270f,
+ 0.935678f, 1.271555f, 0.721672f, -1.129052f, -0.524520f, 0.489375f,
+ -1.222128f, 0.712998f, -0.240325f, -0.374821f, 0.710960f, 0.444263f,
+ -0.360966f, 1.159330f, -1.081063f, 0.615936f, 0.593101f, -0.309546f});
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {0.175016f, 0.297030f, 1.008947f, -0.314262f, -0.197272f, -0.397901f, -0.615684f, -0.441686f,
+ -0.360722f, -0.612660f, -0.599629f, 1.158077f, 0.054589f, 0.835598f, -0.574787f, -0.731885f,
+ 0.722422f, 0.079574f, -0.591538f, 0.747547f, -0.051516f, -0.699099f, 0.720287f, 0.091489f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_kv_cache) {
+ auto model = convert_model("attention_kv_cache.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 1, 3)
+ test_case.add_input<float>({0.326133f, -1.251114f, 0.924027f, -0.184902f, -0.522723f, 1.049009f});
+ // K: (1, 2, 1, 3)
+ test_case.add_input<float>({-0.704344f, -1.408461f, -1.556629f, 0.606010f, -1.280429f, 1.754794f});
+ // V: (1, 2, 1, 3)
+ test_case.add_input<float>({-2.081929f, 1.696456f, 0.211017f, -0.096713f, -0.544919f, 0.399136f});
+ // past_key: (1, 2, 3, 3)
+ test_case.add_input<float>({-0.037635f,
+ 1.103302f,
+ 0.114228f,
+ 0.150302f,
+ -0.363612f,
+ -0.056946f,
+ 0.307802f,
+ -1.710168f,
+ -1.348185f,
+ 0.743264f,
+ 0.170865f,
+ -0.183983f,
+ 0.018434f,
+ 0.347582f,
+ -0.539760f,
+ -0.778305f,
+ 0.195845f,
+ -0.978373f});
+ // past_value: (1, 2, 3, 3)
+ test_case.add_input<float>({0.408253f,
+ -1.702584f,
+ 1.029156f,
+ 0.472597f,
+ 0.256030f,
+ 0.982691f,
+ 1.665474f,
+ 1.014370f,
+ -1.840874f,
+ -1.279577f,
+ -0.624819f,
+ 0.026091f,
+ 0.517659f,
+ -0.725744f,
+ 0.186767f,
+ -0.755383f,
+ -0.611518f,
+ -1.406661f});
+ // Y: (1, 2, 1, 3)
+ test_case.add_expected_output<float>(Shape{1, 2, 1, 3},
+ {0.340146f, 0.676396f, -0.278156f, -0.247441f, -0.581235f, 0.156636f});
+ // present_key: (1, 2, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {-0.037635f, 1.103302f, 0.114228f, 0.150302f, -0.363612f, -0.056946f, 0.307802f, -1.710168f,
+ -1.348185f, -0.704344f, -1.408461f, -1.556629f, 0.743264f, 0.170865f, -0.183983f, 0.018434f,
+ 0.347582f, -0.539760f, -0.778305f, 0.195845f, -0.978373f, 0.606010f, -1.280429f, 1.754794f});
+ // present_value: (1, 2, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {0.408253f, -1.702584f, 1.029156f, 0.472597f, 0.256030f, 0.982691f, 1.665474f, 1.014370f,
+ -1.840874f, -2.081929f, 1.696456f, 0.211017f, -1.279577f, -0.624819f, 0.026091f, 0.517659f,
+ -0.725744f, 0.186767f, -0.755383f, -0.611518f, -1.406661f, -0.096713f, -0.544919f, 0.399136f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_softcap) {
+ auto model = convert_model("attention_softcap.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.923233f, -1.351685f, -0.975873f, 1.053642f, -0.949399f, 2.632382f,
+ 0.493318f, 0.184836f, -0.858358f, 0.700310f, -0.575638f, 0.122010f,
+ 2.560085f, -0.096060f, 1.149273f, -0.703176f, -0.034988f, 1.770801f,
+ -0.626967f, 1.812449f, 0.707752f, -0.562467f, 0.632408f, 0.972554f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({0.621810f, -1.570225f, -0.727137f, -0.247519f, -0.074433f, 0.620672f,
+ 0.177701f, -1.335344f, 0.380198f, 0.610586f, 0.559790f, 1.080781f,
+ 0.833922f, 0.459180f, -0.070166f, -1.660961f, 0.429618f, 0.207688f,
+ 0.271579f, -1.276749f, -1.081057f, 1.053153f, -0.039555f, 0.681501f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({0.028318f, 0.029756f, 0.938284f, -0.516045f, 0.096121f, -0.462275f,
+ -0.434496f, -0.309172f, 0.222134f, -0.478749f, 1.255756f, -0.894607f,
+ -0.186872f, -0.439731f, 1.446978f, 0.196555f, 1.031844f, -1.485560f,
+ 0.267050f, 0.889631f, 0.082284f, 1.065480f, -0.517288f, 1.409347f});
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {-0.217309f, -0.010507f, 0.432435f, -0.418403f, 0.393266f, -0.258099f, -0.274749f, 0.222249f,
+ 0.149638f, -0.297697f, 0.183658f, 0.133947f, 0.548682f, -0.324988f, 1.232658f, 0.393897f,
+ 0.368712f, -0.122598f, 0.260566f, 0.421482f, -0.243643f, 0.329124f, 0.370001f, -0.109046f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_causal_softcap) {
+ auto model = convert_model("attention_causal_softcap.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3) — same inputs as attention_causal test
+ test_case.add_input<float>({0.120296f, 0.514439f, 0.711615f, -1.124642f, -1.534114f, 1.277677f,
+ 0.332314f, -0.748487f, 1.551152f, 0.115675f, 1.179297f, 0.067518f,
+ 2.060748f, 1.755341f, -0.248964f, 0.971571f, 0.645376f, 1.368632f,
+ -0.964923f, 0.686051f, 1.058424f, -1.758739f, -1.183259f, -2.039232f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.269407f, 0.717542f, 1.502357f, 0.074095f, 1.628615f, -1.380101f,
+ -1.703382f, -0.055548f, 0.384065f, -0.032695f, -2.067442f, -0.089120f,
+ -1.304469f, 0.669673f, 0.366598f, -0.939880f, -0.513867f, -1.059214f,
+ -0.062679f, 0.955142f, -0.985726f, 0.504046f, -0.530258f, -0.792873f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.107030f, -1.035242f, -0.553649f, -1.197878f, 1.964725f, 0.035264f,
+ -0.699726f, 0.213980f, -0.112328f, -0.220970f, 0.614167f, 0.757508f,
+ -0.530501f, -0.575818f, -0.275052f, -2.301921f, -1.515191f, 1.366874f,
+ 1.644968f, -0.249036f, 0.576557f, 0.311250f, 3.078881f, 1.119575f});
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {-0.201853f, -0.724072f, -0.430133f, -0.234045f, -0.630293f, -0.405222f, -0.332883f, -0.469627f,
+ -0.355742f, -0.732414f, 0.696189f, -0.129281f, -0.326636f, -0.078002f, 0.361213f, -0.583892f,
+ -0.368495f, 0.273408f, -0.393912f, -0.557303f, 0.172777f, -0.918016f, -0.294001f, 0.962115f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_mqa_3d) {
+ auto model = convert_model("attention_mqa_3d.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 3, 32) — 3D MQA with q_num_heads=4, kv_num_heads=1
+ test_case.add_input<float>(
+ {0.214094f, -1.245739f, 0.173181f, 0.385317f, -0.883857f, 0.153725f, 0.058209f, -1.142970f, 0.357787f,
+ 0.560785f, 1.083051f, 1.053802f, -1.377669f, -0.937825f, 0.515035f, 0.513786f, 0.515048f, 3.852731f,
+ 0.570890f, 1.135566f, 0.954002f, 0.651391f, -0.315269f, 0.758969f, -0.772825f, -0.236819f, -0.485364f,
+ 0.081874f, 2.314659f, -1.867265f, 0.686260f, -1.612716f, -0.471932f, 1.088951f, 0.064280f, -1.077745f,
+ -0.715304f, 0.679598f, -0.730367f, 0.216459f, 0.045572f, -0.651600f, 2.143944f, 0.633919f, -2.025143f,
+ 0.186454f, -0.661786f, 0.852433f, -0.792521f, -0.114736f, 0.504987f, 0.865755f, -1.200296f, -0.334501f,
+ -0.474945f, -0.653329f, 1.765454f, 0.404982f, -1.260884f, 0.917862f, 2.122156f, 1.032465f, -1.519370f,
+ -0.484234f, 1.266911f, -0.707669f, 0.443819f, 0.774634f, -0.926930f, -0.059525f, -3.241267f, -1.024388f,
+ -0.252568f, -1.247783f, 1.632411f, -1.430141f, -0.440044f, 0.130741f, 1.441273f, -1.435862f, 1.163164f,
+ 0.010233f, -0.981509f, 0.462103f, 0.199060f, -0.600217f, 0.069802f, -0.385314f, 0.113517f, 0.662131f,
+ 1.586017f, -1.237815f, 2.133033f, -1.952088f, -0.151785f, 0.588317f});
+ // K: (1, 3, 8)
+ test_case.add_input<float>({0.280992f, -0.622699f, -0.208122f, -0.493001f, -0.589365f, 0.849602f,
+ 0.357015f, -0.692910f, 0.899600f, 0.307300f, 0.812862f, 0.629629f,
+ -0.828995f, -0.560181f, 0.747294f, 0.610370f, -0.020902f, 0.117327f,
+ 1.277665f, -0.591571f, 0.547097f, -0.202193f, -0.217681f, 1.098777f});
+ // V: (1, 3, 8)
+ test_case.add_input<float>({0.825416f, 0.813510f, 1.305479f, 0.021004f, 0.681953f, -0.310267f,
+ 0.324166f, -0.130143f, 0.096996f, 0.595157f, -0.818221f, 2.092387f,
+ -1.006017f, -1.214189f, 1.158111f, 0.791663f, 0.624120f, 0.628345f,
+ -0.012247f, -0.897254f, 0.075805f, -0.677162f, 0.975120f, -0.147057f});
+ test_case.add_expected_output<float>(
+ Shape{1, 3, 32},
+ {0.584764f, 0.724167f, 0.503125f, 0.499875f, 0.105035f, -0.625041f, 0.657639f, 0.136957f, 0.561326f,
+ 0.682043f, 0.229291f, 0.147373f, 0.011446f, -0.687052f, 0.802709f, 0.090234f, 0.468962f, 0.677608f,
+ 0.096148f, 0.687705f, -0.176568f, -0.779856f, 0.830123f, 0.257721f, 0.620987f, 0.722240f, 0.533878f,
+ 0.244191f, 0.174668f, -0.592084f, 0.659237f, 0.063294f, 0.270539f, 0.623238f, -0.452431f, 1.312983f,
+ -0.630656f, -1.021292f, 1.040076f, 0.519944f, 0.573690f, 0.686437f, 0.269369f, 0.120435f, 0.040868f,
+ -0.671063f, 0.786234f, 0.076085f, 0.596900f, 0.703859f, 0.398307f, 0.179405f, 0.106362f, -0.632444f,
+ 0.724410f, 0.069487f, 0.463907f, 0.680591f, 0.107749f, 0.755809f, -0.183249f, -0.781909f, 0.820756f,
+ 0.273910f, 0.387734f, 0.625262f, -0.304596f, 0.584345f, -0.396129f, -0.906919f, 1.017432f, 0.299585f,
+ 0.380968f, 0.640075f, -0.225713f, 0.804712f, -0.392961f, -0.899516f, 0.968444f, 0.344800f, 0.544325f,
+ 0.695915f, 0.290788f, 0.422251f, -0.006723f, -0.690354f, 0.758272f, 0.153030f, 0.550951f, 0.630505f,
+ -0.084503f, -0.401461f, -0.066788f, -0.745357f, 0.977725f, -0.002030f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_kv_cache_causal) {
+ auto model = convert_model("attention_kv_cache_causal.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 1, 3) — single token with KV cache + causal masking
+ test_case.add_input<float>({1.382159f, 0.648710f, -0.167118f, 0.146714f, 1.206509f, -0.816936f});
+ // K: (1, 2, 1, 3)
+ test_case.add_input<float>({0.368673f, -0.393339f, 0.028745f, 1.278452f, 0.191099f, 0.046437f});
+ // V: (1, 2, 1, 3)
+ test_case.add_input<float>({-1.359856f, 0.746254f, 0.645484f, 2.163255f, -0.307778f, 0.219150f});
+ // past_key: (1, 2, 3, 3)
+ test_case.add_input<float>({0.249384f,
+ 1.577453f,
+ -0.095296f,
+ 0.279022f,
+ 0.607897f,
+ 0.186609f,
+ -0.446434f,
+ 0.194090f,
+ 1.073632f,
+ -1.026515f,
+ 0.132970f,
+ -0.700121f,
+ 1.195047f,
+ -1.523187f,
+ -0.558922f,
+ 0.377212f,
+ 1.565524f,
+ -0.065750f});
+ // past_value: (1, 2, 3, 3)
+ test_case.add_input<float>({-0.555200f,
+ 1.881157f,
+ -1.448014f,
+ -2.198806f,
+ 0.440014f,
+ -0.502054f,
+ -1.021233f,
+ 0.708356f,
+ 0.243801f,
+ -0.564079f,
+ -1.280304f,
+ 0.872457f,
+ 0.650201f,
+ -0.099176f,
+ 1.846637f,
+ -1.070085f,
+ -1.525525f,
+ -0.691908f});
+ // Y: (1, 2, 1, 3)
+ test_case.add_expected_output<float>(Shape{1, 2, 1, 3},
+ {-1.230125f, 1.108099f, -0.550524f, -0.184113f, -1.118260f, 0.035077f});
+ // present_key: (1, 2, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {0.249384f, 1.577453f, -0.095296f, 0.279022f, 0.607897f, 0.186609f, -0.446434f, 0.194090f,
+ 1.073632f, 0.368673f, -0.393339f, 0.028745f, -1.026515f, 0.132970f, -0.700121f, 1.195047f,
+ -1.523187f, -0.558922f, 0.377212f, 1.565524f, -0.065750f, 1.278452f, 0.191099f, 0.046437f});
+ // present_value: (1, 2, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {-0.555200f, 1.881157f, -1.448014f, -2.198806f, 0.440014f, -0.502054f, -1.021233f, 0.708356f,
+ 0.243801f, -1.359856f, 0.746254f, 0.645484f, -0.564079f, -1.280304f, 0.872457f, 0.650201f,
+ -0.099176f, 1.846637f, -1.070085f, -1.525525f, -0.691908f, 2.163255f, -0.307778f, 0.219150f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_kv_cache_gqa) {
+ auto model = convert_model("attention_kv_cache_gqa.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 4, 1, 3) — GQA with KV cache, q_num_heads=4, kv_num_heads=2
+ test_case.add_input<float>({-0.045586f,
+ 0.243339f,
+ -0.241236f,
+ 0.352055f,
+ -1.251539f,
+ 1.443765f,
+ -0.082151f,
+ 1.117296f,
+ 0.342725f,
+ 0.456753f,
+ 0.569767f,
+ 0.447709f});
+ // K: (1, 2, 1, 3)
+ test_case.add_input<float>({0.642723f, 1.329153f, 0.196521f, 0.709004f, -0.089736f, 1.440117f});
+ // V: (1, 2, 1, 3)
+ test_case.add_input<float>({-0.676392f, 1.800940f, -0.040158f, -1.430775f, 0.128104f, -0.681052f});
+ // past_key: (1, 2, 3, 3)
+ test_case.add_input<float>({0.840644f,
+ -0.652624f,
+ -0.446183f,
+ -1.889541f,
+ -0.452306f,
+ -2.423879f,
+ -1.583903f,
+ 0.760415f,
+ 0.785800f,
+ 0.425458f,
+ -0.966976f,
+ -0.047711f,
+ -0.003603f,
+ -1.158365f,
+ 1.503398f,
+ 0.877362f,
+ -0.220964f,
+ 0.026886f});
+ // past_value: (1, 2, 3, 3)
+ test_case.add_input<float>({0.208383f,
+ -2.041735f,
+ -0.247177f,
+ -0.681984f,
+ -1.001620f,
+ -0.281100f,
+ 1.797687f,
+ 0.640843f,
+ -0.571179f,
+ 0.572583f,
+ 1.399355f,
+ 0.924634f,
+ 0.059630f,
+ -0.646937f,
+ 0.698223f,
+ 0.393485f,
+ 0.895193f,
+ 0.635172f});
+ // Y: (1, 4, 1, 3)
+ test_case.add_expected_output<float>(Shape{1, 4, 1, 3},
+ {0.076141f,
+ -0.128282f,
+ -0.279242f,
+ -0.229337f,
+ -0.029947f,
+ 0.363563f,
+ 0.338080f,
+ 0.709321f,
+ -0.281886f,
+ -0.308233f,
+ 0.372735f,
+ 0.221593f});
+ // present_key: (1, 2, 4, 3) — kv_num_heads=2, not q_num_heads=4
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {0.840644f, -0.652624f, -0.446183f, -1.889541f, -0.452306f, -2.423879f, -1.583903f, 0.760415f,
+ 0.785800f, 0.642723f, 1.329153f, 0.196521f, 0.425458f, -0.966976f, -0.047711f, -0.003603f,
+ -1.158365f, 1.503398f, 0.877362f, -0.220964f, 0.026886f, 0.709004f, -0.089736f, 1.440117f});
+ // present_value: (1, 2, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {0.208383f, -2.041735f, -0.247177f, -0.681984f, -1.001620f, -0.281100f, 1.797687f, 0.640843f,
+ -0.571179f, -0.676392f, 1.800940f, -0.040158f, 0.572583f, 1.399355f, 0.924634f, 0.059630f,
+ -0.646937f, 0.698223f, 0.393485f, 0.895193f, 0.635172f, -1.430775f, 0.128104f, -0.681052f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_diff_seq_len) {
+ auto model = convert_model("attention_diff_seq_len.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 2, 3) — seq_q=2, seq_kv=6 (cross-attention-like)
+ test_case.add_input<float>({1.049553f,
+ -0.535235f,
+ 1.317394f,
+ 0.197600f,
+ 2.075261f,
+ -0.689188f,
+ 1.735964f,
+ 0.197911f,
+ -0.651418f,
+ -0.483886f,
+ -0.320347f,
+ 0.424166f});
+ // K: (1, 2, 6, 3)
+ test_case.add_input<float>(
+ {0.522835f, -0.573700f, -0.024355f, 2.142270f, 1.727543f, 0.436324f, 0.038003f, 0.120031f, 0.613518f,
+ -1.022793f, -0.257377f, -1.668584f, 0.399223f, 0.647196f, -0.483186f, 1.573987f, -1.225766f, -1.464375f,
+ 0.224452f, 1.047098f, 1.683928f, -0.458884f, 1.078681f, -0.038508f, -0.172627f, 0.883660f, 0.652323f,
+ -1.576392f, 1.476540f, 1.380091f, -0.625563f, 0.395804f, 0.494030f, 0.260674f, -0.550305f, -0.671623f});
+ // V: (1, 2, 6, 3)
+ test_case.add_input<float>(
+ {-0.025554f, 1.172729f, 0.543600f, -0.370614f, 0.771699f, -2.848543f, 1.148766f, -1.739714f, -0.362441f,
+ -1.119670f, -1.294681f, 1.160827f, -0.467701f, 0.346504f, -0.046921f, 0.477041f, 0.076822f, -1.282992f,
+ 0.996267f, -0.493757f, -1.556582f, -0.428115f, 1.500760f, 0.850222f, -0.348652f, -0.349258f, -0.321635f,
+ 2.076748f, 0.381935f, 0.430042f, 1.030283f, 0.238789f, -0.259042f, -0.196350f, -0.071601f, -0.037222f});
+ // Y: (1, 2, 2, 3)
+ test_case.add_expected_output<float>(Shape{1, 2, 2, 3},
+ {0.087744f,
+ 0.192022f,
+ -1.163300f,
+ -0.317679f,
+ 0.342993f,
+ -1.627566f,
+ 0.149217f,
+ 0.118020f,
+ -0.205577f,
+ 0.717496f,
+ 0.189523f,
+ -0.146707f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_softcap_mask_float) {
+ auto model = convert_model("attention_softcap_mask_float.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3) — softcap=2.0 with float mask (manual decomposition path)
+ test_case.add_input<float>({0.727630f, 0.051946f, 0.732640f, -0.080717f, 0.078635f, -1.998201f,
+ 0.916328f, 0.346488f, 0.998010f, -2.896255f, 2.088375f, -0.139590f,
+ 1.108183f, -1.039906f, 0.612774f, -1.053416f, -0.623769f, 1.914031f,
+ -0.190682f, 0.217433f, 0.870068f, 0.495682f, 0.150419f, 0.364961f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({2.403416f, -0.057619f, 0.201099f, 1.050654f, 1.105526f, 1.187030f,
+ 0.638730f, -1.143005f, 1.633432f, -1.146345f, 0.302635f, -0.754276f,
+ -0.064138f, 0.328762f, 0.321357f, 0.421921f, 1.613711f, 0.453534f,
+ -0.244157f, 0.964087f, 1.189471f, -1.227608f, 0.597400f, 0.701173f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.297563f, 1.375707f, -0.150056f, 0.125576f, -0.173072f, 0.015579f,
+ -1.096275f, -1.440051f, 1.594505f, -0.846961f, -0.991392f, -2.153390f,
+ -0.638962f, -1.323090f, 1.642015f, 1.009817f, -0.688150f, 2.252436f,
+ 0.981766f, -0.324831f, -2.499406f, 2.290943f, -1.389573f, -1.645399f});
+ // attn_mask: (1, 1, 4, 4) — lower triangular float mask
+ test_case.add_input<float>({0.0f,
+ -10000.0f,
+ -10000.0f,
+ -10000.0f,
+ 0.0f,
+ 0.0f,
+ -10000.0f,
+ -10000.0f,
+ 0.0f,
+ 0.0f,
+ 0.0f,
+ -10000.0f,
+ 0.0f,
+ 0.0f,
+ 0.0f,
+ 0.0f});
+ // Y: (1, 2, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {-0.337778f, 1.083041f, -0.154105f, -0.339207f, 0.462505f, -0.138030f, -0.370519f, 0.002809f,
+ 0.361551f, -0.745861f, -0.868438f, -1.783549f, 0.011989f, -1.158582f, 0.926068f, 0.141611f,
+ -1.070733f, 1.395292f, 0.616303f, -0.716178f, -0.001065f, 0.855951f, -0.863237f, 0.067472f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_softcap_mask_bool) {
+ auto model = convert_model("attention_softcap_mask_bool.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3) — softcap=2.0 with boolean mask (manual path, bool->float conversion)
+ test_case.add_input<float>({1.022570f, 2.439752f, 1.384273f, 0.563909f, 0.594754f, 0.853416f,
+ 0.758929f, 0.281191f, 0.104201f, -0.062593f, -0.753965f, -0.280675f,
+ -1.692957f, -0.098340f, -0.988591f, -1.103589f, 0.179894f, 1.392002f,
+ 0.918317f, -1.570501f, -0.989628f, 0.940771f, -0.982487f, -0.224633f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({0.550052f, -0.968344f, 0.105376f, -1.334026f, -0.601368f, 0.319782f,
+ -1.592994f, 0.440475f, -0.019638f, 0.552490f, 0.223914f, 1.364140f,
+ 0.125225f, -0.429406f, 0.122298f, 0.543298f, 0.048860f, 0.040592f,
+ -0.701992f, -0.662901f, -1.402605f, 1.749577f, -1.243863f, -0.692905f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.718407f, 0.894924f, -0.294950f, 1.247742f, -0.673491f, 0.278994f,
+ -0.835347f, 2.145149f, -1.187598f, 0.309821f, 0.633777f, 0.413799f,
+ -0.185288f, -0.129821f, 0.043811f, -0.147002f, 0.963879f, 2.210523f,
+ -0.557492f, -1.369803f, -0.088282f, 2.579709f, -0.803675f, 1.639117f});
+ // attn_mask: (1, 1, 4, 4) bool — lower triangular (true=attend)
+ test_case.add_input<bool>(
+ {true, false, false, false, true, true, false, false, true, true, true, false, true, true, true, true});
+ // Y: (1, 2, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {-0.241921f, 0.798990f, -0.230361f, 0.031411f, 0.426047f, -0.109958f, -0.235019f, 0.815910f,
+ -0.332928f, 0.011776f, 0.637913f, -0.187854f, 0.077533f, -0.218485f, 0.436238f, -0.007613f,
+ 0.145932f, 0.949036f, -0.284543f, -0.485247f, 0.518275f, 1.190462f, -0.484776f, 1.201895f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_batch2) {
+ auto model = convert_model("attention_batch2.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (2, 2, 4, 3) — batch_size=2
+ test_case.add_input<float>({1.677701f, -0.553588f, 0.568983f, 1.628397f, -0.379128f, -0.203580f, -0.581681f,
+ -1.014757f, -0.649278f, -1.223940f, 0.034083f, -0.769973f, 0.233786f, -1.555896f,
+ 0.330880f, 0.833529f, -1.993736f, 0.374057f, 1.227669f, -1.209641f, 1.672572f,
+ 0.419019f, -0.705012f, -0.055769f, 0.558327f, 0.076005f, 0.538756f, -0.920674f,
+ 0.169361f, -1.413715f, -0.111226f, -0.903908f, -0.735530f, 1.236093f, 1.091310f,
+ 0.609138f, -1.092313f, -0.316408f, 1.213098f, 0.141717f, 2.319330f, 0.393318f,
+ 0.192049f, -0.309116f, 0.133541f, -0.152470f, 0.708109f, 0.956702f});
+ // K: (2, 2, 4, 3)
+ test_case.add_input<float>({-0.785989f, -1.331233f, -1.836205f, 0.507991f, -1.103367f, -2.152891f, 0.388579f,
+ 2.493000f, -0.006071f, 0.838491f, 0.081829f, -0.098890f, 0.919077f, -0.290275f,
+ 0.267392f, 0.321698f, -0.668090f, 0.992042f, -0.174960f, -0.755745f, 0.536510f,
+ -0.898468f, 0.028181f, -0.009119f, 1.085896f, 0.474698f, -0.025027f, 0.817766f,
+ 1.390208f, 0.557810f, 0.010353f, -1.311836f, -1.065114f, -0.305225f, -0.609512f,
+ -0.186971f, 0.056650f, 0.529693f, -0.070499f, 0.486502f, 0.064474f, -1.975467f,
+ -0.939335f, -0.144088f, -1.209695f, 0.599929f, 1.530751f, 1.218762f});
+ // V: (2, 2, 4, 3)
+ test_case.add_input<float>({-0.213443f, 1.490726f, 0.148667f, -0.337086f, -0.613403f, -0.302470f, -0.388177f,
+ 0.170416f, 0.160574f, 0.003046f, 0.436938f, 1.190646f, 0.949554f, -1.484898f,
+ -2.553921f, 0.934320f, -1.366879f, -0.224765f, -1.170113f, -1.801980f, 0.541463f,
+ 0.759155f, -0.576510f, -2.591042f, -0.546244f, 0.391804f, -1.478912f, 0.183360f,
+ -0.015310f, 0.579292f, 0.119580f, -0.973069f, 1.196571f, -0.158530f, -0.027305f,
+ -0.933268f, -0.443282f, -0.884803f, -0.172946f, 1.711708f, -1.371901f, -1.613561f,
+ 1.471170f, -0.209324f, -0.669073f, 1.039905f, -0.605616f, 1.826010f});
+ // Y: (2, 2, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{2, 2, 4, 3},
+ {-0.166310f, 0.212826f, 0.543234f, -0.213388f, 0.101926f, 0.318213f, -0.244788f, 0.644003f,
+ 0.065711f, -0.246961f, 0.717591f, 0.121477f, 0.259517f, -1.429630f, -0.810381f, 0.316639f,
+ -1.466488f, -0.807307f, 0.461846f, -1.459568f, -0.789374f, 0.353310f, -1.386105f, -1.080682f,
+ -0.116249f, -0.031849f, -0.247218f, -0.044228f, -0.413143f, 0.152197f, -0.035514f, -0.486822f,
+ 0.250045f, -0.074804f, 0.083506f, -0.153397f, 0.785283f, -0.624480f, 0.412796f, 0.880455f,
+ -0.668594f, 1.260619f, 0.931097f, -0.776981f, -0.151116f, 0.801838f, -0.673882f, 0.952639f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_present_no_past) {
+ auto model = convert_model("attention_present_no_past.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 2, 4, 3) — 3 outputs without past (present = current K/V)
+ test_case.add_input<float>({0.677926f, -0.487911f, 2.157308f, -0.605715f, 0.742095f, 0.299293f,
+ 1.301741f, 1.561511f, 0.032004f, -0.753418f, 0.459972f, -0.677715f,
+ 2.013387f, 0.136535f, -0.365322f, 0.184680f, -1.347126f, -0.971614f,
+ 1.200414f, -0.656894f, -1.046911f, 0.536653f, 1.185704f, 0.718953f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({0.996048f, -0.756795f, -1.421811f, 1.501334f, -0.322680f, -0.250833f,
+ 1.328194f, 0.556230f, 0.455888f, 2.165002f, -0.643518f, 0.927840f,
+ 0.057013f, 0.268592f, 1.528468f, 0.507836f, 0.538296f, 1.072507f,
+ -0.364953f, -0.839210f, -1.044809f, -1.966357f, 2.056207f, -1.103208f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.221254f, -0.276813f, 0.307407f, 0.815737f, 0.860474f, -0.583077f,
+ -0.167122f, 0.282580f, -0.248691f, 1.607346f, 0.490975f, 0.734878f,
+ 0.662881f, 1.173474f, 0.181022f, -1.296832f, 0.399688f, -0.651357f,
+ -0.528617f, 0.586364f, 1.238283f, 0.021272f, 0.308833f, 1.702215f});
+ // Y: (1, 2, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {1.135819f, 0.473164f, 0.391118f, 0.387219f, 0.351068f, -0.033320f, 0.515863f, 0.408149f,
+ -0.001368f, 0.225511f, 0.208291f, 0.004389f, -0.568707f, 0.630642f, 0.114516f, -0.462505f,
+ 0.597372f, 1.021277f, -0.496153f, 0.599542f, 0.792319f, -0.305059f, 0.656953f, 0.223635f});
+ // present_key: (1, 2, 4, 3) — same as K (no past to concatenate)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {0.996048f, -0.756795f, -1.421811f, 1.501334f, -0.322680f, -0.250833f, 1.328194f, 0.556230f,
+ 0.455888f, 2.165002f, -0.643518f, 0.927840f, 0.057013f, 0.268592f, 1.528468f, 0.507836f,
+ 0.538296f, 1.072507f, -0.364953f, -0.839210f, -1.044809f, -1.966357f, 2.056207f, -1.103208f});
+ // present_value: (1, 2, 4, 3) — same as V (no past to concatenate)
+ test_case.add_expected_output<float>(
+ Shape{1, 2, 4, 3},
+ {-0.221254f, -0.276813f, 0.307407f, 0.815737f, 0.860474f, -0.583077f, -0.167122f, 0.282580f,
+ -0.248691f, 1.607346f, 0.490975f, 0.734878f, 0.662881f, 1.173474f, 0.181022f, -1.296832f,
+ 0.399688f, -0.651357f, -0.528617f, 0.586364f, 1.238283f, 0.021272f, 0.308833f, 1.702215f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_gqa_causal) {
+ auto model = convert_model("attention_gqa_causal.onnx");
+ auto test_case = test::TestCase(model, s_device);
+ // Q: (1, 4, 4, 3) — GQA with causal masking, q_num_heads=4, kv_num_heads=2
+ test_case.add_input<float>({0.240753f, 2.601683f, 0.565510f, -1.760763f, 0.753342f, 0.381158f, 1.289753f,
+ 0.673181f, -0.138456f, -1.224298f, -0.209023f, -0.850520f, -0.580523f, 0.588578f,
+ 1.669904f, 0.394672f, -1.195883f, 0.444603f, 1.196631f, -0.609783f, -0.134017f,
+ 0.014688f, -0.784898f, 0.648280f, -0.120948f, 0.419532f, -0.887492f, -0.437458f,
+ 0.722381f, -0.372833f, 1.726964f, -0.399636f, 0.224685f, 0.932591f, -1.418366f,
+ -1.760809f, -1.525656f, 1.262584f, -0.551858f, 2.558199f, -0.564248f, 0.184551f,
+ 1.542110f, 2.006093f, 2.061504f, 1.208366f, 1.024063f, 0.592527f});
+ // K: (1, 2, 4, 3)
+ test_case.add_input<float>({0.778361f, -0.551186f, -0.818199f, -0.003374f, -0.170185f, -0.453228f,
+ 0.696387f, 0.955305f, 0.088407f, 1.477530f, -1.141689f, -0.193659f,
+ -0.716822f, -1.866537f, -0.082681f, -0.121748f, 1.513450f, 0.630812f,
+ -1.024187f, 1.854093f, 1.221034f, 0.582098f, -0.226484f, -0.959439f});
+ // V: (1, 2, 4, 3)
+ test_case.add_input<float>({-0.372207f, 1.088749f, 1.884586f, 1.543244f, -0.488849f, -1.119617f,
+ 0.140886f, -1.768439f, 0.323168f, -0.147603f, -0.466036f, -1.594703f,
+ 0.513600f, -0.532701f, -1.169917f, -2.872262f, -0.027515f, 1.772252f,
+ 1.661259f, -0.457096f, -0.602212f, 0.468774f, -0.998385f, 0.301792f});
+ // Y: (1, 4, 4, 3)
+ test_case.add_expected_output<float>(
+ Shape{1, 4, 4, 3},
+ {-0.372207f, 1.088749f, 1.884586f, 1.043291f, -0.077079f, -0.335489f, 0.256285f, -0.624200f,
+ 0.527349f, 0.517354f, -0.237278f, -0.074090f, 0.513600f, -0.532701f, -1.169917f, 0.115278f,
+ -0.473270f, -0.823793f, -0.248875f, -0.389089f, -0.318127f, 0.186889f, -0.530199f, -0.385138f,
+ -0.372207f, 1.088749f, 1.884586f, 0.717689f, 0.191092f, 0.175188f, 0.189007f, -0.193952f,
+ 0.749369f, -0.011048f, 0.054519f, -0.166278f, 0.513600f, -0.532701f, -1.169917f, -1.055831f,
+ -0.298535f, 0.193851f, -0.273476f, -0.274162f, 0.408617f, -0.612912f, -0.347982f, 0.594694f});
+ test_case.run_with_tolerance_as_fp(1e-4f);
+}