Skip to content

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

[ONNX] Fix Softmax translation when axis == rank -1 (#38874)

Andrii Staikov7f95af20ff7d
diff --git a/src/frontends/onnx/frontend/src/op/softmax.cpp b/src/frontends/onnx/frontend/src/op/softmax.cpp
index e601a4e73b5..8f2e34a1de3 100644
--- a/src/frontends/onnx/frontend/src/op/softmax.cpp
+++ b/src/frontends/onnx/frontend/src/op/softmax.cpp
@@ -22,10 +22,7 @@ std::shared_ptr<ov::Node> onnx_softmax(const ov::Output<ov::Node> data, const in
const bool special_zero = false;
return std::make_shared<v1::Reshape>(result, data_shape, special_zero);
}
-} // namespace
-} // namespace ov::frontend::onnx
-namespace ov::frontend::onnx::ai_onnx::opset_1 {
ov::OutputVector softmax(const ov::frontend::onnx::Node& node) {
const auto data = node.get_ov_inputs().at(0);
const auto data_rank = data.get_partial_shape().rank();
@@ -34,47 +31,41 @@ ov::OutputVector softmax(const ov::frontend::onnx::Node& node) {
const auto axis = node.get_attribute_value<int64_t>("axis", 1);
std::shared_ptr<ov::Node> result;
- switch (data_rank.get_length()) {
- case 0: {
- result = v0::Constant::create(data.get_element_type(), ov::Shape{}, {1});
- break;
- }
- default: {
- result = onnx_softmax(data, axis);
- break;
- }
+ if (data_rank.get_length() == 0) {
+ return {v0::Constant::create(data.get_element_type(), ov::Shape{}, {1})};
+ } else {
+ // ONNX Softmax opset 1-12 coerces the input to a 2-D matrix
+ // [prod(d0..d_axis-1), prod(d_axis..d_rank-1)], applies softmax on the second axis and
+ // restores the shape. Flattening at the last axis is a no-op reshape, so it is skipped.
+ const auto normalized_axis = common::normalize_axis(node.get_description(), axis, data_rank);
+ if (normalized_axis == data_rank.get_length() - 1) {
+ return {std::make_shared<v8::Softmax>(data, axis)};
+ } else {
+ return {onnx_softmax(data, normalized_axis)};
+ }
}
-
- return {result};
}
-ONNX_OP("Softmax", OPSET_RANGE(1, 10), ai_onnx::opset_1::softmax);
-} // namespace ov::frontend::onnx::ai_onnx::opset_1
+} // namespace
+} // namespace ov::frontend::onnx
-namespace ov::frontend::onnx::ai_onnx::opset_11 {
+namespace ov::frontend::onnx::ai_onnx::opset_1 {
ov::OutputVector softmax(const ov::frontend::onnx::Node& node) {
const auto data = node.get_ov_inputs().at(0);
const auto data_rank = data.get_partial_shape().rank();
- FRONT_END_GENERAL_CHECK(data_rank.is_static(), "ONNX Softmax data rank needs to be known (static)");
-
const auto axis = node.get_attribute_value<int64_t>("axis", 1);
-
- std::shared_ptr<ov::Node> result;
- switch (data_rank.get_length()) {
- case 0: {
- result = v0::Constant::create(data.get_element_type(), ov::Shape{}, {1});
- break;
- }
- default: {
- // ONNX Softmax opset 11/12 coerces the input to a 2-D matrix
- // [prod(d0..d_axis-1), prod(d_axis..d_rank-1)], applies softmax on the second
- // axis and restores the shape (same semantics as opset 1-10).
- const auto normalized_axis = common::normalize_axis(node.get_description(), axis, data_rank);
- result = onnx_softmax(data, normalized_axis);
- break;
- }
+ FRONT_END_GENERAL_CHECK(data_rank.is_static(), "ONNX Softmax data rank needs to be known (static)");
+ // Opsets 1-10 allow the flattening boundary axis equal to the input rank.
+ if (data_rank.get_length() > 0 && axis == data_rank.get_length()) {
+ return {ov::frontend::onnx::onnx_softmax(data, axis)};
}
+ return ov::frontend::onnx::softmax(node);
+}
+ONNX_OP("Softmax", OPSET_RANGE(1, 10), ai_onnx::opset_1::softmax);
+} // namespace ov::frontend::onnx::ai_onnx::opset_1
- return {result};
+namespace ov::frontend::onnx::ai_onnx::opset_11 {
+ov::OutputVector softmax(const ov::frontend::onnx::Node& node) {
+ return ov::frontend::onnx::softmax(node);
}
ONNX_OP("Softmax", OPSET_RANGE(11, 12), ai_onnx::opset_11::softmax);
} // namespace ov::frontend::onnx::ai_onnx::opset_11
diff --git a/src/frontends/onnx/tests/models/softmax_1D_no_axis.prototxt b/src/frontends/onnx/tests/models/softmax_1D_no_axis.prototxt
new file mode 100644
index 00000000000..7e6e7e32a70
--- /dev/null
+++ b/src/frontends/onnx/tests/models/softmax_1D_no_axis.prototxt
@@ -0,0 +1,39 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ output: "y"
+ op_type: "Softmax"
+ }
+ name: "test_softmax_1D_no_axis"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 7
+}
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index b5d2e9205d3..6dc6756663f 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -33,7 +33,9 @@
#include "gtest/gtest.h"
#include "onnx_utils.hpp"
#include "openvino/op/constant.hpp"
+#include "openvino/op/reshape.hpp"
#include "openvino/op/scatter_elements_update.hpp"
+#include "openvino/op/softmax.hpp"
using namespace ov;
using namespace ov::frontend::onnx::tests;
@@ -1068,6 +1070,15 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_1) {
test_case.run(4);
}
+// axis=2 is the last dimension of the rank-3 input: the flatten/unflatten Reshape pair
+// around Softmax is a no-op here, so it must be skipped (same optimization as opset 11/12).
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_2_no_reshape) {
+ auto model = convert_model("softmax_axis_2.onnx");
+
+ EXPECT_EQ(count_ops_of_type<op::v1::Reshape>(model), 0);
+ EXPECT_EQ(count_ops_of_type<op::v8::Softmax>(model), 1);
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_1_opset11) {
// ONNX Softmax opset 11 (same semantics as opset 1-10) flattens the input to 2-D
// [prod(d0..d_axis-1), prod(d_axis..d_rank-1)], applies softmax on the second axis and
@@ -1219,6 +1230,37 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_negative_1_opset13) {
test_case.run(6);
}
+// opset 11/12 Softmax normally flattens to 2-D around a plain Softmax (2 Reshape nodes).
+// When the (normalized) axis is already the last dimension, that flatten/unflatten is a no-op,
+// so the translator should skip it and emit a plain Softmax directly (no Reshape at all).
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_negative_1_opset11_no_reshape) {
+ auto model = convert_model("softmax_axis_negative_1_opset11.onnx");
+
+ EXPECT_EQ(count_ops_of_type<op::v1::Reshape>(model), 0);
+ EXPECT_EQ(count_ops_of_type<op::v8::Softmax>(model), 1);
+}
+
+// axis=2 on a rank-4 input is NOT the last dimension, so the flatten/unflatten Reshape pair
+// around Softmax must still be present (this is the actual opset 11/12 spec behavior fixed by #37979).
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_2_opset11_keeps_reshape) {
+ auto model = convert_model("softmax_axis_2_opset11.onnx");
+
+ EXPECT_EQ(count_ops_of_type<op::v1::Reshape>(model), 2);
+ EXPECT_EQ(count_ops_of_type<op::v8::Softmax>(model), 1);
+}
+
+// Opsets 1-10 allow axis == rank (flattening boundary past the last dimension): for a rank-1
+// input with axis omitted (defaults to 1, which equals the rank), this coerces to a [N, 1]
+// matrix, so every row has a single element and softmax trivially produces all ones.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_1D_no_axis) {
+ auto model = convert_model("softmax_1D_no_axis.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({1.0f, 2.0f, 3.0f});
+ test_case.add_expected_output<float>(Shape{3}, {1.0f, 1.0f, 1.0f});
+ test_case.run();
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_sub) {
auto model = convert_model("sub.onnx");