Skip to content

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

[Core] Validate out-of-range values on MVN-6 reduction axes (#37908)

Enrico Galli89a76b7ac307
diff --git a/src/core/reference/include/openvino/reference/mvn.hpp b/src/core/reference/include/openvino/reference/mvn.hpp
index a4460d84257..06f3c03341f 100644
--- a/src/core/reference/include/openvino/reference/mvn.hpp
+++ b/src/core/reference/include/openvino/reference/mvn.hpp
@@ -87,21 +87,5 @@ void mvn_6(const T* arg,
}
}
-template <typename T>
-AxisSet mvn_6_reduction_axes(const ov::Tensor& axes_input, size_t rank) {
- const T* a = axes_input.data<T>();
- auto v = std::vector<T>(a, a + axes_input.get_shape()[0]);
- std::vector<size_t> axes(v.size(), 0);
- for (size_t i = 0; i < v.size(); i++) {
- if (v[i] < 0) {
- OPENVINO_ASSERT(rank + v[i] >= 0, "Unexpected axis");
- axes[i] = (size_t)(rank + v[i]);
- } else {
- axes[i] = (size_t)(v[i]);
- }
- }
- return AxisSet(axes);
-}
-
} // namespace reference
} // namespace ov
diff --git a/src/core/src/op/mvn.cpp b/src/core/src/op/mvn.cpp
index 188c6f63e44..385f363434e 100644
--- a/src/core/src/op/mvn.cpp
+++ b/src/core/src/op/mvn.cpp
@@ -7,6 +7,8 @@
#include "compare.hpp"
#include "itt.hpp"
#include "openvino/core/type/element_type_traits.hpp"
+#include "openvino/core/validation_util.hpp"
+#include "openvino/op/constant.hpp"
#include "openvino/reference/mvn.hpp"
// ------------------------------ V0 ------------------------------
@@ -106,6 +108,17 @@ void ov::op::v6::MVN::validate_and_infer_types() {
data_rank.is_dynamic() || cmp::ge(data_rank.get_length(), axes.get_shape()[0]),
"Expected rank for the 'data' input to be higher than axes shape. Got: ",
data);
+
+ if (data_rank.is_static()) {
+ if (const auto axes_constant = ov::util::get_constant_from_source(input_value(1))) {
+ const auto& axes_type = axes_constant->get_element_type();
+ NODE_VALIDATION_CHECK(this,
+ axes_type == element::i32 || axes_type == element::i64,
+ "Expected i32 or i64 element type for the 'axes' input. Got: ",
+ axes_type);
+ ov::util::validate_axes(axes_constant->cast_vector<int64_t>(), data_rank, *this);
+ }
+ }
}
set_output_type(0, get_input_element_type(0), data);
@@ -133,19 +146,11 @@ namespace {
template <ov::element::Type_t ET>
bool evaluate(ov::TensorVector& outputs,
const ov::TensorVector& inputs,
+ const ov::AxisSet& reduction_axes,
bool normalize_variance,
float eps,
ov::op::MVNEpsMode eps_mode) {
using T = typename ov::element_type_traits<ET>::value_type;
- ov::AxisSet reduction_axes;
- auto rank = inputs[0].get_shape().size();
- if (inputs[1].get_element_type() == ov::element::i64) {
- reduction_axes = ov::reference::mvn_6_reduction_axes<int64_t>(inputs[1], rank);
- } else if (inputs[1].get_element_type() == ov::element::i32) {
- reduction_axes = ov::reference::mvn_6_reduction_axes<int32_t>(inputs[1], rank);
- } else {
- OPENVINO_THROW("Unexpected indices type");
- }
ov::reference::mvn_6<T>(inputs[0].data<T>(),
outputs[0].data<T>(),
inputs[0].get_shape(),
@@ -160,10 +165,17 @@ bool evaluate_mvn(ov::TensorVector& outputs,
const ov::TensorVector& inputs,
bool normalize_variance,
float eps,
- ov::op::MVNEpsMode eps_mode) {
+ ov::op::MVNEpsMode eps_mode,
+ const ov::Node& node) {
+ const auto& axes_type = inputs[1].get_element_type();
+ OPENVINO_ASSERT(axes_type == ov::element::i32 || axes_type == ov::element::i64,
+ "Expected i32 or i64 element type for the 'axes' input. Got: ",
+ axes_type);
+ // Computed once here, outside the per-type template, to avoid duplicating axis validation in every instantiation.
+ const auto reduction_axes = ov::util::try_get_normalized_axis_set(inputs[1], inputs[0].get_shape().size(), node);
bool rc = true;
switch (inputs[0].get_element_type()) {
- OPENVINO_TYPE_CASE(evaluate_mvn, f32, outputs, inputs, normalize_variance, eps, eps_mode);
+ OPENVINO_TYPE_CASE(evaluate_mvn, f32, outputs, inputs, reduction_axes, normalize_variance, eps, eps_mode);
default:
rc = false;
break;
@@ -175,7 +187,7 @@ bool evaluate_mvn(ov::TensorVector& outputs,
bool ov::op::v6::MVN::evaluate(ov::TensorVector& outputs, const ov::TensorVector& inputs) const {
OV_OP_SCOPE(v6_MVN_evaluate);
- return mvn::evaluate_mvn(outputs, inputs, get_normalize_variance(), get_eps(), get_eps_mode());
+ return mvn::evaluate_mvn(outputs, inputs, get_normalize_variance(), get_eps(), get_eps_mode(), *this);
}
bool ov::op::v6::MVN::has_evaluate() const {
diff --git a/src/core/tests/type_prop/mvn.cpp b/src/core/tests/type_prop/mvn.cpp
index 92b52e68bfd..a03e6759303 100644
--- a/src/core/tests/type_prop/mvn.cpp
+++ b/src/core/tests/type_prop/mvn.cpp
@@ -4,10 +4,12 @@
#include "openvino/op/mvn.hpp"
+#include "common_test_utils/test_assertions.hpp"
#include "common_test_utils/type_prop.hpp"
using namespace std;
using namespace ov;
+using testing::HasSubstr;
// ------------------------------ V0 ------------------------------
@@ -62,3 +64,30 @@ TEST(type_prop, mvn_6_partial) {
op::MVNEpsMode::INSIDE_SQRT);
ASSERT_TRUE(mvn_partial->get_output_partial_shape(0).same_scheme(PartialShape::dynamic()));
}
+
+TEST(type_prop, mvn_6_axes_out_of_range) {
+ auto data = make_shared<ov::op::v0::Parameter>(element::f32, Shape{2, 2});
+ auto axes = ov::op::v0::Constant::create(element::i64, Shape{1}, {1000});
+
+ OV_EXPECT_THROW(std::ignore = make_shared<op::v6::MVN>(data, axes, true, 1e-6f, op::MVNEpsMode::INSIDE_SQRT),
+ NodeValidationFailure,
+ HasSubstr("Axis"));
+}
+
+TEST(type_prop, mvn_6_axes_out_of_range_negative) {
+ auto data = make_shared<ov::op::v0::Parameter>(element::f32, Shape{2, 2});
+ auto axes = ov::op::v0::Constant::create(element::i64, Shape{1}, {-3});
+
+ OV_EXPECT_THROW(std::ignore = make_shared<op::v6::MVN>(data, axes, true, 1e-6f, op::MVNEpsMode::INSIDE_SQRT),
+ NodeValidationFailure,
+ HasSubstr("Axis"));
+}
+
+TEST(type_prop, mvn_6_axes_invalid_element_type) {
+ auto data = make_shared<ov::op::v0::Parameter>(element::f32, Shape{2, 2});
+ auto axes = ov::op::v0::Constant::create(element::f32, Shape{1}, {1.5f});
+
+ OV_EXPECT_THROW(std::ignore = make_shared<op::v6::MVN>(data, axes, true, 1e-6f, op::MVNEpsMode::INSIDE_SQRT),
+ NodeValidationFailure,
+ HasSubstr("Expected i32 or i64 element type for the 'axes' input"));
+}
diff --git a/src/plugins/template/backend/ops/mvn.cpp b/src/plugins/template/backend/ops/mvn.cpp
index 7579f7ed198..9d60816c7ad 100644
--- a/src/plugins/template/backend/ops/mvn.cpp
+++ b/src/plugins/template/backend/ops/mvn.cpp
@@ -6,6 +6,7 @@
#include "evaluate_node.hpp"
#include "openvino/core/type/element_type_traits.hpp"
+#include "openvino/core/validation_util.hpp"
template <ov::element::Type_t ET>
bool evaluate(const std::shared_ptr<ov::op::v0::MVN>& op, ov::TensorVector& outputs, const ov::TensorVector& inputs) {
@@ -19,38 +20,14 @@ bool evaluate(const std::shared_ptr<ov::op::v0::MVN>& op, ov::TensorVector& outp
return true;
}
-namespace mvn_6_axes {
-template <typename T>
-ov::AxisSet mvn_6_reduction_axes(const ov::Tensor& axes_input, size_t rank) {
- const T* a = axes_input.data<T>();
- auto v = std::vector<T>(a, a + axes_input.get_shape()[0]);
- std::vector<size_t> axes(v.size(), 0);
- for (size_t i = 0; i < v.size(); i++) {
- if (v[i] < 0) {
- if (rank + v[i] < 0) {
- OPENVINO_THROW("Unexpected axis");
- }
- axes[i] = (size_t)(rank + v[i]);
- } else {
- axes[i] = (size_t)(v[i]);
- }
- }
- return ov::AxisSet(axes);
-}
-} // namespace mvn_6_axes
-
template <ov::element::Type_t ET>
bool evaluate(const std::shared_ptr<ov::op::v6::MVN>& op, ov::TensorVector& outputs, const ov::TensorVector& inputs) {
using T = typename ov::element_type_traits<ET>::value_type;
- ov::AxisSet reduction_axes;
- auto rank = inputs[0].get_shape().size();
- if (inputs[1].get_element_type() == ov::element::i64) {
- reduction_axes = mvn_6_axes::mvn_6_reduction_axes<int64_t>(inputs[1], rank);
- } else if (inputs[1].get_element_type() == ov::element::i32) {
- reduction_axes = mvn_6_axes::mvn_6_reduction_axes<int32_t>(inputs[1], rank);
- } else {
- OPENVINO_THROW("Unexpected indices type");
- }
+ const auto& axes_type = inputs[1].get_element_type();
+ OPENVINO_ASSERT(axes_type == ov::element::i32 || axes_type == ov::element::i64,
+ "Expected i32 or i64 element type for the 'axes' input. Got: ",
+ axes_type);
+ const auto reduction_axes = ov::util::try_get_normalized_axis_set(inputs[1], inputs[0].get_shape().size(), *op);
ov::reference::mvn_6<T>(inputs[0].data<T>(),
outputs[0].data<T>(),
inputs[0].get_shape(),
diff --git a/src/plugins/template/tests/functional/op_reference/mvn.cpp b/src/plugins/template/tests/functional/op_reference/mvn.cpp
index eeda900374d..93e94c2f056 100644
--- a/src/plugins/template/tests/functional/op_reference/mvn.cpp
+++ b/src/plugins/template/tests/functional/op_reference/mvn.cpp
@@ -309,3 +309,20 @@ INSTANTIATE_TEST_SUITE_P(
0.38729835, 0.7745967, 1.161895, 1.5491934, -1.5491934, -1.161895, -0.7745967,
-0.38729835, 0., 0.38729835, 0.7745967, 1.161895, 1.5491934}})),
ReferenceMVN6LayerTest::getTestCaseName);
+
+class ReferenceMVN6InvalidAxisTest : public testing::TestWithParam<int64_t>, public CommonReferenceTest {};
+
+TEST_P(ReferenceMVN6InvalidAxisTest, OutOfRangeAxis) {
+ const auto data = std::make_shared<op::v0::Parameter>(element::f32, Shape{2, 2});
+ const auto axes = std::make_shared<op::v0::Parameter>(element::i64, Shape{1});
+ const auto mvn = std::make_shared<op::v6::MVN>(data, axes, false, 1e-6f, op::MVNEpsMode::INSIDE_SQRT);
+ function = std::make_shared<Model>(OutputVector{mvn}, ParameterVector{data, axes});
+ inputData = {reference_tests::CreateTensor(Shape{2, 2}, element::f32, std::vector<float>(4, 1.f)),
+ reference_tests::CreateTensor(Shape{1}, element::i64, std::vector<int64_t>{GetParam()})};
+
+ LoadNetwork();
+ FillInputs();
+ EXPECT_THROW(Infer(), ov::Exception);
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke_MVN6_InvalidAxis, ReferenceMVN6InvalidAxisTest, ::testing::Values(1000, -1000));
diff --git a/src/tests/ov_helpers/ov_lpt_models/src/mvn.cpp b/src/tests/ov_helpers/ov_lpt_models/src/mvn.cpp
index e0197144021..16772ce27b1 100644
--- a/src/tests/ov_helpers/ov_lpt_models/src/mvn.cpp
+++ b/src/tests/ov_helpers/ov_lpt_models/src/mvn.cpp
@@ -82,6 +82,8 @@ std::shared_ptr<ov::Model> MVNFunction::getReference(
ov::op::v0::MVN(dequantizationOpBefore, reductionAxes, normalizeVariance),
dequantizationAfter.empty() ? precision : ov::element::f32);
} else if (opset_version == 6) {
+ const auto overridden_type = dequantizationAfter.empty() ? precision : ov::element::f32;
+ // Override only the data input: MVN-6 axes must stay i32/i64.
mvn = std::make_shared<ov::op::TypeRelaxed<ov::op::v6::MVN>>(
ov::op::v6::MVN(dequantizationOpBefore,
std::make_shared<ov::opset1::Constant>(ov::element::i64,
@@ -90,7 +92,8 @@ std::shared_ptr<ov::Model> MVNFunction::getReference(
normalizeVariance,
1e-9,
ov::op::MVNEpsMode::INSIDE_SQRT),
- dequantizationAfter.empty() ? precision : ov::element::f32);
+ ov::element::TypeVector{overridden_type, ov::element::dynamic},
+ ov::element::TypeVector{overridden_type});
}
auto& rtInfo = mvn->get_rt_info();
rtInfo["Variant::std::string"] = "mvn";