Skip to content

Enhance Device Compatibility#78066

Open
Le-soleile wants to merge 10 commits intoPaddlePaddle:developfrom
Le-soleile:Device
Open

Enhance Device Compatibility#78066
Le-soleile wants to merge 10 commits intoPaddlePaddle:developfrom
Le-soleile:Device

Conversation

@Le-soleile
Copy link
Contributor

@Le-soleile Le-soleile commented Feb 27, 2026

PR Category

Execute Infrastructure

PR Types

Bug fixes

Description

补充的测试#38对比输出:

image

Paddle 兼容层实现的 c10::Device 与 PyTorch 原生在以下边界情况存在行为差异:

  1. CPU 设备 type 枚举值:Paddle 返回 1,PyTorch 返回 0
  2. CPU 默认索引
    • Paddle:index()=0has_index()=true
    • PyTorch:index()=-1has_index()=false
  3. 字符串输出:Paddle 输出 cpu:0,PyTorch 输出 cpu

修改后测试结果变为match

image

修改说明

把 Paddle 的 Device 兼容层行为对齐到 Torch:以前在某些场景下(例如是从底层 place 转成 Device 时),CPU 默认会被当成“有 index=0”,导致输出和 Torch 不一致;现在改成了“只有显式写了 index 才算有 index”,否则统一按 -1 处理,这样 index()/has_index()/str() 的结果就一致了。同时在兼容层里明确区分 CPU/CUDA/GPU,并通过统一的转换函数来互转,避免同一语义在多处重复判断。

是否引起精度变化

@paddle-bot
Copy link

paddle-bot bot commented Feb 27, 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 Feb 27, 2026
@Le-soleile Le-soleile changed the title Enhance compatibility Enhance Device Compatibility Feb 27, 2026
@codecov-commenter
Copy link

codecov-commenter commented Feb 28, 2026

Codecov Report

❌ Patch coverage is 90.24390% with 4 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@647ccf4). Learn more about missing BASE report.

Files with missing lines Patch % Lines
paddle/phi/api/include/compat/c10/core/Device.cpp 77.77% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #78066   +/-   ##
==========================================
  Coverage           ?   90.24%           
==========================================
  Files              ?        3           
  Lines              ?       41           
  Branches           ?        0           
==========================================
  Hits               ?       37           
  Misses             ?        4           
  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.

types = {{
{"cpu", DeviceType::CPU},
{"cuda", DeviceType::CUDA},
{"gpu", DeviceType::GPU},
Copy link
Contributor

Choose a reason for hiding this comment

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

cuda和gpu的区别是什么,这里增加一个gpu的理由是什么

Copy link
Contributor Author

@Le-soleile Le-soleile Mar 2, 2026

Choose a reason for hiding this comment

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

没什么区别,就是这样写也不会报错了: torch::set_device("gpu:0");
是不是有点多余🥲,我看了下torch的代码现在应该只会写cuda了,可能不会有这种情况

Copy link
Contributor Author

Choose a reason for hiding this comment

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

对Torch的兼容应该不需要"gpu",我删掉了

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