From cc249242bf4ac338805241c2b60a7911fcfde9c6 Mon Sep 17 00:00:00 2001 From: Xiang Gao Date: Tue, 7 Mar 2023 14:42:22 -0800 Subject: [PATCH 1/5] Add debugging utility RAII guard for printting scopes --- third_party/nvfuser/csrc/utils.h | 56 ++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/third_party/nvfuser/csrc/utils.h b/third_party/nvfuser/csrc/utils.h index dc08a67d053e6..48cca06932524 100644 --- a/third_party/nvfuser/csrc/utils.h +++ b/third_party/nvfuser/csrc/utils.h @@ -260,9 +260,8 @@ std::vector getSortedKeys( // Based on https://stackoverflow.com/a/9154394 template -static auto hasToStringHelper(int) -> decltype( - std::declval::type>().toString(), - std::true_type{}); +static auto hasToStringHelper(int) + -> decltype(std::declval::type>().toString(), std::true_type{}); template static auto hasToStringHelper(long) -> std::false_type; @@ -367,4 +366,55 @@ std::string toDelimitedString( return toDelimitedString(vec.begin(), vec.end(), delim); } +template +void unrolled_for(func_t fun) { + if constexpr (index < stop) { + fun(std::integral_constant()); + unrolled_for(fun); + } +} + +template +void unrolled_for(func_t fun) { + unrolled_for(fun); +} + +template +void unrolled_for(func_t fun) { + unrolled_for<0, stop>(fun); +} + +template +std::string toDelimitedString( + const std::tuple& args, + std::string delim = ", ") { + std::stringstream ss; + bool first_val = true; + unrolled_for([&](auto i) { + if (!first_val) { + ss << delim; + } + auto item = std::get(args); + ss << Printer::toString(item); + first_val = false; + }); + return ss.str(); +} + +template +class DebugPrintScope { + public: + DebugPrintScope(std::string name, Args... args) : name_(std::move(name)) { + std::cout << "Entering " << name_ << "(" + << toDelimitedString(std::forward_as_tuple(args...)) << ")" + << std::endl; + } + ~DebugPrintScope() { + std::cout << "Leaving " << name_ << std::endl; + } + + private: + std::string name_; +}; + } // namespace nvfuser From cde3fccb75c27f9be4aa83e6abe43d033a123716 Mon Sep 17 00:00:00 2001 From: Xiang Gao Date: Tue, 7 Mar 2023 14:48:00 -0800 Subject: [PATCH 2/5] unformat --- third_party/nvfuser/csrc/utils.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/third_party/nvfuser/csrc/utils.h b/third_party/nvfuser/csrc/utils.h index 48cca06932524..b9afdb18ed605 100644 --- a/third_party/nvfuser/csrc/utils.h +++ b/third_party/nvfuser/csrc/utils.h @@ -260,8 +260,9 @@ std::vector getSortedKeys( // Based on https://stackoverflow.com/a/9154394 template -static auto hasToStringHelper(int) - -> decltype(std::declval::type>().toString(), std::true_type{}); +static auto hasToStringHelper(int) -> decltype( + std::declval::type>().toString(), + std::true_type{}); template static auto hasToStringHelper(long) -> std::false_type; From 165c7f18f55fbdb156e13532a1fb70a9af866f39 Mon Sep 17 00:00:00 2001 From: Xiang Gao Date: Tue, 7 Mar 2023 22:37:02 -0800 Subject: [PATCH 3/5] DEBUG_PRINT_SCOPE --- third_party/nvfuser/csrc/utils.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/third_party/nvfuser/csrc/utils.h b/third_party/nvfuser/csrc/utils.h index b9afdb18ed605..92cc654f20346 100644 --- a/third_party/nvfuser/csrc/utils.h +++ b/third_party/nvfuser/csrc/utils.h @@ -260,9 +260,8 @@ std::vector getSortedKeys( // Based on https://stackoverflow.com/a/9154394 template -static auto hasToStringHelper(int) -> decltype( - std::declval::type>().toString(), - std::true_type{}); +static auto hasToStringHelper(int) + -> decltype(std::declval::type>().toString(), std::true_type{}); template static auto hasToStringHelper(long) -> std::false_type; @@ -418,4 +417,11 @@ class DebugPrintScope { std::string name_; }; +// Note: ##__VA_ARGS__ is not C++ stardard, but it should work on gcc and clang. +// Compared to __VA_ARGS__, ##__VA_ARGS__ automatically remove the preceding +// comma when empty, allowing empty variadic parameters. If using other +// compiler, please use DebugPrintScope directly without this macro. +#define DEBUG_PRINT_SCOPE(...) \ + DebugPrintScope _debug_print_scope(__func__, ##__VA_ARGS__) + } // namespace nvfuser From 8304b3d34aeec99362e35fe6f0cd0f5ffe448f16 Mon Sep 17 00:00:00 2001 From: Xiang Gao Date: Tue, 7 Mar 2023 22:40:00 -0800 Subject: [PATCH 4/5] unformat --- third_party/nvfuser/csrc/utils.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/third_party/nvfuser/csrc/utils.h b/third_party/nvfuser/csrc/utils.h index 92cc654f20346..b1d05e00aa1f5 100644 --- a/third_party/nvfuser/csrc/utils.h +++ b/third_party/nvfuser/csrc/utils.h @@ -259,9 +259,9 @@ std::vector getSortedKeys( } // Based on https://stackoverflow.com/a/9154394 -template -static auto hasToStringHelper(int) - -> decltype(std::declval::type>().toString(), std::true_type{}); +static auto hasToStringHelper(int) -> decltype( + std::declval::type>().toString(), + std::true_type{}); template static auto hasToStringHelper(long) -> std::false_type; From d00ba76826ef8f8c4aa4d8af66f3f4a46665a292 Mon Sep 17 00:00:00 2001 From: Xiang Gao Date: Tue, 7 Mar 2023 22:40:29 -0800 Subject: [PATCH 5/5] fix --- third_party/nvfuser/csrc/utils.h | 1 + 1 file changed, 1 insertion(+) diff --git a/third_party/nvfuser/csrc/utils.h b/third_party/nvfuser/csrc/utils.h index b1d05e00aa1f5..ebb905c518097 100644 --- a/third_party/nvfuser/csrc/utils.h +++ b/third_party/nvfuser/csrc/utils.h @@ -259,6 +259,7 @@ std::vector getSortedKeys( } // Based on https://stackoverflow.com/a/9154394 +template static auto hasToStringHelper(int) -> decltype( std::declval::type>().toString(), std::true_type{});