Skip to content

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

[ONNX] Add support for GroupNormalization v.21 (#32700)

Andrii Staikovec51a2ac517c
diff --git a/src/frontends/onnx/docs/supported_ops.md b/src/frontends/onnx/docs/supported_ops.md
index d0f6fda8d22..279a6e72234 100644
--- a/src/frontends/onnx/docs/supported_ops.md
+++ b/src/frontends/onnx/docs/supported_ops.md
@@ -79,7 +79,7 @@ OpenVINO provides support for operations of Default Opset (empty in table below)
| |Greater |1 |13, 9, 7, 1 | |
| |GreaterOrEqual |16, 12 |16, 12 | |
| |GridSample |16 |22, 20, 16 | |
-| |GroupNormalization |18 |21, 18 | |
+| |GroupNormalization |21, 18 |21, 18 | |
| |HammingWindow |17 |17 | |
| |HannWindow |17 |17 | |
| |HardSigmoid |1 |22, 6, 1 | |
diff --git a/src/frontends/onnx/frontend/src/op/group_normalization.cpp b/src/frontends/onnx/frontend/src/op/group_normalization.cpp
index 10e0e09800a..93ec7e25fa6 100644
--- a/src/frontends/onnx/frontend/src/op/group_normalization.cpp
+++ b/src/frontends/onnx/frontend/src/op/group_normalization.cpp
@@ -5,13 +5,17 @@
#include "openvino/op/group_normalization.hpp"
#include "core/operator_set.hpp"
+#include "exceptions.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
#include "openvino/op/divide.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/unsqueeze.hpp"
+#include "utils/common.hpp"
+using ::ONNX_NAMESPACE::TensorProto_DataType;
using namespace ov::op;
using ov::Shape;
@@ -19,7 +23,7 @@ namespace ov {
namespace frontend {
namespace onnx {
namespace ai_onnx {
-namespace opset_1 {
+namespace opset_18 {
ov::OutputVector group_normalization(const ov::frontend::onnx::Node& node) {
const auto inputs = node.get_ov_inputs();
OPENVINO_ASSERT(inputs.size() == 3);
@@ -51,8 +55,40 @@ ov::OutputVector group_normalization(const ov::frontend::onnx::Node& node) {
return {std::make_shared<v12::GroupNormalization>(data, c_scale, c_bias, num_groups, eps)};
}
-ONNX_OP("GroupNormalization", OPSET_SINCE(1), ai_onnx::opset_1::group_normalization);
-} // namespace opset_1
+ONNX_OP("GroupNormalization", OPSET_RANGE(1, 20), ai_onnx::opset_18::group_normalization);
+} // namespace opset_18
+namespace opset_21 {
+ov::OutputVector group_normalization(const ov::frontend::onnx::Node& node) {
+ const auto inputs = node.get_ov_inputs();
+ OPENVINO_ASSERT(inputs.size() == 3);
+
+ auto default_stash_type_i = static_cast<int64_t>(TensorProto_DataType::TensorProto_DataType_FLOAT);
+ int64_t stash_type_i = node.get_attribute_value<int64_t>("stash_type", default_stash_type_i);
+ element::Type stash_type = common::get_ov_element_type(stash_type_i);
+
+ ov::Output<ov::Node> data = inputs[0]; // Shape [N, C, ...]
+ ov::Output<ov::Node> scale = inputs[1]; // Shape [C]
+ ov::Output<ov::Node> bias = inputs[2]; // Shape [C]
+
+ element::Type original_type = data.get_element_type();
+ bool needs_type_casting = stash_type != original_type;
+ if (needs_type_casting) {
+ data = std::make_shared<ov::op::v0::Convert>(data, stash_type);
+ scale = std::make_shared<ov::op::v0::Convert>(scale, stash_type);
+ bias = std::make_shared<ov::op::v0::Convert>(bias, stash_type);
+ }
+
+ const auto eps = node.get_attribute_value<float>("epsilon", 1e-05f);
+ const auto num_groups = node.get_attribute_value<int64_t>("num_groups");
+
+ ov::Output<ov::Node> op = std::make_shared<v12::GroupNormalization>(data, scale, bias, num_groups, eps);
+ if (needs_type_casting)
+ op = std::make_shared<ov::op::v0::Convert>(op, original_type);
+
+ return {op};
+}
+ONNX_OP("GroupNormalization", OPSET_SINCE(21), ai_onnx::opset_21::group_normalization);
+} // namespace opset_21
} // namespace ai_onnx
} // namespace onnx
} // namespace frontend
diff --git a/src/frontends/onnx/tests/models/group_normalization_21.prototxt b/src/frontends/onnx/tests/models/group_normalization_21.prototxt
new file mode 100644
index 00000000000..1eaae150888
--- /dev/null
+++ b/src/frontends/onnx/tests/models/group_normalization_21.prototxt
@@ -0,0 +1,96 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "scale"
+ input: "bias"
+ output: "y"
+ op_type: "GroupNormalization"
+ attribute {
+ name: "epsilon"
+ f: 0.0099999997764825821
+ type: FLOAT
+ }
+ attribute {
+ name: "num_groups"
+ i: 2
+ type: INT
+ }
+ }
+ name: "test_group_normalization_epsilon"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "bias"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 21
+}
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index 0384bfb6ca7..d1a35bf0bb2 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -6829,6 +6829,47 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_group_normalization_2grp_custom_eps) {
test_case.run_with_tolerance_as_fp(0.000001f);
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_group_normalization_21) {
+ auto model = convert_model("group_normalization_21.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>(
+ {1.764052391052246f, 0.40015721321105957f, 0.978738009929657f, 2.2408931255340576f,
+ 1.8675580024719238f, -0.9772778749465942f, 0.9500884413719177f, -0.15135720372200012f,
+ -0.10321885347366333f, 0.4105985164642334f, 0.14404356479644775f, 1.4542734622955322f,
+ 0.7610377073287964f, 0.12167501449584961f, 0.44386324286460876f, 0.3336743414402008f,
+ 1.4940791130065918f, -0.2051582634449005f, 0.3130677044391632f, -0.8540957570075989f,
+ -2.5529897212982178f, 0.653618574142456f, 0.8644362092018127f, -0.7421650290489197f,
+ 2.269754648208618f, -1.4543657302856445f, 0.04575851559638977f, -0.18718385696411133f,
+ 1.5327792167663574f, 1.4693588018417358f, 0.154947429895401f, 0.37816253304481506f,
+ -0.8877857327461243f, -1.980796456336975f, -0.34791216254234314f, 0.15634897351264954f,
+ 1.2302906513214111f, 1.202379822731018f, -0.38732680678367615f, -0.302302747964859f,
+ -1.0485529899597168f, -1.420017957687378f, -1.7062702178955078f, 1.950775384902954f,
+ -0.5096521973609924f, -0.4380742907524109f, -1.2527953386306763f, 0.7774903774261475f});
+ test_case.add_input<float>(
+ {-1.6138978004455566f, -0.21274028718471527f, -0.8954665660858154f, 0.38690251111984253f});
+ test_case.add_input<float>({-0.5108051300048828, -1.18063223361969f, -0.02818222902715206f, 0.4283318817615509f});
+
+ test_case.add_expected_output<float>(
+ Shape{3, 4, 2, 2},
+ {
+ -1.8928775787353516f, 0.24930185079574585f, -0.6594365239143372f, -2.641819477081299f,
+ -1.384243369102478f, -0.7952562570571899f, -1.1942929029464722f, -0.9662526845932007f,
+ 1.0357780456542969f, 0.03993310034275055f, 0.5565513372421265f, -1.9828449487686157f,
+ 0.6923606395721436f, 0.15695568919181824f, 0.42675745487213135f, 0.3344848155975342f,
+ -2.7151150703430176f, -0.4068778157234192f, -1.1108338832855225f, 0.4746362566947937f,
+ -0.7465286254882812f, -1.3207058906555176f, -1.3584550619125366f, -1.0707759857177734f,
+ -1.4356461763381958f, 1.570522427558899f, 0.35959845781326294f, 0.5476332902908325f,
+ 0.7794156074523926f, 0.7572963237762451f, 0.29886627197265625f, 0.3767174780368805f,
+ 0.6620793342590332f, 2.4348504543304443f, -0.21355000138282776f, -1.0314189195632935f,
+ -1.4788639545440674f, -1.4728968143463135f, -1.1330219507217407f, -1.1511999368667603f,
+ 0.4269883334636688f, 0.7122754454612732f, 0.9321187734603882f, -1.876512050628662f,
+ 0.4104909896850586f, 0.43424272537231445f, 0.16389334201812744f, 0.837604284286499f,
+ });
+
+ test_case.run_with_tolerance_as_fp(0.000001f);
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_mm_nms_rotated) {
auto model = convert_model("mm_nms_rotated.onnx");
diff --git a/src/frontends/onnx/tests/tests_python/test_backend.py b/src/frontends/onnx/tests/tests_python/test_backend.py
index 1ca9486cbcd..f942f4d1843 100644
--- a/src/frontends/onnx/tests/tests_python/test_backend.py
+++ b/src/frontends/onnx/tests/tests_python/test_backend.py
@@ -581,8 +581,6 @@ tests_expected_to_fail = [
"OnnxBackendNodeModelTest.test_gelu_default_2_expanded_cpu",
"OnnxBackendNodeModelTest.test_reduce_log_sum_exp_empty_set_expanded_cpu",
"OnnxBackendNodeModelTest.test_reduce_max_empty_set_cpu",
- "OnnxBackendNodeModelTest.test_group_normalization_epsilon_cpu",
- "OnnxBackendNodeModelTest.test_group_normalization_example_cpu",
"OnnxBackendNodeModelTest.test_qlinearmatmul_3D_int8_float16_cpu",
"OnnxBackendNodeModelTest.test_qlinearmatmul_3D_int8_float32_cpu",
"OnnxBackendNodeModelTest.test_qlinearmatmul_3D_uint8_float16_cpu",