Skip to content

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

[CORE] Fix ov::util::reduce_and returning the data pointer instead of the value (#38171)

Aditya Goyalb64f35df3db0
diff --git a/src/common/transformations/tests/common_optimizations/simplify_shape_of_sub_graph.cpp b/src/common/transformations/tests/common_optimizations/simplify_shape_of_sub_graph.cpp
index 1a666347f08..3eb6dac7ff7 100644
--- a/src/common/transformations/tests/common_optimizations/simplify_shape_of_sub_graph.cpp
+++ b/src/common/transformations/tests/common_optimizations/simplify_shape_of_sub_graph.cpp
@@ -14,6 +14,7 @@
#include "openvino/core/bound_evaluation_util.hpp"
#include "openvino/core/model.hpp"
#include "openvino/op/abs.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
@@ -22,6 +23,7 @@
#include "openvino/op/parameter.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
+#include "openvino/op/subtract.hpp"
#include "openvino/op/unsqueeze.hpp"
#include "openvino/op/util/symbolic_info.hpp"
#include "openvino/opsets/opset7_decl.hpp"
@@ -444,6 +446,37 @@ TEST_F(TransformationTestsF, AbsInTheUnknown) {
}
}
+TEST_F(TransformationTestsF, AbsSinkingKeepsAbsOnNegativeBounds) {
+ {
+ auto lhs = op::v0::Constant::create(element::i64, {3}, {1, -2, 3});
+ auto rhs = op::v0::Constant::create(element::i64, {3}, {4, 1, -1});
+ auto sub = std::make_shared<op::v1::Subtract>(lhs, rhs);
+ auto abs = std::make_shared<op::v0::Abs>(sub);
+
+ model = std::make_shared<Model>(OutputVector{abs}, ParameterVector{});
+ manager.register_pass<pass::AbsSinking>();
+ }
+}
+
+TEST_F(TransformationTestsF, AbsSinkingRemovesAbsOnNonNegativeBounds) {
+ {
+ auto lhs = op::v0::Constant::create(element::i64, {2}, {1, 2});
+ auto rhs = op::v0::Constant::create(element::i64, {2}, {3, 4});
+ auto add = std::make_shared<op::v1::Add>(lhs, rhs);
+ auto abs = std::make_shared<op::v0::Abs>(add);
+
+ model = std::make_shared<Model>(OutputVector{abs}, ParameterVector{});
+ manager.register_pass<pass::AbsSinking>();
+ }
+ {
+ auto lhs = op::v0::Constant::create(element::i64, {2}, {1, 2});
+ auto rhs = op::v0::Constant::create(element::i64, {2}, {3, 4});
+ auto add = std::make_shared<op::v1::Add>(lhs, rhs);
+
+ model_ref = std::make_shared<Model>(OutputVector{add}, ParameterVector{});
+ }
+}
+
// Parameterized test for AbsSinking with constants
struct AbsSinkingConstantTestParams {
std::vector<int64_t> values;
diff --git a/src/core/src/tensor_util.cpp b/src/core/src/tensor_util.cpp
index 9473ee0a47f..33099ca7da0 100644
--- a/src/core/src/tensor_util.cpp
+++ b/src/core/src/tensor_util.cpp
@@ -26,5 +26,5 @@ bool ov::util::reduce_and(const ov::Tensor& t) {
std::iota(axes.data<int64_t>(), axes.data<int64_t>() + t.get_shape().size(), 0);
if (!ov::op::v1::ReduceLogicalAnd().evaluate(outputs, {t, std::move(axes)}))
return false;
- return outputs[0].data<char>();
+ return *outputs[0].data<char>();
}
diff --git a/src/core/tests/bound_evaluate.cpp b/src/core/tests/bound_evaluate.cpp
index 484179ad7f4..c71c5834ebd 100644
--- a/src/core/tests/bound_evaluate.cpp
+++ b/src/core/tests/bound_evaluate.cpp
@@ -9,6 +9,7 @@
#include "common_test_utils/test_assertions.hpp"
#include "common_test_utils/type_prop.hpp"
#include "openvino/op/concat.hpp"
+#include "openvino/op/reduce_prod.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/util/framework_node.hpp"
@@ -17,6 +18,7 @@ using ov::op::v0::Concat;
using ov::op::v0::Constant;
using ov::op::v0::Parameter;
using ov::op::v0::Result;
+using ov::op::v1::ReduceProd;
using ov::op::v1::Subtract;
using ov::op::v3::ShapeOf;
@@ -86,3 +88,28 @@ TEST(BoundEvaluatorTest, no_exception_on_single_bound) {
OV_ASSERT_NO_THROW(sub->evaluate_upper(output));
EXPECT_EQ(o_[0], 10);
}
+
+TEST(BoundEvaluatorTest, reduce_prod_no_bounds_for_possibly_negative_data) {
+ const auto data = std::make_shared<Parameter>(element::f32, PartialShape{{1, 2}, {1, 3}});
+ const auto shape = std::make_shared<ShapeOf>(data);
+ // bounds of shape - 3 are [-2, -2] and [-1, 0], prod of them is not a valid interval
+ const auto shifted = std::make_shared<Subtract>(shape, Constant::create(element::i64, Shape{1}, {3}));
+ const auto axes = Constant::create(element::i64, Shape{1}, {0});
+ const auto prod = std::make_shared<ReduceProd>(shifted, axes, false);
+
+ const auto& [lower, upper] = ov::util::evaluate_both_bounds(prod->output(0));
+ EXPECT_FALSE(lower);
+ EXPECT_FALSE(upper);
+}
+
+TEST(BoundEvaluatorTest, reduce_prod_bounds_for_non_negative_data) {
+ const auto data = std::make_shared<Parameter>(element::f32, PartialShape{{1, 2}, {1, 3}});
+ const auto shape = std::make_shared<ShapeOf>(data);
+ const auto axes = Constant::create(element::i64, Shape{1}, {0});
+ const auto prod = std::make_shared<ReduceProd>(shape, axes, false);
+
+ const auto& [lower, upper] = ov::util::evaluate_both_bounds(prod->output(0));
+ ASSERT_TRUE(lower && upper);
+ EXPECT_EQ(lower.data<int64_t>()[0], 1);
+ EXPECT_EQ(upper.data<int64_t>()[0], 6);
+}