Skip to content

Commit 047976e

Browse files
[ONNX] Added FastGelu from com.microsoft domain (#28715)
Details: Microsoft Contrib Operator "FastGelu" for ONNX RT Tickets: N/A --------- Co-authored-by: Georgy Krivoruchko <georgy.krivoruchko@intel.com>
1 parent 3efb13a commit 047976e

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (C) 2018-2025 Intel Corporation
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
5+
#include "core/operator_set.hpp"
6+
#include "exceptions.hpp"
7+
#include "openvino/frontend/exception.hpp"
8+
#include "openvino/op/add.hpp"
9+
#include "openvino/op/constant.hpp"
10+
#include "openvino/op/gelu.hpp"
11+
#include "utils/common.hpp"
12+
13+
using namespace ov::op;
14+
15+
namespace ov {
16+
namespace frontend {
17+
namespace onnx {
18+
namespace com_microsoft {
19+
namespace opset_1 {
20+
21+
ov::OutputVector fast_gelu(const ov::frontend::onnx::Node& node) {
22+
// Original documentation:
23+
// https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#commicrosoftfastgelu
24+
common::default_op_checks(node, 1);
25+
26+
const auto inputs = node.get_ov_inputs();
27+
const auto& x = inputs[0];
28+
29+
CHECK_VALID_NODE(node,
30+
x.get_element_type() == ov::element::f16 || x.get_element_type() == ov::element::f32 ||
31+
x.get_element_type() == ov::element::bf16,
32+
"Unsupported input data type for X, expected FP16, FP32, or BF16 but got: ",
33+
x.get_element_type());
34+
35+
auto input_with_bias = x;
36+
37+
if (inputs.size() > 1) {
38+
auto& bias = inputs[1];
39+
input_with_bias = std::make_shared<v1::Add>(x, bias);
40+
}
41+
42+
const auto approximation_mode = ov::op::GeluApproximationMode::TANH;
43+
return {std::make_shared<v7::Gelu>(input_with_bias, approximation_mode)};
44+
}
45+
46+
ONNX_OP("FastGelu", OPSET_SINCE(1), com_microsoft::opset_1::fast_gelu, MICROSOFT_DOMAIN);
47+
48+
} // namespace opset_1
49+
} // namespace com_microsoft
50+
} // namespace onnx
51+
} // namespace frontend
52+
} // namespace ov
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
ir_version: 7
2+
producer_name: "OpenVINO ONNX Frontend"
3+
graph {
4+
node {
5+
input: "X"
6+
input: "bias"
7+
output: "Y"
8+
op_type: "FastGelu"
9+
domain: "com.microsoft"
10+
}
11+
name: "test_fast_gelu"
12+
input {
13+
name: "X"
14+
type {
15+
tensor_type {
16+
elem_type: 1
17+
shape {
18+
dim {
19+
dim_value: 2
20+
}
21+
dim {
22+
dim_value: 4
23+
}
24+
dim {
25+
dim_value: 3
26+
}
27+
}
28+
}
29+
}
30+
}
31+
input {
32+
name: "bias"
33+
type {
34+
tensor_type {
35+
elem_type: 1
36+
shape {
37+
dim {
38+
dim_value: 3
39+
}
40+
}
41+
}
42+
}
43+
}
44+
output {
45+
name: "Y"
46+
type {
47+
tensor_type {
48+
elem_type: 1
49+
shape {
50+
dim {
51+
dim_value: 2
52+
}
53+
dim {
54+
dim_value: 4
55+
}
56+
dim {
57+
dim_value: 3
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
opset_import {
65+
version: 1
66+
}

src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,26 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_qlinear_mul) {
16831683
test_case.run();
16841684
}
16851685

1686+
OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_fast_gelu) {
1687+
const auto model = convert_model("com.microsoft/fast_gelu.onnx");
1688+
auto test_case = ov::test::TestCase(model, s_device);
1689+
1690+
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,
1691+
-0.3f, 0.8f, -1.5f, 1.1f, -0.9f, 0.4f, -1.2f, 2.1f, -0.4f, 0.6f, -1.1f, 1.3f};
1692+
const std::vector<float> bias = {0.1f, -0.1f, 0.2f};
1693+
1694+
const std::vector<float> expected_output = {
1695+
0.05398275f, 0.2621714f, 1.0615974f, -0.1378286f, -0.1492447f, 2.1699832f, 1.51072f, -0.0378f,
1696+
0.34569725f, 0.6304f, -0.1696f, 1.2874f, -0.0841f, 0.53025f, -0.12675f, 1.0614f,
1697+
-0.159f, 0.4353f, -0.1492447f, 1.954764f, -0.0841f, 0.53025f, -0.13829723f, 1.39957158f};
1698+
1699+
test_case.add_input<float>(Shape{2, 4, 3}, X);
1700+
test_case.add_input<float>(Shape{3}, bias);
1701+
1702+
test_case.add_expected_output<float>(Shape{2, 4, 3}, expected_output);
1703+
test_case.run_with_tolerance_as_fp(0.0055f);
1704+
}
1705+
16861706
OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_bias_add) {
16871707
const auto model = convert_model("com.microsoft/bias_add.onnx");
16881708
auto test_case = ov::test::TestCase(model, s_device);

0 commit comments

Comments
 (0)