Skip to content

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

[CORE] Update GatedDeltaNet to support GQA (#35472)

Mateusz Mikolajczykf2959dc66e1a
diff --git a/src/core/shape_inference/include/gated_delta_net_shape_inference.hpp b/src/core/shape_inference/include/gated_delta_net_shape_inference.hpp
index a54773d1852..238b5c7c2ec 100644
--- a/src/core/shape_inference/include/gated_delta_net_shape_inference.hpp
+++ b/src/core/shape_inference/include/gated_delta_net_shape_inference.hpp
@@ -31,8 +31,8 @@ std::vector<TRShape> shape_infer(const GatedDeltaNet* op, const std::vector<T>&
NODE_SHAPE_INFER_CHECK(op,
input_shapes,
- q_head_num.compatible(k_head_num) && q_head_num.compatible(v_head_num),
- "The number of heads in query key and value should be the same, but got ",
+ q_head_num.compatible(k_head_num),
+ "The number of heads in query and key should be the same, but got ",
q_head_num,
" and ",
k_head_num);
@@ -50,8 +50,8 @@ std::vector<TRShape> shape_infer(const GatedDeltaNet* op, const std::vector<T>&
NODE_SHAPE_INFER_CHECK(op,
input_shapes,
- gate_head_num.compatible(beta_head_num) && gate_head_num.compatible(q_head_num),
- "The number of heads in gate, beta, and query should be the same, but got ",
+ gate_head_num.compatible(beta_head_num) && gate_head_num.compatible(v_head_num),
+ "The number of heads in gate, beta, and value should be the same, but got ",
gate_head_num,
" and ",
beta_head_num);
diff --git a/src/core/src/op/gated_delta_net.cpp b/src/core/src/op/gated_delta_net.cpp
index 569a7f344b5..32d19b176df 100644
--- a/src/core/src/op/gated_delta_net.cpp
+++ b/src/core/src/op/gated_delta_net.cpp
@@ -13,11 +13,11 @@
namespace {
// Validates input rank and type for a node input.
-inline void input_check(const ov::Node* node,
- size_t idx,
- const std::string_view input_name,
- std::initializer_list<ov::Rank>&& allowed_ranks,
- const std::vector<ov::element::Type>& allowed_types) {
+inline void gdn_input_check(const ov::Node* node,
+ size_t idx,
+ const std::string_view input_name,
+ std::initializer_list<ov::Rank>&& allowed_ranks,
+ const std::vector<ov::element::Type>& allowed_types) {
using namespace ov;
using namespace ov::util;
using namespace ov::element;
@@ -91,12 +91,12 @@ void GatedDeltaNet::validate_and_infer_types() {
NODE_VALIDATION_CHECK(this, get_input_size() == 6, "GatedDeltaNet expects 6 inputs, but it has ", get_input_size());
// format: Node*, input_idx, name, {rank_list}, {type_list}
- input_check(this, 0, "query", {4}, {ov::element::f32, ov::element::f16, ov::element::bf16});
- input_check(this, 1, "key", {4}, {ov::element::f32, ov::element::f16, ov::element::bf16});
- input_check(this, 2, "value", {4}, {ov::element::f32, ov::element::f16, ov::element::bf16});
- input_check(this, 3, "recurrent_state", {4}, {ov::element::f32, ov::element::f16, ov::element::bf16});
- input_check(this, 4, "gate", {3}, {ov::element::f32, ov::element::f16, ov::element::bf16});
- input_check(this, 5, "beta", {3}, {ov::element::f32, ov::element::f16, ov::element::bf16});
+ gdn_input_check(this, 0, "query", {4}, {ov::element::f32, ov::element::f16, ov::element::bf16});
+ gdn_input_check(this, 1, "key", {4}, {ov::element::f32, ov::element::f16, ov::element::bf16});
+ gdn_input_check(this, 2, "value", {4}, {ov::element::f32, ov::element::f16, ov::element::bf16});
+ gdn_input_check(this, 3, "recurrent_state", {4}, {ov::element::f32, ov::element::f16, ov::element::bf16});
+ gdn_input_check(this, 4, "gate", {3}, {ov::element::f32, ov::element::f16, ov::element::bf16});
+ gdn_input_check(this, 5, "beta", {3}, {ov::element::f32, ov::element::f16, ov::element::bf16});
const auto output_shapes = shape_infer(this, ov::util::get_node_input_partial_shapes(*this));
set_output_type(0, get_input_element_type(0), output_shapes[0]);
set_output_type(1, get_input_element_type(3), output_shapes[1]);
diff --git a/src/core/tests/type_prop/gated_delta_net.cpp b/src/core/tests/type_prop/gated_delta_net.cpp
index 6ad8cfaafda..042877696b4 100644
--- a/src/core/tests/type_prop/gated_delta_net.cpp
+++ b/src/core/tests/type_prop/gated_delta_net.cpp
@@ -114,7 +114,7 @@ TEST(type_prop, gated_delta_net_invalid_type) {
testing::HasSubstr("Element type of `query` input should be in"));
}
-TEST(type_prop, gated_delta_net_head_num_mismatch_qkv) {
+TEST(type_prop, gated_delta_net_head_num_mismatch_qk) {
OV_EXPECT_THROW(std::ignore = make_gdn(element::f32,
Shape{2, 5, 4, 8},
Shape{2, 5, 6, 8},
@@ -123,7 +123,7 @@ TEST(type_prop, gated_delta_net_head_num_mismatch_qkv) {
Shape{2, 5, 4},
Shape{2, 5, 4}),
NodeValidationFailure,
- testing::HasSubstr("The number of heads in query key and value should be the same"));
+ testing::HasSubstr("The number of heads in query and key should be the same"));
}
TEST(type_prop, gated_delta_net_head_size_mismatch_qk) {
@@ -147,7 +147,23 @@ TEST(type_prop, gated_delta_net_gate_beta_head_num_mismatch) {
Shape{2, 5, 6},
Shape{2, 5, 4}),
NodeValidationFailure,
- testing::HasSubstr("The number of heads in gate, beta, and query should be the same"));
+ testing::HasSubstr("The number of heads in gate, beta, and value should be the same"));
+}
+
+TEST(type_prop, gated_delta_net_gqa_v_num_heads_greater_than_num_heads) {
+ // GQA: query/key have 4 heads, value/gate/beta/state have 8 heads
+ const auto op = make_gdn(element::f32,
+ Shape{2, 5, 4, 8},
+ Shape{2, 5, 4, 8},
+ Shape{2, 5, 8, 16},
+ Shape{2, 8, 8, 16},
+ Shape{2, 5, 8},
+ Shape{2, 5, 8});
+
+ EXPECT_EQ(op->get_output_size(), 2);
+ EXPECT_EQ(op->get_output_element_type(0), element::f32);
+ EXPECT_EQ(op->get_output_partial_shape(0), PartialShape(Shape{2, 5, 8, 16}));
+ EXPECT_EQ(op->get_output_partial_shape(1), PartialShape(Shape{2, 8, 8, 16}));
}
TEST(type_prop, gated_delta_net_state_shape_mismatch) {