OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[core] Improve bound evaluation by skip nodes which have bounds (#25707)
Pawel Raasze8e471a631b6
diff --git a/src/common/transformations/src/transformations/common_optimizations/reshape_sequence_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/reshape_sequence_fusion.cppindex e51715a34a9..7c37774d031 100644--- a/src/common/transformations/src/transformations/common_optimizations/reshape_sequence_fusion.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/reshape_sequence_fusion.cpp@@ -7,6 +7,7 @@#include <memory>#include <vector>+#include "compare.hpp"#include "itt.hpp"#include "openvino/core/bound_evaluation_util.hpp"#include "openvino/core/rt_info.hpp"@@ -18,30 +19,25 @@namespace {bool has_valid_pattern(const ov::Output<ov::Node>& node_out) {const auto const_node = std::dynamic_pointer_cast<ov::op::v0::Constant>(node_out.get_node_shared_ptr());+ constexpr auto has_special_value = ov::cmp::Less<int64_t>(1);if (!const_node) {- // Lower bound of the value- auto lb = ov::util::evaluate_lower_bound(node_out);- if (!lb)+ // evaluate bounds+ const auto bounds = ov::util::evaluate_both_bounds(node_out);+ const auto& lb = std::get<0>(bounds);+ const auto& ub = std::get<1>(bounds);+ if (!lb || !ub) {return false;- const auto lb_const_node =- std::make_shared<ov::op::v0::Constant>(lb.get_element_type(), lb.get_shape(), lb.data());+ }+ const auto lb_const_node = std::make_shared<ov::op::v0::Constant>(lb);const auto& lb_values = lb_const_node->cast_vector<int64_t>();// The pattern is valid if all lower bound values are higher than zero (not a special number)// or if the lower and upper bounds values are a sign of full dynamism- const bool lb_has_special_val = std::any_of(lb_values.cbegin(), lb_values.cend(), [](int64_t value) {- return value < 1;- });+ const bool lb_has_special_val = std::any_of(lb_values.cbegin(), lb_values.cend(), has_special_value);if (!lb_has_special_val)return true;- // Upper bound of the value- auto ub = ov::util::evaluate_upper_bound(node_out);- if (!ub)- return false;-- const auto ub_const_node =- std::make_shared<ov::op::v0::Constant>(ub.get_element_type(), ub.get_shape(), ub.data());+ const auto ub_const_node = std::make_shared<ov::op::v0::Constant>(ub);const auto& ub_values = ub_const_node->cast_vector<int64_t>();if (lb_values.size() != ub_values.size())return false;@@ -59,9 +55,7 @@ bool has_valid_pattern(const ov::Output<ov::Node>& node_out) {}const auto& values = const_node->cast_vector<int64_t>();// We can not fuse Reshapes if their pattern values have special numbers like -1 and 0- return std::all_of(values.cbegin(), values.cend(), [](int64_t value) {- return value > 0;- });+ return std::none_of(values.cbegin(), values.cend(), has_special_value);}} // namespacediff --git a/src/core/src/bound_evaluate.cpp b/src/core/src/bound_evaluate.cppindex 55c31538e91..152feed5fec 100644--- a/src/core/src/bound_evaluate.cpp+++ b/src/core/src/bound_evaluate.cpp@@ -75,85 +75,221 @@ bool is_type_allocable(const element::Type& type) {return type != element::undefined && type.is_static();}-ov::Tensor evaluate_bound(const Output<Node>& output, bool is_upper, bool invalidate_all_unused_values = true) {- if (is_upper && output.get_tensor().get_upper_value()) {- return output.get_tensor().get_upper_value();- }+/**+ * @brief Checks if node's inputs has bound set and are equal.+ *+ * @param node Node+ * @return true if all inputs have both bounds set and are equal, otherwise false.+ */+bool node_has_same_inputs(const Node& node) {+ const auto& values = node.input_values();++ return std::all_of(values.begin(), values.end(), [](const Output<Node>& value) {+ const auto& t_desc = value.get_tensor();+ return t_desc.has_and_set_bound() || are_equal(t_desc.get_lower_value(), t_desc.get_upper_value());+ });+};- if (!is_upper && output.get_tensor().get_lower_value()) {- return output.get_tensor().get_lower_value();+/**+ * @brief Invalidates unused tensor values for outputs (except Constants)+ *+ * @param outputs Outputs to apply invalidation.+ */+void invalidate_unused_values(const ov::OutputVector& outputs) {+ for (const auto& output : outputs) {+ auto& tensor = output.get_tensor();+ const auto& lower = tensor.get_lower_value();+ const auto& upper = tensor.get_upper_value();+ const auto should_invalidate =+ (lower && shape_size(lower.get_shape()) > 10) || (upper && shape_size(upper.get_shape()) > 10);+ if (should_invalidate && output.get_target_inputs().size() == 1) {+ tensor.invalidate_values();+ }}+}- std::vector<Node*> order;- if (could_propagate(output, order)) {- for (const auto& node : order) {- ov::TensorVector outputs;- for (const auto& out : node->outputs()) {- if (is_type_allocable(out.get_element_type())) {- outputs.emplace_back(out);- } else {- outputs.emplace_back();- }+/** @brief Common base for single bound evaluation. */+struct SingleBound {+ void init(const Node& node) {+ bounds.resize(0);+ for (const auto& output : node.outputs()) {+ if (is_type_allocable(output.get_element_type())) {+ bounds.emplace_back(output);+ } else {+ bounds.emplace_back();}+ }+ }++ void set_same_bound_at_port(descriptor::Tensor& tensor_desc, const size_t port) const {+ tensor_desc.set_lower_value(bounds[port]);+ tensor_desc.set_upper_value(bounds[port]);+ }- if (is_upper ? node->evaluate_upper(outputs) : node->evaluate_lower(outputs)) {- const auto& input_values = node->input_values();- TensorSymbolVector output_symbols(outputs.size());+ TensorVector bounds;+};- bool same_inputs = std::all_of(input_values.begin(), input_values.end(), [](const Output<Node>& input) {- auto& t = input.get_tensor();- return t.has_and_set_bound() || are_equal(t.get_lower_value(), t.get_upper_value());- });+/** @brief Lower bound evaluation specific actions. */+struct LowerBound : SingleBound {+ static bool requires_evaluation(const descriptor::Tensor& tensor_desc) {+ return !tensor_desc.get_lower_value();+ }- for (size_t i = 0; i < outputs.size(); ++i) {- if ((same_inputs || is_upper) && !node->get_output_tensor(i).get_upper_value() && outputs[i]) {- node->get_output_tensor(i).set_upper_value(outputs[i]);- }+protected:+ bool evaluate(const Node& node) {+ return node.evaluate_lower(bounds);+ }- if ((same_inputs || !is_upper) && !node->get_output_tensor(i).get_lower_value() && outputs[i]) {- node->get_output_tensor(i).set_lower_value(outputs[i]);- }+ void set_bound_at_port(descriptor::Tensor& tensor_desc, const size_t port) const {+ tensor_desc.set_lower_value(bounds[port]);+ if (are_equal(bounds[port], tensor_desc.get_upper_value())) {+ tensor_desc.set_upper_value(bounds[port]);+ }+ }+};- if (are_equal(node->get_output_tensor(i).get_lower_value(),- node->get_output_tensor(i).get_upper_value())) {- node->get_output_tensor(i).set_lower_value(node->get_output_tensor(i).get_upper_value());- }- }+/** @brief Upper bound evaluation specific actions. */+struct UpperBound : SingleBound {+ static bool requires_evaluation(const ov::descriptor::Tensor& tensor_desc) {+ return !tensor_desc.get_upper_value();+ }- bool symbols_evaluated = node->evaluate_symbol(output_symbols);- for (size_t i = 0; i < outputs.size(); ++i) {- auto& out_tensor = node->get_output_tensor(i);- if (!out_tensor.get_value_symbol().empty())- continue;- if (symbols_evaluated)- out_tensor.set_value_symbol(output_symbols[i]);- if (outputs[i])- ov::populate_tensor_with_missing_symbols(out_tensor);- }+protected:+ bool evaluate(const ov::Node& node) {+ return node.evaluate_upper(bounds);+ }- for (const auto& input : input_values) {- auto& tensor = input.get_tensor();- bool should_invalidate = invalidate_all_unused_values;- if (tensor.get_lower_value() && shape_size(tensor.get_lower_value().get_shape()) > 10)- should_invalidate |= true;+ void set_bound_at_port(descriptor::Tensor& tensor_desc, const size_t port) const {+ tensor_desc.set_upper_value(bounds[port]);+ if (are_equal(bounds[port], tensor_desc.get_lower_value())) {+ tensor_desc.set_lower_value(bounds[port]);+ }+ }+};- if (tensor.get_upper_value() && shape_size(tensor.get_upper_value().get_shape()) > 10)- should_invalidate |= true;+/** @brief Both bounds evaluation specific actions. */+struct BothBounds {+ static bool requires_evaluation(const descriptor::Tensor& tensor_desc) {+ return !tensor_desc.get_lower_value() || !tensor_desc.get_upper_value();+ }- if (should_invalidate && input.get_target_inputs().size() == 1)- tensor.invalidate_values();- }- propagate_rt_info(node, output);+protected:+ void init(const Node& node) {+ lowers.resize(0);+ uppers.resize(0);++ for (const auto& output : node.outputs()) {+ if (is_type_allocable(output.get_element_type())) {+ lowers.emplace_back(output);+ uppers.emplace_back(output);} else {- break;+ lowers.emplace_back();+ uppers.emplace_back();}}}- if (is_upper)- return output.get_tensor().get_upper_value();- else- return output.get_tensor().get_lower_value();+ void set_same_bound_at_port(descriptor::Tensor& tensor_desc, const size_t port) const {+ tensor_desc.set_lower_value(lowers[port]);+ tensor_desc.set_upper_value(lowers[port]);+ }++ void set_bound_at_port(descriptor::Tensor& tensor_desc, const size_t port) const {+ tensor_desc.set_lower_value(lowers[port]);+ if (are_equal(lowers[port], uppers[port])) {+ tensor_desc.set_upper_value(lowers[port]);+ } else {+ tensor_desc.set_upper_value(uppers[port]);+ }+ }++ bool evaluate(const Node& node) {+ return node.evaluate_lower(lowers) && node.evaluate_upper(uppers);+ }++ ov::TensorVector lowers, uppers;+};++/** @brief Evaluates and sets symbols. */+struct SymbolEvaluator {+ void init() {+ symbols.resize(0);+ }++ void evaluate(const Node& node) {+ symbols.resize(node.get_output_size());+ if (!node.evaluate_symbol(symbols)) {+ symbols.resize(0);+ }+ }++ void set_symbols_at_port(descriptor::Tensor& tensor_desc, const size_t port) const {+ if (!symbols.empty() && tensor_desc.get_value_symbol().empty()) {+ tensor_desc.set_value_symbol(symbols[port]);+ ov::populate_tensor_with_missing_symbols(tensor_desc);+ }+ }++ TensorSymbolVector symbols;+};++/** @brief Evaluator evaluate and set bound depends specified BoundType. */+template <class BoundType>+struct Evaluator : BoundType {+ void init(const Node* n) {+ node = n;+ symbol_evaluator.init();+ BoundType::init(*node);+ }++ void set_bounds_and_symbols() const {+ const auto set_bound =+ node_has_same_inputs(*node) ? &Evaluator::set_same_bound_at_port : &Evaluator::set_bound_at_port;++ for (size_t port = 0; port < node->get_output_size(); ++port) {+ auto& output_tensor_desc = node->get_output_tensor(port);++ (this->*set_bound)(output_tensor_desc, port);+ symbol_evaluator.set_symbols_at_port(output_tensor_desc, port);+ }+ }++ bool evaluate() {+ if (BoundType::evaluate(*node)) {+ symbol_evaluator.evaluate(*node);+ return true;+ } else {+ return false;+ }+ }++ const Node* node;+ SymbolEvaluator symbol_evaluator;+};++/**+ * @brief Evaluate bound(s) algorithm.+ *+ * Calculate required bounds for Node's output.+ *+ * @tparam Evaluator Specify the bound type evaluator.+ * @param output Node's output for which bounds will be calculated.+ */+template <class Evaluator>+void evaluate_bound(const Output<Node>& output) {+ std::vector<Node*> ordered_nodes;+ if (Evaluator::requires_evaluation(output.get_tensor()) && could_propagate(output, ordered_nodes)) {+ auto bound_evaluator = Evaluator{};+ for (const auto& node : ordered_nodes) {+ bound_evaluator.init(node);+ if (!bound_evaluator.evaluate()) {+ break;+ }+ bound_evaluator.set_bounds_and_symbols();+ invalidate_unused_values(node->input_values());+ propagate_rt_info(node, output);+ }+ }}bool default_bound_evaluator(const ov::Node* node,@@ -288,7 +424,11 @@ bool ov::could_propagate(const Output<Node>& output, std::vector<Node*>& result)}}if (can_add) {- result.push_back(node);+ if (is_type<op::v0::Constant>(node)) {+ propagate_rt_info(node, output);+ } else {+ result.push_back(node);+ }nodes_to_do.pop();nodes_done.insert(node);}@@ -300,69 +440,19 @@ bool ov::could_propagate(const Output<Node>& output, std::vector<Node*>& result)}ov::Tensor ov::util::evaluate_lower_bound(const Output<Node>& output) {- return evaluate_bound(output, false);+ evaluate_bound<Evaluator<LowerBound>>(output);+ return output.get_tensor().get_lower_value();}ov::Tensor ov::util::evaluate_upper_bound(const Output<Node>& output) {- return evaluate_bound(output, true);+ evaluate_bound<Evaluator<UpperBound>>(output);+ return output.get_tensor().get_upper_value();}std::pair<ov::Tensor, ov::Tensor> ov::util::evaluate_both_bounds(const Output<Node>& output) {- const auto& output_tensor = output.get_tensor();- if (output_tensor.get_lower_value() && output_tensor.get_upper_value())- return {output_tensor.get_lower_value(), output_tensor.get_upper_value()};- std::vector<Node*> order;- if (could_propagate(output, order)) {- for (const auto& node : order) {- ov::TensorVector outputs_lower, outputs_upper;- for (const auto& out : node->outputs()) {- if (is_type_allocable(out.get_element_type())) {- outputs_lower.emplace_back(out);- outputs_upper.emplace_back(out);- } else {- outputs_lower.emplace_back();- outputs_upper.emplace_back();- }- }- if (!node->evaluate_lower(outputs_lower) || !node->evaluate_upper(outputs_upper)) {- break;- }- auto input_values = node->input_values();- bool same_inputs = std::all_of(input_values.begin(), input_values.end(), [](const Output<Node>& input) {- auto& t = input.get_tensor();- return t.has_and_set_bound() || are_equal(t.get_lower_value(), t.get_upper_value());- });- TensorSymbolVector output_symbols(node->get_output_size());- bool symbols_evaluated = node->evaluate_symbol(output_symbols);- for (size_t i = 0; i < node->get_output_size(); ++i) {- auto& out_tensor = node->get_output_tensor(i);-- out_tensor.set_lower_value(outputs_lower[i]);- if (same_inputs || are_equal(outputs_lower[i], outputs_upper[i])) {- out_tensor.set_upper_value(outputs_lower[i]);- } else {- out_tensor.set_upper_value(outputs_upper[i]);- }-- if (!out_tensor.get_value_symbol().empty())- continue;- if (symbols_evaluated)- out_tensor.set_value_symbol(output_symbols[i]);- ov::populate_tensor_with_missing_symbols(node->get_output_tensor(i));- }- for (const auto& input : node->input_values()) {- auto& tensor = input.get_tensor();- const auto& lower = tensor.get_lower_value();- const auto& upper = tensor.get_upper_value();- const auto should_invalidate =- (lower && shape_size(lower.get_shape()) > 10) || (upper && shape_size(upper.get_shape()) > 10);- if (should_invalidate && input.get_target_inputs().size() == 1)- tensor.invalidate_values();- }- propagate_rt_info(node, output);- }- }- return {output_tensor.get_lower_value(), output_tensor.get_upper_value()};+ evaluate_bound<Evaluator<BothBounds>>(output);+ const auto& output_tensor_desc = output.get_tensor();+ return {output_tensor_desc.get_lower_value(), output_tensor_desc.get_upper_value()};}bool ov::default_lower_bound_evaluator(const Node* node, TensorVector& output_values) {diff --git a/src/core/src/op/constant.cpp b/src/core/src/op/constant.cppindex 9fe2a00c15c..fcf28800ea7 100644--- a/src/core/src/op/constant.cpp+++ b/src/core/src/op/constant.cpp@@ -542,6 +542,11 @@ void Constant::update_identical_flags(bool is_checked, bool identical_value) convoid Constant::validate_and_infer_types() {set_output_type(0, m_element_type, m_shape);+ if (const auto tensor = get_tensor_view()) {+ auto& output_tensor = get_output_tensor(0);+ output_tensor.set_lower_value(tensor);+ output_tensor.set_upper_value(tensor);+ }}bool Constant::visit_attributes(AttributeVisitor& visitor) {@@ -626,7 +631,7 @@ bool Constant::constant_fold(OutputVector&, const OutputVector&) {}const Tensor Constant::get_tensor_view() const {- return m_data ? Tensor{m_element_type, m_shape, m_data->get_ptr(), m_byte_strides} : Tensor{};+ return get_data_ptr() ? Tensor{m_element_type, m_shape, m_data->get_ptr(), m_byte_strides} : Tensor{};}const Strides& Constant::get_strides() const {diff --git a/src/core/tests/constant.cpp b/src/core/tests/constant.cppindex 11eb25188ee..aaf91a4335f 100644--- a/src/core/tests/constant.cpp+++ b/src/core/tests/constant.cpp@@ -1835,6 +1835,7 @@ TEST(constant, float8_e4m3_vector) {ov::op::v0::Constant const_op_from_ptr(ov::element::f8e4m3, data_shape, data_vec.data());EXPECT_EQ(data_vec, const_op_from_ptr.get_vector<ov::float8_e4m3>());+ EXPECT_EQ(const_op_from_ptr.get_strides(), Strides({element::f8e4m3.size()}));}TEST(constant, float8_e5m3_vector) {@@ -1858,6 +1859,7 @@ TEST(constant, float8_e5m3_vector) {ov::op::v0::Constant const_op_from_ptr(ov::element::f8e5m2, data_shape, data_vec.data());EXPECT_EQ(data_vec, const_op_from_ptr.get_vector<ov::float8_e5m2>());+ EXPECT_EQ(const_op_from_ptr.get_strides(), Strides({element::f8e5m2.size()}));}TEST(constant, float8_e8m0_vector) {