Skip to content

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

[ONNX] Added QuickGelu from com.microsoft domain (#27238)

virajwada6eb53506e9b
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/quick_gelu.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/quick_gelu.cpp
new file mode 100644
index 00000000000..c4144be9b5f
--- /dev/null
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/quick_gelu.cpp
@@ -0,0 +1,58 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "core/operator_set.hpp"
+#include "exceptions.hpp"
+#include "openvino/frontend/exception.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/sigmoid.hpp"
+#include "utils/common.hpp"
+
+using namespace ov::op;
+
+namespace ov {
+namespace frontend {
+namespace onnx {
+namespace com_microsoft {
+namespace opset_1 {
+ov::OutputVector quick_gelu(const ov::frontend::onnx::Node& node) {
+ // Original Documentation:
+ // https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.QuickGelu
+ // Goal: Compute x * Sigmoid(alpha * x)
+ common::default_op_checks(node, 1);
+
+ const auto inputs = node.get_ov_inputs();
+ const auto& x = inputs[0];
+
+ // Constrain input type to float16, float, double (f64), bfloat16
+ auto element_type = x.get_element_type();
+ CHECK_VALID_NODE(node,
+ element_type == ov::element::f16 || element_type == ov::element::f32 ||
+ element_type == ov::element::f64 || element_type == ov::element::bf16,
+ "Unsupported input x type, accepted FP16, FP32, FP64, BFP16 but got: ",
+ element_type);
+
+ // Get attribute from node
+ const float alpha = node.get_attribute_value<float>("alpha");
+
+ // Numpy broadcasting rule is automatically applied with mismatched shapes according to:
+ // https://docs.openvino.ai/2022.3/openvino_docs_ops_arithmetic_Multiply_1.html "Tensor with dimension of size 1
+ // will be implicitly broadcasted to match the size of the second tensor." Convert alpha to tensor with size 1
+ const auto alpha_tensor = std::make_shared<v0::Constant>(ov::element::f32, Shape{1}, alpha);
+
+ auto alpha_x = std::make_shared<v1::Multiply>(alpha_tensor, x);
+ auto sig_alpha_x = std::make_shared<v0::Sigmoid>(alpha_x);
+ auto result = std::make_shared<v1::Multiply>(x, sig_alpha_x);
+
+ return {result};
+} // func end
+
+ONNX_OP("QuickGelu", OPSET_SINCE(1), com_microsoft::opset_1::quick_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/quick_gelu.prototxt b/src/frontends/onnx/tests/models/com.microsoft/quick_gelu.prototxt
new file mode 100644
index 00000000000..4fb110fd485
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/quick_gelu.prototxt
@@ -0,0 +1,52 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ name: "test_quick_gelu"
+ node {
+ input: "X"
+ output: "Y"
+ op_type: "QuickGelu"
+ attribute {
+ name: "alpha"
+ f: 0.9974269270896912
+ type: FLOAT
+ }
+ domain: "com.microsoft"
+ }
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 5
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 5
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: "com.microsoft"
+ version: 1
+}
\ No newline at end of file
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 da8189926a4..900fc025d8d 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -1330,3 +1330,29 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_3x17) {
}
test_case.run();
}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_quickgelu) {
+ const auto model = convert_model("com.microsoft/quick_gelu.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ const std::vector<float> input_X{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+ const std::vector<float> output{0.7305524f,
+ 1.7605114f,
+ 2.8566725f,
+ 3.9273243f,
+ 4.9661055f,
+ 5.984934f,
+ 6.9935064f,
+ 7.997261f,
+ 8.998864f,
+ 9.999535f};
+
+ test_case.add_input<float>(Shape{2, 5}, input_X);
+ test_case.add_expected_output<float>(Shape{2, 5}, output);
+
+ if (std::string("${BACKEND_NAME}") == std::string("IE_GPU")) {
+ test_case.run_with_tolerance_as_fp(0.0001f);
+ } else {
+ test_case.run();
+ }
+}