Skip to content

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

[core] OV model class clean-up (#30019)

Pawel Raasz08988e51f5a4
diff --git a/src/core/include/openvino/core/model.hpp b/src/core/include/openvino/core/model.hpp
index 820e17e7acd..64a60ce1d25 100644
--- a/src/core/include/openvino/core/model.hpp
+++ b/src/core/include/openvino/core/model.hpp
@@ -332,24 +332,14 @@ public:
*
* @return constant reference to value from runtime info
*/
- template <class T, class... Args, typename std::enable_if<!std::is_same<T, ov::Any>::value, bool>::type = true>
- const T& get_rt_info(Args... args) const {
- const ov::Any& arg = get_rt_arg<Args...>(m_rt_info, args...);
- return arg.as<T>();
- }
- /**
- * @brief Returns a runtime attribute for the path, throws an ov::Exception if path doesn't exist
- *
- * @tparam T the type of returned value
- * @tparam Args types of variadic arguments
- * @param args path to the runtime attribute
- *
- * @return constant reference to value from runtime info
- */
- template <class T, class... Args, typename std::enable_if<std::is_same<T, ov::Any>::value, bool>::type = true>
+ template <class T, class... Args>
const T& get_rt_info(Args... args) const {
const ov::Any& arg = get_rt_arg<Args...>(m_rt_info, args...);
- return arg;
+ if constexpr (std::is_same_v<T, ov::Any>) {
+ return arg;
+ } else {
+ return arg.as<T>();
+ }
}
/**
@@ -360,24 +350,14 @@ public:
*
* @return constant reference to value from runtime info
*/
- template <class T, typename std::enable_if<!std::is_same<T, ov::Any>::value, bool>::type = true>
- const T& get_rt_info(const std::vector<std::string>& args) const {
- const ov::Any& arg = get_rt_info(m_rt_info, args.cbegin(), args.cend());
- return arg.as<T>();
- }
-
- /**
- * @brief Returns a runtime attribute for the path, throws an ov::Exception if path doesn't exist
- *
- * @tparam T the type of returned value
- * @param args vector with path to the runtime attribute
- *
- * @return constant reference to value from runtime info
- */
- template <class T, typename std::enable_if<std::is_same<T, ov::Any>::value, bool>::type = true>
+ template <class T>
const T& get_rt_info(const std::vector<std::string>& args) const {
const ov::Any& arg = get_rt_info(m_rt_info, args.cbegin(), args.cend());
- return arg;
+ if constexpr (std::is_same_v<T, ov::Any>) {
+ return arg;
+ } else {
+ return arg.as<T>();
+ }
}
/**
@@ -452,20 +432,20 @@ private:
const std::vector<std::string>::const_iterator& end) const;
// Checks rt attribute
- template <class T,
- typename std::enable_if<std::is_same<std::string, T>::value || std::is_same<T, const char*>::value ||
- std::is_same<T, char*>::value,
- bool>::type = true>
+ template <
+ class T,
+ std::enable_if_t<std::is_same_v<std::string, T> || std::is_same_v<T, const char*> || std::is_same_v<T, char*>,
+ bool> = true>
bool has_rt_arg(const ov::AnyMap& rt_info, const T& name) const {
return rt_info.find(name) != rt_info.end();
}
// Checks rt attribute
- template <class T,
- class... Args,
- typename std::enable_if<std::is_same<std::string, T>::value || std::is_same<T, const char*>::value ||
- std::is_same<T, char*>::value,
- bool>::type = true>
+ template <
+ class T,
+ class... Args,
+ std::enable_if_t<std::is_same_v<std::string, T> || std::is_same_v<T, const char*> || std::is_same_v<T, char*>,
+ bool> = true>
bool has_rt_arg(const ov::AnyMap& rt_info, const T& name, Args... args) const {
bool has_attr = has_rt_arg(rt_info, name);
if (!has_attr)
@@ -476,10 +456,10 @@ private:
}
// Allow to get constant attribute for variadic arguments
- template <class T,
- typename std::enable_if<std::is_same<std::string, T>::value || std::is_same<T, const char*>::value ||
- std::is_same<T, char*>::value,
- bool>::type = true>
+ template <
+ class T,
+ std::enable_if_t<std::is_same_v<std::string, T> || std::is_same_v<T, const char*> || std::is_same_v<T, char*>,
+ bool> = true>
const ov::Any& get_rt_arg(const ov::AnyMap& rt_info, const T& name) const {
OPENVINO_ASSERT(rt_info.find(name) != rt_info.end(),
"Cannot get runtime attribute. Path to runtime attribute is incorrect.");
@@ -487,11 +467,11 @@ private:
}
// Allow to get constant attribute for variadic arguments
- template <class T,
- class... Args,
- typename std::enable_if<std::is_same<std::string, T>::value || std::is_same<T, const char*>::value ||
- std::is_same<T, char*>::value,
- bool>::type = true>
+ template <
+ class T,
+ class... Args,
+ std::enable_if_t<std::is_same_v<std::string, T> || std::is_same_v<T, const char*> || std::is_same_v<T, char*>,
+ bool> = true>
const ov::Any& get_rt_arg(const ov::AnyMap& rt_info, const T& name, Args... args) const {
const ov::Any& rt_attr = get_rt_arg<T>(rt_info, name);
const ov::AnyMap& new_map = get_map_from_attr(rt_attr);
@@ -499,20 +479,20 @@ private:
}
// Allow to get attribute for variadic arguments
- template <class T,
- typename std::enable_if<std::is_same<std::string, T>::value || std::is_same<T, const char*>::value ||
- std::is_same<T, char*>::value,
- bool>::type = true>
+ template <
+ class T,
+ std::enable_if_t<std::is_same_v<std::string, T> || std::is_same_v<T, const char*> || std::is_same_v<T, char*>,
+ bool> = true>
ov::Any& get_rt_arg(ov::AnyMap& rt_info, const T& name) {
return get_attr(rt_info[name]);
}
// Allow to get attribute for variadic arguments
- template <class T,
- class... Args,
- typename std::enable_if<std::is_same<std::string, T>::value || std::is_same<T, const char*>::value ||
- std::is_same<T, char*>::value,
- bool>::type = true>
+ template <
+ class T,
+ class... Args,
+ std::enable_if_t<std::is_same_v<std::string, T> || std::is_same_v<T, const char*> || std::is_same_v<T, char*>,
+ bool> = true>
ov::Any& get_rt_arg(ov::AnyMap& rt_info, const T& name, Args... args) {
ov::Any& rt_attr = get_rt_arg<T>(rt_info, name);
ov::AnyMap& new_map = get_map_from_attr(rt_attr);
@@ -539,7 +519,6 @@ private:
static std::atomic<size_t> m_next_instance_id;
std::string m_name;
const std::string m_unique_name;
- size_t m_placement{0};
topological_sort_t m_topological_sorter;
ov::ResultVector m_results;
diff --git a/src/core/src/model.cpp b/src/core/src/model.cpp
index 86580f18e37..5e2b167d6f4 100644
--- a/src/core/src/model.cpp
+++ b/src/core/src/model.cpp
@@ -104,23 +104,23 @@ std::map<ov::Output<ov::Node>, ov::PartialShape> tensor_names_shapes_to_node_sha
const std::map<std::string, ov::PartialShape>& partial_shapes) {
std::map<ov::Output<ov::Node>, ov::PartialShape> const_pshape;
std::unordered_map<ov::Node*, std::string> port_tensor_map;
- for (const auto& it : partial_shapes) {
- const auto port = model->input(it.first);
+ for (const auto& [name, shape] : partial_shapes) {
+ const auto port = model->input(name);
if (port_tensor_map.find(port.get_node()) != port_tensor_map.end()) {
- OPENVINO_ASSERT(it.second == const_pshape.at(port),
+ OPENVINO_ASSERT(shape == const_pshape.at(port),
"Tensor with names {'",
- it.first,
+ name,
"', '",
port_tensor_map[port.get_node()],
"'} has "
"conflicting shapes ",
- it.second,
+ shape,
" and ",
const_pshape.at(port),
", but they define the same tensor");
}
- port_tensor_map[port.get_node()] = it.first;
- const_pshape[port] = it.second;
+ port_tensor_map[port.get_node()] = name;
+ const_pshape[port] = shape;
}
return const_pshape;
}
@@ -128,31 +128,13 @@ std::map<ov::Output<ov::Node>, ov::PartialShape> tensor_names_shapes_to_node_sha
} // namespace
ov::Model::Model(const ResultVector& results, const ov::ParameterVector& parameters, const std::string& name)
- : m_name(name),
- m_unique_name("Model" + to_string(m_next_instance_id.fetch_add(1))),
- m_topological_sorter(ov::topological_sort<std::vector<std::shared_ptr<ov::Node>>>),
- m_results(results),
- m_parameters(parameters) {
- prerequirements(true, false);
-}
+ : Model(results, {}, parameters, name) {}
ov::Model::Model(const OutputVector& results, const ov::ParameterVector& parameters, const std::string& name)
- : m_name(name),
- m_unique_name("Model" + to_string(m_next_instance_id.fetch_add(1))),
- m_topological_sorter(ov::topological_sort<std::vector<std::shared_ptr<ov::Node>>>),
- m_results(as_result_vector(results)),
- m_parameters(parameters) {
- prerequirements(true, false);
-}
+ : Model(as_result_vector(results), parameters, name) {}
ov::Model::Model(const NodeVector& results, const ov::ParameterVector& parameters, const std::string& name)
- : m_name(name),
- m_unique_name("Model" + to_string(m_next_instance_id.fetch_add(1))),
- m_topological_sorter(ov::topological_sort<std::vector<std::shared_ptr<ov::Node>>>),
- m_results(as_result_vector(as_output_vector(results))),
- m_parameters(parameters) {
- prerequirements(true, false);
-}
+ : Model(as_output_vector(results), parameters, name) {}
ov::Model::Model(const std::shared_ptr<Node>& result, const ov::ParameterVector& parameters, const std::string& name)
: Model(verify_node(result)->outputs(), parameters, name) {}
@@ -306,23 +288,24 @@ std::vector<shared_ptr<ov::Node>> ov::Model::get_ordered_ops() const {
lock_guard<mutex> lock(m_model_mutex);
NodeVector nodes;
+ auto node_inserter = std::back_inserter(nodes);
if (m_shared_rt_info->get_use_topological_cache()) {
for (const auto& node : m_cached_ordered_ops) {
if (auto locked_node = node.lock()) {
- nodes.emplace_back(locked_node);
+ *node_inserter = locked_node;
}
}
return nodes;
}
for (const auto& r : get_results()) {
- nodes.emplace_back(r);
+ *node_inserter = r;
}
for (auto& r : get_sinks()) {
- nodes.emplace_back(r);
+ *node_inserter = r;
}
for (auto& param : get_parameters()) {
- nodes.push_back(param);
+ *node_inserter = param;
}
auto order = m_topological_sorter(nodes);