已合并
Rewrite cuda to npu in backend string of init_process_group #39244
Rewrite cuda to npu in backend string of init_process_group #39244
已合并
yinglinwei创建于 6月25日
yinglinwei
6月25日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

https://gitcode.com/Ascend/pytorch/issues/2494

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

该修改将 backend 自动映射从仅支持通信后端替换(nccl→hccl),扩展为同时支持设备类型替换(cuda→npu)

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

不涉及

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

不涉及

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图
image.png
image.png

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 yinglinwei 的贡献)
Yyinglinwei
6月25日 创建了 pull request,commit 43422e4c
Yyinglinwei
6月25日 关联了issue:[Bug]: 自动迁移init_process_group传入backend为"cpu:gloo,cuda:nccl"不成功
ascend-robotascend-robot成员
6月25日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
6月25日 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here
You can get sig-info at here


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
test 王朝, 陈豪 (2/2) 王朝, 陈豪 (2/1)
torch_npu/contrib/transfer_to_npu.py 王朝, 陈豪 (2/2) 王朝, 陈豪 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

ylw1234, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robot
ascend-robot成员
6月25日 评论:

当前仓库存在以下 保护分支

Protected Branch Version Release
master
v2.7.1
v2.12.0
v2.11.0
v2.10.0
v2.9.0
v2.7.1-26.1.0
v2.9.0-26.1.0
v2.12.0-26.1.0
v2.10.0-26.1.0
v2.11.0-26.1.0
ci-test
sync-pr28113--to-v2.9.0

评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
ascend-robot
ascend-robot成员
6月25日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
6月25日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
6月25日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
6月25日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
6月25日 添加了label:docs-ci-pipeline-success
atomgit-bot
atomgit-bot
6月25日 评论:

变更摘要

此 PR 扩展了 transfer_to_npu 模块中 init_process_group 的 backend 自动映射能力:原先仅将通信后端 nccl 替换为 hccl,现在同时支持将设备类型 cuda 替换为 npu。这使得用户传入 backend='cuda:nccl' 时,框架能自动映射为 npu:hccl,保持与原生 PyTorch API 格式的兼容性。

主要改动

  • _wrapper_hccl 装饰器中新增 cudanpu 替换逻辑:在 torch_npu/contrib/transfer_to_npu.py 中,_wrapper_hccl 函数原先仅对位置参数和 backend 关键字参数中的 nccl 做替换;本次改动增加了对 cuda 子串的检测与替换(cudanpu),覆盖 argskwargs 两条路径,使完整格式 cuda:nccl 可一步映射为 npu:hccl

  • test_init_process_group 测试用例对齐新行为:在 test/contrib/test_transfer_to_npu.py 中,将测试用的 backend 从单一 'nccl' 改为 'cuda:nccl'device_idnpu:{RANK} 改为 cuda:{RANK},断言从 'hccl' 更新为 'npu:hccl',验证设备类型与通信后端的联合自动映射正确生效。

likedislike
atomgit-bot
atomgit-bot
6月25日 评论:

代码审查

审查总结

逐文件审查结论:

  • torch_npu/contrib/transfer_to_npu.py — 核心改动。_wrapper_hccl 新增了 'cuda' → 'npu' 的字符串替换逻辑,配合已有的 'nccl' → 'hccl' 替换,支持 'cuda:nccl' 格式的 device-aware backend 参数。逻辑正确:替换顺序(先 nccl 后 cuda)可正确处理 'cuda:nccl' → 'cuda:hccl' → 'npu:hccl'_patch_cuda_patch_profiler 中的 patchs → patches 为纯拼写修正。无功能缺陷。

  • test/contrib/test_transfer_to_npu.py — 集成测试 test_init_process_group 已更新为使用 backend='cuda:nccl' 并断言 get_backend() == 'npu:hccl',与核心改动一致。但 _wrapper_hccl 的单元测试 test_wrapper_hccl_args_and_kwargs 未同步更新以覆盖新增的 'cuda' → 'npu' 替换分支。

按优先级统计:

优先级 数量 说明
P0 0
P1 0
P2 0
P3 1 单元测试未覆盖新增替换分支(其余为信息性确认)

整体风险评估:低。 核心逻辑改动正确,替换顺序合理,集成测试已覆盖端到端行为。唯一可改进点是 wrapper 单元测试的覆盖率。建议合入。

类型 数量
🔴 阻塞 0
🟡 建议 5

💬 仅评论

likedislike
atomgit-bot
atomgit-bot6月25日进行代码检视2
torch_npu/contrib/transfer_to_npu.py
@@ -256,0 +248,4 @@
248+ arg = arg.replace('nccl', 'hccl')
249+ if 'cuda' in arg:
250+ arg = arg.replace('cuda', 'npu')
251+ args_new[idx] = arg
atomgit-bot
atomgit-bot6月25日评论:

🔵 Low Priority

_wrapper_hccl 的 args 处理路径中,对于所有字符串类型的参数,新代码无条件执行 args_new[idx] = arg(第251行),即使该值未发生任何替换。旧代码仅在包含 'nccl' 时才赋值(此时 arg 已通过初始 list(args) 拷贝了原值)。虽然字符串不可变且最终值一致(原地重新赋值相同对象),但这与 kwargs 路径一样引入了无实际效果的操作。建议仅在发生替换时才写入,保持代码意图清晰。

建议:可选优化:仅在 arg 实际发生替换(即 'nccl' in arg'cuda' in arg 为 True)时才执行 args_new[idx] = arg。可以引入一个局部变量标记是否发生替换,或在条件内直接赋值。

likedislike
yinglinwei
6月25日 评论:

此处无需修改。args_new 已经是 args 的浅拷贝,对字符串参数执行 args_new[idx] = arg 即使未发生替换,也只是一次无副作用的引用赋值,性能开销可忽略。当前统一处理并统一回写的写法可减少额外条件分支,使逻辑更直观、易维护。

atomgit-bot
atomgit-bot6月25日进行代码检视2
torch_npu/contrib/transfer_to_npu.py
@@ -361,3 +369,3 @@
361369 
362370 from torch_npu._init.patches.monkey_patches import _apply_patches
363- _apply_patches(patchs)
371+ _apply_patches(patches)
atomgit-bot
atomgit-bot6月25日评论:

🔵 Low Priority

_patch_cuda 函数(第348行)和 _patch_profiler 函数(第361行)中,变量名从 patchs 修正为 patches。这是一个纯拼写修正,行为无变化。

建议:无需修改,该修正正确且安全。

likedislike
yinglinwei
6月25日 评论:

无需修改

atomgit-bot
atomgit-bot6月25日进行代码检视2
torch_npu/contrib/transfer_to_npu.py
@@ -256,0 +257,4 @@
257+ backend = backend.replace('nccl', 'hccl')
258+ if 'cuda' in backend:
259+ backend = backend.replace('cuda', 'npu')
260+ kwargs['backend'] = backend
atomgit-bot
atomgit-bot6月25日评论:

🔵 Low Priority

_wrapper_hccl 的 args 处理循环中(第246-251行),对每个字符串类型参数依次检查 'nccl' in arg'cuda' in arg。当参数同时包含两个子串时(如 'cuda:nccl'),两次替换均会被执行,结果正确。但当参数为 'nccl' 时,先 replace('nccl', 'hccl') 得到 'hccl',再检查 'cuda' in 'hccl' 为 False;当参数为 'cuda:gloo'(无 nccl)时,'nccl' 检查为 False 跳过,'cuda' 检查为 True 执行替换得到 'npu:gloo'。两个 if 语句是顺序独立执行的(非 elif),逻辑正确且符合预期。kwargs 路径(第255-260行)同理。无缺陷。

建议:逻辑正确,无需修改。两个独立的 if(非 elif)是正确的设计,因为部分 backend 字符串可能同时包含 'nccl' 和 'cuda'。

likedislike
yinglinwei
6月25日 评论:

无需修改

atomgit-bot
atomgit-bot6月25日进行代码检视2
test/contrib/test_transfer_to_npu.py
@@ -263,7 +263,7 @@
263263 args_output, kwargs_output = mock_function(*args_input, **kwargs_input)
264264 
265265 self.assertEqual(args_output, expected_args_output)
266266 self.assertEqual(kwargs_output, expected_kwargs_output)
atomgit-bot
atomgit-bot6月25日评论:

🔵 Low Priority

_wrapper_hccl 新增了 'cuda' → 'npu' 的字符串替换逻辑(第249-250行,第258-259行),但对应的单元测试 test_wrapper_hccl_args_and_kwargs(第253-266行)未更新以覆盖该新分支。当前测试仅验证 'nccl' → 'hccl' 替换,未验证包含 'cuda' 的输入(如 'cuda:nccl''npu:hccl')。虽然集成测试 test_init_process_group 端到端覆盖了该路径,但 wrapper 自身的单元测试缺少直接覆盖,可能在后续重构时遗漏回归检测。

建议:在 test_wrapper_hccl_args_and_kwargs 中新增一个测试用例或用参数化扩展,覆盖 'cuda:nccl''npu:hccl' 的替换路径。例如:增加 args_input = ('cuda:nccl',),预期 expected_args_output = ('npu:hccl',);同样在 kwargs 中测试 backend='cuda:nccl'

likedislike
yinglinwei
6月25日 评论:

test_init_process_group用例已经覆盖

atomgit-bot
atomgit-bot6月25日进行代码检视2
torch_npu/contrib/transfer_to_npu.py
@@ -256,0 +257,4 @@
257+ backend = backend.replace('nccl', 'hccl')
258+ if 'cuda' in backend:
259+ backend = backend.replace('cuda', 'npu')
260+ kwargs['backend'] = backend
atomgit-bot
atomgit-bot6月25日评论:

🔵 Low Priority

_wrapper_hccl 的 kwargs 处理路径中,对于任意字符串类型的 backend 值,新代码始终执行 kwargs['backend'] = backend(第260行),即使该值未发生任何替换(如 backend='gloo')。旧代码仅在 backend 包含 'nccl' 时才写入 kwargs['backend']。虽然结果值相同(只是原地重新赋值),但语义上有不必要的字典写入操作。这不是缺陷,但表明代码结构引入了无实际效果的赋值。

建议:可选优化:将 kwargs['backend'] = backend 的赋值移到 if 块内部,仅在实际发生替换时才写入。例如使用一个局部标志位追踪是否发生了替换,或保持旧代码风格:仅在满足替换条件时更新字典。

likedislike
yinglinwei
6月25日 评论:

此处无需修改。kwargs['backend'] = backend 即使在未发生替换时执行,也仅是一次无副作用的 O(1) 字典赋值,不影响功能且性能开销可忽略。当前实现采用“统一处理后统一回写”的方式,减少了额外状态判断和分支复杂度,可提升代码可读性及后续扩展性

yinglinwei
6月25日 评论:

compile

likedislike
ascend-robotascend-robot成员
6月25日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
6月25日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
6月25日 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
6月25日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
6月25日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
6月25日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
6月25日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
6月25日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
6月25日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
6月25日 评论:
流水线 PR-pipeline_pytorch#39079 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
likedislike
wangchao285成员
6月26日 评论:

/approve

likedislike
ascend-robotascend-robot成员
6月26日 添加了label:approved
chenhao_1209成员
6月26日 评论:

/approve

likedislike
ascend-robotascend-robot成员
6月26日 添加了label:lgtm
ascend-robotascend-robot成员
6月26日 解决了最后一个问题
ascend-robotascend-robot成员
6月26日 关闭了关联的issue
ascend-robotascend-robot成员
6月26日 合入了pull request
ascend-robot
ascend-robot成员
6月26日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6月26日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6月26日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6月26日 评论:
流水线 pytorch_gitcode_PR_multiVersion#11408 [ commitID:94c061f8 ] 已完成
likedislike