已合并
[sync] PR-38965: 在A5机器调用div_时加参数rounding_mode=trunc #39163
[sync] PR-38965: 在A5机器调用div_时加参数rounding_mode=trunc #39163
已合并
ascend-robot创建于 6月24日
ascend-robot
ascend-robot成员
6月24日

1. Origin pull request:

https://gitcode.com/Ascend/pytorch/merge_requests/38965

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

Sha Datetime Message
669448cb 2026-06-23 15:34:11 +0800 CST 在A5机器调用div_时加参数rouding_mode=trunc
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 ascend-robot 的贡献)
ascend-robotascend-robot成员
6月24日 创建了 pull request,commit 7153cf80
ascend-robotascend-robot成员
6月24日 关联了issue:[Bug]: test_reduce_scatter_tensor.py中test_reduce_scatter_tensor_avg用例在A5上面跑不通
ascend-robotascend-robot成员
6月24日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
6月24日 评论:

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 renyujin, wjlflyer (2/2) wjlflyer (1/1)
torch_npu/csrc/distributed wjlflyer, renyujin (2/2) wjlflyer (1/1)

💡 Tip:

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

CLA Signature Pass

ascend-ds-bot, thanks for your pull request. All authors of the commits have signed the CLA. 👍

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

Ascend docs pipeline is running...

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

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

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

compile

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

Ascend docs pipeline is running...

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

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

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

变更摘要

此 PR 同步自上游 PR-38965,主要针对 A5(Ascend950)机器上 _reduce_scatter_base 集合通信操作中 div_ 的整数除法取整行为进行修正。在 A5 平台上,当 ReduceOp 为 AVG 时,tensor.div_(getSize()) 的默认取整行为与非 A5 平台不一致,因此该变更新增芯片版本判断,在 A5 机器上显式传入 rounding_mode="trunc"(向零取整),以对齐预期计算结果。

主要改动

  • ProcessGroupHCCL::_reduce_scatter_base 添加 A5 平台的 div_ 取整模式: 在 ProcessGroupHCCL.cpp_reduce_scatter_base 函数后处理回调中,通过 c10_npu::GetSocVersion() >= c10_npu::SocVersion::Ascend950 判断是否为 A5 机器;若是,则对 outputs 张量执行 tensor.div_(getSize(), "trunc") 以向零取整,否则保持原 tensor.div_(getSize()) 不变。
  • 测试文件微调: test/distributed/test_all_to_all_single.py(+1/-1)和 test/distributed/test_reduce_scatter_tensor.py(+11/-7)做了小幅调整,以适应 A5 平台上 _reduce_scatter_base 的取整行为变化。
likedislike
atomgit-bot
atomgit-bot
6月24日 评论:

代码审查

审查总结

本次 diff 涉及 3 个文件的变更,审查结果如下:

各文件审查情况

文件 审查结论
test/distributed/test_all_to_all_single.py ✅ 无问题(typo 修复:dosen'tdoesn't,与 C++ 端一致)
test/distributed/test_reduce_scatter_tensor.py ✅ 无问题(import 清理 + 测试设备范围调整 + 代码格式化)
torch_npu/csrc/distributed/ProcessGroupHCCL.cpp ⚠️ 发现 2 个问题(见下)

发现问题统计

  • P1:1 个 — div_("trunc") 在 float 类型 tensor 上会导致运行时崩溃
  • P2:1 个 — A5 的 div_ rounding_mode 适配不完整,同文件中 ~10 处相同模式未同步修复
  • P3:1 个 — test 文件改动确认性说明(非缺陷)

整体风险评估

中等风险。typo 修复和测试调整无问题,但 C++ 核心逻辑变更存在两个隐患:

  1. P1 问题最严重:在 A5 机器上对 float 类型 tensor 执行 reduce_scatter_tensor + AVG 会直接崩溃。虽然实际触发概率取决于用户场景(是否在 A5 上对 float 数据使用 AVG),但代码路径明确可达,建议在合入前修复。

  2. P2 问题:同一文件中 all_reducereducereduce_scatter_tensor_coalesced 等函数中还有 10+ 处完全相同的 tensor.div_(getSize()) 模式未做 A5 适配,若 A5 整数除法行为确实变化,这些路径在 A5 上会产生静默的数值错误。建议与原始 issue (#2441) 确认修复范围。

类型 数量
🔴 阻塞 1
🟡 建议 2

⛔ 需要修改

likedislike
atomgit-bot
atomgit-bot6月24日进行代码检视1
torch_npu/csrc/distributed/ProcessGroupHCCL.cpp
@@ -6115,1 +6117,4 @@
6117+ } else {
6118+ tensor.div_(getSize());
6119+ }
61156120 }
atomgit-bot
atomgit-bot6月24日评论:

🟡 Medium Priority

此 diff 仅在 _reduce_scatter_base 函数(第 6113-6120 行)中添加了 A5 的 div_("trunc") 处理。但在同一文件的其他函数中,存在 10 处以上 相同的 tensor.div_(getSize()) 调用(均在 ReduceOp::AVG 后处理中),未做 A5 适配:

  • 第 4737 行:all_reduce
  • 第 4997 行:all_reduce(另一重载)中
  • 第 5075 行:reduce
  • 第 5155 行:reduce(另一重载)中
  • 第 5293 行:_reduce_scatter_base_uneven_inner
  • 第 5903 行:reduce_scatter_tensor coalesced 路径中
  • 第 6010 行:reduce_scatter_tensor coalesced 路径(延续)中
  • 第 6183 行:reduce_scatter_tensor_coalesced

如果 A5 机器的整数除法默认行为发生了变化(需要显式 "trunc" 才与非 A5 机器保持一致),那么上述所有位置在 A5 上的整数 AVG 结果将与预期不符,导致数据正确性问题。

证据链

  • _reduce_scatter_base 一处收到了 A5 适配

建议:需要确认原始 issue(#2441)的范围:是所有 collective 操作都需要此修复,还是仅 _reduce_scatter_base 受影响。如果所有路径都需要,应在其他 10+ 处 div_(getSize()) 调用中也加入相同的 A5 判断和 dtype 检查。

likedislike
atomgit-bot
atomgit-bot6月24日进行代码检视1
test/distributed/test_reduce_scatter_tensor.py
@@ -12,3 +9,4 @@
9+from torch_npu.testing.common_utils import create_common_tensor, SupportedDevices
1210from torch_npu.testing.common_distributed import skipIfUnsupportMultiNPU
1311 
1412from test_reduce_scatter import HcclReduceScatterTestBase
atomgit-bot
atomgit-bot6月24日评论:

🔵 Low Priority

test_reduce_scatter_tensor.py 中删除了 import osimport torch.multiprocessing as mp。经检查,该文件的代码中确实没有直接使用 osmp(多进程功能继承自 HcclReduceScatterTestBase),删除是安全的。

test_all_to_all_single.py 中 typo 修复 "dosen't" → "doesn't" 与 C++ 代码中的同款修复一致,正确。

test_reduce_scatter_tensor.py 新增 @SupportedDevices(['Ascend910A', 'Ascend910B', 'Ascend910_93']) 装饰器到 test_reduce_scatter_tensortest_reduce_scatter_tensor_uneven。这些测试使用 SUM op(非 AVG),排除 A5 设备不会影响新增的 div_("trunc") 代码路径的测试覆盖(AVG 测试 test_reduce_scatter_tensor_avgtest_reduce_scatter_tensor_uneven_avg 未加限制,仍可在 A5 上运行)。

这些均属正常的清理和测试策略调整,无问题。

建议:无需修改,此为正常的 import 清理和测试设备范围调整。

likedislike
atomgit-bot
atomgit-bot6月24日进行代码检视1
torch_npu/csrc/distributed/ProcessGroupHCCL.cpp
@@ -6118,0 +6116,4 @@
6116+ tensor.div_(getSize(), "trunc");
6117+ } else {
6118+ tensor.div_(getSize());
6119+ }
atomgit-bot
atomgit-bot6月24日评论:

🟠 High Priority

_reduce_scatter_base 函数中(第 6113-6119 行),A5 机器上对 outputs 中的所有 tensor 无条件调用 tensor.div_(getSize(), "trunc")。PyTorch 的 div_ 仅对整数类型支持 rounding_mode 参数;对 float 类型 tensor 传入 rounding_mode 会抛出 RuntimeError("rounding_mode argument is only supported for integer inputs")。

由于 _reduce_scatter_base 可以处理 float 类型的 tensor(例如用户对 float 数据使用 ReduceOp::AVG),当 output tensor 为 float 类型时,在 A5 机器上会直接崩溃。

触发条件:在 Ascend950(A5)机器上,对 float 类型 tensor 执行 reduce_scatter_tensor 且 op 为 AVG。

证据链

  • getHcclReduceOp(第 138-143 行)对 AVG 无条件返回 HCCL_REDUCE_SUM,不限制输入类型
  • PyTorch 标准实现中 div_rounding_mode 仅支持整数类型

建议:在调用 div_ 前检查 tensor 的 dtype:仅对整数类型(isIntegralType)使用 "trunc" rounding_mode,对 float 类型保持无 rounding_mode 的普通除法。修改为:

改动建议
6119
+ for (auto& tensor : outputs) {
6120
+ if (is_atlas_a5 && at::isIntegralType(tensor.scalar_type(), /*includeBool=*/true)) {
6121
+ tensor.div_(getSize(), "trunc");
6122
+ } else {
6123
+ tensor.div_(getSize());
6119
- }
6124
+ }
6125
+ }
应用建议
likedislike
ascend-robotascend-robot成员
6月24日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
6月24日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
6月24日 评论:
流水线 PR-pipeline_pytorch#38809 已完成
阶段 任务名 状态 详情
编译构建 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
Wwjlflyer成员
6月24日 解决了最后一个问题
wjlflyer成员
6月24日 评论:

/lgtm
/approve

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

/lgtm

likedislike
ascend-robotascend-robot成员
6月24日 添加了label:lgtm
ascend-robotascend-robot成员
6月24日 合入了pull request
ascend-robot
ascend-robot成员
6月24日 评论:

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月24日 评论:
流水线 pytorch_gitcode_PR_multiVersion#11247 [ commitID:ab3c4d70 ] 已完成
likedislike