Skip to content

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

[GGUF] Fuse GatedDeltaNet q/k L2 normalization into the op (#40086)

Maxim Vafin38abacb761f2
diff --git a/src/frontends/gguf/src/op/gated_delta_net.cpp b/src/frontends/gguf/src/op/gated_delta_net.cpp
index e41e8bd01c5..080406b9a18 100644
--- a/src/frontends/gguf/src/op/gated_delta_net.cpp
+++ b/src/frontends/gguf/src/op/gated_delta_net.cpp
@@ -140,10 +140,16 @@ OutputVector translate_gated_delta_net(const NodeContext& context) {
auto beta = context.get_input(4);
auto state = context.get_input(5);
- // ggml normalizes with x / max(||x||, eps); the op's fused normalization adds eps under the
- // square root instead, which differs for small vectors, so normalize here.
- if (context.get_attribute<bool>("fuse_qk_l2norm", false)) {
- const float eps = context.get_attribute<float>("qk_l2_norm_eps", 1e-6f);
+ // ggml normalizes with x / max(||x||, eps); the op computes x / sqrt(||x||^2 + op_eps). With
+ // op_eps = eps^2 both agree except near ||x|| == eps (at most sqrt(2) apart there). Fusing keeps
+ // plugins from running the explicit normalization and its surrounding layout ops in f32. Fall
+ // back to the explicit form when eps^2 is not a normal float.
+ const bool qk_l2norm = context.get_attribute<bool>("fuse_qk_l2norm", false);
+ const float eps = context.get_attribute<float>("qk_l2_norm_eps", 1e-6f);
+ const bool fuse_qk_l2norm = qk_l2norm && std::isnormal(eps * eps);
+ // Paged conversion requires positive eps attributes even when the op does not normalize.
+ const float op_l2_norm_eps = fuse_qk_l2norm ? eps * eps : 1e-6f;
+ if (qk_l2norm && !fuse_qk_l2norm) {
if (const auto joined = join_adjacent_qk(q, k); joined.get_node()) {
const auto axis = ov::op::v0::Constant::create(ov::element::i64, {}, {2});
const auto normalized = std::make_shared<ov::op::v1::Split>(make_l2_norm(joined, eps), axis, 2);
@@ -194,7 +200,15 @@ OutputVector translate_gated_delta_net(const NodeContext& context) {
beta = std::make_shared<ov::op::v1::Multiply>(beta, mask);
}
- auto gdn = std::make_shared<ov::op::internal::GatedDeltaNet>(q, k, v, state, g, beta, false);
+ auto gdn = std::make_shared<ov::op::internal::GatedDeltaNet>(q,
+ k,
+ v,
+ state,
+ g,
+ beta,
+ fuse_qk_l2norm,
+ op_l2_norm_eps,
+ op_l2_norm_eps);
auto attn_4d = gdn->output(0);
auto state_4d = gdn->output(1); // [B, H_v, key_dim, value_dim]
if (split_outputs) {
diff --git a/src/frontends/gguf/tests/test_arch_conversion.cpp b/src/frontends/gguf/tests/test_arch_conversion.cpp
index 549e18bc4bb..315c5cf1864 100644
--- a/src/frontends/gguf/tests/test_arch_conversion.cpp
+++ b/src/frontends/gguf/tests/test_arch_conversion.cpp
@@ -207,8 +207,8 @@ const std::map<std::string, Fingerprint>& fingerprints() {
{"qwen2-dense.gguf.hdr", {373, 10}},
{"qwen3-dense.gguf.hdr", {421, 10}},
{"qwen3moe-moe.gguf.hdr", {572, 10}},
- {"qwen35-dense.gguf.hdr", {399, 10}},
- {"qwen35moe-moe.gguf.hdr", {571, 10}},
+ {"qwen35-dense.gguf.hdr", {392, 10}},
+ {"qwen35moe-moe.gguf.hdr", {564, 10}},
{"smollm3-dense.gguf.hdr", {389, 10}},
{"jais2-dense.gguf.hdr", {334, 10}},
{"lfm3.gguf.hdr", {702, 12}},
diff --git a/src/frontends/gguf/tests/test_gdn_normalization.cpp b/src/frontends/gguf/tests/test_gdn_normalization.cpp
index e920289d0ce..9a18ffd107c 100644
--- a/src/frontends/gguf/tests/test_gdn_normalization.cpp
+++ b/src/frontends/gguf/tests/test_gdn_normalization.cpp
@@ -16,7 +16,9 @@ using namespace ov::op;
enum class QkSlices { Adjacent, Offset, Gap, Strided, SeparateSources, ExplicitShape };
class GGUFQkNormalization : public testing::TestWithParam<QkSlices> {};
+// eps^2 underflows to a subnormal, so the translator keeps ggml's explicit normalization.
TEST_P(GGUFQkNormalization, PreservesIndependentRowsAndClamp) {
+ constexpr float eps = 1e-20f;
constexpr int64_t batch = 2, heads = 2, width = 4, features = 4 * heads * width;
const auto layout = GetParam();
const auto x = std::make_shared<v0::Parameter>(ov::element::f32, ov::PartialShape{batch, 1, -1, features});
@@ -56,7 +58,7 @@ TEST_P(GGUFQkNormalization, PreservesIndependentRowsAndClamp) {
{"state", ov::element::f32, {batch, heads, width, width}}},
std::vector<ov_gguf_test::TensorDesc>{},
ov_gguf_test::TensorDesc{"out", ov::element::f32, qkv_shape},
- std::map<std::string, ov::Any>{{"split_outputs", true}, {"fuse_qk_l2norm", true}, {"qk_l2_norm_eps", 1e-6f}});
+ std::map<std::string, ov::Any>{{"split_outputs", true}, {"fuse_qk_l2norm", true}, {"qk_l2_norm_eps", eps}});
auto values = std::make_shared<ov::frontend::gguf::TensorMap>();
for (const auto& entry : decoder->get_model_inputs())
values->emplace(entry.first, entry.second);
@@ -66,6 +68,9 @@ TEST_P(GGUFQkNormalization, PreservesIndependentRowsAndClamp) {
const auto outputs = ov::frontend::gguf::op::translate_gated_delta_net(context);
const auto gdn = ov::as_type_ptr<internal::GatedDeltaNet>(outputs[0].get_node_shared_ptr());
ASSERT_NE(gdn, nullptr);
+ EXPECT_FALSE(gdn->get_fuse_qk_l2norm());
+ EXPECT_GT(gdn->get_q_l2_norm_eps(), 0.f);
+ EXPECT_GT(gdn->get_k_l2_norm_eps(), 0.f);
const bool joined = layout == QkSlices::Adjacent || layout == QkSlices::Offset;
EXPECT_EQ(ov::is_type<v1::Split>(gdn->get_input_node_shared_ptr(0)), joined);
if (joined) {
@@ -76,7 +81,7 @@ TEST_P(GGUFQkNormalization, PreservesIndependentRowsAndClamp) {
const auto model =
std::make_shared<ov::Model>(ov::OutputVector{gdn->input_value(0), gdn->input_value(1), q}, parameters);
const auto reference = std::make_shared<ov::Model>(
- ov::OutputVector{ov::frontend::gguf::make_l2_norm(q, 1e-6f), ov::frontend::gguf::make_l2_norm(k, 1e-6f), q},
+ ov::OutputVector{ov::frontend::gguf::make_l2_norm(q, eps), ov::frontend::gguf::make_l2_norm(k, eps), q},
parameters);
ov::Core core;
auto compiled = core.compile_model(model, "CPU", ov::hint::inference_precision(ov::element::f32));
@@ -115,4 +120,31 @@ INSTANTIATE_TEST_SUITE_P(GGUFOps,
QkSlices::SeparateSources,
QkSlices::ExplicitShape));
+TEST(GGUFOps, GatedDeltaNetFusesQkNormWithSquaredEps) {
+ const ov::PartialShape qkv_shape{1, -1, 2, 4}, gate_shape{1, -1, 2, 1};
+ const auto decoder = std::make_shared<ov_gguf_test::SingleOpDecoder>(
+ "GGML_OP_GATED_DELTA_NET",
+ std::vector<ov_gguf_test::TensorDesc>{{"q", ov::element::f32, qkv_shape},
+ {"k", ov::element::f32, qkv_shape},
+ {"v", ov::element::f32, qkv_shape},
+ {"g", ov::element::f32, gate_shape},
+ {"beta", ov::element::f32, gate_shape},
+ {"state", ov::element::f32, {1, 2, 4, 4}}},
+ std::vector<ov_gguf_test::TensorDesc>{},
+ ov_gguf_test::TensorDesc{"out", ov::element::f32, qkv_shape},
+ std::map<std::string, ov::Any>{{"split_outputs", true}, {"fuse_qk_l2norm", true}, {"qk_l2_norm_eps", 1e-6f}});
+ auto values = std::make_shared<ov::frontend::gguf::TensorMap>();
+ for (const auto& entry : decoder->get_model_inputs())
+ values->emplace(entry.first, entry.second);
+ const ov::frontend::gguf::NodeContext context(decoder, values);
+ const auto outputs = ov::frontend::gguf::op::translate_gated_delta_net(context);
+ const auto gdn = ov::as_type_ptr<internal::GatedDeltaNet>(outputs[0].get_node_shared_ptr());
+ ASSERT_NE(gdn, nullptr);
+ EXPECT_TRUE(gdn->get_fuse_qk_l2norm());
+ EXPECT_FLOAT_EQ(gdn->get_q_l2_norm_eps(), 1e-12f);
+ EXPECT_FLOAT_EQ(gdn->get_k_l2_norm_eps(), 1e-12f);
+ EXPECT_EQ(gdn->input_value(0), values->at("q"));
+ EXPECT_EQ(gdn->input_value(1), values->at("k"));
+}
+
} // namespace
diff --git a/src/frontends/gguf/tests/test_ops.cpp b/src/frontends/gguf/tests/test_ops.cpp
index 0dc2e3466d2..2fe7b69e3a7 100644
--- a/src/frontends/gguf/tests/test_ops.cpp
+++ b/src/frontends/gguf/tests/test_ops.cpp
@@ -2888,8 +2888,9 @@ TEST(GGUFOps, GatedDeltaNetFused) {
expect_near(out, expected, 1e-4f);
}
-// Requested Q/K normalization follows ggml, x / max(||x||, eps), also for vectors below eps-scale
-// norms: q = k = [1e-4, 0] normalize to [1, 0], so one update with v = [1, 0] attends 1/sqrt(2).
+// Requested Q/K normalization follows ggml, x / max(||x||, eps), also for small vectors (the fused
+// op uses eps^2 under the square root): q = k = [1e-4, 0] normalize to ~[1, 0], so one update with
+// v = [1, 0] attends 1/sqrt(2).
TEST(GGUFOps, GatedDeltaNetFusedQkNormFollowsGgmlForSmallVectors) {
const int64_t B = 1, T = 1, H = 1, D = 2;
auto qkv_shp = ov::PartialShape{B, T, H, D};