-
Notifications
You must be signed in to change notification settings - Fork 6k
[Cpp API Compatibility] add STD_CHECK macro for paddlecodec
#78641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
| #include <iostream> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <memory> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <sstream> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <stdexcept> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <string> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <tuple> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <variant> | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| } // namespace detail | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } // namespace c10 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef STRIP_ERROR_MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| #define STD_TORCH_CHECK_MSG(cond, type, ...) \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| (#cond #type " CHECK FAILED at " C10_STRINGIZE(__FILE__)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| (#cond #type " CHECK FAILED at " C10_STRINGIZE(__FILE__)) | |
| (#cond type " CHECK FAILED at " __FILE__) |
Copilot
AI
Apr 11, 2026
There was a problem hiding this comment.
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
AI
Apr 11, 2026
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stdTorchCheckMsgImplignores themsgparameter and only streamsargs..., so the base text passed fromSTD_TORCH_CHECK_MSG(e.g. "Expected ...") is never included. This makesSTD_TORCH_CHECKexceptions lose the condition context; includemsgin the assembled output (and consider removing the(msg, const char*) -> argsoverload which also drops the prefix).