Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging of the matching process #28587

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
37532da
wip
CuriousPanCake Dec 10, 2024
c11593f
some git fix
CuriousPanCake Jan 8, 2025
a25fa61
wip2
CuriousPanCake Jan 9, 2025
21daf79
wip3
CuriousPanCake Jan 14, 2025
2b6ebe6
wip4: very working prototype
CuriousPanCake Jan 15, 2025
2e050e6
working version
CuriousPanCake Jan 21, 2025
0f08cbc
fix namespace
CuriousPanCake Jan 21, 2025
d3c03db
clear code
CuriousPanCake Jan 21, 2025
738578b
more cleaning
CuriousPanCake Jan 21, 2025
7331229
more clean up
CuriousPanCake Jan 21, 2025
77e1c75
Merge branch 'master' into CVS-151312_exp
CuriousPanCake Jan 21, 2025
dd54af5
cleaning
CuriousPanCake Jan 21, 2025
1fec8fc
supported GenericPattern, many other small improvements
CuriousPanCake Jan 28, 2025
4029bf9
fix exception throwing
CuriousPanCake Jan 28, 2025
14f34d9
add verbose option
CuriousPanCake Jan 28, 2025
1b660bf
added switching on/off using env variable
CuriousPanCake Feb 11, 2025
ec2d547
added colors
CuriousPanCake Feb 12, 2025
b4de07d
almost done
CuriousPanCake Feb 12, 2025
62e4e79
clearing code
CuriousPanCake Feb 13, 2025
d531cbe
more cleaning
CuriousPanCake Feb 13, 2025
8a2258c
removed unnecessary comment
CuriousPanCake Feb 13, 2025
1b290bc
Merge branch 'master' into CVS-151312_exp
CuriousPanCake Feb 13, 2025
6a95342
add linux/macos includes only
CuriousPanCake Feb 13, 2025
0cb7ae3
move readme.md to transformations/docs
CuriousPanCake Feb 13, 2025
66d2bb3
some review concerns
CuriousPanCake Feb 17, 2025
5614250
Merge branch 'master' into CVS-151312_exp
CuriousPanCake Feb 17, 2025
64ecc2f
replace level strings with macro def
CuriousPanCake Feb 17, 2025
9493efe
code style, move docs
CuriousPanCake Feb 17, 2025
ddde0c7
make level string a singleton, rework logs into macro, apply review c…
CuriousPanCake Feb 19, 2025
70eaefc
clear code
CuriousPanCake Feb 19, 2025
b3f222b
code style
CuriousPanCake Feb 19, 2025
6857d04
OPENVINO_LOG_OR
CuriousPanCake Feb 19, 2025
e9d035b
fix review concerns
CuriousPanCake Feb 19, 2025
7497667
rename doc files
CuriousPanCake Feb 20, 2025
614958d
fixed multiple review concerns
CuriousPanCake Feb 20, 2025
169262f
add images
CuriousPanCake Feb 20, 2025
310f6d7
remove images
CuriousPanCake Feb 20, 2025
164be9b
clear code
CuriousPanCake Feb 20, 2025
688ae40
code style
CuriousPanCake Feb 20, 2025
4e55038
Using the friendly name instead of the autogenerated
itikhono Feb 21, 2025
2c525f3
Merge branch 'master' into CVS-151312_exp
itikhono Feb 21, 2025
695db12
Merge branch 'master' into CVS-151312_exp
dorloff Feb 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Logging of the pattern matching

The logging functionality allows to observe/debug the pattern matching process.

In order to utilzie the logging, first, you need to set the CMake flag ```-DENABLE_OPENVINO_DEBUG=ON```

_NOTE: the logging would also work if your build is configured as Release_

In order to start logging, set the environmental variable ```OV_MATCHER_LOGGING_ENABLED``` to ``true/ON`` before running your executable or script as following:
```OV_MATCHER_LOGGING_ENABLED=true ./your_amazing_program```

If you want to log only specific matchers, use the ```OV_MATCHERS_TO_LOG``` environmental variable and provide their names separated as commas:
```OV_MATCHER_LOGGING_ENABLED=true OV_MATCHERS_TO_LOG=EliminateSplitConcat,MarkDequantization ./your_amazing_program```

You can also set the environmental variable ```OV_VERBOSE_LOGGING``` to ```true```, to turn on more verbose logging that would print more information about the nodes taking part in the matching process:
```OV_MATCHER_LOGGING_ENABLED=true OV_VERBOSE_LOGGING=true ./your_amazing_program```

If you have any suggestions for improvement or you observe a bug in logging, feel free to submit changes or contact Andrii Staikov <[email protected]>
15 changes: 15 additions & 0 deletions src/common/transformations/docs/debug_capabilities/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Transformations documentation

## Debug capabilities

* [Matcher logging README](./debug_capabilities/LOGGING_README.md)

## Key Contacts

For assistance regarding snippets, contact a member of [openvino-ie-transformations-maintainers](https://github.com/orgs/openvinotoolkit/teams/openvino-ie-transformations-maintainers) group.

## See also

* [OpenVINO™ README](../../../README.md)
* [OpenVINO Core Components](../../README.md)
* [Developer documentation](../../../docs/dev/index.md)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string>
#include <utility>

#include "openvino/core/log_util.hpp"
#include "openvino/opsets/opset1.hpp"
#include "openvino/opsets/opset2.hpp"
#include "openvino/opsets/opset3.hpp"
Expand All @@ -34,6 +35,7 @@
#include "openvino/pass/pattern/op/label.hpp"
#include "openvino/pass/pattern/op/or.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "openvino/util/log.hpp"

namespace ov {
namespace gen_pattern {
Expand Down Expand Up @@ -811,17 +813,20 @@ class GenericPattern : public ov::pass::pattern::op::Pattern {
pattern_value.get_index(),
"!=",
graph_value.get_index());
OPENVINO_LOG_GENPATTERN1(matcher, pattern_value, graph_value);
return false;
}

auto value_node = graph_value.get_node_shared_ptr();
if (!value_node->get_type_info().is_castable(m_type_info)) {
_VERBOSE_LOG(level, "X OP type mismatch: ", m_signature, " vs ", graph_value);
OPENVINO_LOG_GENPATTERN2(matcher, pattern_value, graph_value);
Comment on lines 822 to +823
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just topic for future improvements, maybe is possible to merge these two log macros into one?

return false;
}

if (!m_vt.predicate(graph_value)) {
_VERBOSE_LOG(level, "X value info mismatch: ", m_signature, " vs ", graph_value);
OPENVINO_LOG_GENPATTERN3(matcher);
return false;
}

Expand All @@ -830,6 +835,7 @@ class GenericPattern : public ov::pass::pattern::op::Pattern {
value_node->visit_attributes(visitor);
if (!visitor.matched()) {
_VERBOSE_LOG(level, "X OP attrs mismatch: ", m_signature, " vs ", graph_value);
OPENVINO_LOG_GENPATTERN4(matcher);
return false;
}
}
Expand All @@ -843,14 +849,28 @@ class GenericPattern : public ov::pass::pattern::op::Pattern {

if (matcher_verbose_enabled())
level.push_back('\t');
OPENVINO_LOG_GENPATTERN5(matcher);
bool ret = matcher->match_arguments(pattern_value.get_node(), graph_value.get_node_shared_ptr());
OPENVINO_LOG_GENPATTERN6(matcher, ret);
if (matcher_verbose_enabled()) {
level.pop_back();
_VERBOSE_LOG(level, ret ? "O" : "X", m_signature, " vs ", graph_value);
}
return ret;
}

#ifdef ENABLE_OPENVINO_DEBUG
std::string get_wraped_type_str(bool verbose) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move it to utils and change it to get GenericPattern as an argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved per discussion

auto version = m_type_info.version_id;
std::string res = "<";
if (verbose)
if (version)
res += version + std::string("::");
res += m_type_info.name + std::string(">");
return res;
}
#endif

private:
const DiscreteTypeInfo& m_type_info;
detail::AttrMap m_attrs;
Expand Down
9 changes: 8 additions & 1 deletion src/common/util/include/openvino/util/env_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <cstdint>
#include <string>

#include <unordered_set>
namespace ov {
namespace util {
/// \brief Get the names environment variable as a string.
Expand All @@ -31,5 +31,12 @@ int32_t getenv_int(const char* env_var, int32_t default_value = -1);
/// \param default_value The value to return if the environment variable is not set.
/// \return Returns the boolean value of the environment variable.
bool getenv_bool(const char* env_var, bool default_value = false);

/// \brief Splits a string using by a single character delimiter
/// without modifying the original string
/// \param str String to split
/// \param delimiter Delimiter to use for splitting
/// \return Returns an unordered set of split strings
std::unordered_set<std::string> split_by_delimiter(std::string str, char delimiter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::unordered_set<std::string> split_by_delimiter(std::string str, char delimiter);
std::unordered_set<std::string> split_by_delimiter(const std::string& str, char delimiter);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or move

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you look at ov::util::join and do opposite to return vector of strings which are split using delimiter?
Can be this function be in same place as join?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

} // namespace util
} // namespace ov
48 changes: 48 additions & 0 deletions src/common/util/include/openvino/util/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@

#include <deque>
#include <functional>
#include <iostream>
#include <sstream>
#include <unordered_set>
#include <vector>

#if defined(__linux__) || (defined(__APPLE__) && defined(__MACH__))
# include <unistd.h>
#endif

#include "openvino/util/env_util.hpp"

namespace ov {
namespace util {

Expand All @@ -17,6 +25,7 @@ enum class LOG_TYPE {
_LOG_TYPE_WARNING,
_LOG_TYPE_INFO,
_LOG_TYPE_DEBUG,
_LOG_TYPE_DEBUG_EMPTY,
};

class LogHelper {
Expand Down Expand Up @@ -50,6 +59,7 @@ class Logger {
};

void default_logger_handler_func(const std::string& s);
void default_logger_handler_func_length(const std::string& s);

#ifdef ENABLE_OPENVINO_DEBUG
/* Template function _write_all_to_stream has duplicates
Expand Down Expand Up @@ -93,6 +103,41 @@ static inline std::ostream& _write_all_to_stream(std::ostream& os, const T& arg,
do { \
ov::util::_write_all_to_stream(OPENVINO_LOG_STREAM(_LOG_TYPE_DEBUG), __VA_ARGS__); \
} while (0)

static const bool logging_enabled = ov::util::getenv_bool("OV_MATCHER_LOGGING_ENABLED");
static const std::unordered_set<std::string> matchers_to_log =
ov::util::split_by_delimiter(ov::util::getenv_string("OV_MATCHERS_TO_LOG"), ',');

static inline bool is_terminal_output() {
# ifdef _WIN32
// No Windows support for colored logs for now.
return false;
# else
static const bool stdout_to_terminal = isatty(fileno(stdout));
return stdout_to_terminal;
# endif
}

# define OPENVINO_RESET (ov::util::is_terminal_output() ? "\033[0m" : "")
# define OPENVINO_RED (ov::util::is_terminal_output() ? "\033[31m" : "")
# define OPENVINO_GREEN (ov::util::is_terminal_output() ? "\033[1;32m" : "")
# define OPENVINO_YELLOW (ov::util::is_terminal_output() ? "\033[33m" : "")
# define OPENVINO_BLOCK_BEG "{"
# define OPENVINO_BLOCK_END "}"
# define OPENVINO_BLOCK_BODY "│"
# define OPENVINO_BLOCK_BODY_RIGHT "├─"

# define OPENVINO_LOG_MATCHING(matcher_ptr, ...) \
do { \
if (ov::util::logging_enabled) { \
if (ov::util::matchers_to_log.empty() || \
ov::util::matchers_to_log.find(matcher_ptr->get_name()) != ov::util::matchers_to_log.end()) { \
ov::util::_write_all_to_stream(OPENVINO_LOG_STREAM(_LOG_TYPE_DEBUG_EMPTY), \
__VA_ARGS__, \
OPENVINO_RESET); \
} \
} \
} while (0)
#else
# define OPENVINO_ERR(...) \
do { \
Expand All @@ -106,6 +151,9 @@ static inline std::ostream& _write_all_to_stream(std::ostream& os, const T& arg,
# define OPENVINO_DEBUG(...) \
do { \
} while (0)
# define OPENVINO_LOG_MATCHING(matcher_ptr, ...) \
do { \
} while (0)
#endif

} // namespace util
Expand Down
13 changes: 13 additions & 0 deletions src/common/util/src/env_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,16 @@ bool ov::util::getenv_bool(const char* env_var, bool default_value) {
}
return rc;
}

std::unordered_set<std::string> ov::util::split_by_delimiter(std::string str, char delimiter) {
std::unordered_set<std::string> res;
size_t pos = 0;
while ((pos = str.find(delimiter)) != std::string::npos) {
res.insert(str.substr(0, pos));
str.erase(0, pos + 1);
Copy link
Contributor

@itikhono itikhono Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to erase?
can we just update the starting position for search?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}
if (pos != str.size() - 1) {
res.insert(str);
}
return res;
}
30 changes: 17 additions & 13 deletions src/common/util/src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,27 @@ ov::util::LogHelper::LogHelper(LOG_TYPE type,
case LOG_TYPE::_LOG_TYPE_DEBUG:
m_stream << "[DEBUG] ";
break;
case LOG_TYPE::_LOG_TYPE_DEBUG_EMPTY:
break;
}

{
static std::mutex m;
time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::lock_guard<std::mutex> lock(m);
auto tm = gmtime(&tt);
if (tm) {
char buffer[256];
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%Sz", tm);
m_stream << buffer << " ";
if (type != LOG_TYPE::_LOG_TYPE_DEBUG_EMPTY) {
{
static std::mutex m;
time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::lock_guard<std::mutex> lock(m);
auto tm = gmtime(&tt);
if (tm) {
char buffer[256];
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%Sz", tm);
m_stream << buffer << " ";
}
}
}

m_stream << util::trim_file_name(file);
m_stream << " " << line;
m_stream << "\t";
m_stream << util::trim_file_name(file);
m_stream << " " << line;
m_stream << "\t";
}
}

ov::util::LogHelper::~LogHelper() {
Expand Down
Loading
Loading