csarofeen / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
http://pytorch.org
Other
26 stars 7 forks source link

Add debugging utility RAII guard for printting scopes #2555

Closed zasdfgbnm closed 1 year ago

zasdfgbnm commented 1 year ago

Example usage:

void my_func(Expr* expr, int i) {
  // This is the only line you need to add
  // Arguments are not required. If you don't want to print arguments, simply don't put them here
  DebugPrintScope g("my_func", expr, i);
  // Below are the original code
  if (i == 0) {
    return;
  }
  auto result = do_something(expr, i);
  if (result) {
    return;
  }
  do_something_else(expr, i);
}
naoyam commented 1 year ago

Looks useful. How about using a macro to provide a function name automatically?

E.g.,

#define DEBUG_PRINT_SCOPE(...) DebugPrintScope _debug_print_scope(__func__, _VA_ARGS_)
zasdfgbnm commented 1 year ago

define DEBUG_PRINT_SCOPE(...) DebugPrintScope _debug_print_scope(func, _VAARGS)

Good suggestion! Added