1#ifndef C10_UTIL_EXCEPTION_H_
2#define C10_UTIL_EXCEPTION_H_
4#include <c10/macros/Macros.h>
5#include <c10/util/Deprecated.h>
6#include <c10/util/StringUtil.h>
7#include <c10/util/variant.h>
16#if defined(_MSC_VER) && _MSC_VER <= 1900
17#define __func__ __FUNCTION__
29class C10_API
Error :
public std::exception {
36 std::vector<std::string> context_;
41 std::string backtrace_;
49 std::string what_without_backtrace_;
61 Error(SourceLocation source_location, std::string msg);
67 const char* condition,
68 const std::string& msg,
69 const std::string& backtrace,
70 const void* caller =
nullptr);
73 Error(std::string msg, std::string backtrace,
const void* caller =
nullptr);
81 const std::string&
msg()
const {
85 const std::vector<std::string>&
context()
const {
96 const char*
what() const noexcept
override {
108 return what_without_backtrace_.c_str();
113 std::string compute_what(
bool include_backtrace)
const;
125 const SourceLocation& source_location,
131 SourceLocation source_location,
137 SourceLocation source_location,
138 ::c10::detail::CompileTimeEmptyString msg,
144 const std::string&
msg()
const;
152 SourceLocation source_location_;
175namespace WarningUtils {
299#define C10_THROW_ERROR(err_type, msg) \
300 throw ::c10::err_type( \
301 {__func__, __FILE__, static_cast<uint32_t>(__LINE__)}, msg)
306#define C10_EXPAND_MSVC_WORKAROUND(x) x
323#if defined(__CUDACC__)
324#define C10_UNLIKELY_OR_CONST(e) e
326#define C10_UNLIKELY_OR_CONST(e) C10_UNLIKELY(e)
333#ifdef STRIP_ERROR_MESSAGES
334#define TORCH_RETHROW(e, ...) throw
336#define TORCH_RETHROW(e, ...) \
338 e.add_context(::c10::str(__VA_ARGS__)); \
362#ifdef STRIP_ERROR_MESSAGES
363#define TORCH_INTERNAL_ASSERT(cond, ...) \
364 if (C10_UNLIKELY_OR_CONST(!(cond))) { \
365 ::c10::detail::torchCheckFail( \
368 static_cast<uint32_t>(__LINE__), \
369 #cond " INTERNAL ASSERT FAILED at " C10_STRINGIZE(__FILE__)); \
377#define TORCH_INTERNAL_ASSERT(cond, ...) \
378 if (C10_UNLIKELY_OR_CONST(!(cond))) { \
379 ::c10::detail::torchInternalAssertFail( \
382 static_cast<uint32_t>(__LINE__), \
384 " INTERNAL ASSERT FAILED at " C10_STRINGIZE(__FILE__) ":" C10_STRINGIZE( \
385 __LINE__) ", please report a bug to PyTorch. ", \
386 c10::str(__VA_ARGS__)); \
410#define TORCH_CHECK_WITH(error_t, cond, ...) \
411 TORCH_CHECK_WITH_MSG(error_t, cond, "", __VA_ARGS__)
413#ifdef STRIP_ERROR_MESSAGES
414#define TORCH_CHECK_MSG(cond, type, ...) \
415 (#cond #type " CHECK FAILED at " C10_STRINGIZE(__FILE__))
416#define TORCH_CHECK_WITH_MSG(error_t, cond, type, ...) \
417 if (C10_UNLIKELY_OR_CONST(!(cond))) { \
418 C10_THROW_ERROR(Error, TORCH_CHECK_MSG(cond, type, __VA_ARGS__)); \
423template <
typename... Args>
425 return ::c10::str(args...);
439#define TORCH_CHECK_MSG(cond, type, ...) \
440 (::c10::detail::torchCheckMsgImpl( \
442 " to be true, but got false. " \
443 "(Could this error message be improved? If so, " \
444 "please report an enhancement request to PyTorch.)", \
446#define TORCH_CHECK_WITH_MSG(error_t, cond, type, ...) \
447 if (C10_UNLIKELY_OR_CONST(!(cond))) { \
448 C10_THROW_ERROR(error_t, TORCH_CHECK_MSG(cond, type, __VA_ARGS__)); \
459 const std::string& msg);
476 const char* userMsg);
482 ::c10::detail::CompileTimeEmptyString ) {
490 const std::string& userMsg);
495#ifdef STRIP_ERROR_MESSAGES
496#define TORCH_CHECK(cond, ...) \
497 if (C10_UNLIKELY_OR_CONST(!(cond))) { \
498 ::c10::detail::torchCheckFail( \
501 static_cast<uint32_t>(__LINE__), \
502 TORCH_CHECK_MSG(cond, "", __VA_ARGS__)); \
505#define TORCH_CHECK(cond, ...) \
506 if (C10_UNLIKELY_OR_CONST(!(cond))) { \
507 ::c10::detail::torchCheckFail( \
510 static_cast<uint32_t>(__LINE__), \
511 TORCH_CHECK_MSG(cond, "", ##__VA_ARGS__)); \
518#if defined(__CUDACC__) || defined(__HIPCC__)
519#define TORCH_CHECK_IF_NOT_ON_CUDA(cond, ...)
521#define TORCH_CHECK_IF_NOT_ON_CUDA(cond, ...) TORCH_CHECK(cond, ##__VA_ARGS__)
530#define TORCH_INTERNAL_ASSERT_DEBUG_ONLY(...) \
532 C10_EXPAND_MSVC_WORKAROUND(TORCH_INTERNAL_ASSERT(__VA_ARGS__))
534#define TORCH_INTERNAL_ASSERT_DEBUG_ONLY(...) \
535 C10_EXPAND_MSVC_WORKAROUND(TORCH_INTERNAL_ASSERT(__VA_ARGS__))
542#define TORCH_CHECK_LINALG(cond, ...) \
543 TORCH_CHECK_WITH_MSG(LinAlgError, cond, "LINALG", __VA_ARGS__)
546#define TORCH_CHECK_INDEX(cond, ...) \
547 TORCH_CHECK_WITH_MSG(IndexError, cond, "INDEX", __VA_ARGS__)
550#define TORCH_CHECK_VALUE(cond, ...) \
551 TORCH_CHECK_WITH_MSG(ValueError, cond, "VALUE", __VA_ARGS__)
554#define TORCH_CHECK_TYPE(cond, ...) \
555 TORCH_CHECK_WITH_MSG(TypeError, cond, "TYPE", __VA_ARGS__)
558#define TORCH_CHECK_NOT_IMPLEMENTED(cond, ...) \
559 TORCH_CHECK_WITH_MSG(NotImplementedError, cond, "TYPE", __VA_ARGS__)
561#ifdef STRIP_ERROR_MESSAGES
562#define WARNING_MESSAGE_STRING(...) \
563 ::c10::detail::CompileTimeEmptyString {}
565#define WARNING_MESSAGE_STRING(...) ::c10::str(__VA_ARGS__)
572#define _TORCH_WARN_WITH(...) ((void)0);
574#define _TORCH_WARN_WITH(warning_t, ...) \
575 ::c10::warn(::c10::Warning( \
577 {__func__, __FILE__, static_cast<uint32_t>(__LINE__)}, \
578 WARNING_MESSAGE_STRING(__VA_ARGS__), \
582#define TORCH_WARN(...) _TORCH_WARN_WITH(::c10::UserWarning, __VA_ARGS__);
584#define TORCH_WARN_DEPRECATION(...) \
585 _TORCH_WARN_WITH(::c10::DeprecationWarning, __VA_ARGS__);
590#define _TORCH_WARN_ONCE(...) \
591 C10_UNUSED static const auto C10_ANONYMOUS_VARIABLE(torch_warn_once_) = \
593 TORCH_WARN(__VA_ARGS__); \
598#define TORCH_WARN_ONCE(...) ((void)0);
600#define TORCH_WARN_ONCE(...) \
601 if (::c10::WarningUtils::get_warnAlways()) { \
602 TORCH_WARN(__VA_ARGS__); \
604 _TORCH_WARN_ONCE(__VA_ARGS__); \
610#define TORCH_CHECK_ARG(cond, argN, ...) \
611 TORCH_CHECK(cond, "invalid argument ", argN, ": ", __VA_ARGS__)
654#define AT_ASSERT(...) \
656 ::c10::detail::deprecated_AT_ASSERT(); \
657 C10_EXPAND_MSVC_WORKAROUND(TORCH_INTERNAL_ASSERT(__VA_ARGS__)); \
668#define AT_ASSERTM(cond, ...) \
670 ::c10::detail::deprecated_AT_ASSERTM(); \
671 C10_EXPAND_MSVC_WORKAROUND(TORCH_INTERNAL_ASSERT(cond, __VA_ARGS__)); \
678#define AT_ERROR(...) \
680 ::c10::detail::deprecated_AT_ERROR(); \
681 C10_EXPAND_MSVC_WORKAROUND(TORCH_CHECK(false, ::c10::str(__VA_ARGS__))); \
Definition: Exception.h:280
Definition: Exception.h:258
The primary ATen error class.
Definition: Exception.h:29
const std::string & msg() const
Definition: Exception.h:81
Error(const char *file, const uint32_t line, const char *condition, const std::string &msg, const std::string &backtrace, const void *caller=nullptr)
const char * what() const noexcept override
Returns the complete error message, including the source location.
Definition: Exception.h:96
Error(std::string msg, std::string backtrace, const void *caller=nullptr)
const void * caller() const noexcept
Definition: Exception.h:100
void add_context(std::string msg)
const std::string & backtrace() const
Definition: Exception.h:89
const std::vector< std::string > & context() const
Definition: Exception.h:85
const char * what_without_backtrace() const noexcept
Returns only the error message string, without source location.
Definition: Exception.h:107
Error(SourceLocation source_location, std::string msg)
Definition: Exception.h:234
Definition: Exception.h:270
Definition: Exception.h:252
Definition: Exception.h:264
Definition: Exception.h:274
Definition: Exception.h:246
Definition: Exception.h:240
Definition: Exception.h:119
Definition: Exception.h:118
Definition: Exception.h:168
virtual ~WarningHandler()=default
virtual void process(const Warning &warning)
The default warning handler. Prints the message to stderr.
Definition: Exception.h:116
Warning(warning_variant_t type, const SourceLocation &source_location, std::string msg, bool verbatim)
c10::variant< UserWarning, DeprecationWarning > warning_variant_t
Definition: Exception.h:121
const std::string & msg() const
Warning(warning_variant_t type, SourceLocation source_location, const char *msg, bool verbatim)
warning_variant_t type() const
const SourceLocation & source_location() const
Warning(warning_variant_t type, SourceLocation source_location, ::c10::detail::CompileTimeEmptyString msg, bool verbatim)
Definition: Exception.h:199
~WarningHandlerGuard()
Definition: Exception.h:207
WarningHandlerGuard(WarningHandler *new_handler)
Definition: Exception.h:203
WarningHandler * get_warning_handler() noexcept(true)
Gets the global warning handler.
bool get_warnAlways(void) noexcept(true)
void set_warning_handler(WarningHandler *handler) noexcept(true)
Sets the global warning handler.
void set_warnAlways(bool) noexcept(true)
The TORCH_WARN_ONCE macro is difficult to test for.
void torchInternalAssertFail(const char *func, const char *file, uint32_t line, const char *condMsg, const char *userMsg)
void deprecated_AT_ASSERTM()
Definition: Exception.h:645
decltype(auto) torchCheckMsgImpl(const char *, const Args &... args)
Definition: Exception.h:424
void deprecated_AT_ERROR()
Definition: Exception.h:625
void torchCheckFail(const char *func, const char *file, uint32_t line, const std::string &msg)
void deprecated_AT_ASSERT()
Definition: Exception.h:635
std::string GetExceptionString(const std::exception &e)
void warn(const Warning &warning)
Definition: Exception.h:220
WarnAlways(bool setting=true)