Skip to content

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

[ONNX FE] Expose input_skip_bias_sum output in SkipLayerNormalization (#37072)

Maxim Vafin88e3c81cb64f
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/skip_layer_normalization.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/skip_layer_normalization.cpp
index c2c06f053eb..5f6a57868f9 100644
--- a/src/frontends/onnx/frontend/src/op/com.microsoft/skip_layer_normalization.cpp
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/skip_layer_normalization.cpp
@@ -2,12 +2,17 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include "core/null_node.hpp"
#include "core/operator_set.hpp"
#include "openvino/frontend/exception.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/constant.hpp"
+#include "openvino/op/divide.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/mvn.hpp"
+#include "openvino/op/reduce_mean.hpp"
+#include "openvino/op/sqrt.hpp"
+#include "openvino/op/subtract.hpp"
using namespace ov::op;
using ov::Shape;
@@ -40,10 +45,51 @@ ov::OutputVector skip_layer_normalization(const ov::frontend::onnx::Node& node)
if (num_nodes > 3) {
result = std::make_shared<v1::Add>(result, nodes[3]);
}
- // spec mentions three outputs (output, mean, inv_std_var) while we support only first one, but:
- // - onnxruntime also doesn't support the last two
- // - we'd have to unroll MVN to have them
- return result->outputs();
+
+ ov::OutputVector results{result->output(0)};
+ const auto num_outputs = node.get_outputs_size();
+ if (num_outputs == 1) {
+ return results;
+ }
+
+ // Spec defines up to 4 outputs: output, mean, inv_std_var, input_skip_bias_sum.
+ // MVN doesn't expose mean/inv_std_var, so they are recomputed via the reference decomposition, but only
+ // when actually consumed downstream (input_skip_bias_sum is commonly used to chain consecutive
+ // SkipLayerNormalization nodes and is already available as `input`).
+ const auto& output_names = node.get_output_names();
+ const auto wanted = [&](size_t i) {
+ return num_outputs > i && output_names.size() > i && !output_names[i].get().empty();
+ };
+ const auto null_output = []() {
+ return std::make_shared<NullNode>()->output(0);
+ };
+
+ constexpr auto keep_dims = true;
+ std::shared_ptr<ov::Node> mean;
+ if (wanted(1) || wanted(2)) {
+ mean = std::make_shared<v1::ReduceMean>(input, reduction_axes, keep_dims);
+ }
+ if (num_outputs >= 2) {
+ results.push_back(wanted(1) ? mean->output(0) : null_output());
+ }
+ if (num_outputs >= 3) {
+ if (wanted(2)) {
+ const auto eps_const = v0::Constant::create(input->get_element_type(), {}, {eps});
+ const auto one_const = v0::Constant::create(input->get_element_type(), {}, {1});
+ auto deviation = std::make_shared<v1::Subtract>(input, mean);
+ auto variance = std::make_shared<v1::ReduceMean>(std::make_shared<v1::Multiply>(deviation, deviation),
+ reduction_axes,
+ keep_dims);
+ auto std_dev = std::make_shared<v0::Sqrt>(std::make_shared<v1::Add>(variance, eps_const));
+ results.push_back(std::make_shared<v1::Divide>(one_const, std_dev)->output(0));
+ } else {
+ results.push_back(null_output());
+ }
+ }
+ if (num_outputs >= 4) {
+ results.push_back(wanted(3) ? input->output(0) : null_output());
+ }
+ return results;
}
ONNX_OP("SkipLayerNormalization", OPSET_SINCE(1), com_microsoft::opset_1::skip_layer_normalization, MICROSOFT_DOMAIN);
} // namespace opset_1
diff --git a/src/frontends/onnx/tests/models/com.microsoft/skip_layer_normalization_with_input_skip_bias_sum.prototxt b/src/frontends/onnx/tests/models/com.microsoft/skip_layer_normalization_with_input_skip_bias_sum.prototxt
new file mode 100644
index 00000000000..705e4793840
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/skip_layer_normalization_with_input_skip_bias_sum.prototxt
@@ -0,0 +1,172 @@
+ir_version: 6
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "input"
+ input: "skip"
+ input: "gamma"
+ input: "beta"
+ input: "bias"
+ output: "out"
+ output: "mean"
+ output: "inv_std_var"
+ output: "input_skip_bias_sum"
+ name: "SkipLayerNorm"
+ op_type: "SkipLayerNormalization"
+ attribute {
+ name: "epsilon"
+ f: 9.999999960041972e-13
+ type: FLOAT
+ }
+ domain: "com.microsoft"
+ }
+ name: "torch-jit-export"
+ initializer {
+ dims: 4
+ data_type: 1
+ name: "gamma"
+ float_data: 0.1
+ float_data: 0.2
+ float_data: 0.3
+ float_data: 0.4
+ }
+ initializer {
+ dims: 4
+ data_type: 1
+ name: "beta"
+ float_data: -0.07
+ float_data: -0.4
+ float_data: 0.22
+ float_data: 0
+ }
+ initializer {
+ dims: 4
+ data_type: 1
+ name: "bias"
+ float_data: -0.07
+ float_data: -0.4
+ float_data: 0.22
+ float_data: 0
+ }
+ input {
+ name: "input"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "skip"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "inv_std_var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "input_skip_bias_sum"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 11
+}
+opset_import {
+ domain: "com.microsoft"
+ 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 991adc540a7..5a0d3624acb 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -142,6 +142,59 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_skip_layer_normalization_with_gamma_be
test_case.run_with_tolerance_as_fp();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_skip_layer_normalization_with_input_skip_bias_sum) {
+ const auto model = convert_model("com.microsoft/skip_layer_normalization_with_input_skip_bias_sum.onnx");
+
+ std::vector<float> input = {
+ 0.54881352f, 0.71518934f, 0.60276335f, 0.54488319f, 0.42365479f, 0.64589411f, 0.43758720f, 0.89177299f,
+ 0.96366274f, 0.38344151f, 0.79172504f, 0.52889490f, 0.56804454f, 0.92559665f, 0.07103606f, 0.08712930f,
+ 0.02021840f, 0.83261985f, 0.77815676f, 0.87001216f, 0.97861832f, 0.79915857f, 0.46147937f, 0.78052920f,
+ };
+ std::vector<float> skip = {
+ 0.11827443f, 0.63992101f, 0.14335328f, 0.94466889f, 0.52184832f, 0.41466194f, 0.26455560f, 0.77423370f,
+ 0.45615032f, 0.56843394f, 0.01878980f, 0.61763549f, 0.61209571f, 0.61693400f, 0.94374806f, 0.68182027f,
+ 0.35950789f, 0.43703195f, 0.69763118f, 0.06022547f, 0.66676670f, 0.67063785f, 0.21038257f, 0.12892629f,
+ };
+ std::vector<float> expected_out = {
+ -0.19721794f, -0.42944565f, 0.18620640f, 0.61282152f, -0.11097327f, -0.59518522f, 0.13393641f, 0.66901535f,
+ 0.04256713f, -0.71902490f, 0.23107991f, 0.17300847f, -0.04390603f, -0.31109563f, 0.51021838f, -0.66914201f,
+ -0.20009395f, -0.43313017f, 0.67281967f, -0.01712347f, 0.09767530f, -0.43024653f, -0.01836969f, -0.29238200f,
+ };
+ // mean = ReduceMean(input + skip + bias, axis=-1, keepdims=1)
+ std::vector<float> expected_mean = {
+ 1.00196671f,
+ 1.03105211f,
+ 1.01968336f,
+ 1.06410110f,
+ 0.95135093f,
+ 1.11162472f,
+ };
+ // inv_std_var = 1 / sqrt(ReduceMean((sum - mean)^2, axis=-1, keepdims=1) + eps)
+ std::vector<float> expected_inv_std_var = {
+ 3.14212680f,
+ 2.63410830f,
+ 3.40978694f,
+ 5.66779423f,
+ 2.02757072f,
+ 3.61555910f,
+ };
+ // input_skip_bias_sum = input + skip + bias, feeds the `skip` input of a subsequent
+ // SkipLayerNormalization node when several layers are chained together
+ std::vector<float> expected_input_skip_bias_sum = {
+ 0.59708798f, 0.95511043f, 0.96611667f, 1.48955202f, 0.87550312f, 0.66055608f, 0.92214286f, 1.66600668f,
+ 1.34981298f, 0.55187547f, 1.03051484f, 1.14653039f, 1.11014020f, 1.14253068f, 1.23478413f, 0.76894957f,
+ 0.30972630f, 0.86965179f, 1.69578791f, 0.93023765f, 1.57538497f, 1.06979644f, 0.89186192f, 0.90945548f,
+ };
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>(input);
+ test_case.add_input<float>(skip);
+ test_case.add_expected_output<float>(expected_out);
+ test_case.add_expected_output<float>(expected_mean);
+ test_case.add_expected_output<float>(expected_inv_std_var);
+ test_case.add_expected_output<float>(expected_input_skip_bias_sum);
+ test_case.run_with_tolerance_as_fp();
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_skip_layer_normalization_with_gamma_beta) {
const auto model = convert_model("com.microsoft/skip_layer_normalization_with_gamma_beta.onnx");