Pull Request已成功合入, 合并人@ascend-robot
(感谢 ascend-robot 的贡献)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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


Ascend docs pipeline is running...


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


compile


Ascend docs pipeline is running...


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


变更摘要
此 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的取整行为变化。


代码审查
审查总结
本次 diff 涉及 3 个文件的变更,审查结果如下:
各文件审查情况
| 文件 | 审查结论 |
|---|---|
test/distributed/test_all_to_all_single.py |
✅ 无问题(typo 修复:dosen't → doesn'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++ 核心逻辑变更存在两个隐患:
-
P1 问题最严重:在 A5 机器上对 float 类型 tensor 执行
reduce_scatter_tensor+ AVG 会直接崩溃。虽然实际触发概率取决于用户场景(是否在 A5 上对 float 数据使用 AVG),但代码路径明确可达,建议在合入前修复。 -
P2 问题:同一文件中
all_reduce、reduce、reduce_scatter_tensor_coalesced等函数中还有 10+ 处完全相同的tensor.div_(getSize())模式未做 A5 适配,若 A5 整数除法行为确实变化,这些路径在 A5 上会产生静默的数值错误。建议与原始 issue (#2441) 确认修复范围。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 2 |
⛔ 需要修改


🟡 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_tensorcoalesced 路径中 - 第 6010 行:
reduce_scatter_tensorcoalesced 路径(延续)中 - 第 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 检查。


🔵 Low Priority
test_reduce_scatter_tensor.py 中删除了 import os 和 import torch.multiprocessing as mp。经检查,该文件的代码中确实没有直接使用 os 或 mp(多进程功能继承自 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_tensor 和 test_reduce_scatter_tensor_uneven。这些测试使用 SUM op(非 AVG),排除 A5 设备不会影响新增的 div_("trunc") 代码路径的测试覆盖(AVG 测试 test_reduce_scatter_tensor_avg 和 test_reduce_scatter_tensor_uneven_avg 未加限制,仍可在 A5 上运行)。
这些均属正常的清理和测试策略调整,无问题。
建议:无需修改,此为正常的 import 清理和测试设备范围调整。


🟠 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 | + } |


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 | ✅ | >>> |


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.




1. Origin pull request:
https://gitcode.com/Ascend/pytorch/merge_requests/38965
2. Original pull request related issue(s):
https://gitcode.com/Ascend/pytorch/issues/2441
3. Original pull request related commit(s):