Skip to content

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

Add conditional bounds invalidation in replace_output() (#33622)

Evgeny Kotov5cdcb9341561
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 33be4a10e11..1a666347f08 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
@@ -11,21 +11,27 @@
#include <string>
#include "common_test_utils/ov_test_utils.hpp"
+#include "openvino/core/bound_evaluation_util.hpp"
#include "openvino/core/model.hpp"
#include "openvino/op/abs.hpp"
+#include "openvino/op/broadcast.hpp"
#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/gather.hpp"
+#include "openvino/op/parameter.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/unsqueeze.hpp"
+#include "openvino/op/util/symbolic_info.hpp"
#include "openvino/opsets/opset7_decl.hpp"
#include "openvino/opsets/opset8_decl.hpp"
#include "openvino/pass/manager.hpp"
#include "transformations/init_node_info.hpp"
+#include "transformations/symbolic_transformations/symbolic_optimizations.hpp"
using namespace testing;
-using namespace ov;
+namespace ov::test {
auto gatherv7 =
[](const std::shared_ptr<Node> input, std::vector<int64_t> indices, bool scalar = false) -> Output<Node> {
@@ -470,3 +476,40 @@ INSTANTIATE_TEST_SUITE_P(AbsSinkingConstantTests,
[](const ::testing::TestParamInfo<AbsSinkingConstantTestParams>& info) {
return info.param.test_name;
});
+
+TEST(AbsSinkingSymbolicOptimizations, BroadcastPatternWithSkipInvalidation) {
+ // Create model: Broadcast(const, Abs(Concat(gather(ShapeOf(param), 0), -1, -1)))
+ PartialShape shape = PartialShape::dynamic(4);
+
+ auto data = std::make_shared<ov::op::v0::Parameter>(element::f32, shape);
+ auto shape_op = std::make_shared<ov::op::v3::ShapeOf>(data);
+
+ // Gather first dimension
+ auto indices = ov::op::v0::Constant::create(element::i64, {1}, {0});
+ auto axis = ov::op::v0::Constant::create(element::i64, {}, {0});
+ auto gather = std::make_shared<ov::op::v8::Gather>(shape_op, indices, axis);
+
+ // Create Concat with negative constants (simulating dynamic dimensions)
+ auto minus_one_1 = ov::op::v0::Constant::create(element::i64, {1}, {-1});
+ auto minus_one_2 = ov::op::v0::Constant::create(element::i64, {1}, {-1});
+ auto concat = std::make_shared<ov::op::v0::Concat>(OutputVector{gather, minus_one_1, minus_one_2}, 0);
+
+ // Abs wrapping the concat
+ auto abs = std::make_shared<ov::op::v0::Abs>(concat);
+
+ // Broadcast uses abs output as target shape
+ // This is the pattern that triggers bounds evaluation and fails with stale -1 values
+ auto broadcast_input = ov::op::v0::Constant::create(element::f32, {1}, {1.0f});
+ auto broadcast = std::make_shared<ov::op::v3::Broadcast>(broadcast_input, abs);
+
+ auto model = std::make_shared<Model>(OutputVector{broadcast}, ParameterVector{data});
+
+ // Run SymbolicOptimizations with full_run=true (which enables AbsSinking)
+ // This should NOT throw an assertion error about negative bounds
+ pass::Manager manager;
+ manager.register_pass<pass::SymbolicOptimizations>();
+
+ // The test passes if no exception is thrown
+ EXPECT_NO_THROW(manager.run_passes(model));
+}
+} // namespace ov::test
diff --git a/src/core/dev_api/openvino/core/bound_evaluation_util.hpp b/src/core/dev_api/openvino/core/bound_evaluation_util.hpp
index 29a9978d78d..4d5418d20b9 100644
--- a/src/core/dev_api/openvino/core/bound_evaluation_util.hpp
+++ b/src/core/dev_api/openvino/core/bound_evaluation_util.hpp
@@ -5,6 +5,7 @@
#pragma once
#include "openvino/core/node.hpp"
+#include "openvino/core/runtime_attribute.hpp"
namespace ov {
@@ -15,6 +16,21 @@ namespace ov {
OPENVINO_API bool could_propagate(const Output<Node>& output, std::vector<Node*>& order);
namespace util {
+/**
+ * @ingroup ov_runtime_attr_api
+ * @brief ForceInvalidation class represents runtime info attribute that forces bounds invalidation
+ * even when SkipInvalidation is set. Used when input source changes and bounds need recalculation.
+ * The attribute is automatically removed after invalidation occurs.
+ */
+class OPENVINO_API ForceInvalidation : public RuntimeAttribute {
+public:
+ OPENVINO_RTTI("ForceInvalidation", "0", RuntimeAttribute);
+ ForceInvalidation() = default;
+ ~ForceInvalidation();
+ bool is_copyable() const override {
+ return false;
+ }
+};
/// \brief Evaluates lower value estimation of the output tensor. Traverses graph up to deduce
/// estimation through it.
@@ -33,5 +49,15 @@ OPENVINO_API Tensor evaluate_upper_bound(const Output<Node>& output);
/// \param output Node output pointing to the tensor for estimation.
/// \return pair with Tensors for lower and upper value estimation.
OPENVINO_API std::pair<Tensor, Tensor> evaluate_both_bounds(const Output<Node>& output);
+
+/// \brief Compares bounds (lower and upper values) of two tensors.
+/// \param lhs First tensor to compare.
+/// \param rhs Second tensor to compare.
+/// \return True if both tensors have the same lower and upper bound values, false otherwise.
+OPENVINO_API bool have_same_bounds(const descriptor::Tensor& lhs, const descriptor::Tensor& rhs);
+
+/// \brief Sets ForceInvalidation attribute on tensor to force bounds invalidation on next invalidate_values() call.
+/// \param tensor Tensor to set the ForceInvalidation attribute on.
+OPENVINO_API void set_force_invalidation(ov::descriptor::Tensor& tensor);
} // namespace util
} // namespace ov
diff --git a/src/core/src/bound_evaluate.cpp b/src/core/src/bound_evaluate.cpp
index dfc4486f1c6..c8e0c8336b1 100644
--- a/src/core/src/bound_evaluate.cpp
+++ b/src/core/src/bound_evaluate.cpp
@@ -7,6 +7,7 @@
#include <stack>
#include "compare.hpp"
+#include "openvino/core/bound_evaluation_util.hpp"
#include "openvino/core/dimension.hpp"
#include "openvino/core/rt_info.hpp"
#include "openvino/core/shape_util.hpp"
@@ -737,3 +738,16 @@ bool ov::default_symbol_evaluator(const Node* node,
}
return false;
}
+
+namespace ov::util {
+ForceInvalidation::~ForceInvalidation() = default;
+
+bool have_same_bounds(const descriptor::Tensor& lhs, const descriptor::Tensor& rhs) {
+ return are_same_tensor(lhs.get_lower_value(), rhs.get_lower_value()) &&
+ are_same_tensor(lhs.get_upper_value(), rhs.get_upper_value());
+}
+
+void set_force_invalidation(descriptor::Tensor& tensor) {
+ tensor.get_rt_info().emplace(ForceInvalidation::get_type_info_static(), nullptr);
+}
+} // namespace ov::util
diff --git a/src/core/src/descriptor/input.cpp b/src/core/src/descriptor/input.cpp
index a77486e6cb6..2e7eac6f708 100644
--- a/src/core/src/descriptor/input.cpp
+++ b/src/core/src/descriptor/input.cpp
@@ -4,9 +4,11 @@
#include "openvino/core/descriptor/input.hpp"
+#include "openvino/core/bound_evaluation_util.hpp"
#include "openvino/core/descriptor/output.hpp"
#include "openvino/core/node.hpp"
#include "openvino/core/type/element_type.hpp"
+#include "openvino/op/util/symbolic_info.hpp"
#include "shared_node_info.hpp"
ov::descriptor::Input::Input(ov::Node* node, size_t index, Output& output)
@@ -32,6 +34,13 @@ ov::descriptor::Input::~Input() {
void ov::descriptor::Input::replace_output(Output& new_output) {
if (m_output != nullptr) {
+ if (!ov::util::have_same_bounds(m_output->get_tensor(), new_output.get_tensor())) {
+ for (size_t port = 0; port < m_node->get_output_size(); ++port) {
+ if (auto& tensor = m_node->get_output_tensor(port); ov::skip_invalidation(tensor)) {
+ ov::util::set_force_invalidation(tensor);
+ }
+ }
+ }
m_output->remove_input(this);
}
new_output.add_input(this);
diff --git a/src/core/src/descriptor/tensor.cpp b/src/core/src/descriptor/tensor.cpp
index 5ec17d792ad..4ee5ad4c973 100644
--- a/src/core/src/descriptor/tensor.cpp
+++ b/src/core/src/descriptor/tensor.cpp
@@ -5,6 +5,7 @@
#include "openvino/core/descriptor/tensor.hpp"
#include "atomic_guard.hpp"
+#include "openvino/core/bound_evaluation_util.hpp"
#include "openvino/core/descriptor_tensor.hpp"
#include "openvino/core/except.hpp"
#include "openvino/core/memory_util.hpp"
@@ -150,7 +151,8 @@ Tensor::Tensor(const element::Type& element_type,
: m_impl(std::make_shared<BasicTensor>(element_type, pshape, names)) {}
void Tensor::invalidate_values() {
- if (ov::skip_invalidation(*this))
+ if (const auto ignore_skip = get_rt_info().extract(util::ForceInvalidation::get_type_info_static());
+ !ignore_skip && ov::skip_invalidation(*this))
return;
m_upper_value = {};
m_lower_value = {};