mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Summary: There are still a few work to be done: - Move logging and unify AT_WARN with LOG(ERROR). - A few header files are still being plumbed through, need cleaning. - caffe2::EnforceNotMet aliasing is not done yet. - need to unify the macros. See c10/util/Exception.h This is mainly a codemod and not causing functional changes. If you find your job failing and trace back to this diff, usually it can be fixed by the following approaches: (1) add //caffe2/c10:c10 to your dependency (or transitive dependency). (2) change objects such as at::Error, at::Optional to the c10 namespace. (3) change functions to the c10 namespace. Especially, caffe2::MakeString is not overridden by the unified c10::str function. Nothing else changes. Please kindly consider not reverting this diff - it involves multiple rounds of rebasing and the fix is usually simple. Contact jiayq@ or AI Platform Dev for details. Pull Request resolved: https://github.com/pytorch/pytorch/pull/12354 Reviewed By: orionr Differential Revision: D10238910 Pulled By: Yangqing fbshipit-source-id: 7794d5bf2797ab0ca6ebaccaa2f7ebbd50ff8f32
94 lines
2.4 KiB
C++
94 lines
2.4 KiB
C++
#include "c10/util/Exception.h"
|
|
#include "c10/util/Backtrace.h"
|
|
#include "c10/util/Type.h"
|
|
|
|
#include <iostream>
|
|
#include <numeric>
|
|
#include <string>
|
|
|
|
namespace c10 {
|
|
|
|
Error::Error(
|
|
const std::string& new_msg,
|
|
const std::string& backtrace,
|
|
const void* caller)
|
|
: msg_stack_{new_msg}, backtrace_(backtrace), caller_(caller) {
|
|
msg_ = msg();
|
|
msg_without_backtrace_ = msg_without_backtrace();
|
|
}
|
|
|
|
// PyTorch-style error message
|
|
Error::Error(SourceLocation source_location, const std::string& msg)
|
|
: Error(
|
|
msg,
|
|
str(" (",
|
|
source_location,
|
|
")\n",
|
|
get_backtrace(/*frames_to_skip=*/2))) {}
|
|
|
|
// Caffe2-style error message
|
|
Error::Error(
|
|
const char* file,
|
|
const int line,
|
|
const char* condition,
|
|
const std::string& msg,
|
|
const std::string& backtrace,
|
|
const void* caller)
|
|
: Error(
|
|
str("[enforce fail at ",
|
|
detail::StripBasename(file),
|
|
":",
|
|
line,
|
|
"] ",
|
|
condition,
|
|
". ",
|
|
msg),
|
|
backtrace,
|
|
caller) {}
|
|
|
|
std::string Error::msg() const {
|
|
return std::accumulate(
|
|
msg_stack_.begin(), msg_stack_.end(), std::string("")) +
|
|
backtrace_;
|
|
}
|
|
|
|
std::string Error::msg_without_backtrace() const {
|
|
return std::accumulate(msg_stack_.begin(), msg_stack_.end(), std::string(""));
|
|
}
|
|
|
|
void Error::AppendMessage(const std::string& new_msg) {
|
|
msg_stack_.push_back(new_msg);
|
|
// Refresh the cache
|
|
// TODO: Calling AppendMessage O(n) times has O(n^2) cost. We can fix
|
|
// this perf problem by populating the fields lazily... if this ever
|
|
// actually is a problem.
|
|
msg_ = msg();
|
|
msg_without_backtrace_ = msg_without_backtrace();
|
|
}
|
|
|
|
void Warning::warn(SourceLocation source_location, std::string msg) {
|
|
warning_handler_(source_location, msg.c_str());
|
|
}
|
|
|
|
void Warning::set_warning_handler(handler_t handler) {
|
|
warning_handler_ = handler;
|
|
}
|
|
|
|
void Warning::print_warning(
|
|
const SourceLocation& source_location,
|
|
const char* msg) {
|
|
std::cerr << "Warning: " << msg << " (" << source_location << ")\n";
|
|
}
|
|
|
|
Warning::handler_t Warning::warning_handler_ = &Warning::print_warning;
|
|
|
|
std::string GetExceptionString(const std::exception& e) {
|
|
#ifdef __GXX_RTTI
|
|
return demangle(typeid(e).name()) + ": " + e.what();
|
|
#else
|
|
return std::string("Exception (no RTTI available): ") + e.what();
|
|
#endif // __GXX_RTTI
|
|
}
|
|
|
|
} // namespace c10
|