Skip to content

[Cpp API Compatibility] Add some headeronly APIs#78662

Open
youge325 wants to merge 2 commits intoPaddlePaddle:developfrom
youge325:cHeader
Open

[Cpp API Compatibility] Add some headeronly APIs#78662
youge325 wants to merge 2 commits intoPaddlePaddle:developfrom
youge325:cHeader

Conversation

@youge325
Copy link
Copy Markdown
Contributor

PR Category

Execute Infrastructure

PR Types

New features

Description

将部分实现移至 paddle/phi/api/include/compat/torch/headeronly 文件夹,并补充 STD_TORCH_CHECK

是否引起精度变化

Copilot AI review requested due to automatic review settings April 13, 2026 09:51
@paddle-bot
Copy link
Copy Markdown

paddle-bot bot commented Apr 13, 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 13, 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 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/c10 and compat/ATen headers to include and reuse the new header-only definitions.
  • Introduced STD_TORCH_CHECK / STD_TORCH_CHECK_MSG macro 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.

Comment on lines +50 to +52
inline std::string stdTorchCheckMsgImpl(const char* /*msg*/,
const Args&... args) {
std::ostringstream oss;
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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;

Copilot uses AI. Check for mistakes.
Comment on lines +74 to +85
#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__)); \
}
Copy link

Copilot AI Apr 13, 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 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.

Suggested change
#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)

Copilot uses AI. Check for mistakes.
Comment on lines +17 to +20
// If you modified DeviceType in this file, please also sync your changes into
// torch/headeronly/core/DeviceType.h.
#include <torch/headeronly/core/DeviceType.h>

Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
@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.

2 participants