diff --git a/src/core/include/ngraph/opsets/opset.hpp b/src/core/include/ngraph/opsets/opset.hpp index 443de5714e4..26c21e237b1 100644 --- a/src/core/include/ngraph/opsets/opset.hpp +++ b/src/core/include/ngraph/opsets/opset.hpp @@ -67,6 +67,8 @@ const NGRAPH_API OpSet& get_opset8(); const NGRAPH_API OpSet& get_opset9(); const NGRAPH_API OpSet& get_opset10(); const NGRAPH_API OpSet& get_opset11(); +const NGRAPH_API OpSet& get_opset12(); +const NGRAPH_API OpSet& get_opset13(); const NGRAPH_API std::map>& get_available_opsets(); } // namespace ngraph NGRAPH_SUPPRESS_DEPRECATED_END diff --git a/src/core/src/graph_util.cpp b/src/core/src/graph_util.cpp index 93457bd1708..8001678dab2 100644 --- a/src/core/src/graph_util.cpp +++ b/src/core/src/graph_util.cpp @@ -10,30 +10,17 @@ #include #include -#include "ngraph/descriptor/input.hpp" -#include "ngraph/descriptor/output.hpp" -#include "ngraph/function.hpp" -#include "ngraph/log.hpp" -#include "ngraph/node.hpp" -#include "ngraph/op/broadcast.hpp" -#include "ngraph/op/constant.hpp" -#include "ngraph/op/parameter.hpp" -#include "ngraph/op/result.hpp" -#include "ngraph/op/tensor_iterator.hpp" -#include "ngraph/op/util/op_types.hpp" -#include "ngraph/opsets/opset5.hpp" -#include "ngraph/opsets/opset8.hpp" -#include "ngraph/pass/manager.hpp" -#include "ngraph/pass/visualize_tree.hpp" -#include "ngraph/rt_info.hpp" -#include "ngraph/util.hpp" #include "openvino/core/descriptor/tensor.hpp" +#include "openvino/core/rt_info.hpp" +#include "openvino/op/broadcast.hpp" +#include "openvino/op/constant.hpp" +#include "openvino/op/util/op_types.hpp" +#include "openvino/pass/manager.hpp" +#include "openvino/pass/visualize_tree.hpp" #include "transformations/common_optimizations/compress_float_constants.hpp" #include "transformations/common_optimizations/fused_names_cleanup.hpp" #include "transformations/common_optimizations/mark_precision_sensitive_shapeof_subgraphs.hpp" -using namespace std; - namespace { void clone_ov_nodes(const std::vector>& nodes, @@ -49,7 +36,7 @@ void clone_ov_nodes(const std::vector>& nodes, } std::vector> cloned_dependencies; for (const auto& dependency : node->get_control_dependencies()) { - shared_ptr& dependent = node_map.at(dependency.get()); + std::shared_ptr& dependent = node_map.at(dependency.get()); if (find(cloned_dependencies.begin(), cloned_dependencies.end(), dependent) == cloned_dependencies.end()) { cloned_dependencies.push_back(dependent); @@ -161,7 +148,7 @@ void replace_node(const std::shared_ptr& target, const OutputVector& repla OPENVINO_ASSERT(target->get_output_size() == replacement_values.size()); - unordered_set> replacement_nodes; + std::unordered_set> replacement_nodes; // For each of target's output O with replacement output O_rep: // For each O's connected downstream input I: // Change I's connected upstream output to O_rep @@ -179,15 +166,15 @@ void replace_node(const std::shared_ptr& target, const OutputVector& repla } void replace_node(const std::shared_ptr& target, const std::shared_ptr& replacement) { - auto default_output_order = vector(target->get_output_size()); + auto default_output_order = std::vector(target->get_output_size()); std::iota(default_output_order.begin(), default_output_order.end(), 0); replace_node(target, replacement, default_output_order); } void replace_nodes(const std::shared_ptr& f, - const unordered_map, shared_ptr>& - parameter_replacement_map, - const unordered_map, shared_ptr>& body_replacement_map) { + const std::unordered_map, + std::shared_ptr>& parameter_replacement_map, + const std::unordered_map, std::shared_ptr>& body_replacement_map) { auto& params = f->get_parameters(); for (size_t i = 0; i < params.size(); i++) { @@ -231,7 +218,7 @@ std::shared_ptr clone_ov_model(const Model& func, std::unordered_map node : func.get_results()) { + for (std::shared_ptr node : func.get_results()) { auto result = ov::as_type_ptr(node_map.at(node.get())); if (!result) { OPENVINO_THROW("Results should be of type op::Result"); @@ -240,7 +227,7 @@ std::shared_ptr clone_ov_model(const Model& func, std::unordered_map(node_map.at(node.get()))); + cloned_sinks.push_back(std::static_pointer_cast(node_map.at(node.get()))); } std::vector> cloned_params; @@ -273,8 +260,8 @@ bool compare_constants(const std::shared_ptr& n1, const std::shared_ptr(n1)->get_value_strings() != - static_pointer_cast(n2)->get_value_strings()) { + if (std::static_pointer_cast(n1)->get_value_strings() != + std::static_pointer_cast(n2)->get_value_strings()) { return false; } @@ -368,7 +355,8 @@ void save_model(const std::shared_ptr& m, const std::string& ou OPENVINO_SUPPRESS_DEPRECATED_START -ngraph::NodeVector ngraph::find_common_args(std::shared_ptr node1, std::shared_ptr node2) { +namespace ngraph { +ov::NodeVector find_common_args(std::shared_ptr node1, std::shared_ptr node2) { std::unordered_set> node1_args; auto compute_node1_args = [&node1_args](const std::shared_ptr& node) { @@ -396,15 +384,15 @@ ngraph::NodeVector ngraph::find_common_args(std::shared_ptr node1, std::sh } // Check if all paths from X to a result go through Y -bool ngraph::is_post_dominated(Node* X, Node* Y) { +bool is_post_dominated(Node* X, Node* Y) { std::unordered_set visited; std::stack> stack; stack.push(X); while (stack.size() > 0) { - ngraph::Node* curr = stack.top(); + ov::Node* curr = stack.top(); visited.insert(curr); - if (ngraph::op::is_output(curr)) { + if (ov::op::util::is_output(curr)) { return false; } stack.pop(); @@ -419,8 +407,8 @@ bool ngraph::is_post_dominated(Node* X, Node* Y) { return true; } -std::vector> ngraph::clone_nodes(const std::vector>& nodes, - NodeMap& node_map) { +std::vector> clone_nodes(const std::vector>& nodes, + NodeMap& node_map) { // for each node in topological order auto sorted_nodes = topological_sort(nodes); for (const auto& node : sorted_nodes) { @@ -433,7 +421,7 @@ std::vector> ngraph::clone_nodes(const std::vector } std::vector> cloned_dependencies; for (auto& dependency : node->get_control_dependencies()) { - shared_ptr& dependent = node_map.at(dependency.get()); + std::shared_ptr& dependent = node_map.at(dependency.get()); if (find(cloned_dependencies.begin(), cloned_dependencies.end(), dependent) == cloned_dependencies.end()) { cloned_dependencies.push_back(dependent); @@ -463,18 +451,18 @@ std::vector> ngraph::clone_nodes(const std::vector // create and return vector of cloned nodes // order matches input vector (not necessarily topological) - std::vector> cloned_nodes; + std::vector> cloned_nodes; for (const auto& node : nodes) { cloned_nodes.push_back(node_map.at(node.get())); } return cloned_nodes; } -std::list> ngraph::clone_nodes(const std::vector>& nodes, - RawNodeOutputMap& output_map) { +std::list> clone_nodes(const std::vector>& nodes, + RawNodeOutputMap& output_map) { // for each node in topological order auto sorted_nodes = topological_sort(nodes); - std::list> cloned_nodes; + std::list> cloned_nodes; for (const auto& node : sorted_nodes) { auto node_outputs = node->outputs(); for (const auto& value : node_outputs) { @@ -488,7 +476,7 @@ std::list> ngraph::clone_nodes(const std::vectorget_control_dependencies()) { for (const auto& dependency_value : dependency->outputs()) { - shared_ptr dependent = output_map.at(dependency_value).get_node_shared_ptr(); + std::shared_ptr dependent = output_map.at(dependency_value).get_node_shared_ptr(); if (find(cloned_dependencies.begin(), cloned_dependencies.end(), dependent) == cloned_dependencies.end()) { cloned_dependencies.push_back(dependent); @@ -514,8 +502,8 @@ std::list> ngraph::clone_nodes(const std::vector& reduce_constant) { - if (auto rc = ov::as_type_ptr(reduce_constant.get_node_shared_ptr())) { +bool is_equal_to_const_value(const std::string& const_value, const Output& reduce_constant) { + if (auto rc = ov::as_type_ptr(reduce_constant.get_node_shared_ptr())) { return (rc->get_all_data_elements_bitwise_identical() && rc->convert_value_to_string(0) == const_value); } else { return false; @@ -535,28 +523,28 @@ bool ngraph::is_equal_to_const_value(const std::string& const_value, const Outpu // | +------[2]------> | | | +------[6]------> | | +------[10]-----> | // | <------[3]------+ | | | <------[7]------+ | | <------[11]-----+ | // +-----+ +-----+ | +-----+ +-----+ +-----+ +-----+ -pair, shared_ptr> ngraph::insert_result_parameter_split( - const shared_ptr& src_node, - const shared_ptr& dst_node) { +std::pair, std::shared_ptr> insert_result_parameter_split( + const std::shared_ptr& src_node, + const std::shared_ptr& dst_node) { if (src_node->get_output_size() != 1) { OPENVINO_THROW("Multiple output per op not supported in graph partition yet."); } // Make parameter node - shared_ptr par_node = - make_shared(src_node->get_output_element_type(0), src_node->get_output_shape(0)); + std::shared_ptr par_node = + std::make_shared(src_node->get_output_element_type(0), src_node->get_output_shape(0)); // Fix input / output among src, dst and par std::vector> dst_inputs = get_inputs_from(*src_node, *dst_node); - NGRAPH_CHECK(dst_inputs.size() == 1, - "insert_result_parameter_split encountered more than " - "one input between the source and destination nodes"); + OPENVINO_ASSERT(dst_inputs.size() == 1, + "insert_result_parameter_split encountered more than " + "one input between the source and destination nodes"); auto& dst_input = dst_inputs[0]; std::vector> src_outputs = get_outputs_to(*src_node, *dst_node); - NGRAPH_CHECK(src_outputs.size() == 1, - "insert_result_parameter_split encountered more than " - "one output between the source and destination nodes"); + OPENVINO_ASSERT(src_outputs.size() == 1, + "insert_result_parameter_split encountered more than " + "one output between the source and destination nodes"); auto& src_output = src_outputs[0]; // Remove [0] @@ -567,7 +555,7 @@ pair, shared_ptr> ngraph:: // Add res node // Add [4], [5], [6], [7] - shared_ptr res_node = make_shared(src_node); + std::shared_ptr res_node = std::make_shared(src_node); return make_pair(res_node, par_node); } @@ -612,58 +600,59 @@ pair, shared_ptr> ngraph:: // Typically new_node is connected to src_node already. The reason we don't create `new_node` // inside the function and return it (similar to ngraph::insert_result_parameter_split) is that // we'll have to templatize its function to call new_node's constructor. -void ngraph::insert_new_node_between(const shared_ptr& src_node, - const shared_ptr& dst_node, - const shared_ptr& new_node) { +void insert_new_node_between(const std::shared_ptr& src_node, + const std::shared_ptr& dst_node, + const std::shared_ptr& new_node) { // Fix input / output std::vector> dst_inputs = get_inputs_from(*src_node, *dst_node); - NGRAPH_CHECK(dst_inputs.size() == 1, - "insert_new_node_between encountered more than one " - "input between the source and destination nodes"); + OPENVINO_ASSERT(dst_inputs.size() == 1, + "insert_new_node_between encountered more than one " + "input between the source and destination nodes"); auto& dst_input = dst_inputs[0]; std::vector> src_outputs = get_outputs_to(*src_node, *dst_node); - NGRAPH_CHECK(src_outputs.size() == 1, - "insert_new_node_between encountered more than one " - "output between the source and destination nodes"); + OPENVINO_ASSERT(src_outputs.size() == 1, + "insert_new_node_between encountered more than one " + "output between the source and destination nodes"); auto& src_output = src_outputs[0]; src_output.remove_target_input(dst_input); // Remove [0] dst_input.replace_source_output(new_node->output(0)); // Remove [0] (again), add [8], remove [1], add [9] } -std::shared_ptr ngraph::make_zero(const element::Type& element_type, const Shape& shape) { - auto zero = op::Constant::create(element_type, Shape{}, {0.0}); +std::shared_ptr make_zero(const element::Type& element_type, const Shape& shape) { + auto zero = ov::op::v0::Constant::create(element_type, Shape{}, {0.0}); if (shape.size() > 0) { - return std::make_shared(zero, - op::Constant::create(element::u64, Shape{shape.size()}, shape)); + return std::make_shared( + zero, + op::v0::Constant::create(element::u64, Shape{shape.size()}, shape)); } return zero; } -std::shared_ptr ngraph::make_constant_from_string(std::string val, - const element::Type& element_type, - const Shape& shape) { +std::shared_ptr make_constant_from_string(std::string val, + const element::Type& element_type, + const Shape& shape) { auto cvals = std::vector(shape_size(shape), val); - return std::make_shared(element_type, shape, cvals); + return std::make_shared(element_type, shape, cvals); } -bool ngraph::is_zero(const Output& reduce_constant) { +bool is_zero(const Output& reduce_constant) { auto result_bool = is_equal_to_const_value("0", reduce_constant); return result_bool; } -bool ngraph::is_one(const Output& reduce_constant) { +bool is_one(const Output& reduce_constant) { auto result_bool = is_equal_to_const_value("1", reduce_constant); return result_bool; } -ngraph::NodeVector ngraph::get_subgraph_outputs(const NodeVector& nodes, - const NodeVector& exclusions, - bool ignore_unused, - bool ignore_output_duplicates) { - std::set> exclusions_set(exclusions.begin(), exclusions.end()); - std::set> nodes_set(nodes.begin(), nodes.end()); +ov::NodeVector get_subgraph_outputs(const NodeVector& nodes, + const NodeVector& exclusions, + bool ignore_unused, + bool ignore_output_duplicates) { + std::set> exclusions_set(exclusions.begin(), exclusions.end()); + std::set> nodes_set(nodes.begin(), nodes.end()); NodeVector outputs; @@ -684,7 +673,7 @@ ngraph::NodeVector ngraph::get_subgraph_outputs(const NodeVector& nodes, return outputs; } -ngraph::NodeVector ngraph::extract_subgraph(const NodeVector& results, const NodeVector& args) { +ov::NodeVector extract_subgraph(const NodeVector& results, const NodeVector& args) { NodeVector subgraph; traverse_nodes( results, @@ -695,15 +684,15 @@ ngraph::NodeVector ngraph::extract_subgraph(const NodeVector& results, const Nod return subgraph; } -bool ngraph::is_used(Node* node) { +bool is_used(Node* node) { std::unordered_set instances_seen; std::stack> stack; stack.push(node); while (stack.size() > 0) { - ngraph::Node* n = stack.top(); + ov::Node* n = stack.top(); if (instances_seen.count(n) == 0) { - if (ngraph::op::is_output(n)) { + if (ov::op::util::is_output(n)) { return true; } instances_seen.insert(n); @@ -718,7 +707,7 @@ bool ngraph::is_used(Node* node) { return false; } -size_t ngraph::get_user_count(Node* node) { +size_t get_user_count(Node* node) { size_t count = 0; for (const auto& node_user : node->get_users()) { count += is_used(node_user.get()); @@ -726,13 +715,13 @@ size_t ngraph::get_user_count(Node* node) { return count; } -bool ngraph::is_strided(const Strides& strides) { +bool is_strided(const Strides& strides) { return std::any_of(strides.begin(), strides.end(), [](size_t stride) { return stride != 1; }); } -bool ngraph::is_valid_rank(const std::shared_ptr& node, std::vector valid_ranks) { +bool is_valid_rank(const std::shared_ptr& node, std::vector valid_ranks) { auto node_rank = node->get_shape().size(); for (auto rank : valid_ranks) { if (rank == node_rank) { @@ -742,15 +731,15 @@ bool ngraph::is_valid_rank(const std::shared_ptr& node, std::vector f, - const std::string& filename, - std::function& attributes)> attributes) { - ngraph::pass::Manager pass_manager; - pass_manager.register_pass(filename, attributes); +void plot_graph(std::shared_ptr f, + const std::string& filename, + std::function& attributes)> attributes) { + ov::pass::Manager pass_manager; + pass_manager.register_pass(filename, attributes); pass_manager.run_passes(std::move(f)); } -std::vector> ngraph::get_inputs_from(Node& src, Node& dst) { +std::vector> get_inputs_from(Node& src, Node& dst) { std::vector> result; for (auto& input : dst.inputs()) { @@ -762,7 +751,7 @@ std::vector> ngraph::get_inputs_from(Node& src, Node return result; } -std::vector> ngraph::get_outputs_to(Node& src, Node& dst) { +std::vector> get_outputs_to(Node& src, Node& dst) { std::vector> result; for (auto& output : src.outputs()) { @@ -783,10 +772,10 @@ std::vector> ngraph::get_outputs_to(Node& src, Node return result; } -static bool check_for_cycles_bkwd(const std::shared_ptr& node, - std::deque>& path, - std::unordered_set>& path_set, - ngraph::NodeVector& cycle_nodes) { +static bool check_for_cycles_bkwd(const std::shared_ptr& node, + std::deque>& path, + std::unordered_set>& path_set, + ov::NodeVector& cycle_nodes) { path.push_back(node); path_set.insert(node); for (size_t i = 0; i < node->inputs().size(); i++) { @@ -808,10 +797,10 @@ static bool check_for_cycles_bkwd(const std::shared_ptr& node, return false; } -static bool check_for_cycles_fwd(const std::shared_ptr& node, - std::deque>& path, - std::unordered_set>& path_set, - ngraph::NodeVector& cycle_nodes) { +static bool check_for_cycles_fwd(const std::shared_ptr& node, + std::deque>& path, + std::unordered_set>& path_set, + ov::NodeVector& cycle_nodes) { path.push_back(node); path_set.insert(node); for (auto& arg : node->get_users()) { @@ -832,7 +821,7 @@ static bool check_for_cycles_fwd(const std::shared_ptr& node, return false; } -bool ngraph::check_for_cycles(const ngraph::Function* func, ngraph::NodeVector& cycle_nodes, bool& is_bkwd_cycle) { +bool check_for_cycles(const ov::Model* func, ov::NodeVector& cycle_nodes, bool& is_bkwd_cycle) { for (const auto& res : func->get_results()) { std::deque> path; // mirror of path stack for faster cycle check @@ -865,3 +854,5 @@ bool ngraph::check_for_cycles(const ngraph::Function* func, ngraph::NodeVector& // no cycles return false; } + +} // namespace ngraph diff --git a/src/core/src/opsets/opset.cpp b/src/core/src/opsets/opset.cpp index 1a61c91e7a1..9adcd22a43c 100644 --- a/src/core/src/opsets/opset.cpp +++ b/src/core/src/opsets/opset.cpp @@ -5,17 +5,11 @@ #include "ngraph/opsets/opset.hpp" #include "itt.hpp" -#include "ngraph/deprecated.hpp" #include "ngraph/log.hpp" #include "openvino/op/ops.hpp" #include "openvino/opsets/opset.hpp" #include "openvino/util/log.hpp" -OPENVINO_SUPPRESS_DEPRECATED_START -ngraph::OpSet::OpSet(const ov::OpSet& opset) : ov::OpSet(opset) {} - -ngraph::OpSet::OpSet(const ngraph::OpSet& opset) : ov::OpSet(opset) {} - ov::OpSet::OpSet(const std::string& name) : m_name(name) {} ov::OpSet::OpSet(const ov::OpSet& opset) { @@ -51,24 +45,6 @@ ov::Node* ov::OpSet::create_insensitive(const std::string& name) const { return m_factory_registry.create(type_info_it->second); } -const std::map>& ngraph::get_available_opsets() { -#define _NGRAPH_REG_OPSET(OPSET) \ - { #OPSET, ngraph::get_##OPSET } - const static std::map> opset_map = {_NGRAPH_REG_OPSET(opset1), - _NGRAPH_REG_OPSET(opset2), - _NGRAPH_REG_OPSET(opset3), - _NGRAPH_REG_OPSET(opset4), - _NGRAPH_REG_OPSET(opset5), - _NGRAPH_REG_OPSET(opset6), - _NGRAPH_REG_OPSET(opset7), - _NGRAPH_REG_OPSET(opset8), - _NGRAPH_REG_OPSET(opset9), - _NGRAPH_REG_OPSET(opset10), - _NGRAPH_REG_OPSET(opset11)}; -#undef _NGRAPH_REG_OPSET - return opset_map; -} - const std::map>& ov::get_available_opsets() { #define _OPENVINO_REG_OPSET(OPSET) \ { #OPSET, ov::get_##OPSET } @@ -232,57 +208,96 @@ const ov::OpSet& ov::get_opset13() { return opset; } -const ngraph::OpSet& ngraph::get_opset1() { +OPENVINO_SUPPRESS_DEPRECATED_START +namespace ngraph { + +OpSet::OpSet(const ov::OpSet& opset) : ov::OpSet(opset) {} + +OpSet::OpSet(const OpSet& opset) : ov::OpSet(opset) {} + +const std::map>& get_available_opsets() { +#define _REG_OPSET(OPSET) \ + { #OPSET, get_##OPSET } + const static std::map> opset_map = {_REG_OPSET(opset1), + _REG_OPSET(opset2), + _REG_OPSET(opset3), + _REG_OPSET(opset4), + _REG_OPSET(opset5), + _REG_OPSET(opset6), + _REG_OPSET(opset7), + _REG_OPSET(opset8), + _REG_OPSET(opset9), + _REG_OPSET(opset10), + _REG_OPSET(opset11), + _REG_OPSET(opset12), + _REG_OPSET(opset13)}; +#undef _REG_OPSET + return opset_map; +} + +const OpSet& get_opset1() { static OpSet opset(ov::get_opset1()); return opset; } -const ngraph::OpSet& ngraph::get_opset2() { +const OpSet& get_opset2() { static OpSet opset(ov::get_opset2()); return opset; } -const ngraph::OpSet& ngraph::get_opset3() { +const OpSet& get_opset3() { static OpSet opset(ov::get_opset3()); return opset; } -const ngraph::OpSet& ngraph::get_opset4() { +const OpSet& get_opset4() { static OpSet opset(ov::get_opset4()); return opset; } -const ngraph::OpSet& ngraph::get_opset5() { +const OpSet& get_opset5() { static OpSet opset(ov::get_opset5()); return opset; } -const ngraph::OpSet& ngraph::get_opset6() { +const OpSet& get_opset6() { static OpSet opset(ov::get_opset6()); return opset; } -const ngraph::OpSet& ngraph::get_opset7() { +const OpSet& get_opset7() { static OpSet opset(ov::get_opset7()); return opset; } -const ngraph::OpSet& ngraph::get_opset8() { +const OpSet& get_opset8() { static OpSet opset(ov::get_opset8()); return opset; } -const ngraph::OpSet& ngraph::get_opset9() { +const OpSet& get_opset9() { static OpSet opset(ov::get_opset9()); return opset; } -const ngraph::OpSet& ngraph::get_opset10() { +const OpSet& get_opset10() { static OpSet opset(ov::get_opset10()); return opset; } -const ngraph::OpSet& ngraph::get_opset11() { +const OpSet& get_opset11() { static OpSet opset(ov::get_opset11()); return opset; } + +const OpSet& get_opset12() { + static OpSet opset(ov::get_opset12()); + return opset; +} + +const OpSet& get_opset13() { + static OpSet opset(ov::get_opset13()); + return opset; +} + +} // namespace ngraph diff --git a/src/core/src/runtime/aligned_buffer.cpp b/src/core/src/runtime/aligned_buffer.cpp index 95ab3f44306..d7c5229fcc0 100644 --- a/src/core/src/runtime/aligned_buffer.cpp +++ b/src/core/src/runtime/aligned_buffer.cpp @@ -8,17 +8,18 @@ #include #include "ngraph/util.hpp" +#include "openvino/util/log.hpp" -using namespace ngraph; -using namespace std; NGRAPH_SUPPRESS_DEPRECATED_START +namespace ngraph { + runtime::AlignedBuffer::AlignedBuffer() : m_allocated_buffer(nullptr), m_aligned_buffer(nullptr), m_byte_size(0) {} runtime::AlignedBuffer::AlignedBuffer(size_t byte_size, size_t alignment) : m_byte_size(byte_size) { m_byte_size = std::max(1, byte_size); size_t allocation_size = m_byte_size + alignment; - m_allocated_buffer = static_cast(ngraph_malloc(allocation_size)); + m_allocated_buffer = new char[allocation_size]; m_aligned_buffer = m_allocated_buffer; size_t mod = (alignment != 0) ? size_t(m_aligned_buffer) % alignment : 0; @@ -38,14 +39,14 @@ runtime::AlignedBuffer::AlignedBuffer(AlignedBuffer&& other) runtime::AlignedBuffer::~AlignedBuffer() { if (m_allocated_buffer != nullptr) { - free(m_allocated_buffer); + delete[] m_allocated_buffer; } } runtime::AlignedBuffer& runtime::AlignedBuffer::operator=(AlignedBuffer&& other) { if (this != &other) { if (m_allocated_buffer != nullptr) { - free(m_allocated_buffer); + delete[] m_allocated_buffer; } m_allocated_buffer = other.m_allocated_buffer; m_aligned_buffer = other.m_aligned_buffer; @@ -56,9 +57,10 @@ runtime::AlignedBuffer& runtime::AlignedBuffer::operator=(AlignedBuffer&& other) } return *this; } +} // namespace ngraph namespace ov { -AttributeAdapter>::AttributeAdapter( - shared_ptr& value) - : DirectValueAccessor>(value) {} +AttributeAdapter>::AttributeAdapter( + std::shared_ptr& value) + : DirectValueAccessor>(value) {} } // namespace ov diff --git a/src/core/src/runtime/tensor.cpp b/src/core/src/runtime/tensor.cpp index 7f0c51fa45f..f7f587d1a95 100644 --- a/src/core/src/runtime/tensor.cpp +++ b/src/core/src/runtime/tensor.cpp @@ -4,10 +4,6 @@ #include "ngraph/runtime/tensor.hpp" -#include "ngraph/log.hpp" -#include "ngraph/runtime/aligned_buffer.hpp" -#include "ngraph/type/element_type.hpp" - using namespace ngraph; using namespace std; diff --git a/src/core/src/shape_util.cpp b/src/core/src/shape_util.cpp index 9ce8512d7a7..72c72c39b68 100644 --- a/src/core/src/shape_util.cpp +++ b/src/core/src/shape_util.cpp @@ -9,10 +9,9 @@ #include "openvino/core/partial_shape.hpp" #include "openvino/core/shape_util.hpp" -using namespace ngraph; - +namespace ngraph { template <> -PartialShape ngraph::project(const PartialShape& shape, const AxisSet& axes) { +PartialShape project(const PartialShape& shape, const AxisSet& axes) { if (shape.rank().is_dynamic()) { return shape; } else { @@ -29,7 +28,7 @@ PartialShape ngraph::project(const PartialShape& shape, const AxisSet& axes) { } template <> -PartialShape ngraph::reduce(const PartialShape& shape, const AxisSet& deleted_axes, bool keep_dims) { +PartialShape reduce(const PartialShape& shape, const AxisSet& deleted_axes, bool keep_dims) { if (shape.rank().is_dynamic()) { return shape; } else { @@ -49,8 +48,8 @@ PartialShape ngraph::reduce(const PartialShape& shape, const AxisSet& deleted_ax } template <> -PartialShape ngraph::inject_pairs(const PartialShape& shape, - std::vector> new_axis_pos_value_pairs) { +PartialShape inject_pairs(const PartialShape& shape, + std::vector> new_axis_pos_value_pairs) { if (shape.rank().is_dynamic()) { return shape; } else { @@ -76,6 +75,7 @@ PartialShape ngraph::inject_pairs(const PartialShape& shape, return PartialShape{result_dims}; } } +} // namespace ngraph namespace ov { template diff --git a/src/core/src/util.cpp b/src/core/src/util.cpp index 3c12bd5cb10..49ae1575101 100644 --- a/src/core/src/util.cpp +++ b/src/core/src/util.cpp @@ -13,21 +13,13 @@ #include #include -#include "ngraph/coordinate_diff.hpp" -#include "ngraph/function.hpp" -#include "ngraph/graph_util.hpp" -#include "ngraph/log.hpp" -#include "ngraph/node.hpp" -#include "ngraph/op/result.hpp" -#include "ngraph/partial_shape.hpp" -#include "ngraph/shape.hpp" #include "openvino/util/common_util.hpp" #include "openvino/util/log.hpp" -NGRAPH_SUPPRESS_DEPRECATED_START -using namespace std; +OPENVINO_SUPPRESS_DEPRECATED_START -void ngraph::dump(ostream& out, const void* _data, size_t _size) { +namespace ngraph { +void dump(std::ostream& out, const void* _data, size_t _size) { auto flags = out.flags(); const uint8_t* data = reinterpret_cast(_data); size_t len = _size; @@ -61,27 +53,27 @@ void ngraph::dump(ostream& out, const void* _data, size_t _size) { out.flags(flags); } -std::string ngraph::to_lower(const std::string& s) { +std::string to_lower(const std::string& s) { return ov::util::to_lower(s); } -std::string ngraph::to_upper(const std::string& s) { +std::string to_upper(const std::string& s) { return ov::util::to_upper(s); } -string ngraph::trim(const string& s) { +std::string trim(const std::string& s) { return ov::util::trim(s); } -vector ngraph::split(const string& src, char delimiter, bool do_trim) { +std::vector split(const std::string& src, char delimiter, bool do_trim) { return ov::util::split(src, delimiter, do_trim); } -size_t ngraph::hash_combine(const std::vector& list) { +size_t hash_combine(const std::vector& list) { return ov::util::hash_combine(list); } -void* ngraph::ngraph_malloc(size_t size) { +void* ngraph_malloc(size_t size) { auto ptr = malloc(size); if (size != 0 && !ptr) { OPENVINO_ERR << "malloc failed to allocate memory of size " << size; @@ -90,13 +82,13 @@ void* ngraph::ngraph_malloc(size_t size) { return ptr; } -void ngraph::ngraph_free(void* ptr) { +void ngraph_free(void* ptr) { if (ptr) { free(ptr); } } -size_t ngraph::round_up(size_t size, size_t alignment) { +size_t round_up(size_t size, size_t alignment) { if (alignment == 0) { return size; } @@ -109,27 +101,27 @@ size_t ngraph::round_up(size_t size, size_t alignment) { return size + alignment - remainder; } -size_t ngraph::stopwatch::get_call_count() const { +size_t stopwatch::get_call_count() const { return m_total_count; } -size_t ngraph::stopwatch::get_seconds() const { - return chrono::duration_cast(get_timer_value()).count(); +size_t stopwatch::get_seconds() const { + return std::chrono::duration_cast(get_timer_value()).count(); } -size_t ngraph::stopwatch::get_milliseconds() const { - return chrono::duration_cast(get_timer_value()).count(); +size_t stopwatch::get_milliseconds() const { + return std::chrono::duration_cast(get_timer_value()).count(); } -size_t ngraph::stopwatch::get_microseconds() const { - return chrono::duration_cast(get_timer_value()).count(); +size_t stopwatch::get_microseconds() const { + return std::chrono::duration_cast(get_timer_value()).count(); } -size_t ngraph::stopwatch::get_nanoseconds() const { +size_t stopwatch::get_nanoseconds() const { return get_timer_value().count(); } -chrono::nanoseconds ngraph::stopwatch::get_timer_value() const { +std::chrono::nanoseconds stopwatch::get_timer_value() const { if (m_active) { return (m_clock.now() - m_start_time); } else { @@ -137,23 +129,22 @@ chrono::nanoseconds ngraph::stopwatch::get_timer_value() const { } } -size_t ngraph::stopwatch::get_total_seconds() const { - return chrono::duration_cast(m_total_time).count(); +size_t stopwatch::get_total_seconds() const { + return std::chrono::duration_cast(m_total_time).count(); } -size_t ngraph::stopwatch::get_total_milliseconds() const { - return chrono::duration_cast(m_total_time).count(); +size_t stopwatch::get_total_milliseconds() const { + return std::chrono::duration_cast(m_total_time).count(); } -size_t ngraph::stopwatch::get_total_microseconds() const { - return chrono::duration_cast(m_total_time).count(); +size_t stopwatch::get_total_microseconds() const { + return std::chrono::duration_cast(m_total_time).count(); } -size_t ngraph::stopwatch::get_total_nanoseconds() const { +size_t stopwatch::get_total_nanoseconds() const { return m_total_time.count(); } -namespace ngraph { template <> float parse_string(const std::string& s) { const char* tmp = s.c_str(); @@ -201,40 +192,30 @@ uint8_t parse_string(const std::string& s) { return result; } -} // namespace ngraph -std::ostream& operator<<(std::ostream& os, const ngraph::NodeVector& nv) { - std::vector names; - for (auto n : nv) { - names.push_back(n->get_name()); - } - os << ngraph::vector_to_string(names); - return os; -} - -ngraph::AxisVector ngraph::get_default_order(const Shape& shape) { +AxisVector get_default_order(const Shape& shape) { return get_default_order(shape.size()); } -ngraph::AxisVector ngraph::get_default_order(const PartialShape& shape) { +AxisVector get_default_order(const PartialShape& shape) { return get_default_order(shape.rank()); } -ngraph::AxisVector ngraph::get_default_order(size_t rank) { +AxisVector get_default_order(size_t rank) { AxisVector default_order(rank); std::iota(begin(default_order), end(default_order), 0); return default_order; } -ngraph::AxisVector ngraph::get_default_order(const Rank& rank) { - NGRAPH_CHECK(rank.is_static(), "Can not calculate default order for dynamic rank"); +AxisVector get_default_order(const Rank& rank) { + OPENVINO_ASSERT(rank.is_static(), "Can not calculate default order for dynamic rank"); AxisVector default_order(rank.get_length()); std::iota(begin(default_order), end(default_order), 0); return default_order; } -void ngraph::parse_version_string(std::string version, size_t& major, size_t& minor, size_t& patch, string& extra) { +void parse_version_string(std::string version, size_t& major, size_t& minor, size_t& patch, std::string& extra) { // Since regex is broken in gcc 4.8 I will just manually parse the version string // Version strings look like `0.25.0-rc.0+7c32240` or `v0.25.0-rc.0+7c32240` size_t start; @@ -242,18 +223,18 @@ void ngraph::parse_version_string(std::string version, size_t& major, size_t& mi extra = ""; start = (version[0] == 'v' ? 1 : 0); end = version.find_first_of('.', start); - string major_str = version.substr(start, end - start); + std::string major_str = version.substr(start, end - start); start = end + 1; end = version.find_first_of('.', start); - string minor_str = version.substr(start, end - start); + std::string minor_str = version.substr(start, end - start); start = end + 1; end = version.find_first_of("-+", start); - string patch_str = version.substr(start, end - start); + std::string patch_str = version.substr(start, end - start); start = end; - if (start != string::npos) { + if (start != std::string::npos) { extra = version.substr(start); } @@ -279,73 +260,74 @@ void ngraph::parse_version_string(std::string version, size_t& major, size_t& mi OPENVINO_THROW("Error parsing version string '", version, "'"); } } +} // namespace ngraph -vector read_float_vector(shared_ptr tv) { - vector float_vec; - ngraph::element::Type element_type = tv->get_element_type(); +std::vector read_float_vector(std::shared_ptr tv) { + std::vector float_vec; + ov::element::Type element_type = tv->get_element_type(); - if (element_type == ngraph::element::boolean) { - vector vec = read_vector(tv); + if (element_type == ov::element::boolean) { + std::vector vec = read_vector(tv); // Changed from vector ctor to explicit for loop to add static_cast // This silences MSVC warnings for (char value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::bf16) { - vector vec = read_vector(tv); - float_vec = ngraph::bfloat16::to_float_vector(vec); - } else if (element_type == ngraph::element::f16) { - vector vec = read_vector(tv); - for (ngraph::float16 value : vec) { + } else if (element_type == ov::element::bf16) { + std::vector vec = read_vector(tv); + float_vec = ov::bfloat16::to_float_vector(vec); + } else if (element_type == ov::element::f16) { + std::vector vec = read_vector(tv); + for (ov::float16 value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::f32) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::f32) { + std::vector vec = read_vector(tv); for (float value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::f64) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::f64) { + std::vector vec = read_vector(tv); for (double value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::i8) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::i8) { + std::vector vec = read_vector(tv); for (int8_t value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::i16) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::i16) { + std::vector vec = read_vector(tv); for (int16_t value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::i32) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::i32) { + std::vector vec = read_vector(tv); for (int32_t value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::i64) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::i64) { + std::vector vec = read_vector(tv); for (int64_t value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::u8) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::u8) { + std::vector vec = read_vector(tv); for (uint8_t value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::u16) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::u16) { + std::vector vec = read_vector(tv); for (uint16_t value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::u32) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::u32) { + std::vector vec = read_vector(tv); for (uint32_t value : vec) { float_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::u64) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::u64) { + std::vector vec = read_vector(tv); for (uint64_t value : vec) { float_vec.push_back(static_cast(value)); } @@ -356,72 +338,72 @@ vector read_float_vector(shared_ptr tv) { return float_vec; } -vector read_index_vector(shared_ptr tv) { - vector index_vec; - ngraph::element::Type element_type = tv->get_element_type(); +std::vector read_index_vector(std::shared_ptr tv) { + std::vector index_vec; + ov::element::Type element_type = tv->get_element_type(); - if (element_type == ngraph::element::boolean) { - vector vec = read_vector(tv); + if (element_type == ov::element::boolean) { + std::vector vec = read_vector(tv); // Changed from vector ctor to explicit for loop to add static_cast // This silences MSVC warnings for (char value : vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::bf16) { - vector vec = read_vector(tv); - vector float_vec = ngraph::bfloat16::to_float_vector(vec); + } else if (element_type == ov::element::bf16) { + std::vector vec = read_vector(tv); + std::vector float_vec = ov::bfloat16::to_float_vector(vec); for (float value : float_vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::f16) { - vector vec = read_vector(tv); - for (ngraph::float16 value : vec) { + } else if (element_type == ov::element::f16) { + std::vector vec = read_vector(tv); + for (ov::float16 value : vec) { index_vec.push_back(static_cast(static_cast(value))); } - } else if (element_type == ngraph::element::f32) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::f32) { + std::vector vec = read_vector(tv); for (float value : vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::f64) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::f64) { + std::vector vec = read_vector(tv); for (double value : vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::i8) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::i8) { + std::vector vec = read_vector(tv); for (int8_t value : vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::i16) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::i16) { + std::vector vec = read_vector(tv); for (int16_t value : vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::i32) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::i32) { + std::vector vec = read_vector(tv); for (int32_t value : vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::i64) { + } else if (element_type == ov::element::i64) { index_vec = read_vector(tv); - } else if (element_type == ngraph::element::u8) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::u8) { + std::vector vec = read_vector(tv); for (uint8_t value : vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::u16) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::u16) { + std::vector vec = read_vector(tv); for (uint16_t value : vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::u32) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::u32) { + std::vector vec = read_vector(tv); for (uint32_t value : vec) { index_vec.push_back(static_cast(value)); } - } else if (element_type == ngraph::element::u64) { - vector vec = read_vector(tv); + } else if (element_type == ov::element::u64) { + std::vector vec = read_vector(tv); for (uint64_t value : vec) { index_vec.push_back(static_cast(value)); } diff --git a/src/core/src/validation_util.cpp b/src/core/src/validation_util.cpp index 3d2f72b8533..2e1db9dd686 100644 --- a/src/core/src/validation_util.cpp +++ b/src/core/src/validation_util.cpp @@ -5,36 +5,25 @@ #include "ngraph/validation_util.hpp" #include -#include -#include #include #include "bound_evaluate.hpp" #include "compare.hpp" #include "ngraph/evaluator.hpp" -#include "ngraph/op/concat.hpp" -#include "ngraph/op/convert.hpp" -#include "ngraph/op/gather.hpp" -#include "ngraph/op/min.hpp" -#include "ngraph/op/minimum.hpp" -#include "ngraph/op/reshape.hpp" -#include "ngraph/op/shape_of.hpp" -#include "ngraph/op/squeeze.hpp" -#include "ngraph/op/unsqueeze.hpp" -#include "ngraph/shape.hpp" -#include "ngraph/type/element_type_traits.hpp" -#include "ngraph/util.hpp" #include "openvino/core/dimension_tracker.hpp" +#include "openvino/op/concat.hpp" +#include "openvino/op/gather.hpp" #include "openvino/op/ops.hpp" #include "sequnce_generator.hpp" #include "validation_util.hpp" -NGRAPH_SUPPRESS_DEPRECATED_START -using namespace std; +OPENVINO_SUPPRESS_DEPRECATED_START -ngraph::Strides ngraph::conv_default_strides(const Node* /* node */, - const PartialShape& data_batch_shape, - const PartialShape& filters_shape) { +namespace ngraph { + +Strides conv_default_strides(const Node* /* node */, + const PartialShape& data_batch_shape, + const PartialShape& filters_shape) { size_t rank; if (data_batch_shape.rank().is_static() && data_batch_shape.rank().get_length() >= 2) { @@ -48,9 +37,9 @@ ngraph::Strides ngraph::conv_default_strides(const Node* /* node */, return Strides(rank, 1); } -ngraph::CoordinateDiff ngraph::conv_default_padding(const Node* /* node */, - const PartialShape& data_batch_shape, - const PartialShape& filters_shape) { +CoordinateDiff conv_default_padding(const Node* /* node */, + const PartialShape& data_batch_shape, + const PartialShape& filters_shape) { size_t rank; if (data_batch_shape.rank().is_static() && data_batch_shape.rank().get_length() >= 2) { @@ -71,16 +60,16 @@ ngraph::CoordinateDiff ngraph::conv_default_padding(const Node* /* node */, // TODO(amprocte): The messages here would be a bit friendlier if we didn't say "after // padding/after dilation" for cases where there is actually no padding/dilation. // -ngraph::PartialShape ngraph::infer_windowed_reduction_output_shape(const Node* node, - const PartialShape& data_shape, - const Strides& data_dilation, - const CoordinateDiff& data_padding_below, - const CoordinateDiff& data_padding_above, - const PartialShape& window_shape, - const Strides& window_strides, - const Strides& window_dilation, - bool is_window_all_in_padding_allowed, - bool ceil_mode) { +PartialShape infer_windowed_reduction_output_shape(const Node* node, + const PartialShape& data_shape, + const Strides& data_dilation, + const CoordinateDiff& data_padding_below, + const CoordinateDiff& data_padding_above, + const PartialShape& window_shape, + const Strides& window_strides, + const Strides& window_dilation, + bool is_window_all_in_padding_allowed, + bool ceil_mode) { PartialShape data_shape_merged{PartialShape::dynamic()}; NODE_VALIDATION_CHECK( @@ -203,13 +192,13 @@ ngraph::PartialShape ngraph::infer_windowed_reduction_output_shape(const Node* n return output_shape; } -void ngraph::validate_conv_params_spatial_dimensions(const Node* node, - const size_t num_spatial_dims, - const op::PadType auto_pad, - Strides& strides, - Strides& dilations, - CoordinateDiff& pads_begin, - CoordinateDiff& pads_end) { +void validate_conv_params_spatial_dimensions(const Node* node, + const size_t num_spatial_dims, + const op::PadType auto_pad, + Strides& strides, + Strides& dilations, + CoordinateDiff& pads_begin, + CoordinateDiff& pads_end) { if (strides.size() == 0) { strides = Strides(num_spatial_dims, 1); } @@ -233,15 +222,15 @@ void ngraph::validate_conv_params_spatial_dimensions(const Node* node, "Pads should be defined for all and only spatial features."); } -ngraph::PartialShape ngraph::validate_and_infer_convolution_forward_output_shape(const Node* node, - const Rank& result_ps_rank, - const PartialShape& data_batch_pshape, - const PartialShape& filters_pshape, - const op::PadType auto_pad, - Strides& strides, - Strides& dilations, - CoordinateDiff& pads_begin, - CoordinateDiff& pads_end) { +PartialShape validate_and_infer_convolution_forward_output_shape(const Node* node, + const Rank& result_ps_rank, + const PartialShape& data_batch_pshape, + const PartialShape& filters_pshape, + const op::PadType auto_pad, + Strides& strides, + Strides& dilations, + CoordinateDiff& pads_begin, + CoordinateDiff& pads_end) { PartialShape result_shape = PartialShape::dynamic(); if (result_ps_rank.is_static()) { const auto num_spatial_dims = result_ps_rank.get_length() - 2; @@ -267,7 +256,7 @@ ngraph::PartialShape ngraph::validate_and_infer_convolution_forward_output_shape pads_end.clear(); const PartialShape filter_spatial_shape = [filters_pshape]() { - vector filter_dims{filters_pshape}; + std::vector filter_dims{filters_pshape}; filter_dims.erase(filter_dims.begin(), filter_dims.begin() + 2); // Remove {C_OUT, C_IN} return PartialShape{filter_dims}; @@ -299,142 +288,18 @@ ngraph::PartialShape ngraph::validate_and_infer_convolution_forward_output_shape return result_shape; } -// -// Infers the output batch shape and element type for convolution fprop. -// -ngraph::PartialShape ov::infer_convolution_forward(const Node* node, - const PartialShape& data_batch_shape, - const Strides& data_dilation, - const CoordinateDiff& data_padding_below, - const CoordinateDiff& data_padding_above, - const PartialShape& filters_shape, - const Strides& filter_strides, - const Strides& filter_dilation) { - Rank data_batch_filters_rank{Rank::dynamic()}; - - NODE_VALIDATION_CHECK(node, - Rank::merge(data_batch_filters_rank, data_batch_shape.rank(), filters_shape.rank()), - "Data batch and filters rank do not match (data batch shape: ", - data_batch_shape, - ", filters shape: ", - filters_shape, - ")."); - - NODE_VALIDATION_CHECK(node, - data_batch_filters_rank.is_dynamic() || data_batch_filters_rank.get_length() >= 3, - "Data batch and filters must have rank of at least 3 (one batch axis, ", - "one input-channel axis, and at least one spatial dimension) ", - "(data batch shape: ", - data_batch_shape, - ", filters shape: ", - filters_shape, - ")."); - - Rank spatial_rank{Rank::dynamic()}; - NODE_VALIDATION_CHECK(node, - Rank::merge(spatial_rank, spatial_rank, data_batch_filters_rank - 2) && - Rank::merge(spatial_rank, spatial_rank, data_dilation.size()) && - Rank::merge(spatial_rank, spatial_rank, data_padding_below.size()) && - Rank::merge(spatial_rank, spatial_rank, data_padding_above.size()) && - Rank::merge(spatial_rank, spatial_rank, filter_strides.size()) && - Rank::merge(spatial_rank, spatial_rank, filter_dilation.size()), - "Ranks for data item shape/filters shape (data batch has shape ", - data_batch_shape, - ", so data item rank is ", - (data_batch_shape.rank() - 2), - " and filters have shape ", - filters_shape, - ", so filters spatial rank is ", - (filters_shape.rank() - 2), - "), data dilation (", - data_dilation, - "), padding below (", - data_padding_below, - "), padding above (", - data_padding_above, - "), filter strides (", - filter_strides, - "), and filter dilation (", - filter_dilation, - ") do not match."); - - Dimension batch_size = (data_batch_shape.rank().is_static() ? data_batch_shape[0] : Dimension::dynamic()); - Dimension data_channel_count = (data_batch_shape.rank().is_static() ? data_batch_shape[1] : Dimension::dynamic()); - PartialShape data_spatial_shape(PartialShape::dynamic(spatial_rank)); - - Dimension filter_output_channel_count = - (filters_shape.rank().is_static() ? filters_shape[0] : Dimension::dynamic()); - Dimension filter_input_channel_count = (filters_shape.rank().is_static() ? filters_shape[1] : Dimension::dynamic()); - PartialShape filter_spatial_shape(PartialShape::dynamic(spatial_rank)); - - // - // Note: spatial_rank is definitely static at this point. - // - - for (int64_t i = 0; i < spatial_rank.get_length(); i++) { - if (data_batch_shape.rank().is_static()) { - data_spatial_shape[i] = data_batch_shape[i + 2]; - } - - if (filters_shape.rank().is_static()) { - filter_spatial_shape[i] = filters_shape[i + 2]; - } - } - - NODE_VALIDATION_CHECK(node, batch_size.is_dynamic() || batch_size.get_length() > 0, "Batch size is zero."); - - Dimension merged_channel_count; - - NODE_VALIDATION_CHECK(node, - Dimension::merge(merged_channel_count, data_channel_count, filter_input_channel_count), - "Data batch channel count (", - data_channel_count, - ") does not match filter input ", - "channel count (", - filter_input_channel_count, - ")."); - - NODE_VALIDATION_CHECK(node, - merged_channel_count.is_dynamic() || merged_channel_count.get_length() > 0, - "Data batch channel count and/or filter input channel count is zero."); - - NODE_VALIDATION_CHECK(node, - filter_output_channel_count.is_dynamic() || filter_output_channel_count.get_length() > 0, - "Filter output channel count is zero."); - - PartialShape data_output_shape = ngraph::infer_windowed_reduction_output_shape(node, - data_spatial_shape, - data_dilation, - data_padding_below, - data_padding_above, - filter_spatial_shape, - filter_strides, - filter_dilation, - true); - - PartialShape batch_output_shape(PartialShape::dynamic(spatial_rank + 2)); - batch_output_shape[0] = batch_size; - batch_output_shape[1] = filter_output_channel_count; - - for (int64_t i = 0; i < spatial_rank.get_length(); i++) { - batch_output_shape[i + 2] = data_output_shape[i]; - } - - return batch_output_shape; -} - // // Infers the output batch shape and element type for batched pooling fprop. // -ngraph::PartialShape ngraph::infer_batched_pooling_forward(const Node* node, - const PartialShape& data_batch_shape, - const CoordinateDiff& data_padding_below, - const CoordinateDiff& data_padding_above, - const PartialShape& window_shape, - const Strides& window_strides, - bool is_window_all_in_padding_allowed, - bool ceil_mode, - const Strides& window_dilation) { +PartialShape infer_batched_pooling_forward(const Node* node, + const PartialShape& data_batch_shape, + const CoordinateDiff& data_padding_below, + const CoordinateDiff& data_padding_above, + const PartialShape& window_shape, + const Strides& window_strides, + bool is_window_all_in_padding_allowed, + bool ceil_mode, + const Strides& window_dilation) { NODE_VALIDATION_CHECK(node, data_batch_shape.rank().is_dynamic() || (data_batch_shape.rank().get_length() >= 3 && data_batch_shape.rank().get_length() <= 5), @@ -517,15 +382,15 @@ ngraph::PartialShape ngraph::infer_batched_pooling_forward(const Node* node, } struct ChannelShapedInputSpec { - ngraph::element::Type m_element_type; - ngraph::PartialShape m_shape; + element::Type m_element_type; + PartialShape m_shape; std::string m_input_name; }; -static std::tuple infer_batch_norm_forward_helper( - const ngraph::Node* node, - ngraph::element::Type input_element_type, - const ngraph::PartialShape& input_shape, +static std::tuple infer_batch_norm_forward_helper( + const Node* node, + element::Type input_element_type, + const PartialShape& input_shape, const std::vector& channel_shaped_inputs) { // Built up a slash-separated string naming all the channel-shaped inputs, for use in error // messages. @@ -541,11 +406,11 @@ static std::tuple= 2, @@ -570,11 +435,11 @@ static std::tuple ngraph::infer_batch_norm_forward( - const Node* node, - element::Type input_element_type, - element::Type gamma_element_type, - element::Type beta_element_type, - element::Type mean_element_type, - element::Type variance_element_type, - const PartialShape& input_shape, - const PartialShape& gamma_shape, - const PartialShape& beta_shape, - const PartialShape& mean_shape, - const PartialShape& variance_shape) { +std::tuple infer_batch_norm_forward(const Node* node, + element::Type input_element_type, + element::Type gamma_element_type, + element::Type beta_element_type, + element::Type mean_element_type, + element::Type variance_element_type, + const PartialShape& input_shape, + const PartialShape& gamma_shape, + const PartialShape& beta_shape, + const PartialShape& mean_shape, + const PartialShape& variance_shape) { return infer_batch_norm_forward_helper(node, input_element_type, input_shape, @@ -634,14 +498,13 @@ std::tuple ng {variance_element_type, variance_shape, "variance"}}); } -std::tuple ngraph::infer_batch_norm_forward( - const Node* node, - element::Type input_element_type, - element::Type gamma_element_type, - element::Type beta_element_type, - const PartialShape& input_shape, - const PartialShape& gamma_shape, - const PartialShape& beta_shape) { +std::tuple infer_batch_norm_forward(const Node* node, + element::Type input_element_type, + element::Type gamma_element_type, + element::Type beta_element_type, + const PartialShape& input_shape, + const PartialShape& gamma_shape, + const PartialShape& beta_shape) { return infer_batch_norm_forward_helper( node, input_element_type, @@ -649,64 +512,13 @@ std::tuple ng {{gamma_element_type, gamma_shape, "gamma"}, {beta_element_type, beta_shape, "beta"}}); } -void ov::infer_auto_padding(const Shape& image_shape, +bool try_apply_auto_padding(const PartialShape& image_shape, const Shape& filter_shape, const Strides& filter_strides, const Strides& filter_dilations, const op::PadType pad_type, CoordinateDiff& padding_above, CoordinateDiff& padding_below) { - const auto image_dims = std::vector(std::begin(image_shape), std::end(image_shape)); - // because image_shape is fully known result of try_apply_infer_auto_padding is ignored - ov::util::try_apply_auto_padding(image_dims, - filter_shape, - filter_strides, - filter_dilations, - pad_type, - padding_above, - padding_below); -} - -bool ov::util::try_apply_auto_padding(const PartialShape& image_shape, - const Shape& filter_shape, - const Strides& filter_strides, - const Strides& filter_dilations, - const op::PadType pad_type, - CoordinateDiff& padding_above, - CoordinateDiff& padding_below) { - OPENVINO_ASSERT(pad_type == op::PadType::SAME_UPPER || pad_type == op::PadType::SAME_LOWER); - - if (image_shape.rank().is_dynamic()) { - return false; - } - const auto image_dims = static_cast>(image_shape); - for (size_t i = 0; i < static_cast(filter_shape.size()); i++) { - if (image_dims[i + 2].is_static()) { - auto image_size = static_cast(image_dims[i + 2].get_length()); - int64_t filter_size = (static_cast(filter_shape[i]) - 1) * filter_dilations[i] + 1; - auto filter_stride = static_cast(filter_strides[i]); - auto output_size = (image_size + filter_stride - 1) / filter_stride; - - auto padding_needed = std::max(int64_t(0), (output_size - 1) * filter_stride + filter_size - image_size); - auto padding_lhs = padding_needed / 2; - auto padding_rhs = padding_needed - padding_lhs; - padding_below.push_back(pad_type == op::PadType::SAME_UPPER ? padding_lhs : padding_rhs); - padding_above.push_back(pad_type == op::PadType::SAME_UPPER ? padding_rhs : padding_lhs); - } else { - padding_below.push_back(0); - padding_above.push_back(0); - } - } - return true; -} - -bool ngraph::try_apply_auto_padding(const PartialShape& image_shape, - const Shape& filter_shape, - const Strides& filter_strides, - const Strides& filter_dilations, - const op::PadType pad_type, - CoordinateDiff& padding_above, - CoordinateDiff& padding_below) { return ov::util::try_apply_auto_padding(image_shape, filter_shape, filter_strides, @@ -716,16 +528,16 @@ bool ngraph::try_apply_auto_padding(const PartialShape& image_shape, padding_below); } -ngraph::PartialShape ngraph::infer_slice_shape(const Node* node, - const PartialShape& input_shape, - const std::vector& begin, - const std::vector& end, - const std::vector& strides, - const AxisSet& begin_mask, - const AxisSet& end_mask, - const AxisSet& new_axis_mask, - const AxisSet& shrink_axis_mask, - const AxisSet& ellipsis_mask) { +PartialShape infer_slice_shape(const Node* node, + const PartialShape& input_shape, + const std::vector& begin, + const std::vector& end, + const std::vector& strides, + const AxisSet& begin_mask, + const AxisSet& end_mask, + const AxisSet& new_axis_mask, + const AxisSet& shrink_axis_mask, + const AxisSet& ellipsis_mask) { if (begin.size() && end.size()) { NODE_VALIDATION_CHECK(node, begin.size() == end.size(), @@ -869,6 +681,381 @@ ngraph::PartialShape ngraph::infer_slice_shape(const Node* node, return dim; } +void opset1::infer_conv_backprop_auto_padding(const Shape& input_data_shape, + const Shape& filters_shape, + const Shape& output_shape, + const Strides& strides, + const Strides& dilations, + const op::PadType auto_pad_type, + const CoordinateDiff& output_padding, + CoordinateDiff& pads_begin, + CoordinateDiff& pads_end) { + OPENVINO_ASSERT(auto_pad_type == op::PadType::SAME_UPPER || auto_pad_type == op::PadType::SAME_LOWER); + + size_t num_spatial_dims = input_data_shape.size(); + OPENVINO_ASSERT(filters_shape.size() == num_spatial_dims && strides.size() == num_spatial_dims && + dilations.size() == num_spatial_dims && pads_begin.size() == num_spatial_dims && + pads_end.size() == num_spatial_dims && output_padding.size() == num_spatial_dims); + + pads_begin = CoordinateDiff(num_spatial_dims); + pads_end = CoordinateDiff(num_spatial_dims); + + for (uint64_t i = 0; i < num_spatial_dims; ++i) { + int total_padding = std::max( + static_cast(strides[i] * (input_data_shape[i] - 1) + dilations[i] * (filters_shape[i] - 1) + 1 - + output_shape[i] + output_padding[i]), + 0); + if (auto_pad_type != op::PadType::SAME_UPPER) { + pads_begin[i] = total_padding / 2; + pads_end[i] = total_padding - pads_begin[i]; + } else { + pads_end[i] = total_padding / 2; + pads_begin[i] = total_padding - pads_end[i]; + } + } +} + +namespace { +/// \brief Scalar variant describes value of an Output, for use in max shape determination +/// +/// For tensor values, we use the maximum value in the tensor +struct MaxValue { + /// \brief No information known about the output + MaxValue() = default; + /// \brief uint64_t assoiated with the output + MaxValue(uint64_t value) : m_value(value) {} + MaxValue(const std::vector& slices, int64_t slice_axis) : m_slices(slices), m_slice_axis(slice_axis) { + m_value = *max_element(m_slices.begin(), m_slices.end()); + } + uint64_t m_value{std::numeric_limits::max()}; + std::vector m_slices; + int64_t m_slice_axis{-1}; +}; + +std::vector exec_constant(Node* node, std::vector& inputs) { + auto result = MaxValue(); + auto op = ov::as_type(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()) { + if (max_val < static_cast(elt)) { + max_val = elt; + } + } + } else { + for (auto elt : op->cast_vector()) { + if (max_val < elt) { + max_val = elt; + } + } + } + result = MaxValue(max_val); + } + return {result}; +} + +std::vector exec_minimum(Node* node, std::vector& inputs) { + uint64_t min_value = std::numeric_limits::max(); + switch (node->get_output_element_type(0)) { + case element::Type_t::i8: + min_value = std::numeric_limits::max(); + break; + case element::Type_t::i16: + min_value = std::numeric_limits::max(); + break; + case element::Type_t::i32: + min_value = std::numeric_limits::max(); + break; + case element::Type_t::i64: + min_value = std::numeric_limits::max(); + break; + case element::Type_t::u8: + min_value = std::numeric_limits::max(); + break; + case element::Type_t::u16: + min_value = std::numeric_limits::max(); + break; + case element::Type_t::u32: + min_value = std::numeric_limits::max(); + break; + case element::Type_t::u64: + min_value = std::numeric_limits::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 exec_concat(Node* node, std::vector& inputs) { + auto op = ov::as_type(node); + std::vector 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 exec_reduce_min(Node* node, std::vector& inputs) { + auto data = inputs.at(0); + if (data.m_slice_axis >= 0 && data.m_slices.size() > 1) { + if (auto indices_const = ov::as_type(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(); + 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 exec_shape_of(Node* node, std::vector& inputs) { + const auto& inputPS = node->get_input_partial_shape(0); + std::vector 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::max()); + } + } + + return {MaxValue(shapeDims, 0)}; +} + +std::vector exec_gather(Node* node, std::vector& inputs) { + auto gather = ov::as_type(node); + + const auto& indices = ov::as_type_ptr(node->input_value(1).get_node_shared_ptr()); + const auto& axis = ov::as_type_ptr(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(); + if (indicesVec.size() != 1 || indicesVec[0] >= static_cast(inputs[0].m_slices.size())) { + return {MaxValue()}; + } + + return {MaxValue(inputs[0].m_slices[indicesVec[0]])}; +} + +std::vector exec_nop(Node* node, std::vector& inputs) { + return {inputs.at(0)}; +} +} // namespace + +std::pair maximum_value(const Output& value) { + static ngraph::Evaluator::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::value_map value_map; + Evaluator evaluator(handlers, value_map); + auto val = evaluator.evaluate(value); + return std::pair(val.m_value < std::numeric_limits::max(), val.m_value); +} + +void evaluate_nodes(std::map& value_map, + std::map& output_tensor_map, + const OutputVector& outputs, + const EvaluationContext& evaluation_context) { + Evaluator evaluator({}, value_map); + evaluator.set_universal_handler( + [&output_tensor_map, &evaluation_context](Node* node, + const HostTensorVector& input_tensors) -> HostTensorVector { + HostTensorVector output_tensors; + for (const auto& v : node->outputs()) { + auto it = output_tensor_map.find(v); + if (it == output_tensor_map.end()) { + auto c = std::make_shared(v); + output_tensors.push_back(c); + } else { + output_tensors.push_back(it->second); + } + } + if (node->evaluate(output_tensors, input_tensors, evaluation_context)) { + return output_tensors; + } else { + OPENVINO_THROW("Evaluation failed on ", node); + } + }); + for (const auto& value : outputs) { + evaluator.evaluate(value); + } +} + +std::shared_ptr get_constant_max_of_type(element::Type_t t) { +#define OPENVINO_TYPE_TO_MAX_CONST(t) \ + case t: \ + return ov::op::v0::Constant::create( \ + t, \ + {}, \ + {std::numeric_limits::value_type>::max()}); \ + break + + switch (t) { + OPENVINO_TYPE_TO_MAX_CONST(element::boolean); + OPENVINO_TYPE_TO_MAX_CONST(element::bf16); + OPENVINO_TYPE_TO_MAX_CONST(element::f16); + OPENVINO_TYPE_TO_MAX_CONST(element::f32); + OPENVINO_TYPE_TO_MAX_CONST(element::f64); + OPENVINO_TYPE_TO_MAX_CONST(element::i8); + OPENVINO_TYPE_TO_MAX_CONST(element::i16); + OPENVINO_TYPE_TO_MAX_CONST(element::i32); + OPENVINO_TYPE_TO_MAX_CONST(element::i64); + OPENVINO_TYPE_TO_MAX_CONST(element::u1); + OPENVINO_TYPE_TO_MAX_CONST(element::u8); + OPENVINO_TYPE_TO_MAX_CONST(element::u16); + OPENVINO_TYPE_TO_MAX_CONST(element::u32); + OPENVINO_TYPE_TO_MAX_CONST(element::u64); + default: + return nullptr; + } +} + +std::shared_ptr get_constant_min_of_type(element::Type_t t) { +#define OPENVINO_TYPE_TO_MIN_CONST(t) \ + case t: \ + return ov::op::v0::Constant::create( \ + t, \ + {}, \ + {std::numeric_limits::value_type>::min()}); \ + break + + switch (t) { + OPENVINO_TYPE_TO_MIN_CONST(element::boolean); + OPENVINO_TYPE_TO_MIN_CONST(element::bf16); + OPENVINO_TYPE_TO_MIN_CONST(element::f16); + OPENVINO_TYPE_TO_MIN_CONST(element::f32); + OPENVINO_TYPE_TO_MIN_CONST(element::f64); + OPENVINO_TYPE_TO_MIN_CONST(element::i8); + OPENVINO_TYPE_TO_MIN_CONST(element::i16); + OPENVINO_TYPE_TO_MIN_CONST(element::i32); + OPENVINO_TYPE_TO_MIN_CONST(element::i64); + OPENVINO_TYPE_TO_MIN_CONST(element::u1); + OPENVINO_TYPE_TO_MIN_CONST(element::u8); + OPENVINO_TYPE_TO_MIN_CONST(element::u16); + OPENVINO_TYPE_TO_MIN_CONST(element::u32); + OPENVINO_TYPE_TO_MIN_CONST(element::u64); + default: + return nullptr; + } +} + +std::shared_ptr get_constant_lowest_of_type(element::Type_t t) { +#define OPENVINO_TYPE_TO_LOWEST_CONST(t) \ + case t: \ + return op::v0::Constant::create(t, \ + {}, \ + {std::numeric_limits::value_type>::lowest()}); \ + break + + switch (t) { + OPENVINO_TYPE_TO_LOWEST_CONST(element::boolean); + OPENVINO_TYPE_TO_LOWEST_CONST(element::bf16); + OPENVINO_TYPE_TO_LOWEST_CONST(element::f16); + OPENVINO_TYPE_TO_LOWEST_CONST(element::f32); + OPENVINO_TYPE_TO_LOWEST_CONST(element::f64); + OPENVINO_TYPE_TO_LOWEST_CONST(element::i8); + OPENVINO_TYPE_TO_LOWEST_CONST(element::i16); + OPENVINO_TYPE_TO_LOWEST_CONST(element::i32); + OPENVINO_TYPE_TO_LOWEST_CONST(element::i64); + OPENVINO_TYPE_TO_LOWEST_CONST(element::u1); + OPENVINO_TYPE_TO_LOWEST_CONST(element::u8); + OPENVINO_TYPE_TO_LOWEST_CONST(element::u16); + OPENVINO_TYPE_TO_LOWEST_CONST(element::u32); + OPENVINO_TYPE_TO_LOWEST_CONST(element::u64); + + case element::undefined: + case element::dynamic: + default: + return nullptr; + } +} + +bool validate_host_tensor_vector(const HostTensorVector& tensor_vector, const size_t& size) { + return (tensor_vector.size() == size) && + std::none_of(tensor_vector.cbegin(), tensor_vector.cend(), ov::cmp::Equal(nullptr)); +} + +} // namespace ngraph + +void ov::infer_auto_padding(const Shape& image_shape, + const Shape& filter_shape, + const Strides& filter_strides, + const Strides& filter_dilations, + const op::PadType pad_type, + CoordinateDiff& padding_above, + CoordinateDiff& padding_below) { + const auto image_dims = std::vector(std::begin(image_shape), std::end(image_shape)); + // because image_shape is fully known result of try_apply_infer_auto_padding is ignored + ov::util::try_apply_auto_padding(image_dims, + filter_shape, + filter_strides, + filter_dilations, + pad_type, + padding_above, + padding_below); +} + +bool ov::util::try_apply_auto_padding(const PartialShape& image_shape, + const Shape& filter_shape, + const Strides& filter_strides, + const Strides& filter_dilations, + const op::PadType pad_type, + CoordinateDiff& padding_above, + CoordinateDiff& padding_below) { + OPENVINO_ASSERT(pad_type == op::PadType::SAME_UPPER || pad_type == op::PadType::SAME_LOWER); + + if (image_shape.rank().is_dynamic()) { + return false; + } + const auto image_dims = static_cast>(image_shape); + for (size_t i = 0; i < static_cast(filter_shape.size()); i++) { + if (image_dims[i + 2].is_static()) { + auto image_size = static_cast(image_dims[i + 2].get_length()); + int64_t filter_size = (static_cast(filter_shape[i]) - 1) * filter_dilations[i] + 1; + auto filter_stride = static_cast(filter_strides[i]); + auto output_size = (image_size + filter_stride - 1) / filter_stride; + + auto padding_needed = std::max(int64_t(0), (output_size - 1) * filter_stride + filter_size - image_size); + auto padding_lhs = padding_needed / 2; + auto padding_rhs = padding_needed - padding_lhs; + padding_below.push_back(pad_type == op::PadType::SAME_UPPER ? padding_lhs : padding_rhs); + padding_above.push_back(pad_type == op::PadType::SAME_UPPER ? padding_rhs : padding_lhs); + } else { + padding_below.push_back(0); + padding_above.push_back(0); + } + } + return true; +} + namespace { const auto normalize_axis_to = [](const int64_t& tensor_rank) { return [&tensor_rank](int64_t& axis) { @@ -880,11 +1067,11 @@ const auto normalize_axis_to = [](const int64_t& tensor_rank) { std::string normalize_axis_error_msg(const int64_t& axis, const int64_t& lower, const int64_t& upper) { return std::string(" Parameter axis ") - .append(to_string(axis)) + .append(std::to_string(axis)) .append(" out of the tensor rank range [") - .append(to_string(lower)) + .append(std::to_string(lower)) .append(", ") - .append(to_string(upper)) + .append(std::to_string(upper)) .append("]."); } } // namespace @@ -920,10 +1107,10 @@ int64_t ov::normalize_axis(const Node* node, std::int64_t axis, const Rank& tens int64_t ov::normalize_axis(const std::string& node_description, std::int64_t axis, const Rank& tensor_rank) { if (axis < 0) { // Handling negative axis requires static tensor rank - NGRAPH_CHECK(tensor_rank.is_static(), - node_description, - " Rank must be static in order to normalize negative axis=", - axis); + OPENVINO_ASSERT(tensor_rank.is_static(), + node_description, + " Rank must be static in order to normalize negative axis=", + axis); } if (tensor_rank.is_dynamic()) { return axis; @@ -957,233 +1144,6 @@ int64_t ov::normalize_axis(const std::string& node_description, return util::normalize(axis, tensor_rank); } -void ngraph::opset1::infer_conv_backprop_auto_padding(const Shape& input_data_shape, - const Shape& filters_shape, - const Shape& output_shape, - const Strides& strides, - const Strides& dilations, - const op::PadType auto_pad_type, - const CoordinateDiff& output_padding, - CoordinateDiff& pads_begin, - CoordinateDiff& pads_end) { - NGRAPH_CHECK(auto_pad_type == op::PadType::SAME_UPPER || auto_pad_type == op::PadType::SAME_LOWER); - - size_t num_spatial_dims = input_data_shape.size(); - NGRAPH_CHECK(filters_shape.size() == num_spatial_dims && strides.size() == num_spatial_dims && - dilations.size() == num_spatial_dims && pads_begin.size() == num_spatial_dims && - pads_end.size() == num_spatial_dims && output_padding.size() == num_spatial_dims); - - pads_begin = CoordinateDiff(num_spatial_dims); - pads_end = CoordinateDiff(num_spatial_dims); - - for (uint64_t i = 0; i < num_spatial_dims; ++i) { - int total_padding = std::max( - static_cast(strides[i] * (input_data_shape[i] - 1) + dilations[i] * (filters_shape[i] - 1) + 1 - - output_shape[i] + output_padding[i]), - 0); - if (auto_pad_type != op::PadType::SAME_UPPER) { - pads_begin[i] = total_padding / 2; - pads_end[i] = total_padding - pads_begin[i]; - } else { - pads_end[i] = total_padding / 2; - pads_begin[i] = total_padding - pads_end[i]; - } - } -} - -namespace { -/// \brief Scalar variant describes value of an Output, for use in max shape determination -/// -/// For tensor values, we use the maximum value in the tensor -struct MaxValue { - /// \brief No information known about the output - MaxValue() = default; - /// \brief uint64_t assoiated with the output - MaxValue(uint64_t value) : m_value(value) {} - MaxValue(const vector& slices, int64_t slice_axis) : m_slices(slices), m_slice_axis(slice_axis) { - m_value = *max_element(m_slices.begin(), m_slices.end()); - } - uint64_t m_value{numeric_limits::max()}; - vector m_slices; - int64_t m_slice_axis{-1}; -}; - -vector exec_constant(ngraph::Node* node, vector& inputs) { - auto result = MaxValue(); - auto op = ov::as_type(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()) { - if (max_val < static_cast(elt)) { - max_val = elt; - } - } - } else { - for (auto elt : op->cast_vector()) { - if (max_val < elt) { - max_val = elt; - } - } - } - result = MaxValue(max_val); - } - return {result}; -} - -vector exec_minimum(ngraph::Node* node, vector& inputs) { - uint64_t min_value = numeric_limits::max(); - switch (node->get_output_element_type(0)) { - case ngraph::element::Type_t::i8: - min_value = numeric_limits::max(); - break; - case ngraph::element::Type_t::i16: - min_value = numeric_limits::max(); - break; - case ngraph::element::Type_t::i32: - min_value = numeric_limits::max(); - break; - case ngraph::element::Type_t::i64: - min_value = numeric_limits::max(); - break; - case ngraph::element::Type_t::u8: - min_value = numeric_limits::max(); - break; - case ngraph::element::Type_t::u16: - min_value = numeric_limits::max(); - break; - case ngraph::element::Type_t::u32: - min_value = numeric_limits::max(); - break; - case ngraph::element::Type_t::u64: - min_value = numeric_limits::max(); - break; - default: - break; - } - min_value = min(min_value, inputs.at(0).m_value); - min_value = min(min_value, inputs.at(1).m_value); - return {MaxValue(min_value)}; -} - -vector exec_concat(ngraph::Node* node, vector& inputs) { - auto op = ov::as_type(node); - vector 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)}; -} - -vector exec_reduce_min(ngraph::Node* node, vector& inputs) { - auto data = inputs.at(0); - if (data.m_slice_axis >= 0 && data.m_slices.size() > 1) { - if (auto indices_const = ov::as_type(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 == ngraph::Shape{1}) { - auto indices = indices_const->cast_vector(); - 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)}; -} - -vector exec_shape_of(ngraph::Node* node, vector& inputs) { - const auto& inputPS = node->get_input_partial_shape(0); - std::vector 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::max()); - } - } - - return {MaxValue(shapeDims, 0)}; -} - -vector exec_gather(ngraph::Node* node, vector& inputs) { - auto gather = ov::as_type(node); - - const auto& indices = ov::as_type_ptr(node->input_value(1).get_node_shared_ptr()); - const auto& axis = ov::as_type_ptr(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(); - if (indicesVec.size() != 1 || indicesVec[0] >= static_cast(inputs[0].m_slices.size())) { - return {MaxValue()}; - } - - return {MaxValue(inputs[0].m_slices[indicesVec[0]])}; -} - -vector exec_nop(ngraph::Node* node, vector& inputs) { - return {inputs.at(0)}; -} -} // namespace - -pair ngraph::maximum_value(const Output& value) { - static Evaluator::op_handler_map handlers = {{op::v0::Concat::get_type_info_static(), exec_concat}, - {op::v0::Constant::get_type_info_static(), exec_constant}, - {op::v0::Convert::get_type_info_static(), exec_nop}, - {op::v1::Gather::get_type_info_static(), exec_gather}, - {op::v1::Minimum::get_type_info_static(), exec_minimum}, - {op::v1::ReduceMin::get_type_info_static(), exec_reduce_min}, - {op::v1::Reshape::get_type_info_static(), exec_nop}, - {op::v3::ShapeOf::get_type_info_static(), exec_shape_of}, - {op::v0::Squeeze::get_type_info_static(), exec_nop}, - {op::v0::Unsqueeze::get_type_info_static(), exec_nop}}; - Evaluator::value_map value_map; - Evaluator evaluator(handlers, value_map); - auto val = evaluator.evaluate(value); - return pair(val.m_value < numeric_limits::max(), val.m_value); -} - -void ngraph::evaluate_nodes(std::map& value_map, - std::map& output_tensor_map, - const OutputVector& outputs, - const EvaluationContext& evaluation_context) { - Evaluator evaluator({}, value_map); - evaluator.set_universal_handler( - [&output_tensor_map, &evaluation_context](Node* node, - const HostTensorVector& input_tensors) -> HostTensorVector { - HostTensorVector output_tensors; - for (const auto& v : node->outputs()) { - auto it = output_tensor_map.find(v); - if (it == output_tensor_map.end()) { - auto c = make_shared(v); - output_tensors.push_back(c); - } else { - output_tensors.push_back(it->second); - } - } - if (node->evaluate(output_tensors, input_tensors, evaluation_context)) { - return output_tensors; - } else { - NGRAPH_CHECK(false, "Evaluation failed on ", node); - } - }); - for (const auto& value : outputs) { - evaluator.evaluate(value); - } -} - bool ov::evaluate_as_partial_shape(const Output& output, PartialShape& pshape) { Tensor lb, ub; std::tie(lb, ub) = ov::evaluate_both_bounds(output); @@ -1193,14 +1153,14 @@ bool ov::evaluate_as_partial_shape(const Output& output, PartialShape& psh ->cast_vector(); auto upper_bound = std::make_shared(ub.get_element_type(), ub.get_shape(), ub.data()) ->cast_vector(); - NGRAPH_CHECK(lower_bound.size() == upper_bound.size()); + OPENVINO_ASSERT(lower_bound.size() == upper_bound.size()); const TensorLabel& labels = output.get_tensor().get_value_label(); - NGRAPH_CHECK(labels.empty() || lower_bound.size() == labels.size()); + OPENVINO_ASSERT(labels.empty() || lower_bound.size() == labels.size()); - vector resulting_pshape(lower_bound.size()); + std::vector resulting_pshape(lower_bound.size()); for (size_t i = 0; i < lower_bound.size(); ++i) { auto low = lower_bound[i], up = upper_bound[i]; - NGRAPH_CHECK(low >= 0 && up >= 0, "Value for partial shape evaluation can't be lower than zero."); + OPENVINO_ASSERT(low >= 0 && up >= 0, "Value for partial shape evaluation can't be lower than zero."); if (output.get_element_type() == element::i32 && low != up) { if (up == std::numeric_limits::max()) up = std::numeric_limits::max(); @@ -1221,104 +1181,10 @@ bool ov::default_label_evaluator(const Node* node, TensorLabelVector& output_lab return default_label_evaluator(node, {0}, output_labels); } -shared_ptr ngraph::get_constant_max_of_type(element::Type_t t) { -#define NGRAPH_TYPE_TO_MAX_CONST(t) \ - case t: \ - return ov::op::v0::Constant::create( \ - t, \ - {}, \ - {std::numeric_limits::value_type>::max()}); \ - break - - switch (t) { - NGRAPH_TYPE_TO_MAX_CONST(element::boolean); - NGRAPH_TYPE_TO_MAX_CONST(element::bf16); - NGRAPH_TYPE_TO_MAX_CONST(element::f16); - NGRAPH_TYPE_TO_MAX_CONST(element::f32); - NGRAPH_TYPE_TO_MAX_CONST(element::f64); - NGRAPH_TYPE_TO_MAX_CONST(element::i8); - NGRAPH_TYPE_TO_MAX_CONST(element::i16); - NGRAPH_TYPE_TO_MAX_CONST(element::i32); - NGRAPH_TYPE_TO_MAX_CONST(element::i64); - NGRAPH_TYPE_TO_MAX_CONST(element::u1); - NGRAPH_TYPE_TO_MAX_CONST(element::u8); - NGRAPH_TYPE_TO_MAX_CONST(element::u16); - NGRAPH_TYPE_TO_MAX_CONST(element::u32); - NGRAPH_TYPE_TO_MAX_CONST(element::u64); - default: - return nullptr; - } -} - -shared_ptr ngraph::get_constant_min_of_type(element::Type_t t) { -#define NGRAPH_TYPE_TO_MIN_CONST(t) \ - case t: \ - return ov::op::v0::Constant::create( \ - t, \ - {}, \ - {std::numeric_limits::value_type>::min()}); \ - break - - switch (t) { - NGRAPH_TYPE_TO_MIN_CONST(element::boolean); - NGRAPH_TYPE_TO_MIN_CONST(element::bf16); - NGRAPH_TYPE_TO_MIN_CONST(element::f16); - NGRAPH_TYPE_TO_MIN_CONST(element::f32); - NGRAPH_TYPE_TO_MIN_CONST(element::f64); - NGRAPH_TYPE_TO_MIN_CONST(element::i8); - NGRAPH_TYPE_TO_MIN_CONST(element::i16); - NGRAPH_TYPE_TO_MIN_CONST(element::i32); - NGRAPH_TYPE_TO_MIN_CONST(element::i64); - NGRAPH_TYPE_TO_MIN_CONST(element::u1); - NGRAPH_TYPE_TO_MIN_CONST(element::u8); - NGRAPH_TYPE_TO_MIN_CONST(element::u16); - NGRAPH_TYPE_TO_MIN_CONST(element::u32); - NGRAPH_TYPE_TO_MIN_CONST(element::u64); - default: - return nullptr; - } -} - -std::shared_ptr ngraph::get_constant_lowest_of_type(element::Type_t t) { -#define NGRAPH_TYPE_TO_LOWEST_CONST(t) \ - case t: \ - return op::v0::Constant::create(t, \ - {}, \ - {std::numeric_limits::value_type>::lowest()}); \ - break - - switch (t) { - NGRAPH_TYPE_TO_LOWEST_CONST(element::boolean); - NGRAPH_TYPE_TO_LOWEST_CONST(element::bf16); - NGRAPH_TYPE_TO_LOWEST_CONST(element::f16); - NGRAPH_TYPE_TO_LOWEST_CONST(element::f32); - NGRAPH_TYPE_TO_LOWEST_CONST(element::f64); - NGRAPH_TYPE_TO_LOWEST_CONST(element::i8); - NGRAPH_TYPE_TO_LOWEST_CONST(element::i16); - NGRAPH_TYPE_TO_LOWEST_CONST(element::i32); - NGRAPH_TYPE_TO_LOWEST_CONST(element::i64); - NGRAPH_TYPE_TO_LOWEST_CONST(element::u1); - NGRAPH_TYPE_TO_LOWEST_CONST(element::u8); - NGRAPH_TYPE_TO_LOWEST_CONST(element::u16); - NGRAPH_TYPE_TO_LOWEST_CONST(element::u32); - NGRAPH_TYPE_TO_LOWEST_CONST(element::u64); - - case element::undefined: - case element::dynamic: - default: - return nullptr; - } -} - -shared_ptr ov::get_constant_from_source(const Output& source) { +std::shared_ptr ov::get_constant_from_source(const Output& source) { return ov::util::get_constant_from_source(source); } -bool ngraph::validate_host_tensor_vector(const HostTensorVector& tensor_vector, const size_t& size) { - return (tensor_vector.size() == size) && - std::none_of(tensor_vector.cbegin(), tensor_vector.cend(), ov::cmp::Equal(nullptr)); -} - bool ov::has_no_labels(const ov::TensorLabel& labels) { return std::all_of(labels.cbegin(), labels.cend(), cmp::Equal(no_label)); } @@ -1381,6 +1247,130 @@ std::shared_ptr ov::util::constantfold_subgraph(const Outp return ov::as_type_ptr(outputs[subgraph_sink.get_index()].get_node_shared_ptr()); } +// +// Infers the output batch shape and element type for convolution fprop. +// +ov::PartialShape ov::infer_convolution_forward(const Node* node, + const PartialShape& data_batch_shape, + const Strides& data_dilation, + const CoordinateDiff& data_padding_below, + const CoordinateDiff& data_padding_above, + const PartialShape& filters_shape, + const Strides& filter_strides, + const Strides& filter_dilation) { + Rank data_batch_filters_rank{Rank::dynamic()}; + + NODE_VALIDATION_CHECK(node, + Rank::merge(data_batch_filters_rank, data_batch_shape.rank(), filters_shape.rank()), + "Data batch and filters rank do not match (data batch shape: ", + data_batch_shape, + ", filters shape: ", + filters_shape, + ")."); + + NODE_VALIDATION_CHECK(node, + data_batch_filters_rank.is_dynamic() || data_batch_filters_rank.get_length() >= 3, + "Data batch and filters must have rank of at least 3 (one batch axis, ", + "one input-channel axis, and at least one spatial dimension) ", + "(data batch shape: ", + data_batch_shape, + ", filters shape: ", + filters_shape, + ")."); + + Rank spatial_rank{Rank::dynamic()}; + NODE_VALIDATION_CHECK(node, + Rank::merge(spatial_rank, spatial_rank, data_batch_filters_rank - 2) && + Rank::merge(spatial_rank, spatial_rank, data_dilation.size()) && + Rank::merge(spatial_rank, spatial_rank, data_padding_below.size()) && + Rank::merge(spatial_rank, spatial_rank, data_padding_above.size()) && + Rank::merge(spatial_rank, spatial_rank, filter_strides.size()) && + Rank::merge(spatial_rank, spatial_rank, filter_dilation.size()), + "Ranks for data item shape/filters shape (data batch has shape ", + data_batch_shape, + ", so data item rank is ", + (data_batch_shape.rank() - 2), + " and filters have shape ", + filters_shape, + ", so filters spatial rank is ", + (filters_shape.rank() - 2), + "), data dilation (", + data_dilation, + "), padding below (", + data_padding_below, + "), padding above (", + data_padding_above, + "), filter strides (", + filter_strides, + "), and filter dilation (", + filter_dilation, + ") do not match."); + + Dimension batch_size = (data_batch_shape.rank().is_static() ? data_batch_shape[0] : Dimension::dynamic()); + Dimension data_channel_count = (data_batch_shape.rank().is_static() ? data_batch_shape[1] : Dimension::dynamic()); + PartialShape data_spatial_shape(PartialShape::dynamic(spatial_rank)); + + Dimension filter_output_channel_count = + (filters_shape.rank().is_static() ? filters_shape[0] : Dimension::dynamic()); + Dimension filter_input_channel_count = (filters_shape.rank().is_static() ? filters_shape[1] : Dimension::dynamic()); + PartialShape filter_spatial_shape(PartialShape::dynamic(spatial_rank)); + + // + // Note: spatial_rank is definitely static at this point. + // + + for (int64_t i = 0; i < spatial_rank.get_length(); i++) { + if (data_batch_shape.rank().is_static()) { + data_spatial_shape[i] = data_batch_shape[i + 2]; + } + + if (filters_shape.rank().is_static()) { + filter_spatial_shape[i] = filters_shape[i + 2]; + } + } + + NODE_VALIDATION_CHECK(node, batch_size.is_dynamic() || batch_size.get_length() > 0, "Batch size is zero."); + + Dimension merged_channel_count; + + NODE_VALIDATION_CHECK(node, + Dimension::merge(merged_channel_count, data_channel_count, filter_input_channel_count), + "Data batch channel count (", + data_channel_count, + ") does not match filter input ", + "channel count (", + filter_input_channel_count, + ")."); + + NODE_VALIDATION_CHECK(node, + merged_channel_count.is_dynamic() || merged_channel_count.get_length() > 0, + "Data batch channel count and/or filter input channel count is zero."); + + NODE_VALIDATION_CHECK(node, + filter_output_channel_count.is_dynamic() || filter_output_channel_count.get_length() > 0, + "Filter output channel count is zero."); + + PartialShape data_output_shape = ngraph::infer_windowed_reduction_output_shape(node, + data_spatial_shape, + data_dilation, + data_padding_below, + data_padding_above, + filter_spatial_shape, + filter_strides, + filter_dilation, + true); + + PartialShape batch_output_shape(PartialShape::dynamic(spatial_rank + 2)); + batch_output_shape[0] = batch_size; + batch_output_shape[1] = filter_output_channel_count; + + for (int64_t i = 0; i < spatial_rank.get_length(); i++) { + batch_output_shape[i + 2] = data_output_shape[i]; + } + + return batch_output_shape; +} + namespace ov { namespace util { using ov::op::v0::Constant; diff --git a/src/inference/src/ie_core.cpp b/src/inference/src/ie_core.cpp index 97dc6382aac..a5babd91927 100644 --- a/src/inference/src/ie_core.cpp +++ b/src/inference/src/ie_core.cpp @@ -31,10 +31,6 @@ #include "ie_plugin_config.hpp" #include "ie_remote_context.hpp" #include "itt.hpp" -#include "ngraph/graph_util.hpp" -#include "ngraph/ngraph.hpp" -#include "ngraph/opsets/opset.hpp" -#include "ngraph/pass/constant_folding.hpp" #include "openvino/core/except.hpp" #include "openvino/core/so_extension.hpp" #include "openvino/op/parameter.hpp"