Skip to content

Commit

Permalink
[Core] Remove ngraph/evaluator.hpp (openvinotoolkit#22576)
Browse files Browse the repository at this point in the history
* Move evaluator into src

* Move Evaluator into ov::

* Remove unsued local functions

which cause CI fails
  • Loading branch information
t-jankowski authored Feb 1, 2024
1 parent b20bc81 commit d161cfb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 194 deletions.
9 changes: 0 additions & 9 deletions src/core/include/ngraph/validation_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ ov::PartialShape infer_slice_shape(const Node* node,
const ov::AxisSet& shrink_axis_mask,
const ov::AxisSet& ellipsis_mask);

/// \brief Try to compute the maximum value of value
/// \return (true, max_value) if can be determined, or (false, numeric_limits<uint64_t>::max())
/// if not.
/// \deprecated Use evaluate_upper_bound instead
OPENVINO_DEPRECATED("The nGraph API is deprecated and will be removed in the 2024.0 release. "
"For instructions on transitioning to the new API, please refer to "
"https://docs.openvino.ai/latest/openvino_2_0_transition_guide.html")
OPENVINO_API std::pair<bool, uint64_t> maximum_value(const ov::Output<Node>& value);

/// \brief Returns a Constant storing scalar value equal to std::numeric_limits<t>::max()
OPENVINO_DEPRECATED("The nGraph API is deprecated and will be removed in the 2024.0 release. "
"For instructions on transitioning to the new API, please refer to "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,31 @@

#pragma once

#if !defined(IN_OV_COMPONENT) && !defined(NGRAPH_LEGACY_HEADER_INCLUDED)
# define NGRAPH_LEGACY_HEADER_INCLUDED
# ifdef _MSC_VER
# pragma message( \
"The nGraph API is deprecated and will be removed in the 2024.0 release. For instructions on transitioning to the new API, please refer to https://docs.openvino.ai/latest/openvino_2_0_transition_guide.html")
# else
# warning("The nGraph API is deprecated and will be removed in the 2024.0 release. For instructions on transitioning to the new API, please refer to https://docs.openvino.ai/latest/openvino_2_0_transition_guide.html")
# endif
#endif

#include <map>
#include <stack>
#include <utility>

#include "ngraph/shape.hpp"
#include "openvino/core/deprecated.hpp"
#include "openvino/core/node.hpp"
#include "openvino/core/type/element_type_traits.hpp"

namespace ngraph {
namespace ov {
/// \brief Execute handlers on a subgraph to compute values
///
///
template <typename V>
class OPENVINO_DEPRECATED("The nGraph API is deprecated and will be removed in the 2024.0 release. "
"For instructions on transitioning to the new API, please refer to "
"https://docs.openvino.ai/latest/openvino_2_0_transition_guide.html") Evaluator {
OPENVINO_SUPPRESS_DEPRECATED_START
class Evaluator {
public:
/// \brief values we compute for outputs
using value_map = std::map<ov::RawNodeOutput, V>;
using value_map = std::map<RawNodeOutput, V>;

/// \brief Handler for a computation of a value about an op
///
/// A handler is passed a Node* and a vector of computed input values. The handler should
/// return a vector of computed output values.
using op_handler = std::function<std::vector<V>(ov::Node* op, std::vector<V>& inputs)>;
using op_handler = std::function<std::vector<V>(Node* op, std::vector<V>& inputs)>;

/// \brief Table of ops with handlers
using op_handler_map = std::map<ov::Node::type_info_t, op_handler>;
using op_handler_map = std::map<Node::type_info_t, op_handler>;

/// \brief construct handler using the provided op handlers.
///
Expand Down Expand Up @@ -80,7 +65,7 @@ class OPENVINO_DEPRECATED("The nGraph API is deprecated and will be removed in t
}

protected:
op_handler get_handler(ov::Node* node) {
op_handler get_handler(Node* node) {
op_handler handler = m_universal_handler;
if (!handler) {
auto it = m_handlers.find(node->get_type_info());
Expand All @@ -100,27 +85,27 @@ class OPENVINO_DEPRECATED("The nGraph API is deprecated and will be removed in t
/// \brief Intstructions for evaluations state machine
class Inst {
protected:
Inst(ov::Node* node) : m_node(node) {}
Inst(Node* node) : m_node(node) {}

public:
virtual ~Inst() {}
virtual void handle(Evaluator& evaluator, InstStack& inst_stack, ov::Node* node) = 0;
ov::Node* get_node() {
virtual void handle(Evaluator& evaluator, InstStack& inst_stack, Node* node) = 0;
Node* get_node() {
return m_node;
}

protected:
ov::Node* m_node;
Node* m_node;
};

/// \brief Ensure value has been analyzed
class ValueInst : public Inst {
public:
ValueInst(const ov::Output<ov::Node>& value) : Inst(value.get_node()), m_index(value.get_index()) {}
ValueInst(const Output<Node>& value) : Inst(value.get_node()), m_index(value.get_index()) {}

ValueInst(const ov::RawNodeOutput& value) : Inst(value.node), m_index(value.index) {}
ValueInst(const RawNodeOutput& value) : Inst(value.node), m_index(value.index) {}

void handle(Evaluator& evaluator, InstStack& inst_stack, ov::Node* node) override {
void handle(Evaluator& evaluator, InstStack& inst_stack, Node* node) override {
// Request to analyze this value if we can
if (auto handler = evaluator.get_handler(node)) {
// Ensure the inputs are processed and then execute the op handler
Expand All @@ -143,9 +128,9 @@ class OPENVINO_DEPRECATED("The nGraph API is deprecated and will be removed in t
/// \brief All arguments have been handled; execute the node handler
class ExecuteInst : public Inst {
public:
ExecuteInst(ov::Node* node, op_handler& handler) : Inst(node), m_handler(handler) {}
ExecuteInst(Node* node, op_handler& handler) : Inst(node), m_handler(handler) {}

void handle(Evaluator& evaluator, InstStack& inst_stack, ov::Node* node) override {
void handle(Evaluator& evaluator, InstStack& inst_stack, Node* node) override {
// Request to execute the handleer. Pass what we know about the inputs to the
// handler and associate the results with the outputs
std::vector<V> inputs;
Expand All @@ -164,7 +149,7 @@ class OPENVINO_DEPRECATED("The nGraph API is deprecated and will be removed in t

public:
/// \brief Determine information about value
V evaluate(const ov::Output<ov::Node>& value) {
V evaluate(const Output<Node>& value) {
InstStack inst_stack;
inst_stack.push(InstPtr(new ValueInst(value)));
while (!inst_stack.empty()) {
Expand All @@ -186,6 +171,5 @@ class OPENVINO_DEPRECATED("The nGraph API is deprecated and will be removed in t
op_handler_map m_handlers;
op_handler m_default_handler;
value_map& m_value_map;
OPENVINO_SUPPRESS_DEPRECATED_END
};
} // namespace ngraph
} // namespace ov
6 changes: 2 additions & 4 deletions src/core/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include <string>
#include <unordered_map>

#include "evaluator.hpp"
#include "itt.hpp"
#include "layout_utils.hpp"
#include "ngraph/evaluator.hpp"
#include "openvino/core/attribute_visitor.hpp"
#include "openvino/core/except.hpp"
#include "openvino/core/meta_data.hpp"
Expand Down Expand Up @@ -521,8 +521,7 @@ bool ov::Model::evaluate(ov::TensorVector& output_tensors,
outputs.push_back(m_sink);
}
// evaluate nodes
OPENVINO_SUPPRESS_DEPRECATED_START
ngraph::Evaluator<ov::Tensor> evaluator({}, value_map);
Evaluator<Tensor> evaluator({}, value_map);
evaluator.set_universal_handler(
[&output_tensor_map, &evaluation_context](Node* node,
const ov::TensorVector& input_tensors) -> ov::TensorVector {
Expand Down Expand Up @@ -551,7 +550,6 @@ bool ov::Model::evaluate(ov::TensorVector& output_tensors,
for (const auto& value : outputs) {
evaluator.evaluate(value);
}
OPENVINO_SUPPRESS_DEPRECATED_END
for (size_t i = 0; i < m_results.size(); ++i) {
auto result = m_results.at(i)->output(0);
output_tensors.at(i) = output_tensor_map[result];
Expand Down
148 changes: 0 additions & 148 deletions src/core/src/validation_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "bound_evaluate.hpp"
#include "compare.hpp"
#include "ngraph/evaluator.hpp"
#include "openvino/core/dimension_tracker.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/gather.hpp"
Expand Down Expand Up @@ -526,155 +525,8 @@ struct MaxValue {
std::vector<uint64_t> m_slices;
int64_t m_slice_axis{-1};
};

std::vector<MaxValue> exec_constant(Node* node, std::vector<MaxValue>& inputs) {
auto result = MaxValue();
auto op = ov::as_type<ov::op::v0::Constant>(node);
auto element_type = op->get_output_element_type(0);
if (element_type.is_integral()) {
uint64_t max_val = 0;
if (element_type.is_signed()) {
for (auto elt : op->cast_vector<int64_t>()) {
if (max_val < static_cast<uint64_t>(elt)) {
max_val = elt;
}
}
} else {
for (auto elt : op->cast_vector<uint64_t>()) {
if (max_val < elt) {
max_val = elt;
}
}
}
result = MaxValue(max_val);
}
return {result};
}

std::vector<MaxValue> exec_minimum(Node* node, std::vector<MaxValue>& inputs) {
uint64_t min_value = std::numeric_limits<uint64_t>::max();
switch (node->get_output_element_type(0)) {
case element::Type_t::i8:
min_value = std::numeric_limits<int8_t>::max();
break;
case element::Type_t::i16:
min_value = std::numeric_limits<int16_t>::max();
break;
case element::Type_t::i32:
min_value = std::numeric_limits<int32_t>::max();
break;
case element::Type_t::i64:
min_value = std::numeric_limits<int64_t>::max();
break;
case element::Type_t::u8:
min_value = std::numeric_limits<uint8_t>::max();
break;
case element::Type_t::u16:
min_value = std::numeric_limits<uint16_t>::max();
break;
case element::Type_t::u32:
min_value = std::numeric_limits<uint32_t>::max();
break;
case element::Type_t::u64:
min_value = std::numeric_limits<uint64_t>::max();
break;
default:
break;
}
min_value = std::min(min_value, inputs.at(0).m_value);
min_value = std::min(min_value, inputs.at(1).m_value);
return {MaxValue(min_value)};
}

std::vector<MaxValue> exec_concat(Node* node, std::vector<MaxValue>& inputs) {
auto op = ov::as_type<ov::op::v0::Concat>(node);
std::vector<uint64_t> slice_maxen;
for (const auto& input : inputs) {
slice_maxen.push_back(input.m_value);
}
auto axis = op->get_concatenation_axis();
return {MaxValue(slice_maxen, axis)};
}

std::vector<MaxValue> exec_reduce_min(Node* node, std::vector<MaxValue>& inputs) {
auto data = inputs.at(0);
if (data.m_slice_axis >= 0 && data.m_slices.size() > 1) {
if (auto indices_const = ov::as_type<op::v0::Constant>(node->get_input_node_ptr(1))) {
if (indices_const->get_output_element_type(0).is_integral()) {
const auto& indices_shape = indices_const->get_output_shape(0);
if (indices_shape == Shape{1}) {
auto indices = indices_const->cast_vector<int64_t>();
auto axis = indices.at(0);
if (axis == data.m_slice_axis) {
return {MaxValue(*min_element(data.m_slices.begin(), data.m_slices.end()))};
}
}
}
}
}
// Noting we can do
return {MaxValue(data.m_value)};
}

std::vector<MaxValue> exec_shape_of(Node* node, std::vector<MaxValue>& inputs) {
const auto& inputPS = node->get_input_partial_shape(0);
std::vector<uint64_t> shapeDims;
for (int64_t i = 0; i < inputPS.rank().get_length(); i++) {
if (inputPS[i].is_static()) {
shapeDims.push_back(inputPS[i].get_length());
} else {
shapeDims.push_back(std::numeric_limits<uint64_t>::max());
}
}

return {MaxValue(shapeDims, 0)};
}

std::vector<MaxValue> exec_gather(Node* node, std::vector<MaxValue>& inputs) {
auto gather = ov::as_type<ov::op::v1::Gather>(node);

const auto& indices = ov::as_type_ptr<op::v0::Constant>(node->input_value(1).get_node_shared_ptr());
const auto& axis = ov::as_type_ptr<op::v0::Constant>(node->input_value(2).get_node_shared_ptr());

if (!indices || !axis) {
return {MaxValue()};
}

if (gather->get_axis() != 0) {
return {MaxValue()};
}

const auto& indicesVec = indices->cast_vector<int64_t>();
if (indicesVec.size() != 1 || indicesVec[0] >= static_cast<int64_t>(inputs[0].m_slices.size())) {
return {MaxValue()};
}

return {MaxValue(inputs[0].m_slices[indicesVec[0]])};
}

std::vector<MaxValue> exec_nop(Node* node, std::vector<MaxValue>& inputs) {
return {inputs.at(0)};
}
} // namespace

std::pair<bool, uint64_t> maximum_value(const ov::Output<Node>& value) {
static ngraph::Evaluator<MaxValue>::op_handler_map handlers = {
{ov::op::v0::Concat::get_type_info_static(), exec_concat},
{ov::op::v0::Constant::get_type_info_static(), exec_constant},
{ov::op::v0::Convert::get_type_info_static(), exec_nop},
{ov::op::v1::Gather::get_type_info_static(), exec_gather},
{ov::op::v1::Minimum::get_type_info_static(), exec_minimum},
{ov::op::v1::ReduceMin::get_type_info_static(), exec_reduce_min},
{ov::op::v1::Reshape::get_type_info_static(), exec_nop},
{ov::op::v3::ShapeOf::get_type_info_static(), exec_shape_of},
{ov::op::v0::Squeeze::get_type_info_static(), exec_nop},
{ov::op::v0::Unsqueeze::get_type_info_static(), exec_nop}};
Evaluator<MaxValue>::value_map value_map;
Evaluator<MaxValue> evaluator(handlers, value_map);
auto val = evaluator.evaluate(value);
return std::pair<bool, uint64_t>(val.m_value < std::numeric_limits<uint64_t>::max(), val.m_value);
}

std::shared_ptr<op::v0::Constant> get_constant_max_of_type(element::Type_t t) {
auto tensor = ov::util::make_tensor_of_max_value(t);
return tensor ? std::make_shared<op::v0::Constant>(tensor) : nullptr;
Expand Down

0 comments on commit d161cfb

Please sign in to comment.