Skip to content

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

[ONNX FE] don't skip total_sequence_length for GroupQueryAttention Node (#32981)

bopeng1234cfbed7ea6c3b
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.cpp
index 14c903a5d20..201349da53e 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
@@ -76,8 +76,13 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(
auto past_key = node->input_value(3);
auto past_value = node->input_value(4);
auto seqlens_k = node->input_value(5);
- auto cos_cache = node->input_value(6);
- auto sin_cache = node->input_value(7);
+ 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";
+ };
// The length of all tokens (past + current) is `seqlens_k` + 1.
// current = Q.shape[2], past = `seqlens_k` + 1 - current
@@ -103,8 +108,8 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(
if (do_rotary) {
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() > 8) {
- position_ids = node->input_value(8).get_node_shared_ptr();
+ if (node->get_input_size() > 9 && !is_null(node->input_value(9))) {
+ position_ids = node->input_value(9).get_node_shared_ptr();
} else {
position_ids = register_new_node<v1::Add>(position_ids, past_seqlen);
}
@@ -164,8 +169,8 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(
// Make attention mask
std::shared_ptr<ov::Node> mask;
- if (node->get_input_size() > 9) {
- auto original_mask = node->input_value(9).get_node_shared_ptr();
+ if (node->get_input_size() > 10 && !is_null(node->input_value(10))) {
+ auto original_mask = node->input_value(10).get_node_shared_ptr();
// Extract mask [num_heads, curr_seqlen, concat_kv_len] from 4D mask [1, num_heads, curr_seqlen, max_kv_len]
auto axes_to_squeeze = register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{1}, {0}));
auto mask_squeezed = register_new_node<v0::Squeeze>(original_mask, axes_to_squeeze);
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 8a48dfa7cb4..26ba01a741c 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
@@ -92,9 +92,6 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {
}
for (std::size_t i = 3; i < onnx_op_inputs.size(); ++i) {
- // skip total_sequence_length
- if (i == 6)
- continue;
ov_op_inputs.push_back(onnx_op_inputs[i]);
}
return std::make_shared<internal::GroupQueryAttention>(ov_op_inputs,
diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary.prototxt
index 179e7935d7d..f120ffbddfd 100644
--- a/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary.prototxt
+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary.prototxt
@@ -7,7 +7,7 @@ graph {
input: "past_key"
input: "past_value"
input: "seqlens_k"
- input: ""
+ input: "total_sequence_length"
input: "cos_cache"
input: "sin_cache"
output: "output"
@@ -106,6 +106,16 @@ graph {
}
}
}
+ input {
+ name: "total_sequence_length"
+ type{
+ tensor_type {
+ elem_type: 6
+ shape {
+ }
+ }
+ }
+ }
input {
name: "cos_cache"
type {
diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary_interleaved.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary_interleaved.prototxt
index 9186bafc770..f9cb8d53c79 100644
--- a/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary_interleaved.prototxt
+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary_interleaved.prototxt
@@ -7,7 +7,7 @@ graph {
input: "past_key"
input: "past_value"
input: "seqlens_k"
- input: ""
+ input: "total_sequence_length"
input: "cos_cache"
input: "sin_cache"
output: "output"
@@ -106,6 +106,16 @@ graph {
}
}
}
+ input {
+ name: "total_sequence_length"
+ type{
+ tensor_type {
+ elem_type: 6
+ shape {
+ }
+ }
+ }
+ }
input {
name: "cos_cache"
type {
diff --git a/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary_posid_bias_null.prototxt b/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary_posid_bias_null.prototxt
new file mode 100644
index 00000000000..6e292dbf2a9
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/gqa_rotary_posid_bias_null.prototxt
@@ -0,0 +1,183 @@
+ir_version: 10
+graph {
+ node {
+ input: "query"
+ input: ""
+ input: ""
+ input: "past_key"
+ input: "past_value"
+ input: "seqlens_k"
+ input: ""
+ input: "cos_cache"
+ input: "sin_cache"
+ input: ""
+ input: ""
+ output: "output"
+ output: "present_key"
+ output: "present_value"
+ name: "GroupQueryAttention_0"
+ op_type: "GroupQueryAttention"
+ attribute {
+ name: "do_rotary"
+ i: 1
+ 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: "cos_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_param: "max_sequence_length" }
+ dim { dim_value: 8 }
+ }
+ }
+ }
+ }
+ input {
+ name: "sin_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_param: "max_sequence_length" }
+ dim { dim_value: 8 }
+ }
+ }
+ }
+ }
+ 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.cpp
index 6bb8c8d2e25..ced7bb8f51d 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -1973,6 +1973,7 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_0_input_1_rotary) {
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> cos_cache = {
0.8437f,
-0.7849f,
@@ -2038,6 +2039,7 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_0_input_1_rotary) {
test_case.add_input<float>(Shape{1, 1, 0, 16}, past_key);
test_case.add_input<float>(Shape{1, 1, 0, 16}, past_value);
test_case.add_input<int>(Shape{1, 1}, seqlens_k);
+ test_case.add_input<int>(Shape{}, total_sequence_length);
test_case.add_input<float>(Shape{1, 8}, cos_cache);
test_case.add_input<float>(Shape{1, 8}, sin_cache);
test_case.add_expected_output<float>(Shape{1, 1, 32}, expected_output);
@@ -2226,6 +2228,93 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_0_input_1_rotary_posid_bias)
test_case.run_with_tolerance_as_fp();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_0_input_1_rotary_posid_bias_null) {
+ const auto model = convert_model("com.microsoft/gqa_rotary_posid_bias_null.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<float> cos_cache = {
+ 0.8437f,
+ -0.7849f,
+ -0.7829f,
+ 0.4581f,
+ -0.9870f,
+ 0.6273f,
+ -0.9483f,
+ -0.9962f,
+ };
+ std::vector<float> sin_cache = {
+ 0.5368f,
+ 0.6196f,
+ -0.6222f,
+ 0.8889f,
+ 0.1605f,
+ -0.7788f,
+ 0.3174f,
+ -0.0872f,
+ };
+
+ 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.2561098f,
+ 1.0199738f,
+ -0.05948371f,
+ -0.16574995f,
+ 2.5059946f,
+ -1.738188f,
+ -0.03158256f,
+ -0.35975295f,
+ 1.0918287f,
+ -0.90313876f,
+ -0.4790303f,
+ 0.67029977f,
+ -0.87039495f,
+ 0.7783688f,
+ -0.81333745f,
+ 0.89886224f};
+
+ 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<float>(Shape{1, 8}, cos_cache);
+ test_case.add_input<float>(Shape{1, 8}, sin_cache);
+ 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_past_0_input_1_rotary_interleaved) {
const auto model = convert_model("com.microsoft/gqa_rotary_interleaved.onnx");
@@ -2240,6 +2329,7 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_0_input_1_rotary_interleaved)
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> cos_cache = {
0.8437f,
-0.7849f,
@@ -2305,6 +2395,7 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_0_input_1_rotary_interleaved)
test_case.add_input<float>(Shape{1, 1, 0, 16}, past_key);
test_case.add_input<float>(Shape{1, 1, 0, 16}, past_value);
test_case.add_input<int>(Shape{1, 1}, seqlens_k);
+ test_case.add_input<int>(Shape{}, total_sequence_length);
test_case.add_input<float>(Shape{1, 8}, cos_cache);
test_case.add_input<float>(Shape{1, 8}, sin_cache);
test_case.add_expected_output<float>(Shape{1, 1, 32}, expected_output);
@@ -2361,6 +2452,7 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_1_input_1_rotary) {
0.8058f,
};
std::vector<int> seqlens_k = {1};
+ std::vector<int> total_sequence_length = {2};
std::vector<float> cos_cache = {
0.8437f,
-0.7849f,
@@ -2420,6 +2512,7 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_1_input_1_rotary) {
test_case.add_input<float>(Shape{1, 1, 1, 16}, past_key);
test_case.add_input<float>(Shape{1, 1, 1, 16}, past_value);
test_case.add_input<int>(Shape{1, 1}, seqlens_k);
+ test_case.add_input<int>(Shape{}, total_sequence_length);
test_case.add_input<float>(Shape{2, 8}, cos_cache);
test_case.add_input<float>(Shape{2, 8}, sin_cache);
test_case.add_expected_output<float>(Shape{1, 1, 32}, expected_output);
@@ -2712,6 +2805,7 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_1_input_1_rotary_interleaved)
0.8058f,
};
std::vector<int> seqlens_k = {1};
+ std::vector<int> total_sequence_length = {2};
std::vector<float> cos_cache = {
0.8437f,
-0.7849f,
@@ -2771,6 +2865,7 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_1_input_1_rotary_interleaved)
test_case.add_input<float>(Shape{1, 1, 1, 16}, past_key);
test_case.add_input<float>(Shape{1, 1, 1, 16}, past_value);
test_case.add_input<int>(Shape{1, 1}, seqlens_k);
+ test_case.add_input<int>(Shape{}, total_sequence_length);
test_case.add_input<float>(Shape{2, 8}, cos_cache);
test_case.add_input<float>(Shape{2, 8}, sin_cache);
test_case.add_expected_output<float>(Shape{1, 1, 32}, expected_output);
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
index b106d51c7bb..7acf4c6522c 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
@@ -118,8 +118,9 @@ public:
auto past_key = node->input_value(3);
auto past_value = node->input_value(4);
auto seqlens_k = node->input_value(5);
- auto cos_cache = node->input_value(6);
- auto sin_cache = node->input_value(7);
+ auto total_sequence_length = node->input_value(6);
+ auto cos_cache = node->input_value(7);
+ auto sin_cache = node->input_value(8);
// The length of all tokens (past + current) is `seqlens_k` + 1.
// current = Q.shape[2], past = `seqlens_k` + 1 - current