Skip to content

Commit

Permalink
[Core] move evaluate_* from ov to ov::util namespace (openvinotoolkit…
Browse files Browse the repository at this point in the history
…#25619)

### Details:
- Align approach -- some of evaluate_* functions already are under
ov::util namespace.
### Tickets:
 - CVS-147171
  • Loading branch information
akuporos authored Jul 19, 2024
1 parent 05ac1b5 commit b26602f
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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());
if (!const_node) {
// Lower bound of the value
auto lb = ov::evaluate_lower_bound(node_out);
auto lb = ov::util::evaluate_lower_bound(node_out);
if (!lb)
return false;
const auto lb_const_node =
Expand All @@ -36,7 +36,7 @@ bool has_valid_pattern(const ov::Output<ov::Node>& node_out) {
return true;

// Upper bound of the value
auto ub = ov::evaluate_upper_bound(node_out);
auto ub = ov::util::evaluate_upper_bound(node_out);
if (!ub)
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pass::AbsSinking::AbsSinking() {
graph_got_changed = true;
}
for (const auto& abs : abs_ops) {
auto bounds = ov::evaluate_both_bounds(abs->input_value(0));
auto bounds = ov::util::evaluate_both_bounds(abs->input_value(0));
if (ov::util::reduce_and(ov::util::greater_equal(bounds.first, 0))) {
replace_output_update_name(abs->output(0), abs->input_value(0));
graph_got_changed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void optimize_value_usage(ov::Output<ov::Node>& output, STS_map& symbol_shape_so
get_alternative_source_from_value_or_shape_source(symbol_shape_source, symbol, output, symbol_value_source);

if (alternative_source.get_node_shared_ptr() != nullptr) {
evaluate_both_bounds(alternative_source);
ov::util::evaluate_both_bounds(alternative_source);
output.replace(alternative_source);
} else {
// in case we can not optimize it -- it is symbol which appeared just now on the value path
Expand Down
3 changes: 3 additions & 0 deletions src/core/dev_api/openvino/core/bound_evaluation_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace ov {
/// \return True if bounds can be propagated for output and order vector has valid data, otherwise false.
OPENVINO_API bool could_propagate(const Output<Node>& output, std::vector<Node*>& order);

namespace util {

/// \brief Evaluates lower value estimation of the output tensor. Traverses graph up to deduce
/// estimation through it.
/// \param Node output pointing to the tensor for estimation.
Expand All @@ -31,4 +33,5 @@ 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);
} // namespace util
} // namespace ov
2 changes: 1 addition & 1 deletion src/core/shape_inference/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ ov::optional<TResult> get_input_bounds(const ov::Node* op, size_t port, const IT
out->reserve(lowers.size());
std::transform(lowers.cbegin(), lowers.cend(), lowers.cbegin(), std::back_inserter(*out), make_bound(et));
} else if (port < op->get_input_size()) {
auto bounds = ov::evaluate_both_bounds(op->get_input_source_output(port));
auto bounds = ov::util::evaluate_both_bounds(op->get_input_source_output(port));

if (bounds.first && bounds.second) {
const auto& et = bounds.first.get_element_type();
Expand Down
16 changes: 8 additions & 8 deletions src/core/src/bound_evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ bool ov::could_propagate(const Output<Node>& output, std::vector<Node*>& result)
return status;
}

ov::Tensor ov::evaluate_lower_bound(const Output<Node>& output) {
ov::Tensor ov::util::evaluate_lower_bound(const Output<Node>& output) {
return evaluate_bound(output, false);
}

ov::Tensor ov::evaluate_upper_bound(const Output<Node>& output) {
ov::Tensor ov::util::evaluate_upper_bound(const Output<Node>& output) {
return evaluate_bound(output, true);
}

std::pair<ov::Tensor, ov::Tensor> ov::evaluate_both_bounds(const Output<Node>& output) {
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()};
Expand Down Expand Up @@ -381,10 +381,10 @@ bool ov::interval_bound_evaluator(const Node* node,
OPENVINO_ASSERT(node->get_input_size() == 2);

const auto num_of_outputs = node->get_output_size();
auto low_0 = ov::evaluate_lower_bound(node->get_input_source_output(0));
auto low_1 = ov::evaluate_lower_bound(node->get_input_source_output(1));
auto up_0 = ov::evaluate_upper_bound(node->get_input_source_output(0));
auto up_1 = ov::evaluate_upper_bound(node->get_input_source_output(1));
auto low_0 = ov::util::evaluate_lower_bound(node->get_input_source_output(0));
auto low_1 = ov::util::evaluate_lower_bound(node->get_input_source_output(1));
auto up_0 = ov::util::evaluate_upper_bound(node->get_input_source_output(0));
auto up_1 = ov::util::evaluate_upper_bound(node->get_input_source_output(1));
if (!low_0 || !low_1 || !up_0 || !up_1)
return false;

Expand Down Expand Up @@ -534,7 +534,7 @@ bool ov::has_and_set_equal_bounds(const Output<Node>& source) {
if (op::util::is_constant(source.get_node_shared_ptr()))
return true;

auto bounds = ov::evaluate_both_bounds(source);
auto bounds = ov::util::evaluate_both_bounds(source);
return are_same_tensor(bounds.first, bounds.second);
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/src/op/divide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ bool evaluate_bound(const Node* node, TensorVector& output_values, bool is_upper
OPENVINO_ASSERT(PartialShape::broadcast_merge_into(input_shape, input2.get_partial_shape(), node->get_autob()),
"Argument shapes in divide operation are inconsistent.");

const auto input1_low = evaluate_lower_bound(input1);
const auto input1_low = ov::util::evaluate_lower_bound(input1);
if (!input1_low)
return false;
const auto input1_up = evaluate_upper_bound(input1);
const auto input1_up = ov::util::evaluate_upper_bound(input1);
if (!input1_up)
return false;
const auto input2_low = evaluate_lower_bound(input2);
const auto input2_low = ov::util::evaluate_lower_bound(input2);
if (!input2_low)
return false;
const auto input2_up = evaluate_upper_bound(input2);
const auto input2_up = ov::util::evaluate_upper_bound(input2);
if (!input2_up)
return false;

Expand Down
4 changes: 2 additions & 2 deletions src/core/src/op/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ namespace {
* @return Vector with inputs bounds tensors.
*/
TensorVector get_bounds(const Node* const op) {
auto&& v_bounds = ov::evaluate_both_bounds(op->input_value(0));
auto&& m_bounds = ov::evaluate_both_bounds(op->input_value(1));
auto&& v_bounds = ov::util::evaluate_both_bounds(op->input_value(0));
auto&& m_bounds = ov::util::evaluate_both_bounds(op->input_value(1));
return {std::move(v_bounds.first),
std::move(v_bounds.second),
std::move(m_bounds.first),
Expand Down
4 changes: 2 additions & 2 deletions src/core/tests/bound_evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ TEST_F(EvaluateBoundTest, no_exception_when_node_has_output_with_dynamic_rank) {
fn_op->set_output_type(1, element::i32, PartialShape{{1, 4}});
fn_op->validate_and_infer_types();

EXPECT_NO_THROW(evaluate_both_bounds(fn_op));
EXPECT_NO_THROW(ov::util::evaluate_both_bounds(fn_op));
}

TEST_F(EvaluateBoundTest, no_exception_when_node_has_output_with_dynamic_element_type) {
fn_op->set_output_type(0, element::dynamic, PartialShape{4});
fn_op->set_output_type(1, element::dynamic, PartialShape{4});
fn_op->validate_and_infer_types();

EXPECT_NO_THROW(evaluate_both_bounds(fn_op));
EXPECT_NO_THROW(ov::util::evaluate_both_bounds(fn_op));
}

using BoundEvaluatorTest = ::testing::Test;
Expand Down

0 comments on commit b26602f

Please sign in to comment.