Skip to content

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

[ONNX FE] Softmax opset 11/12: coerce the input to 2-D before softmax (#37979)

HuEnwei846e87abac00
diff --git a/src/frontends/onnx/frontend/src/op/softmax.cpp b/src/frontends/onnx/frontend/src/op/softmax.cpp
index 9803f11dec4..7e9e845fa1c 100644
--- a/src/frontends/onnx/frontend/src/op/softmax.cpp
+++ b/src/frontends/onnx/frontend/src/op/softmax.cpp
@@ -9,6 +9,7 @@
#include "openvino/op/constant.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
+#include "utils/common.hpp"
#include "utils/reshape.hpp"
using namespace ov::op;
@@ -65,7 +66,11 @@ ov::OutputVector softmax(const ov::frontend::onnx::Node& node) {
break;
}
default: {
- result = std::make_shared<v8::Softmax>(data, axis);
+ // 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;
}
}
diff --git a/src/frontends/onnx/tests/models/softmax_axis_1_opset12.prototxt b/src/frontends/onnx/tests/models/softmax_axis_1_opset12.prototxt
new file mode 100644
index 00000000000..56d3ee4448b
--- /dev/null
+++ b/src/frontends/onnx/tests/models/softmax_axis_1_opset12.prototxt
@@ -0,0 +1,45 @@
+ir_version: 8
+graph {
+ node {
+ input: "x"
+ output: "y"
+ op_type: "Softmax"
+ attribute {
+ name: "axis"
+ i: 1
+ type: INT
+ }
+ }
+ name: "g"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 5
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 12
+}
diff --git a/src/frontends/onnx/tests/models/softmax_axis_2_opset11.prototxt b/src/frontends/onnx/tests/models/softmax_axis_2_opset11.prototxt
new file mode 100644
index 00000000000..6336be73ffa
--- /dev/null
+++ b/src/frontends/onnx/tests/models/softmax_axis_2_opset11.prototxt
@@ -0,0 +1,48 @@
+ir_version: 8
+graph {
+ node {
+ input: "x"
+ output: "y"
+ op_type: "Softmax"
+ attribute {
+ name: "axis"
+ i: 2
+ type: INT
+ }
+ }
+ name: "g"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 11
+}
diff --git a/src/frontends/onnx/tests/models/softmax_invalid_axis_opset11.prototxt b/src/frontends/onnx/tests/models/softmax_invalid_axis_opset11.prototxt
new file mode 100644
index 00000000000..91445a2910a
--- /dev/null
+++ b/src/frontends/onnx/tests/models/softmax_invalid_axis_opset11.prototxt
@@ -0,0 +1,42 @@
+ir_version: 8
+graph {
+ node {
+ input: "x"
+ output: "y"
+ op_type: "Softmax"
+ attribute {
+ name: "axis"
+ i: 5
+ type: INT
+ }
+ }
+ name: "test_softmax_invalid_axis"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 11
+}
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index ac20fda6542..0290481c7ce 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -1069,6 +1069,9 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_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
+ // restores the shape. Expected output equals the opset-6 (onnx_model_softmax_axis_1) result.
auto model = convert_model("softmax_axis_1_opset11.onnx");
auto test_case = ov::test::TestCase(model, s_device);
@@ -1077,20 +1080,84 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_1_opset11) {
// clang-format off
test_case.add_expected_output<float>(
Shape{3, 4, 5},
- {0.88890495f, 0.04825497f, 0.27088348f, 0.04490523f, 0.02037154f,
- 0.06955369f, 0.31998834f, 0.39223197f, 0.68041159f, 0.05141776f,
- 0.02566661f, 0.5885689f, 0.12453075f, 0.06257374f, 0.03019055f,
- 0.01587475f, 0.0431878f, 0.21235381f, 0.21210944f, 0.89802015f,
-
- 0.31752626f, 0.19442629f, 0.0546935f, 0.06279221f, 0.36823282f,
- 0.10362164f, 0.06523066f, 0.24006419f, 0.03103672f, 0.32987983f,
- 0.55743381f, 0.473766f, 0.61451431f, 0.09486084f, 0.03722801f,
- 0.02141829f, 0.26657706f, 0.090728f, 0.81131024f, 0.26465935f,
-
- 0.08619648f, 0.43343993f, 0.3877785f, 0.04523505f, 0.15625437f,
- 0.61900597f, 0.01653285f, 0.06394322f, 0.56592636f, 0.27376196f,
- 0.11201305f, 0.31654337f, 0.21947994f, 0.07893034f, 0.05236297f,
- 0.18278451f, 0.23348385f, 0.32879834f, 0.30990825f, 0.5176207f});
+ {0.22757064f, 0.00868076f, 0.03277484f, 0.00773243f, 0.0055188f,
+ 0.0178066f, 0.05756383f, 0.04745709f, 0.11716303f, 0.01392945f,
+ 0.00657097f, 0.10587974f, 0.01506727f, 0.01077484f, 0.00817884f,
+ 0.00406413f, 0.00776921f, 0.0256932f, 0.03652405f, 0.24328028f,
+
+ 0.06217413f, 0.02201481f, 0.00689594f, 0.01944171f, 0.09399488f,
+ 0.02028993f, 0.00738604f, 0.03026811f, 0.00960958f, 0.08420492f,
+ 0.10914991f, 0.05364435f, 0.07748005f, 0.02937079f, 0.0095028f,
+ 0.00419387f, 0.03018442f, 0.01143929f, 0.2511977f, 0.06755678f,
+
+ 0.00587593f, 0.04548053f, 0.0346656f, 0.02252594f, 0.03742775f,
+ 0.04219705f, 0.00173478f, 0.00571623f, 0.2818174f, 0.06557446f,
+ 0.00763582f, 0.03321466f, 0.01962049f, 0.03930537f, 0.01254255f,
+ 0.01246025f, 0.02449929f, 0.02939305f, 0.15432668f, 0.12398617f});
+ // clang-format on
+
+ test_case.run(4);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_1_opset12) {
+ auto model = convert_model("softmax_axis_1_opset12.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>(SOFTMAX_INPUT);
+
+ // clang-format off
+ test_case.add_expected_output<float>(
+ Shape{3, 4, 5},
+ {0.22757064f, 0.00868076f, 0.03277484f, 0.00773243f, 0.0055188f,
+ 0.0178066f, 0.05756383f, 0.04745709f, 0.11716303f, 0.01392945f,
+ 0.00657097f, 0.10587974f, 0.01506727f, 0.01077484f, 0.00817884f,
+ 0.00406413f, 0.00776921f, 0.0256932f, 0.03652405f, 0.24328028f,
+
+ 0.06217413f, 0.02201481f, 0.00689594f, 0.01944171f, 0.09399488f,
+ 0.02028993f, 0.00738604f, 0.03026811f, 0.00960958f, 0.08420492f,
+ 0.10914991f, 0.05364435f, 0.07748005f, 0.02937079f, 0.0095028f,
+ 0.00419387f, 0.03018442f, 0.01143929f, 0.2511977f, 0.06755678f,
+
+ 0.00587593f, 0.04548053f, 0.0346656f, 0.02252594f, 0.03742775f,
+ 0.04219705f, 0.00173478f, 0.00571623f, 0.2818174f, 0.06557446f,
+ 0.00763582f, 0.03321466f, 0.01962049f, 0.03930537f, 0.01254255f,
+ 0.01246025f, 0.02449929f, 0.02939305f, 0.15432668f, 0.12398617f});
+ // clang-format on
+
+ test_case.run(4);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softmax_axis_2_opset11) {
+ // rank-4 input with axis not equal to the last dimension: the opset-11 flattening spans
+ // prod(d_axis..d_rank-1) = 3 * 4 = 12 elements, not the single dimension 3.
+ auto model = convert_model("softmax_axis_2_opset11.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ // clang-format off
+ test_case.add_input<float>(
+ Shape{2, 2, 3, 4},
+ {1.749455f, -0.286073f, -0.484565f, -2.653319f, -0.008285f, -0.319631f,
+ -0.536629f, 0.315403f, 0.421051f, -1.065603f, -0.886240f, -0.475733f,
+ 0.689682f, 0.561192f, -1.305549f, -1.119475f, 0.736837f, 1.574634f,
+ -0.031075f, -0.683447f, 1.095630f, -0.309577f, 0.725752f, 1.549072f,
+ 0.630080f, 0.073493f, 0.732271f, -0.642575f, -0.178093f, -0.573955f,
+ -0.204375f, -0.486495f, -0.185775f, -0.380536f, 0.088978f, 0.063672f,
+ 0.296347f, 1.402771f, -1.546863f, 1.295619f, -0.237250f, -1.232346f,
+ -0.172420f, 0.091838f, 1.067558f, -1.061634f, 0.217348f, 0.117820f});
+ // clang-format on
+
+ // clang-format off
+ test_case.add_expected_output<float>(
+ Shape{2, 2, 3, 4},
+ {0.41784897f, 0.05457588f, 0.04475038f, 0.00511586f, 0.07205142f, 0.05277481f,
+ 0.04248012f, 0.09959062f, 0.11068808f, 0.02502967f, 0.02994688f, 0.04514737f,
+ 0.08578264f, 0.07543916f, 0.01166491f, 0.01405052f, 0.08992461f, 0.20784001f,
+ 0.04172330f, 0.02172987f, 0.12873612f, 0.03158106f, 0.08893329f, 0.20259455f,
+ 0.15566795f, 0.08922295f, 0.17241704f, 0.04360057f, 0.06937678f, 0.04669748f,
+ 0.06757717f, 0.05096557f, 0.06884588f, 0.05666230f, 0.09061530f, 0.08835097f,
+ 0.07506169f, 0.22695102f, 0.01188290f, 0.20389034f, 0.04402305f, 0.01627479f,
+ 0.04697160f, 0.06117883f, 0.16231214f, 0.01930432f, 0.06936006f, 0.06278921f});
// clang-format on
test_case.run(4);
diff --git a/src/frontends/onnx/tests/onnx_import_exceptions.cpp b/src/frontends/onnx/tests/onnx_import_exceptions.cpp
index e491a2acbaa..59c37cf1c4d 100644
--- a/src/frontends/onnx/tests/onnx_import_exceptions.cpp
+++ b/src/frontends/onnx/tests/onnx_import_exceptions.cpp
@@ -113,3 +113,20 @@ TEST(onnx_importer, exception_msg_onnx_reduce_wrong_type_v3) {
TEST(onnx_importer, no_exception_onnx_reduce_wrong_type_v4) {
EXPECT_NO_THROW(convert_model("reduce_wrong_type_v4.onnx"));
}
+
+TEST(onnx_importer, exception_msg_softmax_axis_out_of_range) {
+ try {
+ convert_model("softmax_invalid_axis_opset11.onnx");
+ // Should have thrown, so fail if it didn't
+ FAIL() << "ONNX Importer did not detected incorrect model!";
+ } catch (const ::ov::Exception& e) {
+ EXPECT_HAS_SUBSTRING(e.what(), std::string("out of tensor range"));
+ }
+ // On MacOS after we re-throw ov::Exception exception, we couldn't catch it as is,
+ // thus below workaround.
+ catch (const std::exception& e) {
+ EXPECT_HAS_SUBSTRING(e.what(), std::string("out of tensor range"));
+ } catch (...) {
+ FAIL() << "The ONNX model importer failed for unexpected reason";
+ }
+}