OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[ONNX FE] Support Swish-24 operation (#37176)
Maxim Vafineafc39a8c965
diff --git a/docs/articles_en/documentation/compatibility-and-support/supported-operations.rst b/docs/articles_en/documentation/compatibility-and-support/supported-operations.rstindex eb9a95d7dfc..77b1f9bf986 100644--- a/docs/articles_en/documentation/compatibility-and-support/supported-operations.rst+++ b/docs/articles_en/documentation/compatibility-and-support/supported-operations.rst@@ -240,6 +240,7 @@ Data as of OpenVINO 2025.4.1 (December 18, 2025).STFTSubSum+ SwishTanTanhThresholdedReludiff --git a/src/frontends/onnx/docs/supported_ops.md b/src/frontends/onnx/docs/supported_ops.mdindex 67c7a94813f..51924ad7bca 100644--- a/src/frontends/onnx/docs/supported_ops.md+++ b/src/frontends/onnx/docs/supported_ops.md@@ -196,6 +196,7 @@ OpenVINO provides support for operations of Default Opset (empty in table below)| |StringSplit | |20 | || |Sub |7, 1 |14, 13, 7, 6, 1 | || |Sum |8, 1 |13, 8, 6, 1 | |+| |Swish |24 |24 | || |Tan |7 |22, 7 | || |Tanh |1 |13, 6, 1 | || |TfIdfVectorizer | |9 |Supported through openvino_tokenizers|diff --git a/src/frontends/onnx/frontend/src/core/transform.hpp b/src/frontends/onnx/frontend/src/core/transform.hppindex 0ecde83786d..529b732ef71 100644--- a/src/frontends/onnx/frontend/src/core/transform.hpp+++ b/src/frontends/onnx/frontend/src/core/transform.hpp@@ -38,8 +38,7 @@ static const std::vector<std::string> legacy_ops_to_fixup = {"DeformableConv2D","GroupNorm","Normalize","PriorBox",- "PriorBoxClustered",- "Swish"};+ "PriorBoxClustered"};/// \brief Add support for models with custom operators mistakenly registered in/// "ai.onnx" domain.diff --git a/src/frontends/onnx/frontend/src/op/swish.cpp b/src/frontends/onnx/frontend/src/op/swish.cppnew file mode 100644index 00000000000..d5824213779--- /dev/null+++ b/src/frontends/onnx/frontend/src/op/swish.cpp@@ -0,0 +1,52 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/op/swish.hpp"++#include "core/operator_set.hpp"+#include "exceptions.hpp"+#include "openvino/op/convert.hpp"+#include "utils/reshape.hpp"++using namespace ov::op;++namespace ov {+namespace frontend {+namespace onnx {+namespace ai_onnx {+namespace opset_24 {++ov::OutputVector swish(const ov::frontend::onnx::Node& node) {+ // Operator definition: https://onnx.ai/onnx/operators/onnx__Swish.html+ // Y = X * Sigmoid(alpha * X)+ const auto inputs = node.get_ov_inputs();+ const auto& data = inputs.at(0);++ // A second input is accepted for backward compatibility with the legacy OpenVINO Swish,+ // which passes beta as an input instead of the standard "alpha" attribute.+ ov::Output<ov::Node> alpha;+ if (inputs.size() > 1) {+ CHECK_VALID_NODE(node,+ !node.has_attribute("alpha"),+ "Swish expects either an 'alpha' attribute or a beta input, but not both.");+ alpha = ov::frontend::onnx::reshape::interpret_as_scalar(inputs.at(1));+ if (alpha.get_element_type() != data.get_element_type()) {+ alpha = std::make_shared<v0::Convert>(alpha, data.get_element_type());+ }+ } else {+ alpha = node.get_attribute_as_constant<float>("alpha", 1.0f, data.get_element_type());+ }++ return {std::make_shared<v4::Swish>(data, alpha)};+}++// Swish is a part of the standard ONNX domain since opset 24, but it is registered since opset 1,+// because OperatorsBridge requires a translator for every version imported by a model.+ONNX_OP("Swish", OPSET_SINCE(1), ai_onnx::opset_24::swish);++} // namespace opset_24+} // namespace ai_onnx+} // namespace onnx+} // namespace frontend+} // namespace ovdiff --git a/src/frontends/onnx/tests/models/swish_opset24.prototxt b/src/frontends/onnx/tests/models/swish_opset24.prototxtnew file mode 100644index 00000000000..a8fc7a2ce9a--- /dev/null+++ b/src/frontends/onnx/tests/models/swish_opset24.prototxt@@ -0,0 +1,40 @@+ir_version: 10+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ output: "Y"+ op_type: "Swish"+ }+ name: "test_swish_opset24"+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 6+ }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 6+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 24+}diff --git a/src/frontends/onnx/tests/models/swish_opset24_alpha.prototxt b/src/frontends/onnx/tests/models/swish_opset24_alpha.prototxtnew file mode 100644index 00000000000..3fd00cf48d0--- /dev/null+++ b/src/frontends/onnx/tests/models/swish_opset24_alpha.prototxt@@ -0,0 +1,45 @@+ir_version: 10+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "X"+ output: "Y"+ op_type: "Swish"+ attribute {+ name: "alpha"+ f: 0.5+ type: FLOAT+ }+ }+ name: "test_swish_opset24_alpha"+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 6+ }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 6+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 24+}diff --git a/src/frontends/onnx/tests/models/swish_opset24_beta.prototxt b/src/frontends/onnx/tests/models/swish_opset24_beta.prototxtnew file mode 100644index 00000000000..9ea29cfb387--- /dev/null+++ b/src/frontends/onnx/tests/models/swish_opset24_beta.prototxt@@ -0,0 +1,55 @@+ir_version: 10+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ output: "beta"+ op_type: "Constant"+ attribute {+ name: "value"+ t {+ data_type: 1+ dims: 1+ float_data: 0.75+ name: "const_tensor"+ }+ type: TENSOR+ }+ }+ node {+ input: "X"+ input: "beta"+ output: "Y"+ op_type: "Swish"+ }+ name: "test_swish_opset24_beta"+ 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 {+ domain: ""+ version: 24+}diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cppindex d3847aef52c..fe5d188cfb0 100644--- a/src/frontends/onnx/tests/onnx_import.in.cpp+++ b/src/frontends/onnx/tests/onnx_import.in.cpp@@ -9076,3 +9076,35 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_opset24_qk_output_mode2) {0.873769f});test_case.run_with_tolerance_as_fp(1e-4f);}++// Standard ONNX opset-24 Swish: Y = X * Sigmoid(alpha * X). The translator maps it to+// ov::op::v4::Swish so that plugins can execute it as a single activation.+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_swish_opset24) {+ auto model = convert_model("swish_opset24.onnx");++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{6}, {-4.0f, -1.0f, -0.5f, 0.0f, 0.5f, 4.0f});+ test_case.add_expected_output<float>(Shape{6}, {-0.071945f, -0.268941f, -0.188770f, 0.0f, 0.311230f, 3.928055f});++ test_case.run_with_tolerance_as_fp(2.0e-5f);+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_swish_opset24_alpha) {+ auto model = convert_model("swish_opset24_alpha.onnx");++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{6}, {-4.0f, -1.0f, -0.5f, 0.0f, 0.5f, 4.0f});+ test_case.add_expected_output<float>(Shape{6}, {-0.476812f, -0.377541f, -0.218912f, 0.0f, 0.281088f, 3.523188f});++ test_case.run_with_tolerance_as_fp(2.0e-5f);+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_swish_opset24_beta_input) {+ auto model = convert_model("swish_opset24_beta.onnx");++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{3}, {-0.5f, 0.0f, 0.5f});+ test_case.add_expected_output<float>(Shape{3}, {-0.2036667f, 0.0f, 0.2963333f});++ test_case.run_with_tolerance_as_fp(2.0e-5f);+}