Skip to content

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

[ONNX] Added FastGelu from com.microsoft domain (#28715)

Vatsala Shanubhag047976e36904
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/fast_gelu.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/fast_gelu.cpp
new file mode 100644
index 00000000000..d5f5a9049ca
--- /dev/null
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/fast_gelu.cpp
@@ -0,0 +1,52 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "core/operator_set.hpp"
+#include "exceptions.hpp"
+#include "openvino/frontend/exception.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/gelu.hpp"
+#include "utils/common.hpp"
+
+using namespace ov::op;
+
+namespace ov {
+namespace frontend {
+namespace onnx {
+namespace com_microsoft {
+namespace opset_1 {
+
+ov::OutputVector fast_gelu(const ov::frontend::onnx::Node& node) {
+ // Original documentation:
+ // https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#commicrosoftfastgelu
+ common::default_op_checks(node, 1);
+
+ const auto inputs = node.get_ov_inputs();
+ const auto& x = inputs[0];
+
+ CHECK_VALID_NODE(node,
+ x.get_element_type() == ov::element::f16 || x.get_element_type() == ov::element::f32 ||
+ x.get_element_type() == ov::element::bf16,
+ "Unsupported input data type for X, expected FP16, FP32, or BF16 but got: ",
+ x.get_element_type());
+
+ auto input_with_bias = x;
+
+ if (inputs.size() > 1) {
+ auto& bias = inputs[1];
+ input_with_bias = std::make_shared<v1::Add>(x, bias);
+ }
+
+ const auto approximation_mode = ov::op::GeluApproximationMode::TANH;
+ return {std::make_shared<v7::Gelu>(input_with_bias, approximation_mode)};
+}
+
+ONNX_OP("FastGelu", OPSET_SINCE(1), com_microsoft::opset_1::fast_gelu, MICROSOFT_DOMAIN);
+
+} // namespace opset_1
+} // namespace com_microsoft
+} // namespace onnx
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/onnx/tests/models/com.microsoft/fast_gelu.prototxt b/src/frontends/onnx/tests/models/com.microsoft/fast_gelu.prototxt
new file mode 100644
index 00000000000..87938d9ebf3
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/fast_gelu.prototxt
@@ -0,0 +1,66 @@
+ir_version: 7
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "X"
+ input: "bias"
+ output: "Y"
+ op_type: "FastGelu"
+ domain: "com.microsoft"
+ }
+ name: "test_fast_gelu"
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "bias"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 1
+}
diff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
index 1324c116804..09a96ef3a1e 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -1683,6 +1683,26 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinear_mul) {
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_fast_gelu) {
+ const auto model = convert_model("com.microsoft/fast_gelu.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ const std::vector<float> X = {0.0f, 0.5f, 1.0f, -0.5f, -1.0f, 2.0f, 1.5f, -2.0f, 0.3f, 0.7f, -0.7f, 1.2f,
+ -0.3f, 0.8f, -1.5f, 1.1f, -0.9f, 0.4f, -1.2f, 2.1f, -0.4f, 0.6f, -1.1f, 1.3f};
+ const std::vector<float> bias = {0.1f, -0.1f, 0.2f};
+
+ const std::vector<float> expected_output = {
+ 0.05398275f, 0.2621714f, 1.0615974f, -0.1378286f, -0.1492447f, 2.1699832f, 1.51072f, -0.0378f,
+ 0.34569725f, 0.6304f, -0.1696f, 1.2874f, -0.0841f, 0.53025f, -0.12675f, 1.0614f,
+ -0.159f, 0.4353f, -0.1492447f, 1.954764f, -0.0841f, 0.53025f, -0.13829723f, 1.39957158f};
+
+ test_case.add_input<float>(Shape{2, 4, 3}, X);
+ test_case.add_input<float>(Shape{3}, bias);
+
+ test_case.add_expected_output<float>(Shape{2, 4, 3}, expected_output);
+ test_case.run_with_tolerance_as_fp(0.0055f);
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_bias_add) {
const auto model = convert_model("com.microsoft/bias_add.onnx");
auto test_case = ov::test::TestCase(model, s_device);