OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Out-of-bounds Read in ONNX com.microsoft.GroupQueryAttention Converter (#34726)
Szymon Gutaj652cf8989c15
diff --git a/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp b/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cppindex 201349da53e..ce1d86b258d 100644--- a/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp+++ b/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp@@ -77,8 +77,6 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(auto past_value = node->input_value(4);auto seqlens_k = node->input_value(5);auto total_sequence_length = node->input_value(6);- auto cos_cache = node->input_value(7);- auto sin_cache = node->input_value(8);auto is_null = [](const ov::Output<ov::Node>& output) {return output.get_node_shared_ptr()->description() == "NullNode";@@ -106,6 +104,9 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(const auto curr_seqlen_scalar = register_new_node<v0::Squeeze>(current_seqlen);if (do_rotary) {+ auto cos_cache = node->input_value(7);+ auto sin_cache = node->input_value(8);+ov::Output<ov::Node> position_ids =register_new_node<v4::Range>(zero_without_shape, curr_seqlen_scalar, one_without_shape, ov::element::i64);if (node->get_input_size() > 9 && !is_null(node->input_value(9))) {diff --git a/src/core/src/op/group_query_attention.cpp b/src/core/src/op/group_query_attention.cppindex cef2646ef36..c340a39a949 100644--- a/src/core/src/op/group_query_attention.cpp+++ b/src/core/src/op/group_query_attention.cpp@@ -26,7 +26,8 @@ GroupQueryAttention::GroupQueryAttention(const OutputVector& args,void GroupQueryAttention::validate_and_infer_types() {OV_OP_SCOPE(GroupQueryAttention_validate_and_infer_types);// GroupQueryAttention expects the following inputs:- // query, key, value, past_key, past_value, seqlens_k, cos_cache, sin_cache.+ // query, key, value, past_key, past_value, seqlens_k, total_sequence_length.+ // In the rotary embedding case, following inputs are also required: cos_cache, sin_cache.// All qkv tensors should have the shape [batch, num_heads, seq_len, head_size] ([B, N, S, H]).// The operation produces three outputs:// 1. Output tensor of shape [B, S, N * H].@@ -49,7 +50,7 @@ void GroupQueryAttention::validate_and_infer_types() {const auto& element_type = get_input_element_type(0);NODE_VALIDATION_CHECK(this,element_type == element::f32 || element_type == element::f16,- "GroupQueryAttention only suuports f32 and f16");+ "GroupQueryAttention supports following element types: {f32, f16}");set_output_type(0, element_type, PartialShape{batch_size, sequence_len, head_size * m_num_heads});for (auto&& port : {1, 2}) {diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cppindex cf839ffcef3..5534f278694 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@@ -9,6 +9,7 @@#include "core/null_node.hpp"#include "core/operator_set.hpp"#include "exceptions.hpp"+#include "openvino/frontend/exception.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/divide.hpp"#include "openvino/op/reshape.hpp"@@ -28,8 +29,15 @@ using ov::frontend::onnx::attention::get_dimensions;namespace opset_1 {ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {- // At least given "query" and "seqlens_k"- common::default_op_checks(node, 2);+ constexpr size_t inputs_count_min = 7; // Taken from ONNX spec+ constexpr size_t inputs_count_max = 14; // Taken from ONNX spec++ // Minimum required inputs basing on the spec and ONNX Runtime code: 7+ // 0: packed QKV (mandatory)+ // 3-4: possibly null (if unused)+ // 5: seqlens_k (mandatory)+ // 6: total_sequence_length (mandatory in the spec)+ common::default_op_checks(node, inputs_count_min, inputs_count_max);const auto onnx_op_inputs = node.get_ov_inputs();const auto num_heads = node.get_attribute_value<int64_t>("num_heads");@@ -38,7 +46,16 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {const auto do_rotary = node.get_attribute_value<int64_t>("do_rotary", 0);const auto rotary_interleaved = node.get_attribute_value<int64_t>("rotary_interleaved", 0);- CHECK_VALID_NODE(node, onnx_op_inputs.size() >= 9, "Expected at least 9 inputs, but got: ", onnx_op_inputs.size());+ if (0 != do_rotary) {+ constexpr size_t cos_cache_index = 7;+ constexpr size_t sin_cache_index = 8;++ FRONT_END_OP_CONVERSION_CHECK(common::is_input_valid(onnx_op_inputs, sin_cache_index) &&+ common::is_input_valid(onnx_op_inputs, cos_cache_index),+ "GroupQueryAttention: cos_cache and sin_cache inputs are required when "+ "do_rotary is enabled.");+ }+// In ONNX, the format of input QKV is [B, S, N*H] and of past_kv is [B, N, S, H]// In OV, we always use [B, N, S, H]auto perm = v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});@@ -46,13 +63,21 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {auto Q = onnx_op_inputs[0];auto K = onnx_op_inputs[1];auto V = onnx_op_inputs[2];++ FRONT_END_OP_CONVERSION_CHECK(!ov::op::util::is_null(Q), "GroupQueryAttention: Expecting Q/QKV not null.");++ const auto& seqlens_k = onnx_op_inputs[5];++ FRONT_END_OP_CONVERSION_CHECK(!ov::op::util::is_null(seqlens_k),+ "GroupQueryAttention: Expecting seqlens_k not null.");+const auto q_shape_node = std::make_shared<v3::ShapeOf>(Q);const auto batch_size_node = detail::get_dimensions(q_shape_node, {0});const auto current_seqlen_size_node = detail::get_dimensions(q_shape_node, {1});const auto hidden_size_node = detail::get_dimensions(q_shape_node, {2});OutputVector ov_op_inputs;- if (ov::op::util::is_null(K)) {+ if (ov::op::util::is_null(K) && ov::op::util::is_null(V)) {auto total_num_heads_node =v0::Constant::create(ov::element::i64, ov::Shape{1}, {num_heads + kv_num_heads + kv_num_heads});auto head_size_node = std::make_shared<v1::Divide>(hidden_size_node, total_num_heads_node);@@ -65,7 +90,13 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {auto split = ov::op::util::make_split(inputs_qkv, {num_heads, kv_num_heads, kv_num_heads}, 1);std::copy(split.begin(), split.end(), std::back_inserter(ov_op_inputs));++ FRONT_END_OP_CONVERSION_CHECK(ov_op_inputs.size() == 3,+ "GroupQueryAttention: Expecting QKV split to produce 3 outputs.");} else {+ FRONT_END_OP_CONVERSION_CHECK(!ov::op::util::is_null(K), "GroupQueryAttention: Expecting K not null.");+ FRONT_END_OP_CONVERSION_CHECK(!ov::op::util::is_null(V), "GroupQueryAttention: Expecting V not null.");+auto num_heads_node = v0::Constant::create(ov::element::i64, ov::Shape{1}, {num_heads});auto head_size_node = std::make_shared<v1::Divide>(hidden_size_node, num_heads_node);auto q_shape = std::make_shared<v0::Concat>(@@ -74,7 +105,7 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {Q = std::make_shared<v1::Reshape>(Q, q_shape, false)->output(0);Q = std::make_shared<v1::Transpose>(Q, perm);- ov_op_inputs.push_back(Q);+ ov_op_inputs.push_back(std::move(Q));auto kv_num_heads_node = v0::Constant::create(ov::element::i64, ov::Shape{1}, {kv_num_heads});auto kv_shape = std::make_shared<v0::Concat>(@@ -85,13 +116,14 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {V = std::make_shared<v1::Reshape>(V, kv_shape, false)->output(0);K = std::make_shared<v1::Transpose>(K, perm);V = std::make_shared<v1::Transpose>(V, perm);- ov_op_inputs.push_back(K);- ov_op_inputs.push_back(V);+ ov_op_inputs.push_back(std::move(K));+ ov_op_inputs.push_back(std::move(V));}- for (std::size_t i = 3; i < onnx_op_inputs.size(); ++i) {+ for (size_t i = ov_op_inputs.size(); i < onnx_op_inputs.size(); ++i) {ov_op_inputs.push_back(onnx_op_inputs[i]);}+return std::make_shared<internal::GroupQueryAttention>(ov_op_inputs,num_heads,kv_num_heads,diff --git a/src/frontends/onnx/frontend/src/utils/common.cpp b/src/frontends/onnx/frontend/src/utils/common.cppindex 6fb1649aa45..06676be96d8 100644--- a/src/frontends/onnx/frontend/src/utils/common.cpp+++ b/src/frontends/onnx/frontend/src/utils/common.cpp@@ -101,14 +101,17 @@ void default_op_checks(const Node& node, size_t min_inputs_size, size_t max_inpuinputs.size());}-bool is_input_valid(const Node& node, size_t index) {- const auto& inputs = node.get_ov_inputs();+bool is_input_valid(const ov::OutputVector& inputs, size_t index) {if (index >= inputs.size())return false;const auto node_ptr = inputs[index].get_node_shared_ptr();return node_ptr != nullptr && !ov::as_type_ptr<NullNode>(node_ptr);}+bool is_input_valid(const Node& node, size_t index) {+ return is_input_valid(node.get_ov_inputs(), index);+}+std::shared_ptr<ov::Node> get_monotonic_range_along_node_rank(const ov::Output<ov::Node>& value,int64_t start_value,int64_t step) {diff --git a/src/frontends/onnx/frontend/src/utils/common.hpp b/src/frontends/onnx/frontend/src/utils/common.hppindex 91321359778..0c6314f944f 100644--- a/src/frontends/onnx/frontend/src/utils/common.hpp+++ b/src/frontends/onnx/frontend/src/utils/common.hpp@@ -37,9 +37,16 @@ void default_op_checks(const Node& node, size_t min_inputs_size);/// \param[in] max_inputs_size Maximum amount of inputs expectedvoid default_op_checks(const Node& node, size_t min_inputs_size, size_t max_inputs_size);-/// \brief Function does a returns if input exists and is not null+/// @brief Function checks if input exists and is not null+/// @param[in] inputs Node inputs to check+/// @param[in] index Input index to check+/// \return True if input exists and is not null, false otherwise+bool is_input_valid(const ov::OutputVector& inputs, size_t index);++/// @brief Function checks if input exists and is not null/// \param[in] node Node to check/// \param[in] index Input index to check+/// \return True if input exists and is not null, false otherwisebool is_input_valid(const Node& node, size_t index);/// \brief Return a monotonic sequence.diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_non_rotary_7_inputs.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_non_rotary_7_inputs.prototxtnew file mode 100644index 00000000000..d412a0d88fe--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_non_rotary_7_inputs.prototxt@@ -0,0 +1,165 @@+ir_version: 10+graph {+ node {+ input: "query"+ input: ""+ input: ""+ input: "past_key"+ input: "past_value"+ input: "seqlens_k"+ input: "total_sequence_length"+ output: "output"+ output: "present_key"+ output: "present_value"+ name: "GroupQueryAttention_0"+ op_type: "GroupQueryAttention"+ attribute {+ name: "do_rotary"+ i: 0+ type: INT+ }+ attribute {+ name: "kv_num_heads"+ i: 1+ type: INT+ }+ attribute {+ name: "local_window_size"+ i: -1+ type: INT+ }+ attribute {+ name: "num_heads"+ i: 2+ type: INT+ }+ attribute {+ name: "rotary_interleaved"+ i: 0+ type: INT+ }+ attribute {+ name: "smooth_softmax"+ i: 0+ type: INT+ }+ attribute {+ name: "softcap"+ f: 0+ type: FLOAT+ }+ domain: "com.microsoft"+ }+ name: "GroupQueryAttention_Graph"+ input {+ name: "query"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_param: "sequence_length" }+ dim { dim_value: 64 }+ }+ }+ }+ }+ input {+ name: "past_key"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_value: 1 }+ dim { dim_param: "past_sequence_length" }+ dim { dim_value: 16 }+ }+ }+ }+ }+ input {+ name: "past_value"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_value: 1 }+ dim { dim_param: "past_sequence_length" }+ dim { dim_value: 16 }+ }+ }+ }+ }+ input {+ name: "seqlens_k"+ type {+ tensor_type {+ elem_type: 6+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_value: 1 }+ }+ }+ }+ }+ input {+ name: "total_sequence_length"+ type{+ tensor_type {+ elem_type: 6+ shape {+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_param: "sequence_length" }+ dim { dim_value: 32 }+ }+ }+ }+ }+ output {+ name: "present_key"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_value: 1 }+ dim { dim_param: "total_sequence_length" }+ dim { dim_value: 16 }+ }+ }+ }+ }+ output {+ name: "present_value"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_value: 1 }+ dim { dim_param: "total_sequence_length" }+ dim { dim_value: 16 }+ }+ }+ }+ }+}+opset_import {+ version: 11+}+opset_import {+ domain: "com.microsoft"+ version: 1+}diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_oob.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_oob.prototxtnew file mode 100644index 00000000000..13210b8ee12--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_oob.prototxt@@ -0,0 +1,114 @@+ir_version: 10+graph {+ node {+ input: "query"+ input: "past_key"+ input: "past_value"+ output: "output"+ output: "present_key"+ output: "present_value"+ name: "GroupQueryAttention_0"+ op_type: "GroupQueryAttention"+ attribute {+ name: "num_heads"+ i: 2+ type: INT+ }+ attribute {+ name: "kv_num_heads"+ i: 1+ type: INT+ }+ domain: "com.microsoft"+ }+ name: "GroupQueryAttention_OOB_Graph"+ input {+ name: "query"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_param: "sequence_length" }+ dim { dim_value: 64 }+ }+ }+ }+ }+ input {+ name: "past_key"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_value: 1 }+ dim { dim_param: "past_sequence_length" }+ dim { dim_value: 16 }+ }+ }+ }+ }+ input {+ name: "past_value"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_value: 1 }+ dim { dim_param: "past_sequence_length" }+ dim { dim_value: 16 }+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_param: "sequence_length" }+ dim { dim_value: 32 }+ }+ }+ }+ }+ output {+ name: "present_key"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_value: 1 }+ dim { dim_param: "total_sequence_length" }+ dim { dim_value: 16 }+ }+ }+ }+ }+ output {+ name: "present_value"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_param: "batch_size" }+ dim { dim_value: 1 }+ dim { dim_param: "total_sequence_length" }+ dim { dim_value: 16 }+ }+ }+ }+ }+}+opset_import {+ version: 11+}+opset_import {+ domain: "com.microsoft"+ version: 1+}diff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cppindex ec1dcf92c56..31ee0a0801d 100644--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp@@ -2874,6 +2874,86 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_1_input_1_rotary_interleaved)test_case.run_with_tolerance_as_fp();}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_minimum_inputs_passes) {+ const auto model = convert_model("com.microsoft/gqa_non_rotary_7_inputs.onnx");++ std::vector<float> query = {+ -1.1258f, -1.1524f, -0.2506f, -0.4339f, 0.8487f, 0.6920f, -0.3160f, -2.1152f, 0.3223f, -1.2633f, 0.3500f,+ 0.3081f, 0.1198f, 1.2377f, 1.1168f, -0.2473f, -1.3527f, -1.6959f, 0.5667f, 0.7935f, 0.5988f, -1.5551f,+ -0.3414f, 1.8530f, 0.7502f, -0.5855f, -0.1734f, 0.1835f, 1.3894f, 1.5863f, 0.9463f, -0.8437f, 1.6459f,+ -1.3602f, 0.3446f, 0.5199f, -2.6133f, -1.6965f, -0.2282f, 0.2800f, 0.2469f, 0.0769f, 0.3380f, 0.4544f,+ 0.4569f, -0.8654f, 0.7813f, -0.9268f, -0.2188f, -2.4351f, -0.0729f, -0.0340f, 0.9625f, 0.3492f, -0.9215f,+ -0.0562f, -0.6227f, -0.4637f, 1.9218f, -0.4025f, 0.1239f, 1.1648f, 0.9234f, 1.3873f,+ };+ std::vector<float> past_key = {};+ std::vector<float> past_value = {};+ std::vector<int> seqlens_k = {0};+ std::vector<int> total_sequence_length = {1};++ std::vector<float> expected_output = {-0.2188f, -2.4351f, -0.0729f, -0.034f, 0.9625f, 0.3492f, -0.9215f, -0.0562f,+ -0.6227f, -0.4637f, 1.9218f, -0.4025f, 0.1239f, 1.1648f, 0.9234f, 1.3873f,+ -0.2188f, -2.4351f, -0.0729f, -0.034f, 0.9625f, 0.3492f, -0.9215f, -0.0562f,+ -0.6227f, -0.4637f, 1.9218f, -0.4025f, 0.1239f, 1.1648f, 0.9234f, 1.3873f};++ std::vector<float> expected_present_key = {1.64590001f,+ -1.36020004f,+ 0.34459999f,+ 0.51990002f,+ -2.61330008f,+ -1.69649994f,+ -0.22820000f,+ 0.28000000f,+ 0.24690000f,+ 0.07689999f,+ 0.33799999f,+ 0.45440000f,+ 0.45690000f,+ -0.86540001f,+ 0.78130000f,+ -0.92680001f};++ std::vector<float> expected_present_value = {-0.2188f,+ -2.4351f,+ -0.0729f,+ -0.034f,+ 0.9625f,+ 0.3492f,+ -0.9215f,+ -0.0562f,+ -0.6227f,+ -0.4637f,+ 1.9218f,+ -0.4025f,+ 0.1239f,+ 1.1648f,+ 0.9234f,+ 1.3873f};++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{1, 1, 64}, query);+ test_case.add_input<float>(Shape{1, 1, 0, 16}, past_key);+ test_case.add_input<float>(Shape{1, 1, 0, 16}, past_value);+ test_case.add_input<int>(Shape{1, 1}, seqlens_k);+ test_case.add_input<int>(Shape{}, total_sequence_length);+ test_case.add_expected_output<float>(Shape{1, 1, 32}, expected_output);+ test_case.add_expected_output<float>(Shape{1, 1, 1, 16}, expected_present_key);+ test_case.add_expected_output<float>(Shape{1, 1, 1, 16}, expected_present_value);+ test_case.run_with_tolerance_as_fp();+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_insufficient_inputs_throws) {+ try {+ convert_model("com.microsoft/gqa_oob.onnx");+ FAIL() << "ONNX Importer did not detect insufficient inputs for GroupQueryAttention";+ } catch (const ::ov::frontend::OpConversionFailure& e) {+ EXPECT_THAT(e.what(), testing::AllOf(testing::HasSubstr("expected"), testing::HasSubstr(" 7 to 14 inputs")));+ } catch (const std::exception& e) {+ EXPECT_THAT(e.what(), testing::AllOf(testing::HasSubstr("expected"), testing::HasSubstr(" 7 to 14 inputs")));+ } catch (...) {+ FAIL() << "Unexpected exception type thrown";+ }+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinear_reducemean_i8) {const auto model = convert_model("com.microsoft/qlinear_reducemean_i8.onnx");auto test_case = ov::test::TestCase(model, s_device);