Skip to content

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

[GGUF FE] Honor SOFT_MAX axis for gpt-oss expert weights (#39476)

Maxim Vafin1e77c94f47d4
diff --git a/.github/workflows/ubuntu_22.yml b/.github/workflows/ubuntu_22.yml
index 5decb0a5f9d..6454364e9b6 100644
--- a/.github/workflows/ubuntu_22.yml
+++ b/.github/workflows/ubuntu_22.yml
@@ -579,7 +579,7 @@ jobs:
runner: 'aks-linux-4-cores-16gb'
image: ${{ fromJSON(needs.docker.outputs.images).ov_build.ubuntu_22_04_x64 }}
run-real-models: ${{ github.event_name == 'schedule' }}
- genai-ref: 2ccabebb8d5ab70673525a777a92f2bf62fce136
+ genai-ref: ccb13a1c79c067234e9af56167b360037a9dc07f
GGUF_LlamaCpp_Validation:
name: GGUF Frontend x llama.cpp validation
@@ -763,7 +763,7 @@ jobs:
Overall_Status:
name: ci/gha_overall_status
needs: [Smart_CI, Build, Debian_Packages, Samples, Conformance, CXX_Unit_Tests, Python_Unit_Tests, TensorFlow_Layer_Tests, Pytorch_Layer_Tests,
- CPU_Functional_Tests, TensorFlow_Models_Tests_Precommit, PyTorch_Models_Tests, JAX_Models_Tests_Precommit, GGUF_Models_Tests_Precommit, GGUF_GenAI_Validation,
+ CPU_Functional_Tests, TensorFlow_Models_Tests_Precommit, PyTorch_Models_Tests, JAX_Models_Tests_Precommit, GGUF_Models_Tests_Precommit,
NVIDIA_Plugin, Openvino_tokenizers, iGPU,
Keras3_OpenVINO_Backend, iGPU_RoPE_Tests]
if: ${{ always() }}
diff --git a/src/frontends/gguf/src/op/softmax.cpp b/src/frontends/gguf/src/op/softmax.cpp
index a029743d8dd..9fe8acb1bae 100644
--- a/src/frontends/gguf/src/op/softmax.cpp
+++ b/src/frontends/gguf/src/op/softmax.cpp
@@ -83,9 +83,21 @@ OutputVector translate_soft_max(const NodeContext& context) {
ov::Output<Node> res;
// ggml SOFT_MAX reduces ne[0] == the OV last axis (rank-3 attention -> 2, rank-4 router -> 3);
- // derive it from the rank instead of hardcoding 2. Reused by both Softmax paths below.
+ // derive it from the rank instead of hardcoding 2. The native builder may request another
+ // axis, e.g. the gpt-oss top-k gating weights [1,T,K,1] normalize over K. Reused by both
+ // Softmax paths below.
const auto& in_ps = input0.get_partial_shape();
- const int64_t softmax_axis = in_ps.rank().is_static() ? in_ps.rank().get_length() - 1 : 2;
+ const bool has_softmax_axis = context.has_attribute("softmax_axis");
+ const int64_t softmax_axis = has_softmax_axis ? context.get_attribute<int64_t>("softmax_axis")
+ : (in_ps.rank().is_static() ? in_ps.rank().get_length() - 1 : 2);
+ if (has_softmax_axis && in_ps.rank().is_static()) {
+ const int64_t rank = in_ps.rank().get_length();
+ FRONT_END_OP_CONVERSION_CHECK(-rank <= softmax_axis && softmax_axis < rank,
+ "SOFT_MAX softmax_axis ",
+ softmax_axis,
+ " is out of range for input rank ",
+ rank);
+ }
float scale = context.get_attribute<float>("scale", 1.0f);
float max_bias = context.get_attribute<float>("max_bias", 0.0f);
@@ -100,6 +112,11 @@ OutputVector translate_soft_max(const NodeContext& context) {
const bool has_mask = context.get_input_size() > 1 && !second_input_is_sinks;
const bool has_sinks = second_input_is_sinks || context.get_input_size() > 2;
const int sinks_input_idx = second_input_is_sinks ? 1 : 2;
+ if (has_sinks && has_softmax_axis) {
+ const bool last_axis =
+ softmax_axis == -1 || (in_ps.rank().is_static() && softmax_axis == in_ps.rank().get_length() - 1);
+ FRONT_END_OP_CONVERSION_CHECK(last_axis, "SOFT_MAX with attention sinks supports only the last softmax_axis");
+ }
if (!has_mask) {
if (has_sinks) {
diff --git a/src/frontends/gguf/tests/test_builder_api.cpp b/src/frontends/gguf/tests/test_builder_api.cpp
index 0107698e918..1ea27973a5a 100644
--- a/src/frontends/gguf/tests/test_builder_api.cpp
+++ b/src/frontends/gguf/tests/test_builder_api.cpp
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include <algorithm>
#include <cmath>
#include <limits>
#include <numeric>
@@ -19,6 +20,7 @@
#include "openvino/op/assign.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/pass/manager.hpp"
+#include "openvino/runtime/core.hpp"
#include "projector.hpp"
using namespace ov::frontend::gguf;
@@ -661,6 +663,101 @@ TEST(GGUFBuilderAPI, ErnieInterleavesDenseAndExpertLayers) {
}
}
+TEST(GGUFBuilderAPI, GptOssExpertMixUsesSoftmaxOverSelectedExperts) {
+ constexpr size_t D = 8, F = 6, E = 4, K = 2, T = 3;
+ constexpr float alpha = 1.702f, limit = 7.f, weight_scale = 2.5f;
+ Environment env;
+ env.decoder();
+ env.integer("test.block_count", 1);
+ env.integer("test.embedding_length", D);
+ env.integer("test.expert_count", E);
+ env.integer("test.expert_used_count", K);
+ env.real("test.expert_weights_scale", weight_scale);
+ size_t seed = 0;
+ const auto filled = [&](const ov::Shape& shape) {
+ ov::Tensor t(ov::element::f32, shape);
+ for (size_t i = 0; i < t.get_size(); ++i)
+ t.data<float>()[i] = 0.4f * std::sin(0.37f * static_cast<float>(i) + 1.3f * static_cast<float>(++seed));
+ return t;
+ };
+ const std::string p = "blk.0.";
+ env.weights[p + "ffn_gate_inp.weight"] = filled({E, D});
+ env.weights[p + "ffn_gate_inp.bias"] = filled({E});
+ env.weights[p + "ffn_gate_exps.weight"] = filled({E, F, D});
+ env.weights[p + "ffn_gate_exps.bias"] = filled({E, F});
+ env.weights[p + "ffn_up_exps.weight"] = filled({E, F, D});
+ env.weights[p + "ffn_up_exps.bias"] = filled({E, F});
+ env.weights[p + "ffn_down_exps.weight"] = filled({E, D, F});
+ env.weights[p + "ffn_down_exps.bias"] = filled({E, D});
+ env.architecture("gpt-oss");
+ DecoderConfig config(env.parameters(RopeMode::Neox), env.weights);
+ ASSERT_TRUE(config.layer_is_moe(0));
+ ASSERT_TRUE(config.moe_softmax_weight);
+ ASSERT_TRUE(config.moe_swiglu_oai);
+ ASSERT_FLOAT_EQ(config.expert_weights_scale, weight_scale);
+
+ GgufGraphContext graph(env.context);
+ graph.configure_decoder(RopeMode::Neox);
+ graph.set_output(graph.decoder_ffn(0, graph.add_input("x", ov::element::f32, {1, 1, -1, D})));
+ auto model = convert(graph.finish());
+
+ const auto x = filled({1, 1, T, D});
+ ov::Core core;
+ auto request =
+ core.compile_model(model, "CPU", ov::hint::inference_precision(ov::element::f32)).create_infer_request();
+ request.set_input_tensor(x);
+ request.infer();
+ const auto result = request.get_output_tensor();
+
+ const auto w = [&](const std::string& name) {
+ return env.weights.at(p + name).data.data<const float>();
+ };
+ const float* xs = x.data<const float>();
+ for (size_t t = 0; t < T; ++t) {
+ const float* xt = xs + t * D;
+ std::vector<float> logits(E);
+ for (size_t e = 0; e < E; ++e) {
+ logits[e] = w("ffn_gate_inp.bias")[e];
+ for (size_t d = 0; d < D; ++d)
+ logits[e] += w("ffn_gate_inp.weight")[e * D + d] * xt[d];
+ }
+ std::vector<size_t> order(E);
+ std::iota(order.begin(), order.end(), size_t{0});
+ std::sort(order.begin(), order.end(), [&](size_t a, size_t b) {
+ return logits[a] > logits[b];
+ });
+ float denom = 0.f;
+ for (size_t k = 0; k < K; ++k)
+ denom += std::exp(logits[order[k]] - logits[order[0]]);
+
+ std::vector<float> expected(D, 0.f);
+ for (size_t k = 0; k < K; ++k) {
+ const size_t e = order[k];
+ const float gate_weight = weight_scale * std::exp(logits[e] - logits[order[0]]) / denom;
+ std::vector<float> act(F);
+ for (size_t f = 0; f < F; ++f) {
+ float gate = w("ffn_gate_exps.bias")[e * F + f];
+ float up = w("ffn_up_exps.bias")[e * F + f];
+ for (size_t d = 0; d < D; ++d) {
+ gate += w("ffn_gate_exps.weight")[(e * F + f) * D + d] * xt[d];
+ up += w("ffn_up_exps.weight")[(e * F + f) * D + d] * xt[d];
+ }
+ gate = std::min(gate, limit);
+ up = std::clamp(up, -limit, limit);
+ act[f] = gate / (1.f + std::exp(-alpha * gate)) * (up + 1.f);
+ }
+ for (size_t d = 0; d < D; ++d) {
+ float down = w("ffn_down_exps.bias")[e * D + d];
+ for (size_t f = 0; f < F; ++f)
+ down += w("ffn_down_exps.weight")[(e * D + d) * F + f] * act[f];
+ expected[d] += gate_weight * down;
+ }
+ }
+ for (size_t d = 0; d < D; ++d)
+ EXPECT_NEAR(result.data<const float>()[t * D + d], expected[d], 1e-5f) << "token " << t << " dim " << d;
+ }
+}
+
TEST(GGUFBuilderAPI, Exaone64LayerDefaultsIncludeLocalRopeAndSlidingWindow) {
Environment env;
env.decoder();
diff --git a/src/frontends/gguf/tests/test_ops.cpp b/src/frontends/gguf/tests/test_ops.cpp
index b60992592b4..0dc2e3466d2 100644
--- a/src/frontends/gguf/tests/test_ops.cpp
+++ b/src/frontends/gguf/tests/test_ops.cpp
@@ -12,6 +12,7 @@
#include <cmath>
#include <functional>
+#include "common_test_utils/test_assertions.hpp"
#include "op_table.hpp"
#include "op_test_utils.hpp"
#include "openvino/core/type/bfloat16.hpp"
@@ -425,6 +426,57 @@ TEST(GGUFOps, SoftMax) {
expect_near(out, expected, 1e-5f);
}
+// The native builder can select the softmax axis. gpt-oss normalizes its top-k gating weights
+// [1,T,K,1] over K; the default last axis has size 1 and would turn every weight into 1.
+TEST(GGUFOps, SoftMaxHonorsExplicitAxis) {
+ const size_t T = 2, K = 3;
+ auto model = SingleOpBuilder()
+ .op("GGML_OP_SOFT_MAX")
+ .input("x", ov::element::f32, {1, T, K, 1})
+ .output("out", ov::element::f32, {1, T, K, 1})
+ .attr<int64_t>("softmax_axis", 2)
+ .build();
+
+ std::vector<float> x{0.5f, -1.f, 2.f, 3.f, 0.f, 1.f};
+ auto out = run_on_cpu(model, {{"x", make_f32_tensor({1, T, K, 1}, x)}});
+
+ std::vector<float> expected(x.size());
+ for (size_t t = 0; t < T; ++t) {
+ float sum = 0.f;
+ for (size_t k = 0; k < K; ++k)
+ sum += std::exp(x[t * K + k]);
+ for (size_t k = 0; k < K; ++k)
+ expected[t * K + k] = std::exp(x[t * K + k]) / sum;
+ }
+ expect_near(out, expected, 1e-5f);
+}
+
+TEST(GGUFOps, SoftMaxRejectsOutOfRangeAxis) {
+ for (int64_t axis : {int64_t{4}, int64_t{-5}}) {
+ SCOPED_TRACE(axis);
+ OV_EXPECT_THROW(SingleOpBuilder()
+ .op("GGML_OP_SOFT_MAX")
+ .input("x", ov::element::f32, {1, 2, 3, 1})
+ .output("out", ov::element::f32, {1, 2, 3, 1})
+ .attr<int64_t>("softmax_axis", axis)
+ .build(),
+ ov::Exception,
+ testing::HasSubstr("is out of range for input rank"));
+ }
+}
+
+TEST(GGUFOps, SoftMaxSinksRejectNonLastAxis) {
+ OV_EXPECT_THROW(SingleOpBuilder()
+ .op("GGML_OP_SOFT_MAX")
+ .input("x", ov::element::f32, {1, 2, 1, 3})
+ .input("sinks", ov::element::f32, {1, 1, 1, 2})
+ .output("out", ov::element::f32, {1, 2, 1, 3})
+ .attr<int64_t>("softmax_axis", 2)
+ .build(),
+ ov::Exception,
+ testing::HasSubstr("attention sinks supports only the last softmax_axis"));
+}
+
// Softmax with ALiBi (max_bias > 0): per-head slope applied to the mask.
TEST(GGUFOps, SoftMaxAlibi) {
const uint32_t n_head = 6;