[Cpp API Compatibility] Add some headeronly APIs#78662
[Cpp API Compatibility] Add some headeronly APIs#78662youge325 wants to merge 2 commits intoPaddlePaddle:developfrom
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
There was a problem hiding this comment.
Pull request overview
This PR refactors the C++ API compatibility layer by extracting several compat implementations into a new paddle/phi/api/include/compat/torch/headeronly directory and introducing a STD_TORCH_CHECK-style macro for header-only usage.
Changes:
- Added new header-only compat headers for Exception utilities, TensorAccessor, ScalarType, and DeviceType under
compat/torch/headeronly. - Refactored existing
compat/c10andcompat/ATenheaders to include and reuse the new header-only definitions. - Introduced
STD_TORCH_CHECK/STD_TORCH_CHECK_MSGmacro infrastructure for header-only checks.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| paddle/phi/api/include/compat/torch/headeronly/util/Exception.h | Adds header-only check helpers/macros (STD_TORCH_CHECK*) and related message building. |
| paddle/phi/api/include/compat/torch/headeronly/core/TensorAccessor.h | Adds header-only TensorAccessor / PackedTensorAccessor implementations adapted from upstream. |
| paddle/phi/api/include/compat/torch/headeronly/core/ScalarType.h | Adds header-only ScalarType foundational definitions and helpers. |
| paddle/phi/api/include/compat/torch/headeronly/core/DeviceType.h | Adds header-only DeviceType enum/constants and hashing support. |
| paddle/phi/api/include/compat/c10/util/Exception.h | Includes the new header-only exception utilities into the existing compat exception header. |
| paddle/phi/api/include/compat/c10/core/ScalarType.h | Switches ScalarType foundational definitions to come from the new header-only ScalarType header. |
| paddle/phi/api/include/compat/c10/core/DeviceType.h | Switches DeviceType foundational definitions to come from the new header-only DeviceType header. |
| paddle/phi/api/include/compat/ATen/core/TensorAccessor.h | Refactors ATen TensorAccessor types to alias the new header-only implementations. |
💡 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; |
There was a problem hiding this comment.
stdTorchCheckMsgImpl ignores the leading msg parameter in the variadic overload, so STD_TORCH_CHECK_MSG(...) drops the standard "Expected ..." prefix whenever any extra args are passed (and STD_TORCH_CHECK always passes __func__/__FILE__/__LINE__). This makes the thrown message miss the failed condition context. Consider streaming msg into the ostringstream before appending args..., or alternatively include the condition string (#cond) in the args that are concatenated.
| inline std::string stdTorchCheckMsgImpl(const char* /*msg*/, | |
| const Args&... args) { | |
| std::ostringstream oss; | |
| inline std::string stdTorchCheckMsgImpl(const char* msg, | |
| const Args&... args) { | |
| std::ostringstream oss; | |
| oss << msg; |
| #define STD_TORCH_CHECK(cond, ...) \ | ||
| 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 raw if (...) { ... } without a do { ... } while (0) wrapper. This macro can break if/else call sites due to dangling-else binding and is not statement-safe. Please wrap the macro body in a do { ... } while (0) (or equivalent) so it behaves as a single statement in all contexts.
| #define STD_TORCH_CHECK(cond, ...) \ | |
| if (C10_UNLIKELY_OR_CONST(!(cond))) { \ | |
| throw std::runtime_error(STD_TORCH_CHECK_MSG(cond, \ | |
| "", \ | |
| __func__, \ | |
| ", ", \ | |
| __FILE__, \ | |
| ":", \ | |
| __LINE__, \ | |
| ", ", \ | |
| ##__VA_ARGS__)); \ | |
| } | |
| #define STD_TORCH_CHECK(cond, ...) \ | |
| do { \ | |
| if (C10_UNLIKELY_OR_CONST(!(cond))) { \ | |
| throw std::runtime_error(STD_TORCH_CHECK_MSG(cond, \ | |
| "", \ | |
| __func__, \ | |
| ", ", \ | |
| __FILE__, \ | |
| ":", \ | |
| __LINE__, \ | |
| ", ", \ | |
| ##__VA_ARGS__)); \ | |
| } \ | |
| } while (0) |
| // If you modified DeviceType in this file, please also sync your changes into | ||
| // torch/headeronly/core/DeviceType.h. | ||
| #include <torch/headeronly/core/DeviceType.h> | ||
|
|
There was a problem hiding this comment.
c10/core/DeviceType.h previously re-exported c10::{DeviceType,kCUDA,kCPU,...} into namespace torch, and this PR removes those aliases. torch/all.h brings in c10/core/DeviceType.h via c10/core/Device.h, and code/tests (e.g. test/cpp/compat/ATen_basic_test.cc) use torch::kCUDA; this will stop compiling. Please restore the namespace torch { using c10::DeviceType; using c10::kCUDA; ... } re-exports (either here or in the included headeronly DeviceType header) to maintain the existing API surface.
|
/re-run all-failed |
PR Category
Execute Infrastructure
PR Types
New features
Description
将部分实现移至
paddle/phi/api/include/compat/torch/headeronly文件夹,并补充STD_TORCH_CHECK宏是否引起精度变化
否