Skip to content

Commit

Permalink
Apply CC to ov::OpSet (openvinotoolkit#13189)
Browse files Browse the repository at this point in the history
* Apply CC to ov::OpSet

* Add CC tests for OpSet

* Apply clang-format
  • Loading branch information
vurusovs authored Sep 28, 2022
1 parent 6717a0c commit b2f486f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/core/include/openvino/opsets/opset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class OPENVINO_API OpSet {

public:
OpSet() = default;
OpSet(const std::string& name);
virtual ~OpSet() = default;
std::set<NodeTypeInfo>::size_type size() const {
std::lock_guard<std::mutex> guard(get_mutex());
Expand Down Expand Up @@ -115,6 +116,7 @@ class OPENVINO_API OpSet {
}

ngraph::FactoryRegistry<ov::Node> m_factory_registry;
std::string m_name;
std::set<NodeTypeInfo> m_op_types;
std::map<std::string, NodeTypeInfo> m_name_type_info_map;
std::map<std::string, NodeTypeInfo> m_case_insensitive_type_info_map;
Expand Down Expand Up @@ -171,5 +173,9 @@ const OPENVINO_API OpSet& get_opset7();
* @ingroup ov_opset_cpp_api
*/
const OPENVINO_API OpSet& get_opset8();
/**
* @brief Returns opset9
* @ingroup ov_opset_cpp_api
*/
const OPENVINO_API OpSet& get_opset9();
} // namespace ov
14 changes: 14 additions & 0 deletions src/core/src/itt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,35 @@ OV_ITT_DOMAIN(ov_op, "ov::Op");
} // namespace itt
} // namespace ov
OV_CC_DOMAINS(ov_op);
OV_CC_DOMAINS(ov_opset);

/*
* REGISTER_OP registers Operation creation inside OpSet
* INSERT_OP macro allows to ignore some Operations inside OpSet if it's creation wasn't registered
*/
#if defined(SELECTIVE_BUILD_ANALYZER)
# define OV_OP_SCOPE(region) OV_SCOPE(ov_op, region)
# define OV_PASS_CALLBACK(matcher) \
openvino::itt::handle_t m_callback_handle; \
m_callback_handle = openvino::itt::handle(matcher->get_name()); \
OV_ITT_SCOPED_TASK(SIMPLE_ov_pass, m_callback_handle)
# define REGISTER_OP(opset_name, op_name) \
OV_ITT_SCOPED_TASK(SIMPLE_ov_opset, openvino::itt::handle(opset_name + "_" + op_name))
# define INSERT_OP(opset_name, op_name, op_namespace) opset.insert<op_namespace::op_name>()
#elif defined(SELECTIVE_BUILD)
# define OV_OP_SCOPE(region) \
if (OV_CC_SCOPE_IS_ENABLED(OV_PP_CAT3(ov_op, _, region)) == 0) \
throw ngraph::ngraph_error(std::string(OV_PP_TOSTRING(OV_PP_CAT3(ov_op, _, region))) + " is disabled!")
# define OV_PASS_CALLBACK(matcher)
# define REGISTER_OP(opset_name, op_name)
# define INSERT_OP(opset_name, op_name, op_namespace) \
if (OV_CC_SCOPE_IS_ENABLED(OV_PP_CAT4(ov_opset_, opset_name, _, op_name)) == 1) \
opset.insert<op_namespace::op_name>()
#else
# define OV_OP_SCOPE(region) OV_ITT_SCOPED_TASK(ov::itt::domains::ov_op, OV_PP_TOSTRING(region))
# define OV_PASS_CALLBACK(matcher)
# define REGISTER_OP(opset_name, op_name)
# define INSERT_OP(opset_name, op_name, op_namespace) opset.insert<op_namespace::op_name>()
#endif

#define NGRAPH_TYPE_CASE(region, a, ...) \
Expand Down
48 changes: 28 additions & 20 deletions src/core/src/opsets/opset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

#include "ngraph/opsets/opset.hpp"

#include "../itt.hpp"
#include "ngraph/log.hpp"
#include "ngraph/ops.hpp"

ngraph::OpSet::OpSet(const ov::OpSet& opset) : ov::OpSet(opset) {}

ov::OpSet::OpSet(const std::string& name) : m_name(name) {}

std::mutex& ov::OpSet::get_mutex() {
static std::mutex opset_mutex;
return opset_mutex;
Expand All @@ -20,108 +23,113 @@ ov::Node* ov::OpSet::create(const std::string& name) const {
NGRAPH_WARN << "Couldn't create operator of type: " << name << " . Operation not registered in opset.";
return nullptr;
}
REGISTER_OP(m_name, name);
return m_factory_registry.create(type_info_it->second);
}

ov::Node* ov::OpSet::create_insensitive(const std::string& name) const {
auto type_info_it = m_case_insensitive_type_info_map.find(to_upper_name(name));
return type_info_it == m_case_insensitive_type_info_map.end() ? nullptr
: m_factory_registry.create(type_info_it->second);
if (type_info_it == m_case_insensitive_type_info_map.end()) {
NGRAPH_WARN << "Couldn't create operator of type: " << name << " . Operation not registered in opset.";
return nullptr;
}
REGISTER_OP(m_name, name);
return m_factory_registry.create(type_info_it->second);
}

const ov::OpSet& ov::get_opset1() {
static OpSet opset;
static OpSet opset("opset1");
static std::once_flag flag;
std::call_once(flag, [&]() {
#define _OPENVINO_OP_REG(NAME, NAMESPACE) opset.insert<NAMESPACE::NAME>();
#define _OPENVINO_OP_REG(NAME, NAMESPACE) INSERT_OP(opset1, NAME, NAMESPACE);
#include "openvino/opsets/opset1_tbl.hpp"
#undef _OPENVINO_OP_REG
});
return opset;
}

const ov::OpSet& ov::get_opset2() {
static OpSet opset;
static OpSet opset("opset2");
static std::once_flag flag;
std::call_once(flag, [&]() {
#define _OPENVINO_OP_REG(NAME, NAMESPACE) opset.insert<NAMESPACE::NAME>();
#define _OPENVINO_OP_REG(NAME, NAMESPACE) INSERT_OP(opset2, NAME, NAMESPACE);
#include "openvino/opsets/opset2_tbl.hpp"
#undef _OPENVINO_OP_REG
});
return opset;
}

const ov::OpSet& ov::get_opset3() {
static OpSet opset;
static OpSet opset("opset3");
static std::once_flag flag;
std::call_once(flag, [&]() {
#define _OPENVINO_OP_REG(NAME, NAMESPACE) opset.insert<NAMESPACE::NAME>();
#define _OPENVINO_OP_REG(NAME, NAMESPACE) INSERT_OP(opset3, NAME, NAMESPACE);
#include "openvino/opsets/opset3_tbl.hpp"
#undef _OPENVINO_OP_REG
});
return opset;
}

const ov::OpSet& ov::get_opset4() {
static OpSet opset;
static OpSet opset("opset4");
static std::once_flag flag;
std::call_once(flag, [&]() {
#define _OPENVINO_OP_REG(NAME, NAMESPACE) opset.insert<NAMESPACE::NAME>();
#define _OPENVINO_OP_REG(NAME, NAMESPACE) INSERT_OP(opset4, NAME, NAMESPACE);
#include "openvino/opsets/opset4_tbl.hpp"
#undef _OPENVINO_OP_REG
});
return opset;
}

const ov::OpSet& ov::get_opset5() {
static OpSet opset;
static OpSet opset("opset5");
static std::once_flag flag;
std::call_once(flag, [&]() {
#define _OPENVINO_OP_REG(NAME, NAMESPACE) opset.insert<NAMESPACE::NAME>();
#define _OPENVINO_OP_REG(NAME, NAMESPACE) INSERT_OP(opset5, NAME, NAMESPACE);
#include "openvino/opsets/opset5_tbl.hpp"
#undef _OPENVINO_OP_REG
});
return opset;
}

const ov::OpSet& ov::get_opset6() {
static OpSet opset;
static OpSet opset("opset6");
static std::once_flag flag;
std::call_once(flag, [&]() {
#define _OPENVINO_OP_REG(NAME, NAMESPACE) opset.insert<NAMESPACE::NAME>();
#define _OPENVINO_OP_REG(NAME, NAMESPACE) INSERT_OP(opset6, NAME, NAMESPACE);
#include "openvino/opsets/opset6_tbl.hpp"
#undef _OPENVINO_OP_REG
});
return opset;
}

const ov::OpSet& ov::get_opset7() {
static OpSet opset;
static OpSet opset("opset7");
static std::once_flag flag;
std::call_once(flag, [&]() {
#define _OPENVINO_OP_REG(NAME, NAMESPACE) opset.insert<NAMESPACE::NAME>();
#define _OPENVINO_OP_REG(NAME, NAMESPACE) INSERT_OP(opset7, NAME, NAMESPACE);
#include "openvino/opsets/opset7_tbl.hpp"
#undef _OPENVINO_OP_REG
});
return opset;
}

const ov::OpSet& ov::get_opset8() {
static OpSet opset;
static OpSet opset("opset8");
static std::once_flag flag;
std::call_once(flag, [&]() {
#define _OPENVINO_OP_REG(NAME, NAMESPACE) opset.insert<NAMESPACE::NAME>();
#define _OPENVINO_OP_REG(NAME, NAMESPACE) INSERT_OP(opset8, NAME, NAMESPACE);
#include "openvino/opsets/opset8_tbl.hpp"
#undef _OPENVINO_OP_REG
});
return opset;
}

const ov::OpSet& ov::get_opset9() {
static OpSet opset;
static OpSet opset("opset9");
static std::once_flag flag;
std::call_once(flag, [&]() {
#define _OPENVINO_OP_REG(NAME, NAMESPACE) opset.insert<NAMESPACE::NAME>();
#define _OPENVINO_OP_REG(NAME, NAMESPACE) INSERT_OP(opset9, NAME, NAMESPACE);
#include "openvino/opsets/opset9_tbl.hpp"
#undef _OPENVINO_OP_REG
});
Expand Down
18 changes: 16 additions & 2 deletions src/core/tests/conditional_compilation/ngraph_cc_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//

#include <ngraph/except.hpp>
#include <openvino/op/abs.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/opsets/opset.hpp>

#include "gtest/gtest.h"

Expand All @@ -24,18 +27,29 @@ TEST(conditional_compilation, collect_op_scope) {
#define ov_op_Scope0 1
int n = 0;

// Simple scope is enabled
// Simple Scope0 is enabled
OV_OP_SCOPE(Scope0);
n = 42;
EXPECT_EQ(n, 42);

// Simple scope is disabled
// Simple Scope1 is enabled regardless of macros
OV_OP_SCOPE(Scope1);
n = 43;
EXPECT_EQ(n, 43);
#undef ov_op_Scope0
}

TEST(conditional_compilation, collect_ops_in_opset) {
ov::OpSet opset("test_opset1");
INSERT_OP(test_opset1, Abs, ov::op::v0);
EXPECT_NE(opset.create("Abs"), nullptr);
EXPECT_NE(opset.create_insensitive("Abs"), nullptr);

INSERT_OP(test_opset1, Constant, ov::op::v0);
EXPECT_NE(opset.create("Constant"), nullptr);
EXPECT_NE(opset.create_insensitive("Constant"), nullptr);
}

#undef SELECTIVE_BUILD_ANALYZER

#ifdef SELECTIVE_BUILD_ANALYZER_ON
Expand Down
18 changes: 16 additions & 2 deletions src/core/tests/conditional_compilation/ngraph_cc_off.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//

#include <ngraph/except.hpp>
#include <openvino/op/abs.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/opsets/opset.hpp>

#include "gtest/gtest.h"

Expand All @@ -21,17 +24,28 @@ using namespace std;
TEST(conditional_compilation, op_scope_with_disabled_cc) {
int n = 0;

// Simple scope is enabled
// Simple Scope0 is enabled
OV_OP_SCOPE(Scope0);
n = 42;
EXPECT_EQ(n, 42);

// Simple scope is disabled
// Simple Scope1 is enabled regardless of macros
OV_OP_SCOPE(Scope1);
n = 43;
EXPECT_EQ(n, 43);
}

TEST(conditional_compilation, all_ops_enabled_in_opset) {
ov::OpSet opset("test_opset2");
INSERT_OP(test_opset2, Abs, ov::op::v0);
EXPECT_NE(opset.create("Abs"), nullptr);
EXPECT_NE(opset.create_insensitive("Abs"), nullptr);

INSERT_OP(test_opset2, Constant, ov::op::v0);
EXPECT_NE(opset.create("Constant"), nullptr);
EXPECT_NE(opset.create_insensitive("Constant"), nullptr);
}

#ifdef SELECTIVE_BUILD_ANALYZER_ON
# define SELECTIVE_BUILD_ANALYZER
#elif defined(SELECTIVE_BUILD_ON)
Expand Down
21 changes: 18 additions & 3 deletions src/core/tests/conditional_compilation/ngraph_cc_on.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//

#include <ngraph/except.hpp>
#include <openvino/op/abs.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/opsets/opset.hpp>

#include "gtest/gtest.h"

Expand All @@ -23,18 +26,30 @@ using namespace std;
TEST(conditional_compilation, disabled_op_scope) {
#define ov_op_Scope0 1
int n = 0;
const std::string errMsg = "ngraph_op_Scope1 is disabled!";

// Simple scope is enabled
// Simple Scope0 is enabled
OV_OP_SCOPE(Scope0);
n = 42;
EXPECT_EQ(n, 42);

// Simple scope is disabled
// Simple Scope1 is disabled and throws exception
ASSERT_THROW(OV_OP_SCOPE(Scope1), ngraph::ngraph_error);
#undef ov_op_Scope0
}

TEST(conditional_compilation, disabled_Constant_in_opset) {
ov::OpSet opset("test_opset3");
#define ov_opset_test_opset3_Abs 1
INSERT_OP(test_opset3, Abs, ov::op::v0);
EXPECT_NE(opset.create("Abs"), nullptr);
EXPECT_NE(opset.create_insensitive("Abs"), nullptr);
#undef ov_opset_test_opset3_Abs

INSERT_OP(test_opset3, Constant, ov::op::v0);
EXPECT_EQ(opset.create("Constant"), nullptr);
EXPECT_EQ(opset.create_insensitive("Constant"), nullptr);
}

#undef SELECTIVE_BUILD

#ifdef SELECTIVE_BUILD_ANALYZER_ON
Expand Down

0 comments on commit b2f486f

Please sign in to comment.