forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathException.cpp
261 lines (214 loc) · 6.34 KB
/
Exception.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <c10/util/Exception.h>
#include <c10/util/Logging.h>
#include <c10/util/Type.h>
#include <sstream>
#include <string>
#include <utility>
namespace c10 {
Error::Error(std::string msg, Backtrace backtrace, const void* caller)
: msg_(std::move(msg)), backtrace_(std::move(backtrace)), caller_(caller) {
refresh_what();
}
// PyTorch-style error message
// Error::Error(SourceLocation source_location, const std::string& msg)
// NB: This is defined in Logging.cpp for access to GetFetchStackTrace
// Caffe2-style error message
Error::Error(
const char* file,
const uint32_t line,
const char* condition,
const std::string& msg,
Backtrace backtrace,
const void* caller)
: Error(
str("[enforce fail at ",
detail::StripBasename(file),
":",
line,
"] ",
condition,
". ",
msg),
std::move(backtrace),
caller) {}
std::string Error::compute_what(bool include_backtrace) const {
std::ostringstream oss;
oss << msg_;
if (context_.size() == 1) {
// Fold error and context in one line
oss << " (" << context_[0] << ")";
} else {
for (const auto& c : context_) {
oss << "\n " << c;
}
}
if (include_backtrace && backtrace_) {
oss << "\n" << backtrace_->get();
}
return oss.str();
}
const Backtrace& Error::backtrace() const {
return backtrace_;
}
const char* Error::what() const noexcept {
return what_
.ensure([this] {
try {
return compute_what(/*include_backtrace*/ true);
} catch (...) {
// what() is noexcept, we need to return something here.
return std::string{"<Error computing Error::what()>"};
}
})
.c_str();
}
void Error::refresh_what() {
// Do not compute what_ eagerly, as it would trigger the computation of the
// backtrace. Instead, invalidate it, it will be computed on first access.
// refresh_what() is only called by non-const public methods which are not
// supposed to be called concurrently with any other method, so it is safe to
// invalidate here.
what_.reset();
what_without_backtrace_ = compute_what(/*include_backtrace*/ false);
}
void Error::add_context(std::string new_msg) {
context_.push_back(std::move(new_msg));
// TODO: Calling add_context 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.
// NB: If you do fix this, make sure you do it in a thread safe way!
// what() is almost certainly expected to be thread safe even when
// accessed across multiple threads
refresh_what();
}
namespace detail {
void torchCheckFail(
const char* func,
const char* file,
uint32_t line,
const std::string& msg) {
throw ::c10::Error({func, file, line}, msg);
}
void torchCheckFail(
const char* func,
const char* file,
uint32_t line,
const char* msg) {
throw ::c10::Error({func, file, line}, msg);
}
void torchInternalAssertFail(
const char* func,
const char* file,
uint32_t line,
const char* condMsg,
const char* userMsg) {
torchCheckFail(func, file, line, c10::str(condMsg, userMsg));
}
// This should never be called. It is provided in case of compilers
// that don't do any dead code stripping in debug builds.
void torchInternalAssertFail(
const char* func,
const char* file,
uint32_t line,
const char* condMsg,
const std::string& userMsg) {
torchCheckFail(func, file, line, c10::str(condMsg, userMsg));
}
} // namespace detail
namespace WarningUtils {
namespace {
WarningHandler* getBaseHandler() {
static WarningHandler base_warning_handler_ = WarningHandler();
return &base_warning_handler_;
}
class ThreadWarningHandler {
public:
ThreadWarningHandler() = delete;
static WarningHandler* get_handler() {
if (!warning_handler_) {
warning_handler_ = getBaseHandler();
}
return warning_handler_;
}
static void set_handler(WarningHandler* handler) {
warning_handler_ = handler;
}
private:
static thread_local WarningHandler* warning_handler_;
};
thread_local WarningHandler* ThreadWarningHandler::warning_handler_ = nullptr;
} // namespace
void set_warning_handler(WarningHandler* handler) noexcept(true) {
ThreadWarningHandler::set_handler(handler);
}
WarningHandler* get_warning_handler() noexcept(true) {
return ThreadWarningHandler::get_handler();
}
bool warn_always = false;
void set_warnAlways(bool setting) noexcept(true) {
warn_always = setting;
}
bool get_warnAlways() noexcept(true) {
return warn_always;
}
WarnAlways::WarnAlways(bool setting /*=true*/)
: prev_setting(get_warnAlways()) {
set_warnAlways(setting);
}
WarnAlways::~WarnAlways() {
set_warnAlways(prev_setting);
}
} // namespace WarningUtils
void warn(const Warning& warning) {
WarningUtils::ThreadWarningHandler::get_handler()->process(warning);
}
Warning::Warning(
warning_variant_t type,
const SourceLocation& source_location,
std::string msg,
const bool verbatim)
: type_(type),
source_location_(source_location),
msg_(std::move(msg)),
verbatim_(verbatim) {}
Warning::Warning(
warning_variant_t type,
SourceLocation source_location,
detail::CompileTimeEmptyString msg,
const bool verbatim)
: Warning(type, source_location, "", verbatim) {}
Warning::Warning(
warning_variant_t type,
SourceLocation source_location,
const char* msg,
const bool verbatim)
: type_(type),
source_location_(source_location),
msg_(std::string(msg)),
verbatim_(verbatim) {}
Warning::warning_variant_t Warning::type() const {
return type_;
}
const SourceLocation& Warning::source_location() const {
return source_location_;
}
const std::string& Warning::msg() const {
return msg_;
}
bool Warning::verbatim() const {
return verbatim_;
}
void WarningHandler::process(const Warning& warning) {
LOG_AT_FILE_LINE(
WARNING, warning.source_location().file, warning.source_location().line)
<< "Warning: " << warning.msg() << " (function "
<< warning.source_location().function << ")";
}
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