[Cpp API Compatibility] add STD_CHECK macro for paddlecodec#78641
[Cpp API Compatibility] add STD_CHECK macro for paddlecodec#78641youge325 wants to merge 3 commits intoPaddlePaddle:developfrom
STD_CHECK macro for paddlecodec#78641Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
There was a problem hiding this comment.
Pull request overview
This PR updates the C10 compatibility exception utilities to avoid CHECK macro redefinition warnings by removing the unused CHECK macro and introducing a new std::exception-based check macro intended for downstream (paddlecodec) usage.
Changes:
- Removed the
CHECKmacro definition fromc10/util/Exception.h. - Added
STD_TORCH_CHECK/STD_TORCH_CHECK_MSGmacros and supporting helpers to throwstd::runtime_erroron check failure. - Added
<stdexcept>include forstd::runtime_error.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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; |
There was a problem hiding this comment.
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).
| 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(); |
|
|
||
| #ifdef STRIP_ERROR_MESSAGES | ||
| #define STD_TORCH_CHECK_MSG(cond, type, ...) \ | ||
| (#cond #type " CHECK FAILED at " C10_STRINGIZE(__FILE__)) |
There was a problem hiding this comment.
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.
| (#cond #type " CHECK FAILED at " C10_STRINGIZE(__FILE__)) | |
| (#cond type " CHECK FAILED at " __FILE__) |
| if (C10_UNLIKELY_OR_CONST(!(cond))) { \ | ||
| throw std::runtime_error(STD_TORCH_CHECK_MSG(cond, \ | ||
| "", \ | ||
| __func__, \ | ||
| ", ", \ | ||
| __FILE__, \ | ||
| ":", \ | ||
| __LINE__, \ | ||
| ", ", \ | ||
| ##__VA_ARGS__)); \ | ||
| } |
There was a problem hiding this comment.
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.
| 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) |
| #define STD_TORCH_CHECK(cond, ...) \ | ||
| if (C10_UNLIKELY_OR_CONST(!(cond))) { \ |
There was a problem hiding this comment.
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).
| // Check for a given boolean condition. | ||
| #ifndef CHECK | ||
| #define CHECK(condition) PD_CHECK(condition, "CHECK failed : ", #condition) | ||
| #endif |
There was a problem hiding this comment.
确实不能删,FlashMLA 编译报错了
|
/re-run all-failed |
PR Category
Execute Infrastructure
PR Types
New features
Description
新增
STD_CHECK宏,同时删除从未被使用的的CHECK宏,以此消除重定义的警告是否引起精度变化
否