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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
|
#include <c10/util/Backtrace.h>
#include <c10/util/CallOnce.h>
#include <c10/util/Flags.h>
#include <c10/util/Lazy.h>
#include <c10/util/Logging.h>
#include <c10/util/env.h>
#ifdef FBCODE_CAFFE2
#include <folly/synchronization/SanitizeThread.h>
#endif
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <algorithm>
#include <iostream>
// Common code that we use regardless of whether we use glog or not.
C10_DEFINE_bool(
caffe2_use_fatal_for_enforce,
false,
"If set true, when CAFFE_ENFORCE is not met, abort instead "
"of throwing an exception.");
namespace c10 {
namespace {
std::function<::c10::Backtrace()>& GetFetchStackTrace() {
static std::function<::c10::Backtrace()> func = []() {
return get_lazy_backtrace(/*frames_to_skip=*/1);
};
return func;
}
} // namespace
void SetStackTraceFetcher(std::function<::c10::Backtrace()> fetcher) {
GetFetchStackTrace() = std::move(fetcher);
}
void SetStackTraceFetcher(std::function<string()> fetcher) {
SetStackTraceFetcher([fetcher = std::move(fetcher)] {
return std::make_shared<PrecomputedLazyValue<std::string>>(fetcher());
});
}
void ThrowEnforceNotMet(
const char* file,
const int line,
const char* condition,
const std::string& msg,
const void* caller) {
c10::Error e(file, line, condition, msg, GetFetchStackTrace()(), caller);
if (FLAGS_caffe2_use_fatal_for_enforce) {
LOG(FATAL) << e.msg();
}
throw std::move(e);
}
void ThrowEnforceNotMet(
const char* file,
const int line,
const char* condition,
const char* msg,
const void* caller) {
ThrowEnforceNotMet(file, line, condition, std::string(msg), caller);
}
void ThrowEnforceFiniteNotMet(
const char* file,
const int line,
const char* condition,
const std::string& msg,
const void* caller) {
throw c10::EnforceFiniteError(
file, line, condition, msg, GetFetchStackTrace()(), caller);
}
void ThrowEnforceFiniteNotMet(
const char* file,
const int line,
const char* condition,
const char* msg,
const void* caller) {
ThrowEnforceFiniteNotMet(file, line, condition, std::string(msg), caller);
}
namespace {
class PyTorchStyleBacktrace : public OptimisticLazyValue<std::string> {
public:
PyTorchStyleBacktrace(SourceLocation source_location)
: backtrace_(GetFetchStackTrace()()), source_location_(source_location) {}
private:
std::string compute() const override {
return str(
"Exception raised from ",
source_location_,
" (most recent call first):\n",
backtrace_->get());
}
::c10::Backtrace backtrace_;
SourceLocation source_location_;
};
} // namespace
// PyTorch-style error message
// (This must be defined here for access to GetFetchStackTrace)
Error::Error(SourceLocation source_location, std::string msg)
: Error(
std::move(msg),
std::make_shared<PyTorchStyleBacktrace>(source_location)) {}
using APIUsageLoggerType = std::function<void(const std::string&)>;
using APIUsageMetadataLoggerType = std::function<void(
const std::string&,
const std::map<std::string, std::string>& metadata_map)>;
using DDPUsageLoggerType = std::function<void(const DDPLoggingData&)>;
namespace {
bool IsAPIUsageDebugMode() {
auto val = c10::utils::get_env("PYTORCH_API_USAGE_STDERR");
return val.has_value() && !val.value().empty(); // any non-empty value
}
void APIUsageDebug(const string& event) {
// use stderr to avoid messing with glog
std::cerr << "PYTORCH_API_USAGE " << event << '\n';
}
APIUsageLoggerType* GetAPIUsageLogger() {
static APIUsageLoggerType func =
IsAPIUsageDebugMode() ? &APIUsageDebug : [](const string&) {};
return &func;
}
APIUsageMetadataLoggerType* GetAPIUsageMetadataLogger() {
static APIUsageMetadataLoggerType func =
[](const std::string&,
const std::map<std::string, std::string>& metadata_map) {};
return &func;
}
DDPUsageLoggerType* GetDDPUsageLogger() {
static DDPUsageLoggerType func = [](const DDPLoggingData&) {};
return &func;
}
auto& EventSampledHandlerRegistry() {
static auto& registry =
*new std::map<std::string, std::unique_ptr<EventSampledHandler>>();
return registry;
}
} // namespace
void InitEventSampledHandlers(
std::vector<
std::pair<std::string_view, std::unique_ptr<EventSampledHandler>>>
handlers) {
static c10::once_flag flag;
c10::call_once(flag, [&]() {
auto& registry = EventSampledHandlerRegistry();
for (auto& [event, handler] : handlers) {
auto entry = registry.find(std::string{event});
if (entry == registry.end()) {
entry = registry.emplace(event, nullptr).first;
}
entry->second = std::move(handler);
}
});
}
const std::unique_ptr<EventSampledHandler>& GetEventSampledHandler(
std::string_view event) {
static std::mutex guard;
auto& registry = EventSampledHandlerRegistry();
// The getter can be executed from different threads.
std::lock_guard<std::mutex> lock(guard);
auto entry = registry.find(std::string{event});
if (entry == registry.end()) {
entry = registry.emplace(event, nullptr).first;
}
return entry->second;
}
void SetAPIUsageLogger(std::function<void(const std::string&)> logger) {
TORCH_CHECK(logger);
*GetAPIUsageLogger() = std::move(logger);
}
void SetAPIUsageMetadataLogger(
std::function<void(
const std::string&,
const std::map<std::string, std::string>& metadata_map)> logger) {
TORCH_CHECK(logger);
*GetAPIUsageMetadataLogger() = std::move(logger);
}
void SetPyTorchDDPUsageLogger(
std::function<void(const DDPLoggingData&)> logger) {
TORCH_CHECK(logger);
*GetDDPUsageLogger() = std::move(logger);
}
static int64_t GLOBAL_RANK = -1;
void SetGlobalRank(int64_t rank) {
GLOBAL_RANK = rank;
}
void LogAPIUsage(const std::string& event) try {
if (auto logger = GetAPIUsageLogger())
(*logger)(event);
// NOLINTNEXTLINE(bugprone-empty-catch)
} catch (std::bad_function_call&) {
// static destructor race
}
void LogAPIUsageMetadata(
const std::string& context,
const std::map<std::string, std::string>& metadata_map) try {
if (auto logger = GetAPIUsageMetadataLogger())
(*logger)(context, metadata_map);
// NOLINTNEXTLINE(bugprone-empty-catch)
} catch (std::bad_function_call&) {
// static destructor race
}
void LogPyTorchDDPUsage(const DDPLoggingData& ddpData) try {
if (auto logger = GetDDPUsageLogger())
(*logger)(ddpData);
// NOLINTNEXTLINE(bugprone-empty-catch)
} catch (std::bad_function_call&) {
// static destructor race
}
namespace detail {
bool LogAPIUsageFakeReturn(const std::string& event) try {
if (auto logger = GetAPIUsageLogger())
(*logger)(event);
return true;
// NOLINTNEXTLINE(bugprone-empty-catch)
} catch (std::bad_function_call&) {
// static destructor race
return true;
}
namespace {
void setLogLevelFlagFromEnv();
} // namespace
} // namespace detail
} // namespace c10
#if defined(C10_USE_GFLAGS) && defined(C10_USE_GLOG)
// When GLOG depends on GFLAGS, these variables are being defined in GLOG
// directly via the GFLAGS definition, so we will use DECLARE_* to declare
// them, and use them in Caffe2.
// GLOG's minloglevel
DECLARE_int32(minloglevel);
// GLOG's verbose log value.
DECLARE_int32(v);
// GLOG's logtostderr value
DECLARE_bool(logtostderr);
#endif // defined(C10_USE_GFLAGS) && defined(C10_USE_GLOG)
#if !defined(C10_USE_GLOG)
// This backward compatibility flags are in order to deal with cases where
// Caffe2 are not built with glog, but some init flags still pass in these
// flags. They may go away in the future.
C10_DEFINE_int32(minloglevel, 0, "Equivalent to glog minloglevel");
C10_DEFINE_int32(v, 0, "Equivalent to glog verbose");
C10_DEFINE_bool(logtostderr, false, "Equivalent to glog logtostderr");
#endif // !defined(c10_USE_GLOG)
#ifdef C10_USE_GLOG
// Provide easy access to the above variables, regardless whether GLOG is
// dependent on GFLAGS or not. Note that the namespace (fLI, fLB) is actually
// consistent between GLOG and GFLAGS, so we can do the below declaration
// consistently.
namespace c10 {
using fLB::FLAGS_logtostderr;
using fLI::FLAGS_minloglevel;
using fLI::FLAGS_v;
} // namespace c10
C10_DEFINE_int(
caffe2_log_level,
google::GLOG_WARNING,
"The minimum log level that caffe2 will output.");
namespace c10 {
namespace {
void initGoogleLogging(char const* name) {
#if !defined(_MSC_VER)
// This trick can only be used on UNIX platforms
if (!::google::IsGoogleLoggingInitialized())
#endif
{
::google::InitGoogleLogging(name);
#if !defined(_MSC_VER)
// This is never defined on Windows
::google::InstallFailureSignalHandler();
#endif
}
}
} // namespace
void initLogging() {
detail::setLogLevelFlagFromEnv();
UpdateLoggingLevelsFromFlags();
}
bool InitCaffeLogging(int* argc, char** argv) {
if (*argc == 0) {
return true;
}
initGoogleLogging(argv[0]);
UpdateLoggingLevelsFromFlags();
return true;
}
void UpdateLoggingLevelsFromFlags() {
#ifdef FBCODE_CAFFE2
// TODO(T82645998): Fix data race exposed by TSAN.
folly::annotate_ignore_thread_sanitizer_guard g(__FILE__, __LINE__);
#endif
// If caffe2_log_level is set and is lower than the min log level by glog,
// we will transfer the caffe2_log_level setting to glog to override that.
FLAGS_minloglevel = std::min(FLAGS_caffe2_log_level, FLAGS_minloglevel);
// If caffe2_log_level is explicitly set, let's also turn on logtostderr.
if (FLAGS_caffe2_log_level < google::GLOG_WARNING) {
FLAGS_logtostderr = 1;
}
// Also, transfer the caffe2_log_level verbose setting to glog.
if (FLAGS_caffe2_log_level < 0) {
FLAGS_v = std::min(FLAGS_v, -FLAGS_caffe2_log_level);
}
}
void ShowLogInfoToStderr() {
FLAGS_logtostderr = 1;
FLAGS_minloglevel = std::min(FLAGS_minloglevel, google::GLOG_INFO);
}
} // namespace c10
#else // !C10_USE_GLOG
#ifdef ANDROID
#include <android/log.h>
#endif // ANDROID
C10_DEFINE_int(
caffe2_log_level,
c10::GLOG_WARNING,
"The minimum log level that caffe2 will output.");
namespace c10 {
void initLogging() {
detail::setLogLevelFlagFromEnv();
}
bool InitCaffeLogging(int* argc, char** argv) {
// When doing InitCaffeLogging, we will assume that caffe's flag parser has
// already finished.
if (*argc == 0)
return true;
if (!c10::CommandLineFlagsHasBeenParsed()) {
std::cerr << "InitCaffeLogging() has to be called after "
"c10::ParseCommandLineFlags. Modify your program to make sure "
"of this."
<< '\n';
return false;
}
if (FLAGS_caffe2_log_level > GLOG_FATAL) {
std::cerr << "The log level of Caffe2 has to be no larger than GLOG_FATAL("
<< GLOG_FATAL << "). Capping it to GLOG_FATAL." << '\n';
FLAGS_caffe2_log_level = GLOG_FATAL;
}
return true;
}
void UpdateLoggingLevelsFromFlags() {}
void ShowLogInfoToStderr() {
FLAGS_caffe2_log_level = GLOG_INFO;
}
MessageLogger::MessageLogger(const char* file, int line, int severity)
: severity_(severity) {
if (severity_ < FLAGS_caffe2_log_level) {
// Nothing needs to be logged.
return;
}
#ifdef ANDROID
tag_ = "native";
#else // !ANDROID
tag_ = "";
#endif // ANDROID
time_t rawtime = 0;
time(&rawtime);
#ifndef _WIN32
struct tm raw_timeinfo = {0};
struct tm* timeinfo = &raw_timeinfo;
localtime_r(&rawtime, timeinfo);
#else
// is thread safe on Windows
struct tm* timeinfo = localtime(&rawtime);
#endif
#ifndef _WIN32
// Get the current nanoseconds since epoch
struct timespec ts = {0};
clock_gettime(CLOCK_MONOTONIC, &ts);
long ns = ts.tv_nsec;
#else
long ns = 0;
#endif
if (GLOBAL_RANK != -1) {
stream_ << "[rank" << GLOBAL_RANK << "]:";
}
stream_ << "[" << CAFFE2_SEVERITY_PREFIX[std::min(4, GLOG_FATAL - severity_)]
<< (timeinfo->tm_mon + 1) * 100 + timeinfo->tm_mday
<< std::setfill('0') << " " << std::setw(2) << timeinfo->tm_hour
<< ":" << std::setw(2) << timeinfo->tm_min << ":" << std::setw(2)
<< timeinfo->tm_sec << "." << std::setw(9) << ns << " "
<< c10::detail::StripBasename(std::string(file)) << ":" << line
<< "] ";
}
// Output the contents of the stream to the proper channel on destruction.
MessageLogger::~MessageLogger() {
if (severity_ < FLAGS_caffe2_log_level) {
// Nothing needs to be logged.
return;
}
stream_ << "\n";
#ifdef ANDROID
static const int android_log_levels[] = {
ANDROID_LOG_FATAL, // LOG_FATAL
ANDROID_LOG_ERROR, // LOG_ERROR
ANDROID_LOG_WARN, // LOG_WARNING
ANDROID_LOG_INFO, // LOG_INFO
ANDROID_LOG_DEBUG, // VLOG(1)
ANDROID_LOG_VERBOSE, // VLOG(2) .. VLOG(N)
};
int android_level_index = GLOG_FATAL - std::min(GLOG_FATAL, severity_);
int level = android_log_levels[std::min(android_level_index, 5)];
// Output the log string the Android log at the appropriate level.
__android_log_print(level, tag_, "%s", stream_.str().c_str());
// Indicate termination if needed.
if (severity_ == GLOG_FATAL) {
__android_log_print(ANDROID_LOG_FATAL, tag_, "terminating.\n");
}
#else // !ANDROID
if (severity_ >= FLAGS_caffe2_log_level) {
// If not building on Android, log all output to std::cerr.
std::cerr << stream_.str();
// Simulating the glog default behavior: if the severity is above INFO,
// we flush the stream so that the output appears immediately on std::cerr.
// This is expected in some of our tests.
if (severity_ > GLOG_INFO) {
std::cerr << std::flush;
}
}
#endif // ANDROID
if (severity_ == GLOG_FATAL) {
DealWithFatal();
}
}
} // namespace c10
#endif // !C10_USE_GLOG
namespace c10::detail {
namespace {
void setLogLevelFlagFromEnv() {
auto level_env = c10::utils::get_env("TORCH_CPP_LOG_LEVEL");
// Not set, fallback to the default level (i.e. WARNING).
std::string level{level_env.has_value() ? level_env.value() : ""};
if (level.empty()) {
return;
}
std::transform(
level.begin(), level.end(), level.begin(), [](unsigned char c) {
return toupper(c);
});
if (level == "0" || level == "INFO") {
FLAGS_caffe2_log_level = 0;
return;
}
if (level == "1" || level == "WARNING") {
FLAGS_caffe2_log_level = 1;
return;
}
if (level == "2" || level == "ERROR") {
FLAGS_caffe2_log_level = 2;
return;
}
if (level == "3" || level == "FATAL") {
FLAGS_caffe2_log_level = 3;
return;
}
std::cerr
<< "`TORCH_CPP_LOG_LEVEL` environment variable cannot be parsed. Valid values are "
"`INFO`, `WARNING`, `ERROR`, and `FATAL` or their numerical equivalents `0`, `1`, "
"`2`, and `3`."
<< '\n';
}
} // namespace
} // namespace c10::detail
|