pytorch  1.8.2
About: PyTorch provides Tensor computation (like NumPy) with strong GPU acceleration and Deep Neural Networks (in Python) built on a tape-based autograd system. LTS (Long Term Support) release.
  Fossies Dox: pytorch-1.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Logging.h
Go to the documentation of this file.
1#ifndef C10_UTIL_LOGGING_H_
2#define C10_UTIL_LOGGING_H_
3
4#include <climits>
5#include <exception>
6#include <functional>
7#include <limits>
8#include <sstream>
9
10#include <c10/macros/Macros.h>
11#include <c10/util/Exception.h>
12#include <c10/util/Flags.h>
13#include <c10/util/StringUtil.h>
14
15// CAFFE2_LOG_THRESHOLD is a compile time flag that would allow us to turn off
16// logging at compile time so no logging message below that level is produced
17// at all. The value should be between INT_MIN and CAFFE_FATAL.
18#ifndef CAFFE2_LOG_THRESHOLD
19// If we have not defined the compile time log threshold, we keep all the
20// log cases.
21#define CAFFE2_LOG_THRESHOLD INT_MIN
22#endif // CAFFE2_LOG_THRESHOLD
23
24// Below are different implementations for glog and non-glog cases.
25#ifdef C10_USE_GLOG
27#else // !C10_USE_GLOG
29#endif // C10_USE_GLOG
30
31C10_DECLARE_int(caffe2_log_level);
32C10_DECLARE_bool(caffe2_use_fatal_for_enforce);
33
34// Some versions of GLOG support less-spammy version of LOG_EVERY_MS. If it's
35// not available - just short-circuit to the always working one one.
36// We define the C10_ name to avoid confusing other files
37#ifdef LOG_EVERY_MS
38#define C10_LOG_EVERY_MS(severity, ms) LOG_EVERY_MS(severity, ms)
39#else
40#define C10_LOG_EVERY_MS(severity, ms) LOG(severity)
41#endif
42
43// Same for LOG_FIRST_N
44#ifdef LOG_FIRST_N
45#define C10_LOG_FIRST_N(severity, n) LOG_FIRST_N(severity, n)
46#else
47#define C10_LOG_FIRST_N(severity, n) LOG(severity)
48#endif
49
50// Same for LOG_EVERY_N
51#ifdef LOG_EVERY_N
52#define C10_LOG_EVERY_N(severity, n) LOG_EVERY_N(severity, n)
53#else
54#define C10_LOG_EVERY_N(severity, n) LOG(severity)
55#endif
56
57namespace c10 {
58
59using std::string;
60
61// Functions that we use for initialization.
62C10_API bool InitCaffeLogging(int* argc, char** argv);
64
65[[noreturn]] C10_API void ThrowEnforceNotMet(
66 const char* file,
67 const int line,
68 const char* condition,
69 const std::string& msg,
70 const void* caller = nullptr);
71
72[[noreturn]] C10_API void ThrowEnforceFiniteNotMet(
73 const char* file,
74 const int line,
75 const char* condition,
76 const std::string& msg,
77 const void* caller = nullptr);
78
79constexpr bool IsUsingGoogleLogging() {
80#ifdef C10_USE_GLOG
81 return true;
82#else
83 return false;
84#endif
85}
86
87/**
88 * A utility to allow one to show log info to stderr after the program starts.
89 *
90 * This is similar to calling GLOG's --logtostderr, or setting caffe2_log_level
91 * to smaller than INFO. You are recommended to only use this in a few sparse
92 * cases, such as when you want to write a tutorial or something. Normally, use
93 * the commandline flags to set the log level.
94 */
96
97C10_API void SetStackTraceFetcher(std::function<string(void)> fetcher);
98
100
101#define CAFFE_ENFORCE(condition, ...) \
102 do { \
103 if (C10_UNLIKELY(!(condition))) { \
104 ::c10::ThrowEnforceNotMet( \
105 __FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__)); \
106 } \
107 } while (false)
108
109#define CAFFE_ENFORCE_FINITE(condition, ...) \
110 do { \
111 if (C10_UNLIKELY(!(condition))) { \
112 ::c10::ThrowEnforceFiniteNotMet( \
113 __FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__)); \
114 } \
115 } while (false)
116
117#define CAFFE_ENFORCE_WITH_CALLER(condition, ...) \
118 do { \
119 if (C10_UNLIKELY(!(condition))) { \
120 ::c10::ThrowEnforceNotMet( \
121 __FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__), this); \
122 } \
123 } while (false)
124
125#define CAFFE_THROW(...) \
126 ::c10::ThrowEnforceNotMet(__FILE__, __LINE__, "", ::c10::str(__VA_ARGS__))
127
128/**
129 * Rich logging messages
130 *
131 * CAFFE_ENFORCE_THAT can be used with one of the "checker functions" that
132 * capture input argument values and add it to the exception message. E.g.
133 * `CAFFE_ENFORCE_THAT(Equals(foo(x), bar(y)), "Optional additional message")`
134 * would evaluate both foo and bar only once and if the results are not equal -
135 * include them in the exception message.
136 *
137 * Some of the basic checker functions like Equals or Greater are already
138 * defined below. Other header might define customized checkers by adding
139 * functions to caffe2::enforce_detail namespace. For example:
140 *
141 * namespace caffe2 { namespace enforce_detail {
142 * inline EnforceFailMessage IsVector(const vector<int64_t>& shape) {
143 * if (shape.size() == 1) { return EnforceOK(); }
144 * return c10::str("Shape ", shape, " is not a vector");
145 * }
146 * }}
147 *
148 * With further usages like `CAFFE_ENFORCE_THAT(IsVector(Input(0).dims()))`
149 *
150 * Convenient wrappers for binary operations like CAFFE_ENFORCE_EQ are provided
151 * too. Please use them instead of CHECK_EQ and friends for failures in
152 * user-provided input.
153 */
154
155namespace enforce_detail {
156
158
160 public:
161#ifdef _MSC_VER
162 // MSVC + NVCC ignores constexpr and will issue a warning if included.
163 /* implicit */ EnforceFailMessage(EnforceOK) : msg_(nullptr) {}
164#else
165 constexpr /* implicit */ EnforceFailMessage(EnforceOK) : msg_(nullptr) {}
166#endif
171
172 // Catch all wrong usages like CAFFE_ENFORCE_THAT(x < y)
173 template <class... Args>
174 /* implicit */ EnforceFailMessage(Args...) {
175 static_assert(
176 // This stands for an "impossible" condition. Plain `false` doesn't
177 // trick compiler enough.
178 sizeof...(Args) == std::numeric_limits<std::size_t>::max(),
179 "CAFFE_ENFORCE_THAT has to be used with one of special check functions "
180 "like `Equals`. Use CAFFE_ENFORCE for simple boolean checks.");
181 }
182
183 /* implicit */ EnforceFailMessage(std::string&& msg);
184
185 inline bool bad() const {
186 return msg_ != nullptr;
187 }
188 std::string get_message_and_free(const std::string& extra) const {
189 std::string r;
190 if (extra.empty()) {
191 r = std::move(*msg_);
192 } else {
193 r = ::c10::str(std::move(*msg_), ". ", extra);
194 }
195 delete msg_;
196 return r;
197 }
198
199 private:
200 std::string* msg_{};
201};
202
203#define BINARY_COMP_HELPER(name, op) \
204 template <typename T1, typename T2> \
205 inline EnforceFailMessage name(const T1& x, const T2& y) { \
206 if (x op y) { \
207 return EnforceOK(); \
208 } \
209 return c10::str(x, " vs ", y); \
210 }
217#undef BINARY_COMP_HELPER
218
219#define CAFFE_ENFORCE_THAT_IMPL(condition, expr, ...) \
220 do { \
221 using namespace ::c10::enforce_detail; \
222 const EnforceFailMessage& CAFFE_ENFORCE_THAT_IMPL_r_ = (condition); \
223 if (C10_UNLIKELY(CAFFE_ENFORCE_THAT_IMPL_r_.bad())) { \
224 ::c10::ThrowEnforceNotMet( \
225 __FILE__, \
226 __LINE__, \
227 expr, \
228 CAFFE_ENFORCE_THAT_IMPL_r_.get_message_and_free( \
229 ::c10::str(__VA_ARGS__))); \
230 } \
231 } while (false)
232
233#define CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(condition, expr, ...) \
234 do { \
235 using namespace ::c10::enforce_detail; \
236 const EnforceFailMessage& CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER_r_ = \
237 (condition); \
238 if (C10_UNLIKELY(CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER_r_.bad())) { \
239 ::c10::ThrowEnforceNotMet( \
240 __FILE__, \
241 __LINE__, \
242 expr, \
243 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER_r_.get_message_and_free( \
244 ::c10::str(__VA_ARGS__)), \
245 this); \
246 } \
247 } while (false)
248} // namespace enforce_detail
249
250#define CAFFE_ENFORCE_THAT(condition, ...) \
251 CAFFE_ENFORCE_THAT_IMPL((condition), #condition, __VA_ARGS__)
252
253#define CAFFE_ENFORCE_EQ(x, y, ...) \
254 CAFFE_ENFORCE_THAT_IMPL(Equals((x), (y)), #x " == " #y, __VA_ARGS__)
255#define CAFFE_ENFORCE_NE(x, y, ...) \
256 CAFFE_ENFORCE_THAT_IMPL(NotEquals((x), (y)), #x " != " #y, __VA_ARGS__)
257#define CAFFE_ENFORCE_LE(x, y, ...) \
258 CAFFE_ENFORCE_THAT_IMPL(LessEquals((x), (y)), #x " <= " #y, __VA_ARGS__)
259#define CAFFE_ENFORCE_LT(x, y, ...) \
260 CAFFE_ENFORCE_THAT_IMPL(Less((x), (y)), #x " < " #y, __VA_ARGS__)
261#define CAFFE_ENFORCE_GE(x, y, ...) \
262 CAFFE_ENFORCE_THAT_IMPL(GreaterEquals((x), (y)), #x " >= " #y, __VA_ARGS__)
263#define CAFFE_ENFORCE_GT(x, y, ...) \
264 CAFFE_ENFORCE_THAT_IMPL(Greater((x), (y)), #x " > " #y, __VA_ARGS__)
265#define CAFFE_ENFORCE_EQ_WITH_CALLER(x, y, ...) \
266 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER( \
267 Equals((x), (y)), #x " == " #y, __VA_ARGS__)
268#define CAFFE_ENFORCE_NE_WITH_CALLER(x, y, ...) \
269 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER( \
270 NotEquals((x), (y)), #x " != " #y, __VA_ARGS__)
271#define CAFFE_ENFORCE_LE_WITH_CALLER(x, y, ...) \
272 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER( \
273 LessEquals((x), (y)), #x " <= " #y, __VA_ARGS__)
274#define CAFFE_ENFORCE_LT_WITH_CALLER(x, y, ...) \
275 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(Less((x), (y)), #x " < " #y, __VA_ARGS__)
276#define CAFFE_ENFORCE_GE_WITH_CALLER(x, y, ...) \
277 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER( \
278 GreaterEquals((x), (y)), #x " >= " #y, __VA_ARGS__)
279#define CAFFE_ENFORCE_GT_WITH_CALLER(x, y, ...) \
280 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER( \
281 Greater((x), (y)), #x " > " #y, __VA_ARGS__)
282
283/**
284 * Very lightweight logging for the first time API usage. It's beneficial for
285 * tracking of individual functionality usage in larger applications.
286 *
287 * In order to ensure light-weightedness of logging, we utilize static variable
288 * trick - LogAPIUsage will be invoked only once and further invocations will
289 * just do an atomic check.
290 *
291 * Example:
292 * // Logs caller info with an arbitrary text event, if there is a usage.
293 * C10_LOG_API_USAGE_ONCE("my_api");
294 */
295#define C10_LOG_API_USAGE_ONCE(...) \
296 C10_UNUSED static bool C10_ANONYMOUS_VARIABLE(logFlag) = \
297 ::c10::detail::LogAPIUsageFakeReturn(__VA_ARGS__);
298
299// API usage logging capabilities
300C10_API void SetAPIUsageLogger(std::function<void(const std::string&)> logger);
301C10_API void LogAPIUsage(const std::string& context);
302
303// PyTorch ddp usage logging capabilities
304// DDPLoggingData holds data that can be logged in applications
305// for analysis and debugging. Data structure is defined in
306// c10 directory so that it can be easily imported by both c10
307// and torch files.
308// TODO -- right now starting with logging a small set of straightforward
309// fields, will add more fields as follow ups such as performance stats,
310// internal states and env variables and etc.
312 // Data that can be got during DistributedDataParallel construction time
314 int rank;
315 std::string module_name;
322 std::string backend_name;
323};
324
327
328namespace detail {
329// Return value is needed to do the static variable initialization trick
330C10_API bool LogAPIUsageFakeReturn(const std::string& context);
331}
332
333} // namespace c10
334
335#endif // C10_UTIL_LOGGING_H_
#define C10_API
Definition: Export.h:91
C10_DECLARE_bool(caffe2_use_fatal_for_enforce)
#define BINARY_COMP_HELPER(name, op)
Definition: Logging.h:203
C10_DECLARE_int(caffe2_log_level)
uint32_t max
Definition: Resource.cpp:270
Args({2<< 5}) -> Args({2<< 8}) ->Args({2<< 12}) ->Args({2<< 14})
const int r
The primary ATen error class.
Definition: Exception.h:28
std::string get_message_and_free(const std::string &extra) const
Definition: Logging.h:188
EnforceFailMessage(EnforceFailMessage &&)=default
EnforceFailMessage & operator=(const EnforceFailMessage &)=delete
constexpr EnforceFailMessage(EnforceOK)
Definition: Logging.h:165
EnforceFailMessage & operator=(EnforceFailMessage &&)=delete
EnforceFailMessage(const EnforceFailMessage &)=delete
bool LogAPIUsageFakeReturn(const std::string &event)
Definition: Logging.cpp:123
EnforceFailMessage GreaterEquals(const T1 &x, const T2 &y)
Definition: Logging.h:214
EnforceFailMessage LessEquals(const T1 &x, const T2 &y)
Definition: Logging.h:216
EnforceFailMessage Greater(const T1 &x, const T2 &y)
Definition: Logging.h:213
EnforceFailMessage NotEquals(const T1 &x, const T2 &y)
Definition: Logging.h:212
EnforceFailMessage Equals(const T1 &x, const T2 &y)
Definition: Logging.h:211
EnforceFailMessage Less(const T1 &x, const T2 &y)
Definition: Logging.h:215
constexpr remove_reference_t< T > && move(T &&t) noexcept
Definition: variant.h:418
This file contains functionality to take a C++ function and infer its c10::FunctionSchema.
Definition: alias_info.h:7
void ShowLogInfoToStderr()
A utility to allow one to show log info to stderr after the program starts.
Definition: Logging.cpp:257
void SetStackTraceFetcher(std::function< string(void)> fetcher)
Definition: Logging.cpp:35
bool InitCaffeLogging(int *argc, char **argv)
Definition: Logging.cpp:234
void SetPyTorchDDPUsageLogger(std::function< void(const c10::DDPLoggingData &)> logger)
Definition: Logging.cpp:103
void LogAPIUsage(const std::string &event)
Definition: Logging.cpp:108
void SetAPIUsageLogger(std::function< void(const std::string &)> logger)
Definition: Logging.cpp:98
void LogPyTorchDDPUsage(const c10::DDPLoggingData &ddpData)
Definition: Logging.cpp:115
constexpr bool IsUsingGoogleLogging()
Definition: Logging.h:79
void ThrowEnforceNotMet(const char *file, const int line, const char *condition, const std::string &msg, const void *caller)
Definition: Logging.cpp:39
void UpdateLoggingLevelsFromFlags()
Definition: Logging.cpp:255
void ThrowEnforceFiniteNotMet(const char *file, const int line, const char *condition, const std::string &msg, const void *caller)
Definition: Logging.cpp:52
parameter efficient embedding termed TT which can be plugged in into any model and trained end to end The benefits of our compressed TT layer are twofold instead of storing huge embedding it stores a sequence of much smaller dimensional and dimensional necessary for reconstructing the required which allows compressing the model significantly at the cost of a negligible performance drop the overall number of parameters can be relatively which allows to use larger batches or train efficiently in a case of limited resources DOC vector< int >
$ where the softplus function
Definition: softplus_op.cc:49
Module caffe2.python.context.
char ** argv
std::string backend_name
Definition: Logging.h:322
bool gradient_as_bucket_view
Definition: Logging.h:321
std::string module_name
Definition: Logging.h:315
bool find_unused_parameters
Definition: Logging.h:320
std::vector< int > device_ids
Definition: Logging.h:316
bool broadcast_buffers
Definition: Logging.h:318