Skip to content

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

[ONNX Frontend] Reshape Scale & Bias inputs of Normalization layer if shapes are mismatched but compatible (#32630)

Andrii Staikov1d7c154f6863
diff --git a/src/frontends/onnx/frontend/src/op/layer_normalization.cpp b/src/frontends/onnx/frontend/src/op/layer_normalization.cpp
index b4598ca8ba3..6e01cd8e0cc 100644
--- a/src/frontends/onnx/frontend/src/op/layer_normalization.cpp
+++ b/src/frontends/onnx/frontend/src/op/layer_normalization.cpp
@@ -4,19 +4,24 @@
#include "core/operator_set.hpp"
#include "exceptions.hpp"
+#include "openvino/core/validation_util.hpp"
#include "openvino/op/add.hpp"
+#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/convert_like.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/mvn.hpp"
#include "openvino/op/range.hpp"
+#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
+#include "openvino/op/slice.hpp"
#include "openvino/op/squeeze.hpp"
#include "utils/common.hpp"
using namespace ov::op;
using namespace ov::op::v0;
using namespace ov::op::v1;
+using namespace ov::util;
using ::ONNX_NAMESPACE::TensorProto_DataType;
using ov::Shape;
@@ -76,8 +81,32 @@ ov::OutputVector layer_normalization(const ov::frontend::onnx::Node& node) {
if (needs_type_casting)
normalized = std::make_shared<ConvertLike>(normalized, inputs.at(0));
- auto scaled = std::make_shared<Multiply>(normalized, inputs.at(1));
- auto biased = (num_inputs == 3 ? std::make_shared<Add>(scaled, inputs.at(2))->output(0) : scaled->output(0));
+ ov::Output<ov::Node> normalized_shape = std::make_shared<v0::ShapeOf>(normalized);
+ ov::Output<ov::Node> sub_shape = std::make_shared<v8::Slice>(normalized_shape,
+ Constant::create(element::i64, {1}, {axis}),
+ Constant::create(element::i64, {1}, {INT_MAX}),
+ Constant::create(element::i64, {1}, {1}));
+ auto normalized_rank = normalized.get_partial_shape().rank();
+
+ auto scale = inputs.at(1);
+ auto scale_rank = scale.get_partial_shape().rank();
+ if ((scale_rank.is_dynamic() && normalized_rank.is_dynamic()) ||
+ ((scale_rank.is_static() && normalized_rank.is_static()) &&
+ scale_rank.get_length() + normalize_axis(axis, normalized_rank.get_length()) !=
+ static_cast<size_t>(normalized_rank.get_length()))) {
+ scale = std::make_shared<v1::Reshape>(scale, sub_shape, false);
+ }
+ auto scaled = std::make_shared<Multiply>(normalized, scale);
+
+ auto bias = inputs.at(2);
+ auto bias_rank = bias.get_partial_shape().rank();
+ if ((bias_rank.is_dynamic() && normalized_rank.is_dynamic()) ||
+ ((bias_rank.is_static() && normalized_rank.is_static()) &&
+ bias_rank.get_length() + normalize_axis(axis, normalized_rank.get_length()) !=
+ static_cast<size_t>(normalized_rank.get_length()))) {
+ bias = std::make_shared<v1::Reshape>(bias, sub_shape, false);
+ }
+ auto biased = (num_inputs == 3 ? std::make_shared<Add>(scaled, bias)->output(0) : scaled->output(0));
return ov::OutputVector{biased};
}
diff --git a/src/frontends/onnx/tests/models/com.microsoft/normalization_with_different_scale_and_bias_shape.prototxt b/src/frontends/onnx/tests/models/com.microsoft/normalization_with_different_scale_and_bias_shape.prototxt
new file mode 100644
index 00000000000..cc56da8133a
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/normalization_with_different_scale_and_bias_shape.prototxt
@@ -0,0 +1,167 @@
+ir_version: 8
+producer_name: "OpenVINO ONNX Frontend Test"
+graph {
+ node {
+ input: "input"
+ output: "avgpool_output"
+ op_type: "AveragePool"
+ attribute {
+ name: "ceil_mode"
+ i: 0
+ type: INT
+ }
+ attribute {
+ name: "count_include_pad"
+ i: 0
+ type: INT
+ }
+ attribute {
+ name: "kernel_shape"
+ ints: 2
+ ints: 2
+ type: INTS
+ }
+ attribute {
+ name: "strides"
+ ints: 2
+ ints: 2
+ type: INTS
+ }
+ }
+ node {
+ input: "avgpool_output"
+ output: "space2depth_output"
+ op_type: "SpaceToDepth"
+ attribute {
+ name: "blocksize"
+ i: 2
+ type: INT
+ }
+ }
+ node {
+ input: "space2depth_output"
+ input: "scale"
+ input: "B"
+ output: "layernorm_output"
+ op_type: "LayerNormalization"
+ attribute {
+ name: "axis"
+ i: 1
+ type: INT
+ }
+ attribute {
+ name: "epsilon"
+ f: 9.9999997473787516e-06
+ type: FLOAT
+ }
+ attribute {
+ name: "stash_type"
+ i: 1
+ type: INT
+ }
+ }
+ node {
+ input: "layernorm_output"
+ input: "indices"
+ output: "gather_output"
+ op_type: "Gather"
+ attribute {
+ name: "axis"
+ i: 1
+ type: INT
+ }
+ }
+ node {
+ input: "gather_output"
+ output: "final_output"
+ op_type: "AveragePool"
+ attribute {
+ name: "ceil_mode"
+ i: 0
+ type: INT
+ }
+ attribute {
+ name: "count_include_pad"
+ i: 0
+ type: INT
+ }
+ attribute {
+ name: "kernel_shape"
+ ints: 2
+ ints: 2
+ type: INTS
+ }
+ attribute {
+ name: "strides"
+ ints: 2
+ ints: 2
+ type: INTS
+ }
+ }
+ name: "ComplexModel"
+ initializer {
+ dims: 48
+ data_type: 1
+ name: "scale"
+ raw_data: "\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?"
+ }
+ initializer {
+ dims: 48
+ data_type: 1
+ name: "B"
+ raw_data: "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
+ }
+ initializer {
+ dims: 4
+ data_type: 7
+ name: "indices"
+ raw_data: "\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\002\000\000\000\000\000\000\000\003\000\000\000\000\000\000\000"
+ }
+ input {
+ name: "input"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 8
+ }
+ dim {
+ dim_value: 8
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "final_output"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 14
+}
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 e97e607afa2..7fbec64c0f2 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -52,6 +52,71 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_bias_gelu) {
test_case.run_with_tolerance_as_fp(1e-6f);
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_normalization_with_different_scale_and_bias_shape) {
+ const auto model = convert_model("com.microsoft/normalization_with_different_scale_and_bias_shape.onnx");
+
+ std::vector<float> input = {
+ -0.6251510977745056f, 0.007134991232305765f, -1.3456748723983765f, 1.770784616470337f,
+ 1.5288246870040894f, -0.4477598965167999f, 1.0393754243850708f, 1.2390025854110718f,
+ -1.525909662246704f, -1.9435287714004517f, -0.22405049204826355f, -0.575035035610199f,
+ -0.8939334750175476f, 1.128310203552246f, 0.27096450328826904f, 0.17594467103481293f,
+ 0.5188482999801636f, 0.5845427513122559f, 0.026239866390824318f, 0.7220980525016785f,
+ 0.11664538085460663f, -0.7396676540374756f, 2.742187738418579f, 0.045687586069107056f,
+ -0.9467469453811646f, -1.1450406312942505f, -0.0008551082573831081f, -0.27351295948028564f,
+ -0.045971859246492386f, -0.9819456338882446f, -1.3347723484039307f, 0.8798799514770508f,
+ 0.01310710422694683f, -0.4074785113334656f, 1.3393157720565796f, 0.2833789885044098f,
+ 0.7235508561134338f, -0.1269005686044693f, 1.3178980350494385f, -0.30620941519737244f,
+ 0.6693164706230164f, 0.4393235146999359f, -1.025836706161499f, 0.9186087846755981f,
+ 0.7535707354545593f, -0.6770780086517334f, -0.176866352558136f, 0.9682378172874451f,
+ 0.7283672094345093f, -1.3557320833206177f, 0.4494202733039856f, 0.1719498336315155f,
+ 1.5455526113510132f, 0.5787205696105957f, -1.5483653545379639f, 0.8506511449813843f,
+ -1.0740782022476196f, 0.9321322441101074f, -1.7071808576583862f, -1.4863122701644897f,
+ 1.078175663948059f, 0.7962296009063721f, -0.6971403956413269f, -0.2511046826839447f,
+ -0.49446341395378113f, 0.14767688512802124f, -0.3556131422519684f, -0.8179923295974731f,
+ -0.14287325739860535f, 0.466299831867218f, -0.28673285245895386f, -0.2690933644771576f,
+ -0.7565494179725647f, 0.6324114799499512f, 0.7540034651756287f, 0.5514761209487915f,
+ -0.9617162346839905f, 1.8611818552017212f, 0.15651093423366547f, -0.2886978089809418f,
+ 0.08920352905988693f, -0.7431173920631409f, 0.791591227054596f, 0.7870765924453735f,
+ 0.6610082983970642f, 0.3587363064289093f, 0.813174307346344f, 0.10545399785041809f,
+ 0.8162460923194885f, 0.7449513077735901f, -0.21549087762832642f, 0.4003535509109497f,
+ -0.9745061993598938f, -0.20204021036624908f, 0.3731934726238251f, 1.0289499759674072f,
+ 2.0338852405548096f, 0.3192354738712311f, -0.25873664021492004f, -0.2961551249027252f,
+ -0.33034631609916687f, -0.18024343252182007f, -0.12908896803855896f, 0.2964698076248169f,
+ -1.1107416152954102f, 0.4810034930706024f, -0.32520192861557007f, -0.05360089987516403f,
+ -0.5922203063964844f, 0.11996021866798401f, 0.008174659684300423f, -0.2785191833972931f,
+ -0.36002877354621887f, 1.2043665647506714f, 1.2149795293807983f, 1.164088487625122f,
+ 0.37097078561782837f, 1.437663197517395f, 1.7179683446884155f, 0.38527265191078186f,
+ -0.20332343876361847f, 0.3521358072757721f, -0.3977925777435303f, -1.1281023025512695f,
+ -0.5123989582061768f, 0.9586845636367798f, 0.1254643350839615f, 1.10258948802948f,
+ -0.4802491366863251f, 1.2417951822280884f, 1.077864170074463f, 1.1157876253128052f,
+ -0.07131173461675644f, -0.07585251331329346f, -2.117938280105591f, -1.687069058418274f,
+ 2.07312273979187f, 1.0686067342758179f, 1.3857795000076294f, -0.6013308167457581f,
+ -0.2426421195268631f, 1.9826184511184692f, -1.3718016147613525f, 1.5097757577896118f,
+ -0.9077845811843872f, 0.3679441809654236f, 1.4302239418029785f, 0.8393189311027527f,
+ 0.1696036458015442f, 1.5365674495697021f, -1.8343969583511353f, 1.3744423389434814f,
+ -0.6777172088623047f, 1.2976163625717163f, -0.15583312511444092f, -0.6545937061309814f,
+ 0.1523059755563736f, 0.18897348642349243f, -0.008208949118852615f, -0.015685556456446648f,
+ 0.4733433425426483f, 2.0492160320281982f, 0.8597576022148132f, 0.17651300132274628f,
+ 0.7300487160682678f, 0.4610871374607086f, 1.3461068868637085f, 0.7895026803016663f,
+ 0.15649409592151642f, -0.20071466267108917f, 0.582892119884491f, -0.6431581974029541f,
+ 0.318866103887558f, -2.343296766281128f, -1.883471131324768f, -0.4693567156791687f,
+ -1.0765290260314941f, 0.5347458124160767f, -0.16472244262695312f, 0.4766247868537903f,
+ 2.4349000453948975f, -0.7073322534561157f, -1.999008059501648f, 0.2707614600658417f,
+ 0.32679104804992676f, 0.8631436824798584f, 0.3018684387207031f, 2.1454594135284424f,
+ 0.7967919111251831f, -0.5919809937477112f, -1.9091647863388062f, -0.825406551361084f,
+ };
+ std::vector<float> expected = {
+ -0.47632578f,
+ -0.09772638f,
+ 0.64558005f,
+ 0.4519717f,
+ };
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>(input);
+ test_case.add_expected_output<float>(expected);
+ test_case.run_with_tolerance_as_fp();
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_skip_layer_normalization_with_gamma_beta_bias) {
const auto model = convert_model("com.microsoft/skip_layer_normalization_with_gamma_beta_bias.onnx");