Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions paddle/phi/api/include/compat/c10/util/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <iostream>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <variant>
Expand Down Expand Up @@ -62,8 +63,62 @@ namespace c10 {
#define TORCH_CHECK_LT(val1, val2) TORCH_CHECK_OP(val1, val2, <)
#define TORCH_CHECK_GE(val1, val2) TORCH_CHECK_OP(val1, val2, >=)
#define TORCH_CHECK_GT(val1, val2) TORCH_CHECK_OP(val1, val2, >)

#ifndef C10_UNLIKELY_OR_CONST
#if defined(__CUDACC__)
#define C10_UNLIKELY_OR_CONST(e) e
#elif defined(__GNUC__) || defined(__clang__)
#define C10_UNLIKELY_OR_CONST(e) (__builtin_expect(static_cast<bool>(e), 0))
#else
#define C10_UNLIKELY_OR_CONST(e) e
#endif
#endif

namespace detail {

template <typename... Args>
inline std::string stdTorchCheckMsgImpl(const char* /*msg*/,
const Args&... args) {
std::ostringstream oss;
((oss << args), ...);
return oss.str();
}

inline const char* stdTorchCheckMsgImpl(const char* msg) { return msg; }

inline const char* stdTorchCheckMsgImpl(const char* /*msg*/, const char* args) {
return args;
Comment on lines +80 to +90
Copy link

Copilot AI Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdTorchCheckMsgImpl ignores the msg parameter and only streams args..., so the base text passed from STD_TORCH_CHECK_MSG (e.g. "Expected ...") is never included. This makes STD_TORCH_CHECK exceptions lose the condition context; include msg in the assembled output (and consider removing the (msg, const char*) -> args overload which also drops the prefix).

Suggested change
inline std::string stdTorchCheckMsgImpl(const char* /*msg*/,
const Args&... args) {
std::ostringstream oss;
((oss << args), ...);
return oss.str();
}
inline const char* stdTorchCheckMsgImpl(const char* msg) { return msg; }
inline const char* stdTorchCheckMsgImpl(const char* /*msg*/, const char* args) {
return args;
inline std::string stdTorchCheckMsgImpl(const char* msg,
const Args&... args) {
std::ostringstream oss;
oss << msg;
((oss << args), ...);
return oss.str();
}
inline const char* stdTorchCheckMsgImpl(const char* msg) { return msg; }
inline std::string stdTorchCheckMsgImpl(const char* msg, const char* args) {
std::ostringstream oss;
oss << msg << args;
return oss.str();

Copilot uses AI. Check for mistakes.
}

} // namespace detail
} // namespace c10

#ifdef STRIP_ERROR_MESSAGES
#define STD_TORCH_CHECK_MSG(cond, type, ...) \
(#cond #type " CHECK FAILED at " C10_STRINGIZE(__FILE__))
Copy link

Copilot AI Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the STRIP_ERROR_MESSAGES branch, C10_STRINGIZE(__FILE__) will stringify the already-string-literal __FILE__ token and produce extra quotes/escapes in the message. Also #type will stringify the "" token when type is passed as "", resulting in a literal "\"\"" fragment; consider using __FILE__ directly and avoid stringizing a string-literal type argument.

Suggested change
(#cond #type " CHECK FAILED at " C10_STRINGIZE(__FILE__))
(#cond type " CHECK FAILED at " __FILE__)

Copilot uses AI. Check for mistakes.
#else
#define STD_TORCH_CHECK_MSG(cond, type, ...) \
(::c10::detail::stdTorchCheckMsgImpl( \
"Expected " #cond \
" to be true, but got false. " \
"(Could this error message be improved? If so, " \
"please report an enhancement request to PyTorch.)", \
##__VA_ARGS__))
#endif

#define STD_TORCH_CHECK(cond, ...) \
if (C10_UNLIKELY_OR_CONST(!(cond))) { \
Comment on lines +109 to +110
Copy link

Copilot AI Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description/title mention adding a STD_CHECK macro, but this header introduces STD_TORCH_CHECK / STD_TORCH_CHECK_MSG and there is no STD_CHECK symbol in the repo. Either rename the macro to match the intended API (STD_CHECK) or update the PR description (and any downstream expectations in paddlecodec).

Copilot uses AI. Check for mistakes.
throw std::runtime_error(STD_TORCH_CHECK_MSG(cond, \
"", \
__func__, \
", ", \
__FILE__, \
":", \
__LINE__, \
", ", \
##__VA_ARGS__)); \
}
Comment on lines +110 to +120
Copy link

Copilot AI Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STD_TORCH_CHECK expands to a naked if (...) { ... } without a do { ... } while (0) wrapper. This is not statement-safe and can break if/else chains at call sites; wrap the macro body to behave like a single statement.

Suggested change
if (C10_UNLIKELY_OR_CONST(!(cond))) { \
throw std::runtime_error(STD_TORCH_CHECK_MSG(cond, \
"", \
__func__, \
", ", \
__FILE__, \
":", \
__LINE__, \
", ", \
##__VA_ARGS__)); \
}
do { \
if (C10_UNLIKELY_OR_CONST(!(cond))) { \
throw std::runtime_error(STD_TORCH_CHECK_MSG(cond, \
"", \
__func__, \
", ", \
__FILE__, \
":", \
__LINE__, \
", ", \
##__VA_ARGS__)); \
} \
} while (false)

Copilot uses AI. Check for mistakes.

enum class C10ErrorType {
NotImplementedError,
Error,
Expand Down
Loading