Skip to content

Commit

Permalink
[core]Optimize OV assert macors to reduce CPU plugin binary size (ope…
Browse files Browse the repository at this point in the history
…nvinotoolkit#21180)

* Optimize OV assertions to reduce bin-size of libs

* Migrate assertion leftovers in CPU plugin

* Add NotImplemented::create to support use with macro
OPENVINO_ASSERT_HELPER

* Remove CheckLocInfo struct

---------

Co-authored-by: Ilya Lavrenov <ilya.lavrenov@intel.com>
  • Loading branch information
praasz and ilya-lavrenov authored Nov 21, 2023
1 parent d2dcdf8 commit 5d6d6a2
Show file tree
Hide file tree
Showing 57 changed files with 358 additions and 278 deletions.
1 change: 0 additions & 1 deletion src/core/include/ngraph/check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace ngraph {
using ov::write_all_to_stream;

using CheckFailure = ov::AssertFailure;
using ov::CheckLocInfo;
} // namespace ngraph

#define NGRAPH_CHECK_HELPER2(exc_class, ctx, check, ...) OPENVINO_ASSERT_HELPER2(exc_class, ctx, check, __VA_ARGS__)
Expand Down
91 changes: 51 additions & 40 deletions src/core/include/openvino/core/except.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,56 @@

namespace ov {

struct CheckLocInfo {
const char* file;
int line;
const char* check_string;
};

/// Base error for ov runtime errors.
class OPENVINO_API Exception : public std::runtime_error {
public:
OPENVINO_DEPRECATED("This constructor is deprecated and will be removed, please use OPENVINO_THROW instead")
explicit Exception(const std::string& what_arg);

[[noreturn]] static void create(const CheckLocInfo& check_loc_info, const std::string& explanation);
[[noreturn]] static void create(const char* file, int line, const std::string& explanation);
virtual ~Exception();

static const std::string default_msg;

protected:
static std::string make_what(const CheckLocInfo& check_loc_info,
static std::string make_what(const char* file,
int line,
const char* check_string,
const std::string& context_info,
const std::string& explanation);
};

static inline std::ostream& write_all_to_stream(std::ostream& str) {
return str;
}

template <typename T, typename... TS>
static inline std::ostream& write_all_to_stream(std::ostream& str, const T& arg, TS&&... args) {
return write_all_to_stream(str << arg, args...);
std::ostream& write_all_to_stream(std::ostream& str, T&& arg, TS&&... args) {
return write_all_to_stream(str << arg, std::forward<TS>(args)...);
}

template <class T>
static inline std::string stringify(T&& arg) {
template <class T,
typename std::enable_if<!std::is_same<typename std::decay<T>::type, std::string>::value>::type* = nullptr>
std::string stringify(T&& arg) {
std::stringstream stream;
stream << arg;
return stream.str();
}

template <class T,
typename std::enable_if<std::is_same<typename std::decay<T>::type, std::string>::value>::type* = nullptr>
T& stringify(T&& arg) {
return arg;
}

/// Base class for check failure exceptions.
class OPENVINO_API AssertFailure : public Exception {
public:
[[noreturn]] static void create(const CheckLocInfo& check_loc_info,
[[noreturn]] static void create(const char* file,
int line,
const char* check_string,
const std::string& context_info,
const std::string& explanation);
~AssertFailure() override;

protected:
OPENVINO_SUPPRESS_DEPRECATED_START
Expand All @@ -68,10 +73,15 @@ class OPENVINO_API AssertFailure : public Exception {
/// Exception class to be thrown on not implemented code
class OPENVINO_API NotImplemented : public AssertFailure {
public:
[[noreturn]] static void create(const CheckLocInfo& check_loc_info,
const std::string& context_info,
const std::string& explanation);
~NotImplemented() override;
[[noreturn]] static void create(const char* file, int line, const std::string& explanation);

[[noreturn]] OPENVINO_DEPRECATED(
"This function is deprecated and will be removed, please use "
"OPENVINO_THROW_NOT_IMPLEMENTED instead") static void create(const char* file,
int line,
const char*,
const std::string&,
const std::string& explanation);

static const std::string default_msg;

Expand Down Expand Up @@ -142,36 +152,36 @@ class OPENVINO_API NotImplemented : public AssertFailure {
// The "..." may be filled with expressions of any type that has an "operator<<" overload for
// insertion into std::ostream.
//
#define OPENVINO_ASSERT_HELPER2(exc_class, ctx, check, ...) \
do { \
if (!(check)) { \
::std::stringstream ss___; \
::ov::write_all_to_stream(ss___, __VA_ARGS__); \
exc_class::create((::ov::CheckLocInfo{__FILE__, __LINE__, #check}), (ctx), ss___.str()); \
} \
#define OPENVINO_ASSERT_HELPER2(exc_class, ctx, check, ...) \
do { \
if (!(check)) { \
::std::ostringstream ss___; \
::ov::write_all_to_stream(ss___, __VA_ARGS__); \
exc_class::create(__FILE__, __LINE__, (#check), (ctx), ss___.str()); \
} \
} while (0)

#define OPENVINO_ASSERT_HELPER1(exc_class, ctx, check) \
do { \
if (!(check)) { \
exc_class::create((::ov::CheckLocInfo{__FILE__, __LINE__, #check}), (ctx), exc_class::default_msg); \
} \
#define OPENVINO_ASSERT_HELPER1(exc_class, ctx, check) \
do { \
if (!(check)) { \
exc_class::create(__FILE__, __LINE__, (#check), (ctx), exc_class::default_msg); \
} \
} while (0)

#define OPENVINO_ASSERT_HELPER(exc_class, ctx, ...) CALL_OVERLOAD(OPENVINO_ASSERT_HELPER, exc_class, ctx, __VA_ARGS__)

// Helper macros for OPENVINO_THROW which is special case of OPENVINO_ASSERT_HELPER without some not required
// parameters for ov::Exception, as result reduce binary size.
#define OPENVINO_THROW_HELPER2(exc_class, ctx, ...) \
do { \
::std::stringstream ss___; \
::ov::write_all_to_stream(ss___, __VA_ARGS__); \
exc_class::create((::ov::CheckLocInfo{__FILE__, __LINE__, nullptr}), ss___.str()); \
#define OPENVINO_THROW_HELPER2(exc_class, ctx, ...) \
do { \
::std::ostringstream ss___; \
::ov::write_all_to_stream(ss___, __VA_ARGS__); \
exc_class::create(__FILE__, __LINE__, ss___.str()); \
} while (0)

#define OPENVINO_THROW_HELPER1(exc_class, ctx, explanation) \
do { \
exc_class::create((::ov::CheckLocInfo{__FILE__, __LINE__, nullptr}), ::ov::stringify(explanation)); \
#define OPENVINO_THROW_HELPER1(exc_class, ctx, explanation) \
do { \
exc_class::create(__FILE__, __LINE__, ::ov::stringify(explanation)); \
} while (0)

#define OPENVINO_THROW_HELPER(exc_class, ctx, ...) CALL_OVERLOAD(OPENVINO_THROW_HELPER, exc_class, ctx, __VA_ARGS__)
Expand All @@ -191,9 +201,10 @@ class OPENVINO_API NotImplemented : public AssertFailure {
#define OPENVINO_THROW(...) OPENVINO_THROW_HELPER(::ov::Exception, ov::Exception::default_msg, __VA_ARGS__)

#define OPENVINO_THROW_NOT_IMPLEMENTED(...) \
OPENVINO_ASSERT_HELPER(::ov::NotImplemented, ::ov::Exception::default_msg, false, __VA_ARGS__)
OPENVINO_THROW_HELPER(::ov::NotImplemented, ::ov::Exception::default_msg, __VA_ARGS__)

#define OPENVINO_NOT_IMPLEMENTED OPENVINO_ASSERT_HELPER(::ov::NotImplemented, ::ov::Exception::default_msg, false)
#define OPENVINO_NOT_IMPLEMENTED \
OPENVINO_THROW_HELPER(::ov::NotImplemented, ::ov::Exception::default_msg, ::ov::Exception::default_msg)

#define GLUE(x, y) x y

Expand Down
12 changes: 9 additions & 3 deletions src/core/include/openvino/core/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,16 @@ using RawNodeOutputMap = std::map<RawNodeOutput, Output<Node>>;

class OPENVINO_API NodeValidationFailure : public ov::AssertFailure {
public:
[[noreturn]] static void create(const CheckLocInfo& check_loc_info,
[[noreturn]] static void create(const char* file,
int line,
const char* check_string,
const Node* node,
const std::string& explanation);

template <class TShape>
[[noreturn]] static void create(const CheckLocInfo& check_loc_info,
[[noreturn]] static void create(const char* file,
int line,
const char* check_string,
std::pair<const Node*, const std::vector<TShape>*>&& ctx,
const std::string& explanation);

Expand All @@ -543,7 +547,9 @@ class OPENVINO_API NodeValidationFailure : public ov::AssertFailure {
* @param explanation Exception explanation string.
*/
template <>
OPENVINO_API void NodeValidationFailure::create(const CheckLocInfo& check_loc_info,
OPENVINO_API void NodeValidationFailure::create(const char* file,
int line,
const char* check_string,
std::pair<const Node*, const std::vector<PartialShape>*>&& ctx,
const std::string& explanation);
} // namespace ov
Expand Down
35 changes: 21 additions & 14 deletions src/core/src/except.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@

ov::Exception::Exception(const std::string& what_arg) : std::runtime_error(what_arg) {}

void ov::Exception::create(const CheckLocInfo& check_loc_info, const std::string& explanation) {
void ov::Exception::create(const char* file, int line, const std::string& explanation) {
OPENVINO_SUPPRESS_DEPRECATED_START
throw ov::Exception(make_what(check_loc_info, default_msg, explanation));
throw ov::Exception(make_what(file, line, nullptr, default_msg, explanation));
OPENVINO_SUPPRESS_DEPRECATED_END
}

std::string ov::Exception::make_what(const CheckLocInfo& check_loc_info,
std::string ov::Exception::make_what(const char* file,
int line,
const char* check_string,
const std::string& context_info,
const std::string& explanation) {
std::stringstream ss;
if (check_loc_info.check_string) {
ss << "Check '" << check_loc_info.check_string << "' failed at " << util::trim_file_name(check_loc_info.file)
<< ":" << check_loc_info.line;
if (check_string) {
ss << "Check '" << check_string << "' failed at " << util::trim_file_name(file) << ":" << line;
} else {
ss << "Exception from " << util::trim_file_name(check_loc_info.file) << ":" << check_loc_info.line;
ss << "Exception from " << util::trim_file_name(file) << ":" << line;
}
if (!context_info.empty()) {
ss << ":" << std::endl << context_info;
Expand All @@ -38,18 +39,24 @@ ov::Exception::~Exception() = default;

const std::string ov::Exception::default_msg{};

void ov::AssertFailure::create(const CheckLocInfo& check_loc_info,
void ov::AssertFailure::create(const char* file,
int line,
const char* check_string,
const std::string& context_info,
const std::string& explanation) {
throw ov::AssertFailure(make_what(check_loc_info, context_info, explanation));
throw ov::AssertFailure(make_what(file, line, check_string, context_info, explanation));
}
ov::AssertFailure::~AssertFailure() = default;

void ov::NotImplemented::create(const CheckLocInfo& check_loc_info,
const std::string& context_info,
void ov::NotImplemented::create(const char* file, int line, const std::string& explanation) {
throw ov::NotImplemented(make_what(file, line, nullptr, default_msg, explanation));
}

void ov::NotImplemented::create(const char* file,
int line,
const char*,
const std::string&,
const std::string& explanation) {
throw ov::NotImplemented(make_what(check_loc_info, context_info, explanation));
create(file, line, explanation);
}
ov::NotImplemented::~NotImplemented() = default;

const std::string ov::NotImplemented::default_msg{"Not Implemented"};
15 changes: 11 additions & 4 deletions src/core/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ static const char idx_txt[] = "index '";
static const char out_of_range_txt[] = "' out of range";
} // namespace

void ov::NodeValidationFailure::create(const CheckLocInfo& check_loc_info,
void ov::NodeValidationFailure::create(const char* file,
int line,
const char* check_string,
const Node* node,
const std::string& explanation) {
throw ov::NodeValidationFailure(make_what(check_loc_info, node_validation_failure_loc_string(node), explanation));
throw ov::NodeValidationFailure(
make_what(file, line, check_string, node_validation_failure_loc_string(node), explanation));
}

template <>
void ov::NodeValidationFailure::create(const CheckLocInfo& check_loc_info,
void ov::NodeValidationFailure::create(const char* file,
int line,
const char* check_string,
std::pair<const Node*, const std::vector<PartialShape>*>&& ctx,
const std::string& explanation) {
throw ov::NodeValidationFailure(make_what(check_loc_info,
throw ov::NodeValidationFailure(make_what(file,
line,
check_string,
node_validation_failure_loc_string(ctx.first),
op::validate::shape_infer_explanation_str(*ctx.second, explanation)));
}
Expand Down
20 changes: 15 additions & 5 deletions src/frontends/common/include/openvino/frontend/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace ov {
namespace frontend {
class FRONTEND_API GeneralFailure : public AssertFailure {
public:
[[noreturn]] static void create(const CheckLocInfo& check_loc_info,
[[noreturn]] static void create(const char* file,
int line,
const char* check_string,
const std::string& context_info,
const std::string& explanation);

Expand All @@ -24,7 +26,9 @@ class FRONTEND_API GeneralFailure : public AssertFailure {

class FRONTEND_API InitializationFailure : public AssertFailure {
public:
[[noreturn]] static void create(const CheckLocInfo& check_loc_info,
[[noreturn]] static void create(const char* file,
int line,
const char* check_string,
const std::string& context_info,
const std::string& explanation);

Expand All @@ -34,7 +38,9 @@ class FRONTEND_API InitializationFailure : public AssertFailure {

class FRONTEND_API OpValidationFailure : public AssertFailure {
public:
[[noreturn]] static void create(const CheckLocInfo& check_loc_info,
[[noreturn]] static void create(const char* file,
int line,
const char* check_string,
const std::string& context_info,
const std::string& explanation);

Expand All @@ -44,7 +50,9 @@ class FRONTEND_API OpValidationFailure : public AssertFailure {

class FRONTEND_API OpConversionFailure : public AssertFailure {
public:
[[noreturn]] static void create(const CheckLocInfo& check_loc_info,
[[noreturn]] static void create(const char* file,
int line,
const char* check_string,
const std::string& context_info,
const std::string& explanation);

Expand All @@ -54,7 +62,9 @@ class FRONTEND_API OpConversionFailure : public AssertFailure {

class FRONTEND_API NotImplementedFailure : public AssertFailure {
public:
[[noreturn]] static void create(const CheckLocInfo& check_loc_info,
[[noreturn]] static void create(const char* file,
int line,
const char* check_string,
const std::string& context_info,
const std::string& explanation);

Expand Down
Loading

0 comments on commit 5d6d6a2

Please sign in to comment.