Skip to content

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

[ONNX FE] Support training mode of BatchNormalization (#37170)

Maxim Vafin92a2642cd890
diff --git a/src/frontends/onnx/frontend/src/op/batch_norm.cpp b/src/frontends/onnx/frontend/src/op/batch_norm.cpp
index 413799fb89b..9e1b6c7d659 100644
--- a/src/frontends/onnx/frontend/src/op/batch_norm.cpp
+++ b/src/frontends/onnx/frontend/src/op/batch_norm.cpp
@@ -10,12 +10,103 @@
#include "core/null_node.hpp"
#include "core/operator_set.hpp"
#include "exceptions.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/range.hpp"
+#include "openvino/op/reduce_mean.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/shape_of.hpp"
+#include "openvino/op/squeeze.hpp"
+#include "openvino/op/subtract.hpp"
using namespace ov::op;
namespace ov {
namespace frontend {
namespace onnx {
namespace ai_onnx {
+namespace {
+// Axes of all dimensions except the channel one (index 1), used to calculate per-channel statistics.
+ov::Output<ov::Node> statistics_axes(const ov::Output<ov::Node>& x) {
+ const auto& rank = x.get_partial_shape().rank();
+ if (rank.is_static()) {
+ std::vector<int64_t> axes{0};
+ for (int64_t axis = 2; axis < rank.get_length(); ++axis) {
+ axes.push_back(axis);
+ }
+ return v0::Constant::create(ov::element::i64, ov::Shape{axes.size()}, axes);
+ }
+
+ const auto shape_of_x = std::make_shared<v3::ShapeOf>(x, ov::element::i64);
+ const auto rank_of_x = std::make_shared<v0::Squeeze>(std::make_shared<v3::ShapeOf>(shape_of_x, ov::element::i64));
+ const auto start = v0::Constant::create(ov::element::i64, ov::Shape{}, {2});
+ const auto step = v0::Constant::create(ov::element::i64, ov::Shape{}, {1});
+ const auto spatial_axes = std::make_shared<v4::Range>(start, rank_of_x, step, ov::element::i64);
+ const auto batch_axis = v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});
+ return std::make_shared<v0::Concat>(ov::OutputVector{batch_axis, spatial_axes}, 0);
+}
+
+struct TrainingBatchNorm {
+ ov::Output<ov::Node> y;
+ ov::Output<ov::Node> running_mean;
+ ov::Output<ov::Node> running_var;
+ ov::Output<ov::Node> current_mean;
+ ov::Output<ov::Node> current_var;
+};
+
+// Normalizes the input with statistics calculated for the current batch and updates the running statistics:
+// running_mean = input_mean * momentum + current_mean * (1 - momentum)
+// running_var = input_var * momentum + current_var * (1 - momentum)
+TrainingBatchNorm make_training_batch_norm(const ov::Output<ov::Node>& x,
+ const ov::Output<ov::Node>& scale,
+ const ov::Output<ov::Node>& bias,
+ const ov::Output<ov::Node>& mean,
+ const ov::Output<ov::Node>& var,
+ double epsilon,
+ double momentum) {
+ // ONNX requires the batch statistics to be calculated in float to avoid overflow for float16 inputs
+ const auto& x_type = x.get_element_type();
+ const bool accumulate_in_f32 = x_type.is_real() && x_type.bitwidth() < ov::element::f32.bitwidth();
+ const ov::Output<ov::Node> data =
+ accumulate_in_f32 ? std::make_shared<v0::Convert>(x, ov::element::f32)->output(0) : x;
+
+ const auto axes = statistics_axes(data);
+ const auto mean_keep_dims = std::make_shared<v1::ReduceMean>(data, axes, true);
+ const auto deviation = std::make_shared<v1::Subtract>(data, mean_keep_dims);
+ const auto var_keep_dims =
+ std::make_shared<v1::ReduceMean>(std::make_shared<v1::Multiply>(deviation, deviation), axes, true);
+
+ const auto channels_shape = v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1});
+ const ov::Output<ov::Node> mean_1d = std::make_shared<v1::Reshape>(mean_keep_dims, channels_shape, false);
+ const ov::Output<ov::Node> var_1d = std::make_shared<v1::Reshape>(var_keep_dims, channels_shape, false);
+
+ // BatchNormInference requires all its inputs to have the same element type
+ const ov::Output<ov::Node> current_mean =
+ accumulate_in_f32 ? std::make_shared<v1::ConvertLike>(mean_1d, x)->output(0) : mean_1d;
+ const ov::Output<ov::Node> current_var =
+ accumulate_in_f32 ? std::make_shared<v1::ConvertLike>(var_1d, x)->output(0) : var_1d;
+
+ const auto y = std::make_shared<v5::BatchNormInference>(x, scale, bias, current_mean, current_var, epsilon);
+
+ const auto update_running_stat = [&](const ov::Output<ov::Node>& running,
+ const ov::Output<ov::Node>& current) -> std::shared_ptr<ov::Node> {
+ const auto momentum_const = v0::Constant::create(running.get_element_type(), ov::Shape{}, {momentum});
+ const auto rest_const = v0::Constant::create(running.get_element_type(), ov::Shape{}, {1.0 - momentum});
+ return std::make_shared<v1::Add>(
+ std::make_shared<v1::Multiply>(running, momentum_const),
+ std::make_shared<v1::Multiply>(std::make_shared<v1::ConvertLike>(current, running), rest_const));
+ };
+ // the running statistics are updated with the values calculated in the accumulation precision
+ const auto running_mean = update_running_stat(mean, mean_1d);
+ const auto running_var = update_running_stat(var, var_1d);
+
+ return {y, running_mean, running_var, current_mean, current_var};
+}
+} // namespace
+
namespace opset_1 {
// This version supports ONNX BatchNormalization-1 and BatchNormalization-6
ov::OutputVector batch_norm(const ov::frontend::onnx::Node& node) {
@@ -23,39 +114,37 @@ ov::OutputVector batch_norm(const ov::frontend::onnx::Node& node) {
auto x = inputs.at(0);
auto scale = inputs.at(1);
auto bias = inputs.at(2);
- ov::Output<ov::Node> mean;
- ov::Output<ov::Node> var;
- double epsilon{node.get_attribute_value<double>("epsilon", 1e-5)};
+ CHECK_VALID_NODE(node,
+ inputs.size() >= 5,
+ "Cannot create OpenVINO batch norm with unsupported number of inputs: ",
+ inputs.size());
+ auto mean = inputs.at(3);
+ auto var = inputs.at(4);
- // Currently only BatchNormalization inference mode is supported by OpenVINO
- std::int64_t is_test{node.get_attribute_value<std::int64_t>("is_test", 1)};
- CHECK_VALID_NODE(node, is_test, "only 'is_test' mode is supported.");
+ double epsilon{node.get_attribute_value<double>("epsilon", 1e-5)};
+ double momentum{node.get_attribute_value<double>("momentum", 0.9)};
- // optional outputs
- auto after_bn_mean = std::make_shared<NullNode>();
- auto after_bn_var = std::make_shared<NullNode>();
- auto saved_mean = std::make_shared<NullNode>();
- auto saved_var = std::make_shared<NullNode>();
+ // 'is_test' equal to 0 means that the operator works in the training mode
+ const bool is_test = node.get_attribute_value<std::int64_t>("is_test", 1) != 0;
- if (inputs.size() >= 5) {
- mean = inputs.at(3);
- var = inputs.at(4);
+ if (is_test) {
return {std::make_shared<v5::BatchNormInference>(x, scale, bias, mean, var, epsilon),
- after_bn_mean,
- after_bn_var,
- saved_mean,
- saved_var};
+ std::make_shared<NullNode>(),
+ std::make_shared<NullNode>(),
+ std::make_shared<NullNode>(),
+ std::make_shared<NullNode>()};
}
- OPENVINO_THROW("Cannot create OpenVINO batch norm with unsupported number of inputs");
+ const auto bn = make_training_batch_norm(x, scale, bias, mean, var, epsilon, momentum);
+ return {bn.y, bn.running_mean, bn.running_var, bn.current_mean, bn.current_var};
}
ONNX_OP("BatchNormalization", OPSET_RANGE(1, 6), ai_onnx::opset_1::batch_norm);
} // namespace opset_1
/*
Opset 6 is skipped because there are no significant difference between opset1 and opset6.
Found difference is:
- 1. In Training, the computation of ReduceMean and ReduceVar uses float
+ 1. In Training, the mean and variance reductions use float
to avoid overflow for float16 inputs.
*/
@@ -66,16 +155,25 @@ ov::OutputVector batch_norm(const ov::frontend::onnx::Node& node) {
auto x = inputs.at(0);
auto scale = inputs.at(1);
auto bias = inputs.at(2);
+
+ CHECK_VALID_NODE(node,
+ inputs.size() >= 5,
+ "Cannot create OpenVINO batch norm with unsupported number of inputs: ",
+ inputs.size());
auto mean = inputs.at(3);
auto var = inputs.at(4);
double epsilon{node.get_attribute_value<double>("epsilon", 1e-5)};
- // Attribute "spatial" is ignored, as we only support inference mode of
- // BatchNormalization
+ double momentum{node.get_attribute_value<double>("momentum", 0.9)};
+ // Attribute "spatial" is ignored, as only the per-channel normalization is supported
- CHECK_VALID_NODE(node, node.get_outputs_size() == 1, "Training mode of BatchNormalization is not supported.");
+ // More than one output means that the operator works in the training mode
+ if (node.get_outputs_size() == 1) {
+ return {std::make_shared<v5::BatchNormInference>(x, scale, bias, mean, var, epsilon)};
+ }
- return {std::make_shared<v5::BatchNormInference>(x, scale, bias, mean, var, epsilon)};
+ const auto bn = make_training_batch_norm(x, scale, bias, mean, var, epsilon, momentum);
+ return {bn.y, bn.running_mean, bn.running_var, bn.current_mean, bn.current_var};
}
ONNX_OP("BatchNormalization", OPSET_RANGE(7, 13), ai_onnx::opset_7::batch_norm);
} // namespace opset_7
@@ -95,23 +193,38 @@ ov::OutputVector batch_norm(const ov::frontend::onnx::Node& node) {
auto x = inputs.at(0);
auto scale = inputs.at(1);
auto bias = inputs.at(2);
+
+ CHECK_VALID_NODE(node,
+ inputs.size() >= 5,
+ "Cannot create OpenVINO batch norm with unsupported number of inputs: ",
+ inputs.size());
auto mean = inputs.at(3);
auto var = inputs.at(4);
double epsilon{node.get_attribute_value<double>("epsilon", 1e-5)};
- int64_t training_mode{node.get_attribute_value<int64_t>("training_mode", 0)};
+ double momentum{node.get_attribute_value<double>("momentum", 0.9)};
+ const bool training_mode = node.get_attribute_value<int64_t>("training_mode", 0) != 0;
CHECK_VALID_NODE(node,
- training_mode == false && node.get_outputs_size() == 1,
- "Training mode of BatchNormalization is not supported.");
- return {std::make_shared<v5::BatchNormInference>(x, scale, bias, mean, var, epsilon)};
+ training_mode || node.get_outputs_size() == 1,
+ "Number of outputs greater than one requires the training mode to be enabled.");
+
+ if (!training_mode) {
+ return {std::make_shared<v5::BatchNormInference>(x, scale, bias, mean, var, epsilon)};
+ }
+
+ const auto bn = make_training_batch_norm(x, scale, bias, mean, var, epsilon, momentum);
+ if (node.get_outputs_size() == 1) {
+ return {bn.y};
+ }
+ return {bn.y, bn.running_mean, bn.running_var};
}
ONNX_OP("BatchNormalization", OPSET_SINCE(14), ai_onnx::opset_14::batch_norm);
} // namespace opset_14
/*
Opset 15 is skipped because there are no significant difference between opset14 and opset15.
Found difference is:
- 1. In Training, the computation of ReduceMean and ReduceVar uses float
+ 1. In Training, the mean and variance reductions use float
to avoid overflow for float16 inputs.
*/
diff --git a/src/frontends/onnx/tests/__init__.py b/src/frontends/onnx/tests/__init__.py
index 51073be0782..19df22f32b3 100644
--- a/src/frontends/onnx/tests/__init__.py
+++ b/src/frontends/onnx/tests/__init__.py
@@ -115,7 +115,6 @@ xfail_issue_58676 = xfail_test(reason="AssertionError: Not equal to tolerance rt
skip_issue_58676 = pytest.mark.skip(reason="AssertionError: Not equal to tolerance rtol=0.001, atol=1e-07")
xfail_issue_onnx_models_140 = xfail_test(reason="https://github.com/onnx/models/issues/140")
-xfail_issue_63033 = xfail_test(reason="BatchNormalization: Training mode is not supported")
xfail_issue_63043 = xfail_test(reason="Recurrent node expects constants as W, R, B inputs.")
skip_rng_tests = pytest.mark.skip(reason="Tests use random number generator with no seed.")
diff --git a/src/frontends/onnx/tests/models/batchnorm_opset7_invalid_inputs_number.prototxt b/src/frontends/onnx/tests/models/batchnorm_opset7_invalid_inputs_number.prototxt
new file mode 100644
index 00000000000..469bfd1f10f
--- /dev/null
+++ b/src/frontends/onnx/tests/models/batchnorm_opset7_invalid_inputs_number.prototxt
@@ -0,0 +1,85 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "s"
+ input: "bias"
+ output: "y"
+ op_type: "BatchNormalization"
+ }
+ name: "test_batchnorm_invalid_inputs_number"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "s"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "bias"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 7
+}
diff --git a/src/frontends/onnx/tests/models/batchnorm_training_mode_f16.prototxt b/src/frontends/onnx/tests/models/batchnorm_training_mode_f16.prototxt
new file mode 100644
index 00000000000..75edcc532ba
--- /dev/null
+++ b/src/frontends/onnx/tests/models/batchnorm_training_mode_f16.prototxt
@@ -0,0 +1,147 @@
+ir_version: 8
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "s"
+ input: "bias"
+ input: "mean"
+ input: "var"
+ output: "y"
+ output: "out_mean"
+ output: "out_var"
+ op_type: "BatchNormalization"
+ attribute {
+ name: "training_mode"
+ i: 1
+ type: INT
+ }
+ }
+ name: "test_batchnorm_training_mode_f16"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 10
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "s"
+ type {
+ tensor_type {
+ elem_type: 10
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "bias"
+ type {
+ tensor_type {
+ elem_type: 10
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 10
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out_mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out_var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 15
+}
diff --git a/src/frontends/onnx/tests/models/batchnorm_training_mode_opset1.prototxt b/src/frontends/onnx/tests/models/batchnorm_training_mode_opset1.prototxt
new file mode 100644
index 00000000000..8c034011f90
--- /dev/null
+++ b/src/frontends/onnx/tests/models/batchnorm_training_mode_opset1.prototxt
@@ -0,0 +1,184 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "s"
+ input: "bias"
+ input: "mean"
+ input: "var"
+ output: "y"
+ output: "out_mean"
+ output: "out_var"
+ output: "saved_mean"
+ output: "saved_var"
+ op_type: "BatchNormalization"
+ attribute {
+ name: "consumed_inputs"
+ ints: 0
+ ints: 0
+ ints: 0
+ ints: 1
+ ints: 1
+ type: INTS
+ }
+ attribute {
+ name: "is_test"
+ i: 0
+ type: INT
+ }
+ }
+ name: "test_batchnorm_training_mode"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "s"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "bias"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out_mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out_var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "saved_mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "saved_var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 1
+}
diff --git a/src/frontends/onnx/tests/models/batchnorm_training_mode_opset14.prototxt b/src/frontends/onnx/tests/models/batchnorm_training_mode_opset14.prototxt
new file mode 100644
index 00000000000..05d0f44bc83
--- /dev/null
+++ b/src/frontends/onnx/tests/models/batchnorm_training_mode_opset14.prototxt
@@ -0,0 +1,147 @@
+ir_version: 7
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "s"
+ input: "bias"
+ input: "mean"
+ input: "var"
+ output: "y"
+ output: "out_mean"
+ output: "out_var"
+ op_type: "BatchNormalization"
+ attribute {
+ name: "training_mode"
+ i: 1
+ type: INT
+ }
+ }
+ name: "test_batchnorm_training_mode"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "s"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "bias"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out_mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out_var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 14
+}
diff --git a/src/frontends/onnx/tests/models/batchnorm_training_mode_opset14_single_output.prototxt b/src/frontends/onnx/tests/models/batchnorm_training_mode_opset14_single_output.prototxt
new file mode 100644
index 00000000000..0274a90accf
--- /dev/null
+++ b/src/frontends/onnx/tests/models/batchnorm_training_mode_opset14_single_output.prototxt
@@ -0,0 +1,119 @@
+ir_version: 7
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "s"
+ input: "bias"
+ input: "mean"
+ input: "var"
+ output: "y"
+ op_type: "BatchNormalization"
+ attribute {
+ name: "training_mode"
+ i: 1
+ type: INT
+ }
+ }
+ name: "test_batchnorm_training_mode_single_output"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "s"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "bias"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 14
+}
diff --git a/src/frontends/onnx/tests/models/batchnorm_training_mode_opset9.prototxt b/src/frontends/onnx/tests/models/batchnorm_training_mode_opset9.prototxt
new file mode 100644
index 00000000000..85c7297fdd2
--- /dev/null
+++ b/src/frontends/onnx/tests/models/batchnorm_training_mode_opset9.prototxt
@@ -0,0 +1,170 @@
+ir_version: 4
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "s"
+ input: "bias"
+ input: "mean"
+ input: "var"
+ output: "y"
+ output: "out_mean"
+ output: "out_var"
+ output: "saved_mean"
+ output: "saved_var"
+ op_type: "BatchNormalization"
+ }
+ name: "test_batchnorm_training_mode"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "s"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "bias"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out_mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out_var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "saved_mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "saved_var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 9
+}
diff --git a/src/frontends/onnx/tests/models/dynamic_shapes/batchnorm_training_mode_dyn_rank.prototxt b/src/frontends/onnx/tests/models/dynamic_shapes/batchnorm_training_mode_dyn_rank.prototxt
new file mode 100644
index 00000000000..148d584bbad
--- /dev/null
+++ b/src/frontends/onnx/tests/models/dynamic_shapes/batchnorm_training_mode_dyn_rank.prototxt
@@ -0,0 +1,119 @@
+ir_version: 7
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "s"
+ input: "bias"
+ input: "mean"
+ input: "var"
+ output: "y"
+ output: "out_mean"
+ output: "out_var"
+ op_type: "BatchNormalization"
+ attribute {
+ name: "training_mode"
+ i: 1
+ type: INT
+ }
+ }
+ name: "test_batchnorm_training_mode_dyn_rank"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ }
+ }
+ }
+ input {
+ name: "s"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "bias"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ }
+ }
+ }
+ output {
+ name: "out_mean"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "out_var"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 14
+}
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index c328a3c0e6e..d3847aef52c 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -570,6 +570,131 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_batch_norm_opset15) {
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_batch_norm_training_mode_opset14) {
+ // Batch Normalization in the training mode (training_mode=1)
+ auto model = convert_model("batchnorm_training_mode_opset14.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({-1.f, 0.f, 1.f, 2.f, 3.f, 4.f}); // data {1, 2, 1, 3}
+ test_case.add_input<float>({1.f, 1.5f}); // scale
+ test_case.add_input<float>({0.f, 1.f}); // bias
+ test_case.add_input<float>({0.f, 3.f}); // mean
+ test_case.add_input<float>({1.f, 1.5f}); // var
+ // the current batch statistics are used for the normalization
+ test_case.add_expected_output<float>(Shape{1, 2, 1, 3},
+ {-1.2247356f, 0.f, 1.2247356f, -0.83710337f, 1.f, 2.8371034f});
+ test_case.add_expected_output<float>(Shape{2}, {0.f, 3.f}); // running mean
+ test_case.add_expected_output<float>(Shape{2}, {0.96666664f, 1.4166666f}); // running var
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_batch_norm_training_mode_opset9) {
+ // Batch Normalization in the training mode (more than one output)
+ auto model = convert_model("batchnorm_training_mode_opset9.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({-1.f, 0.f, 1.f, 2.f, 3.f, 4.f}); // data {1, 2, 1, 3}
+ test_case.add_input<float>({1.f, 1.5f}); // scale
+ test_case.add_input<float>({0.f, 1.f}); // bias
+ test_case.add_input<float>({0.f, 3.f}); // mean
+ test_case.add_input<float>({1.f, 1.5f}); // var
+ test_case.add_expected_output<float>(Shape{1, 2, 1, 3},
+ {-1.2247356f, 0.f, 1.2247356f, -0.83710337f, 1.f, 2.8371034f});
+ test_case.add_expected_output<float>(Shape{2}, {0.f, 3.f}); // running mean
+ test_case.add_expected_output<float>(Shape{2}, {0.96666664f, 1.4166666f}); // running var
+ test_case.add_expected_output<float>(Shape{2}, {0.f, 3.f}); // saved mean
+ test_case.add_expected_output<float>(Shape{2}, {0.6666667f, 0.6666667f}); // saved var
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_batch_norm_training_mode_opset1) {
+ // Batch Normalization in the training mode (is_test=0)
+ auto model = convert_model("batchnorm_training_mode_opset1.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({-1.f, 0.f, 1.f, 2.f, 3.f, 4.f}); // data {1, 2, 1, 3}
+ test_case.add_input<float>({1.f, 1.5f}); // scale
+ test_case.add_input<float>({0.f, 1.f}); // bias
+ test_case.add_input<float>({0.f, 3.f}); // mean
+ test_case.add_input<float>({1.f, 1.5f}); // var
+ test_case.add_expected_output<float>(Shape{1, 2, 1, 3},
+ {-1.2247356f, 0.f, 1.2247356f, -0.83710337f, 1.f, 2.8371034f});
+ test_case.add_expected_output<float>(Shape{2}, {0.f, 3.f}); // running mean
+ test_case.add_expected_output<float>(Shape{2}, {0.96666664f, 1.4166666f}); // running var
+ test_case.add_expected_output<float>(Shape{2}, {0.f, 3.f}); // saved mean
+ test_case.add_expected_output<float>(Shape{2}, {0.6666667f, 0.6666667f}); // saved var
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_batch_norm_training_mode_opset14_single_output) {
+ // Batch Normalization with training_mode=1 but only the "Y" output requested; the running statistics
+ // outputs must not be produced since they were not declared in the graph.
+ auto model = convert_model("batchnorm_training_mode_opset14_single_output.onnx");
+ EXPECT_EQ(model->get_output_size(), 1);
+
+ // "Y" is normalized with the current batch statistics, so the "mean"/"var" inputs are not consumed when the
+ // running statistics outputs are not requested. Depending on the frontend implementation those dangling
+ // parameters may or may not be pruned from the resulting model, so only add inputs for the parameters that
+ // are actually present.
+ const auto num_params = model->get_parameters().size();
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({-1.f, 0.f, 1.f, 2.f, 3.f, 4.f}); // data {1, 2, 1, 3}
+ test_case.add_input<float>({1.f, 1.5f}); // scale
+ test_case.add_input<float>({0.f, 1.f}); // bias
+ if (num_params > 3) {
+ test_case.add_input<float>({0.f, 3.f}); // mean (unused for the "Y" output)
+ test_case.add_input<float>({1.f, 1.5f}); // var (unused for the "Y" output)
+ }
+ test_case.add_expected_output<float>(Shape{1, 2, 1, 3},
+ {-1.2247356f, 0.f, 1.2247356f, -0.83710337f, 1.f, 2.8371034f});
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_batch_norm_opset7_invalid_inputs_number) {
+ // BatchNormalization without the mean/var inputs must fail with a descriptive frontend error
+ // instead of an out-of-range access.
+ EXPECT_THROW(convert_model("batchnorm_opset7_invalid_inputs_number.onnx"), ov::Exception);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_batch_norm_training_mode_f16) {
+ // Batch Normalization in the training mode with a float16 input. ONNX requires the batch statistics
+ // to be calculated in float, otherwise the accumulated sum overflows the float16 range.
+ auto model = convert_model("batchnorm_training_mode_f16.onnx");
+
+ for (const auto& op : model->get_ordered_ops()) {
+ if (std::string(op->get_type_name()) == "ReduceMean") {
+ EXPECT_EQ(op->get_output_element_type(0), element::f32);
+ }
+ }
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<ov::float16>({ov::float16(30000.f),
+ ov::float16(30016.f),
+ ov::float16(29984.f),
+ ov::float16(30000.f),
+ ov::float16(1.f),
+ ov::float16(2.f),
+ ov::float16(3.f),
+ ov::float16(4.f)}); // data {1, 2, 1, 4}
+ test_case.add_input<ov::float16>({ov::float16(1.f), ov::float16(1.f)}); // scale
+ test_case.add_input<ov::float16>({ov::float16(0.f), ov::float16(0.f)}); // bias
+ test_case.add_input<float>({0.f, 0.f}); // mean
+ test_case.add_input<float>({1.f, 1.f}); // var
+ test_case.add_expected_output<ov::float16>(Shape{1, 2, 1, 4},
+ {ov::float16(0.f),
+ ov::float16(1.414f),
+ ov::float16(-1.414f),
+ ov::float16(0.f),
+ ov::float16(-1.342f),
+ ov::float16(-0.4473f),
+ ov::float16(0.4473f),
+ ov::float16(1.342f)});
+ test_case.add_expected_output<float>(Shape{2}, {3000.f, 0.25f}); // running mean
+ test_case.add_expected_output<float>(Shape{2}, {13.7f, 1.025f}); // running var
+ test_case.run_with_tolerance_as_fp(0.01f);
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_relu) {
// Simple ReLU test
auto model = convert_model("relu.onnx");
diff --git a/src/frontends/onnx/tests/onnx_import_dyn_shapes.in.cpp b/src/frontends/onnx/tests/onnx_import_dyn_shapes.in.cpp
index ddfd8091ce1..9466011a23a 100644
--- a/src/frontends/onnx/tests/onnx_import_dyn_shapes.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_dyn_shapes.in.cpp
@@ -1214,3 +1214,20 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_eye_like_dyn_rank) {
test_case.run();
}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_batch_norm_training_mode_dyn_rank) {
+ const auto model = convert_model("dynamic_shapes/batchnorm_training_mode_dyn_rank.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>(Shape{1, 2, 1, 3}, {-1.f, 0.f, 1.f, 2.f, 3.f, 4.f}); // data
+ test_case.add_input<float>(Shape{2}, {1.f, 1.5f}); // scale
+ test_case.add_input<float>(Shape{2}, {0.f, 1.f}); // bias
+ test_case.add_input<float>(Shape{2}, {0.f, 3.f}); // mean
+ test_case.add_input<float>(Shape{2}, {1.f, 1.5f}); // var
+ test_case.add_expected_output<float>(Shape{1, 2, 1, 3},
+ {-1.2247356f, 0.f, 1.2247356f, -0.83710337f, 1.f, 2.8371034f});
+ test_case.add_expected_output<float>(Shape{2}, {0.f, 3.f}); // running mean
+ test_case.add_expected_output<float>(Shape{2}, {0.96666664f, 1.4166666f}); // running var
+
+ test_case.run();
+}
diff --git a/src/frontends/onnx/tests/runtime/ie/unit_test.manifest b/src/frontends/onnx/tests/runtime/ie/unit_test.manifest
index 4ee2eaec07f..7f5055f7121 100644
--- a/src/frontends/onnx/tests/runtime/ie/unit_test.manifest
+++ b/src/frontends/onnx/tests/runtime/ie/unit_test.manifest
@@ -133,6 +133,7 @@ onnx_model_depth_to_space_dynamic_input
onnx_model_space_to_depth_dynamic_input
onnx_model_eye_like_dyn_shape
onnx_model_eye_like_dyn_rank
+onnx_model_batch_norm_training_mode_dyn_rank
# Constant network
diff --git a/src/frontends/onnx/tests/tests_python/test_backend.py b/src/frontends/onnx/tests/tests_python/test_backend.py
index 342eff3d7d2..b4dd7cea43f 100644
--- a/src/frontends/onnx/tests/tests_python/test_backend.py
+++ b/src/frontends/onnx/tests/tests_python/test_backend.py
@@ -29,7 +29,6 @@ from tests import (
xfail_issue_47323,
xfail_issue_48052,
xfail_issue_52463,
- xfail_issue_63033,
xfail_issue_63043,
xfail_issue_63137,
xfail_issue_69444,
@@ -276,11 +275,6 @@ tests_expected_to_fail = [
"OnnxBackendNodeModelTest.test_sequence_insert_at_back_cpu",
"OnnxBackendNodeModelTest.test_sequence_insert_at_front_cpu",
),
- (
- xfail_issue_63033,
- "OnnxBackendNodeModelTest.test_batchnorm_epsilon_training_mode_cpu",
- "OnnxBackendNodeModelTest.test_batchnorm_example_training_mode_cpu",
- ),
(
xfail_issue_63043,
"OnnxBackendNodeModelTest.test_gru_batchwise_cpu",