Skip to content

[Cpp API Compatibility] add STD_CHECK macro for paddlecodec#78641

Open
youge325 wants to merge 3 commits intoPaddlePaddle:developfrom
youge325:cSTD_CHECK
Open

[Cpp API Compatibility] add STD_CHECK macro for paddlecodec#78641
youge325 wants to merge 3 commits intoPaddlePaddle:developfrom
youge325:cSTD_CHECK

Conversation

@youge325
Copy link
Copy Markdown
Contributor

PR Category

Execute Infrastructure

PR Types

New features

Description

新增 STD_CHECK 宏,同时删除从未被使用的的 CHECK 宏,以此消除重定义的警告

是否引起精度变化

Copilot AI review requested due to automatic review settings April 11, 2026 03:57
@paddle-bot
Copy link
Copy Markdown

paddle-bot bot commented Apr 11, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Apr 11, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 CHECK macro definition from c10/util/Exception.h.
  • Added STD_TORCH_CHECK / STD_TORCH_CHECK_MSG macros and supporting helpers to throw std::runtime_error on check failure.
  • Added <stdexcept> include for std::runtime_error.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +75 to +85
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;
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.

#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.
Comment on lines +105 to +115
if (C10_UNLIKELY_OR_CONST(!(cond))) { \
throw std::runtime_error(STD_TORCH_CHECK_MSG(cond, \
"", \
__func__, \
", ", \
__FILE__, \
":", \
__LINE__, \
", ", \
##__VA_ARGS__)); \
}
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.
Comment on lines +104 to +105
#define STD_TORCH_CHECK(cond, ...) \
if (C10_UNLIKELY_OR_CONST(!(cond))) { \
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.
Comment on lines -53 to -56
// Check for a given boolean condition.
#ifndef CHECK
#define CHECK(condition) PD_CHECK(condition, "CHECK failed : ", #condition)
#endif
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这个是 #75874 引入的,FlashMLA 最新版不需要这个么

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

我编译试试,如果确实需要就先不删了

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

确实不能删,FlashMLA 编译报错了

@youge325
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants