4
4
#include < c10/macros/Macros.h>
5
5
#include < c10/util/Deprecated.h>
6
6
#include < c10/util/StringUtil.h>
7
+ #include < c10/util/variant.h>
7
8
8
9
#include < cstddef>
9
10
#include < exception>
@@ -112,17 +113,66 @@ class C10_API Error : public std::exception {
112
113
std::string compute_what (bool include_backtrace) const ;
113
114
};
114
115
116
+ class C10_API Warning {
117
+ public:
118
+ class C10_API UserWarning {};
119
+ class C10_API DeprecationWarning {};
120
+
121
+ using warning_variant_t = c10::variant<UserWarning, DeprecationWarning>;
122
+
123
+ Warning (
124
+ warning_variant_t type,
125
+ const SourceLocation& source_location,
126
+ const std::string& msg,
127
+ bool verbatim);
128
+
129
+ Warning (
130
+ warning_variant_t type,
131
+ SourceLocation source_location,
132
+ const char * msg,
133
+ bool verbatim);
134
+
135
+ Warning (
136
+ warning_variant_t type,
137
+ SourceLocation source_location,
138
+ ::c10::detail::CompileTimeEmptyString msg,
139
+ bool verbatim);
140
+
141
+ // Getters for members
142
+ warning_variant_t type () const ;
143
+ const SourceLocation& source_location () const ;
144
+ const std::string& msg () const ;
145
+ bool verbatim () const ;
146
+
147
+ private:
148
+ // The type of warning
149
+ warning_variant_t type_;
150
+
151
+ // Where the warning happened.
152
+ SourceLocation source_location_;
153
+
154
+ // The actual warning message.
155
+ std::string msg_;
156
+
157
+ // See note: [Verbatim Warnings]
158
+ bool verbatim_;
159
+ };
160
+
161
+ using UserWarning = Warning::UserWarning;
162
+ using DeprecationWarning = Warning::DeprecationWarning;
163
+
164
+ // Issue a warning with a given message. Dispatched to the current
165
+ // warning handler.
166
+ void C10_API warn (const Warning& warning);
167
+
115
168
class C10_API WarningHandler {
116
169
public:
117
170
virtual ~WarningHandler () = default ;
118
171
// / The default warning handler. Prints the message to stderr.
119
- virtual void process (
120
- const SourceLocation& source_location,
121
- const std::string& msg,
122
- const bool verbatim);
172
+ virtual void process (const Warning& warning);
123
173
};
124
174
125
- namespace Warning {
175
+ namespace WarningUtils {
126
176
127
177
// Note: [Verbatim Warnings]
128
178
// Warnings originating in C++ code can appear out-of-place to Python users:
@@ -137,20 +187,6 @@ namespace Warning {
137
187
// context in their warnings should set verbatim to true so their warnings
138
188
// appear without modification.
139
189
140
- // / Issue a warning with a given message. Dispatched to the current
141
- // / warning handler.
142
- C10_API void warn (
143
- const SourceLocation& source_location,
144
- const std::string& msg,
145
- bool verbatim);
146
- C10_API void warn (
147
- SourceLocation source_location,
148
- const char * msg,
149
- bool verbatim);
150
- C10_API void warn (
151
- SourceLocation source_location,
152
- ::c10::detail::CompileTimeEmptyString msg,
153
- bool verbatim);
154
190
// / Sets the global warning handler. This is not thread-safe, so it should
155
191
// / generally be called once during initialization or while holding the GIL
156
192
// / for programs that use python.
@@ -165,11 +201,11 @@ class C10_API WarningHandlerGuard {
165
201
166
202
public:
167
203
WarningHandlerGuard (WarningHandler* new_handler)
168
- : prev_handler_(c10::Warning ::get_warning_handler()) {
169
- c10::Warning ::set_warning_handler (new_handler);
204
+ : prev_handler_(c10::WarningUtils ::get_warning_handler()) {
205
+ c10::WarningUtils ::set_warning_handler (new_handler);
170
206
}
171
207
~WarningHandlerGuard () {
172
- c10::Warning ::set_warning_handler (prev_handler_);
208
+ c10::WarningUtils ::set_warning_handler (prev_handler_);
173
209
}
174
210
};
175
211
@@ -190,7 +226,7 @@ struct C10_API WarnAlways {
190
226
bool prev_setting;
191
227
};
192
228
193
- } // namespace Warning
229
+ } // namespace WarningUtils
194
230
195
231
// Used in ATen for out-of-bound indices that can reasonably only be detected
196
232
// lazily inside a kernel (See: advanced indexing). These turn into
@@ -516,53 +552,43 @@ namespace detail {
516
552
#define TORCH_CHECK_NOT_IMPLEMENTED (cond, ...) \
517
553
TORCH_CHECK_WITH_MSG (NotImplementedError, cond, " TYPE" , __VA_ARGS__)
518
554
555
+ #ifdef STRIP_ERROR_MESSAGES
556
+ #define WARNING_MESSAGE_STRING (...) \
557
+ ::c10::detail::CompileTimeEmptyString {}
558
+ #else
559
+ #define WARNING_MESSAGE_STRING (...) ::c10::str(__VA_ARGS__)
560
+ #endif
561
+
519
562
// Report a warning to the user. Accepts an arbitrary number of extra
520
563
// arguments which are concatenated into the warning message using operator<<
521
564
//
522
- #ifdef STRIP_ERROR_MESSAGES
523
- # define TORCH_WARN (...) \
524
- ::c10::Warning::warn ( \
565
+ #define TORCH_WARN_WITH ( warning_t , ...) \
566
+ ::c10::warn (::c10::Warning( \
567
+ warning_t (), \
525
568
{__func__, __FILE__, static_cast <uint32_t >(__LINE__)}, \
526
- ::c10::detail::CompileTimeEmptyString{}, \
527
- false )
528
- #else
529
- #define TORCH_WARN (...) \
530
- ::c10::Warning::warn ( \
531
- {__func__, __FILE__, static_cast <uint32_t >(__LINE__)}, \
532
- ::c10::str (__VA_ARGS__), \
533
- false)
534
- #endif
569
+ WARNING_MESSAGE_STRING (__VA_ARGS__), \
570
+ false));
571
+
572
+ #define TORCH_WARN (...) TORCH_WARN_WITH(::c10::UserWarning, __VA_ARGS__);
573
+
574
+ #define TORCH_WARN_DEPRECATION (...) \
575
+ TORCH_WARN_WITH (::c10::DeprecationWarning, __VA_ARGS__);
535
576
536
577
// Report a warning to the user only once. Accepts an arbitrary number of extra
537
578
// arguments which are concatenated into the warning message using operator<<
538
579
//
539
- #ifdef STRIP_ERROR_MESSAGES
540
- #define _TORCH_WARN_ONCE (...) \
541
- C10_UNUSED static const auto C10_ANONYMOUS_VARIABLE (torch_warn_once_) = \
542
- [&] { \
543
- ::c10::Warning::warn ( \
544
- {__func__, __FILE__, static_cast <uint32_t >(__LINE__)}, \
545
- ::c10::detail::CompileTimeEmptyString{}, \
546
- false ); \
547
- return true ; \
548
- }()
549
- #else
550
580
#define _TORCH_WARN_ONCE (...) \
551
581
C10_UNUSED static const auto C10_ANONYMOUS_VARIABLE (torch_warn_once_) = \
552
582
[&] { \
553
- ::c10::Warning::warn ( \
554
- {__func__, __FILE__, static_cast <uint32_t >(__LINE__)}, \
555
- ::c10::str (__VA_ARGS__), \
556
- false); \
583
+ TORCH_WARN (__VA_ARGS__); \
557
584
return true ; \
558
585
}()
559
- #endif
560
586
561
- #define TORCH_WARN_ONCE (...) \
562
- if (::c10::Warning ::get_warnAlways ()) { \
563
- TORCH_WARN (__VA_ARGS__); \
564
- } else { \
565
- _TORCH_WARN_ONCE (__VA_ARGS__); \
587
+ #define TORCH_WARN_ONCE (...) \
588
+ if (::c10::WarningUtils ::get_warnAlways()) { \
589
+ TORCH_WARN (__VA_ARGS__); \
590
+ } else { \
591
+ _TORCH_WARN_ONCE (__VA_ARGS__); \
566
592
}
567
593
568
594
// Report an error with a specific argument
0 commit comments