Skip to content

[Cpp API Compatibility] Align device related APIs#78551

Merged
SigureMo merged 2 commits intoPaddlePaddle:developfrom
youge325:align-device-api
Apr 2, 2026
Merged

[Cpp API Compatibility] Align device related APIs#78551
SigureMo merged 2 commits intoPaddlePaddle:developfrom
youge325:align-device-api

Conversation

@youge325
Copy link
Copy Markdown
Contributor

@youge325 youge325 commented Apr 1, 2026

PR Category

Execute Infrastructure

PR Types

Improvements

Description

拆分自 #78484

Align Device 相关接口,补齐 c10::Devicec10::DeviceType 的 PyTorch C++ API 兼容层。

变更详情

1. c10/core/DeviceType.h 扩展

  • 新增 DeviceType::PrivateUse1 / kPrivateUse1 别名
  • 新增 std::hash<c10::DeviceType> 特化

2. c10/core/Device.h 接口补齐

新增以下方法:

// 比较运算符
bool operator!=(const Device& other) const noexcept;

// 修改设备索引
void set_index(DeviceIndex index);

// 设备谓词
bool is_privateuseone() const noexcept;
bool is_xpu() const noexcept;
bool is_ipu() const noexcept;
bool is_mps() const noexcept;

// 能力查询
bool supports_as_strided() const noexcept;

// std::hash 支持
struct hash<Device>;

3. c10/core/Device.cpp 字符串解析对齐

  • 实现与 PyTorch 一致的严格字符串解析规则
  • 支持 privateuseone 设备类型
  • 正确处理非法格式如 cuda:-1cuda:01cuda:1:2

Windows 兼容性处理
为规避 Windows 头文件中的 ERROR 宏污染,内部状态枚举使用 kStart/kIndexStart/kIndexRest/kError 命名(而非 PyTorch 上游的 START/INDEX_START/INDEX_REST/ERROR),但对外行为完全一致。

4. c10/util/Exception.h TORCH_CHECK_OP 对齐

对齐 TORCH_CHECK_EQ/NE/LT/LE/GT/GE 等宏的报错前缀格式,使其与 PyTorch 的异常消息格式一致。

相关文档

是否引起精度变化

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

paddle-bot bot commented Apr 1, 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 1, 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 continues the C++ API compatibility work by aligning the compat c10::DeviceType / c10::Device surface area and parsing behavior with upstream expectations (notably around PrivateUse1 / privateuseone), and adds tests to lock in the parity.

Changes:

  • Add DeviceType::PrivateUse1 aliasing CUSTOM, plus kPrivateUse1 and hashing support for DeviceType.
  • Expand c10::Device interface (parity helpers like is_privateuseone(), set_index(), supports_as_strided()), add hashing for Device, and rework device-string parsing (including privateuseone).
  • Adjust TORCH_CHECK_OP error text and extend C++ compat tests to cover the new device behaviors (including unordered_map hashing).

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/cpp/compat/c10_Device_test.cc Adds parity tests for new Device/DeviceType behaviors and hashing usage.
paddle/phi/api/include/compat/c10/util/Exception.h Updates TORCH_CHECK_OP error message formatting.
paddle/phi/api/include/compat/c10/core/DeviceType.h Introduces PrivateUse1 / kPrivateUse1 and std::hash<DeviceType>.
paddle/phi/api/include/compat/c10/core/Device.h Adds device parity methods, validation calls, and std::hash<Device>.
paddle/phi/api/include/compat/c10/core/Device.cpp Extends parsing to privateuseone and reimplements device-string parsing logic.
.gitignore Ignores .codex.

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

TORCH_CHECK(false,
"Expected one of cpu, cuda, ipu, xpu, privateuseone device type "
"at start of device string: ",
device_string);
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

parse_type() can reach the end of a non-void function without returning a DeviceType (after the TORCH_CHECK(false, ...)). Even if TORCH_CHECK throws at runtime, this is still UB in the language rules and may trigger -Wreturn-type/-Werror build failures. Add an explicit return (or an unreachable annotation) after the check to satisfy the compiler.

Suggested change
device_string);
device_string);
return DeviceType::CPU; // Unreachable, added to satisfy compiler.

Copilot uses AI. Check for mistakes.
Comment on lines +168 to +178
namespace std {
template <>
struct hash<c10::Device> {
size_t operator()(c10::Device d) const noexcept {
static_assert(sizeof(c10::DeviceType) == 1, "DeviceType is not 8-bit");
static_assert(sizeof(c10::DeviceIndex) == 1, "DeviceIndex is not 8-bit");
uint32_t bits = static_cast<uint32_t>(static_cast<uint8_t>(d.type()))
<< 16 |
static_cast<uint32_t>(static_cast<uint8_t>(d.index()));
return std::hash<uint32_t>{}(bits);
}
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

std::hash<c10::Device>::operator() takes the key by value, which will copy c10::Device (including its std::string custom_device_type_) every time the hash is computed. This is avoidable overhead for unordered_map/unordered_set usage. Take the parameter as const c10::Device& instead, and consider incorporating custom_device_type_ into the hash to reduce collisions since operator== includes it.

Copilot uses AI. Check for mistakes.
@youge325
Copy link
Copy Markdown
Contributor Author

youge325 commented Apr 1, 2026

/re-run all-failed

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.91525% with 3 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@638c7c3). Learn more about missing BASE report.

Files with missing lines Patch % Lines
paddle/phi/api/include/compat/c10/core/Device.cpp 91.89% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #78551   +/-   ##
==========================================
  Coverage           ?   94.91%           
==========================================
  Files              ?        2           
  Lines              ?       59           
  Branches           ?        0           
==========================================
  Hits               ?       56           
  Misses             ?        3           
  Partials           ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@ShigureNyako ShigureNyako left a comment

Choose a reason for hiding this comment

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

整体看下来我这边倾向于 approve。

我重点核对了:

  • c10/core/Device.cpp 的字符串解析状态机和 privateuseone 解析,行为与 upstream PyTorch 当前实现保持一致;
  • c10/core/Device.h 新增的 operator!= / set_index / 设备谓词 / supports_as_strided() / hash<Device> 等接口,覆盖了这次 PR 声称补齐的 Device 主路径;
  • c10/util/Exception.hTORCH_CHECK_OP 的报错前缀,已对齐为 PyTorch 的 Check failed: ... (lhs vs. rhs). 形式;
  • test/cpp/compat/c10_Device_test.cc 新增用例已经覆盖 strict parsing、PrivateUse1、谓词和 unordered_map 主路径。

关于 BREAKING CHANGE:这里最明显的行为变化是设备字符串解析变严格了,像 cuda:-1 / cuda:01 / cuda:1:2 现在会抛异常;但这正是向 upstream PyTorch 语义收敛,我认为属于预期的兼容性修正,不是额外引入的 breaking risk。

小建议:.gitignore 里的 .codex 变更和本 PR 主题关系不大,最好后续单独拆出去,保持拆分 PR 更聚焦。

Copy link
Copy Markdown
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTMeow 🐾

@SigureMo SigureMo merged commit 1a009f4 into PaddlePaddle:develop Apr 2, 2026
118 of 122 checks passed
@SigureMo SigureMo changed the title [Cpp API Compatibility] Align device api [Cpp API Compatibility] Align device related APIs Apr 2, 2026
@youge325 youge325 deleted the align-device-api branch April 2, 2026 05:41
liuhao2638 pushed a commit to liuhao2638/Paddle that referenced this pull request Apr 7, 2026
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.

5 participants