已合并
[sync] PR-38965: 在A5机器调用div_时加参数rounding_mode=trunc #39167
[sync] PR-38965: 在A5机器调用div_时加参数rounding_mode=trunc #39167
已合并
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 26e3f878
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 wjlflyer, renyujin (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
xiaoqi-zhou成员
6月24日 评论:

compile

likedislike
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
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 的变更同步到 v2.12.0 分支,核心目的是在 A5 机器上调用 div_ 时添加 rounding_mode=trunc 参数。当 _reduce_scatter_base 操作使用 AVG(求平均)归约操作时,针对 Ascend950 及以上芯片(A5),除法运算显式指定截断取整模式,以确保整型数据的 AVG 计算结果与预期一致。

主要改动

  • ProcessGroupHCCL.cpp_reduce_scatter_base 函数中 A5 机器 AVG 操作的取整策略调整:在集体通信后处理中,新增 is_atlas_a5 芯片版本检测逻辑,当芯片为 Ascend950+ 时,对输出 tensor 调用 tensor.div_(getSize(), "trunc") 使用截断取整;非 A5 机器保持原有 tensor.div_(getSize()) 行为不变。

  • test_reduce_scatter_tensor.py — 新增整型 AVG 归约测试用例:新增 test_reduce_scatter_tensor_avg 测试方法,使用 np.int32np.int8 整型数据配合 dist.ReduceOp.AVG 进行 reduce_scatter_tensor 操作,验证截断取整模式下结果的正确性。

  • test_all_to_all_single.py — 微小调整:文件仅有一行增删的微调,无实质性逻辑变更。

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

代码审查

审查总结

本 diff 共涉及 3 个文件,审查结果如下:

文件 审查结论
test/distributed/test_all_to_all_single.py 仅修复错误消息中的拼写错误("dosen't" → "doesn't"),无问题
test/distributed/test_reduce_scatter_tensor.py 移除未使用的 import、增加 SupportedDevices 装饰器限制两个 SUM 测试在非 A5 设备运行、代码换行格式化。发现 1 个 P3 建议性问题
torch_npu/csrc/distributed/ProcessGroupHCCL.cpp 多处拼写修正 + 核心变更:在 _reduce_scatter_base 的 AVG 后处理中为 A5 设备添加 div_("trunc")。发现 1 个 P2 问题

发现统计

  • P0:0 个
  • P1:0 个
  • P2:1 个(A5 上浮点张量 div_("trunc") 可能导致截断而非真除法)
  • P3:1 个(A5 上 SUM 模式 reduce_scatter 测试覆盖缺失)

整体风险评估:变更风险中等。核心变更(div_ 添加 rounding_mode="trunc")对整数类型是正确且必要的修复,但对浮点类型存在潜在的行为差异。当前测试覆盖仅涵盖整数类型的 AVG 路径,浮点路径是潜伏风险。建议在合入前确认 A5 上浮点 div_ 是否需要 rounding_mode,或根据 dtype 条件性添加。

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

💬 仅评论

likedislike
atomgit-bot
atomgit-bot6月24日进行代码检视1
test/distributed/test_reduce_scatter_tensor.py
@@ -61,3 +59,4 @@
6159 pg.reduce_scatter_tensor(output, input_tensor)
6260 
61+ @SupportedDevices(['Ascend910A', 'Ascend910B', 'Ascend910_93'])
6362 @skipIfUnsupportMultiNPU(2)
atomgit-bot
atomgit-bot6月24日评论:

🔵 Low Priority

变更位置:test/distributed/test_reduce_scatter_tensor.py 第 94–95 行和 61–62 行,新增 @SupportedDevices(['Ascend910A', 'Ascend910B', 'Ascend910_93']) 装饰器到 test_reduce_scatter_tensor_uneventest_reduce_scatter_tensor 两个测试方法。

这两个测试方法使用 ReduceOp.SUM(默认值),不涉及本 PR 修改的 ReduceOp.AVGdiv_("trunc") 代码路径。新增装饰器后,这些 SUM 测试将在 A5(Ascend950)设备上被跳过。

影响:A5 上 reduce_scatter_tensor(SUM 模式)的测试覆盖被移除。这可能是预期行为(SUM 在 A5 上尚未支持或存在其他问题),且与本 PR 的 div_ 修改无直接关联,但缺少对同一函数非 AVG 路径的 A5 测试覆盖是一个测试空白。

建议:确认 SUM 路径在 A5 上是否需要跳过。如果需要跳过,建议添加注释说明原因;如果不需要,移除 SupportedDevices 装饰器以保持 A5 上的测试覆盖。

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日评论:

🟡 Medium Priority

变更位置:torch_npu/csrc/distributed/ProcessGroupHCCL.cpp 第 6113–6119 行,_reduce_scatter_base 函数的 ReduceOp::AVG 后处理分支。

问题:在 A5(Ascend950+)设备上,div_ 调用被无条件添加了 rounding_mode="trunc" 参数。根据 PyTorch ATen 的实现(BinaryOps.cpp),当 rounding_mode="trunc" 时调用的是 div_trunc_stub(截断除法),而非 div_true_stub(真除法)。这对整数张量是正确的(整数除法需要截断),但对浮点张量会改变语义——原本的 tensor.div_(getSize()) 是浮点真除法(计算数学平均值),而 tensor.div_(getSize(), "trunc") 会执行向零截断,得到的是截断后的结果而非真正的平均值。

影响:若在 A5 上使用 ReduceOp::AVG 搭配浮点类型张量调用 reduce_scatter_tensor,输出的平均值将是截断的而非精确的,与非 A5 设备行为不一致。

缓解因素:当前活跃测试 test_reduce_scatter_tensor_avg(第 116 行)仅使用 np.int32, np.int8 整数类型;浮点 AVG 测试 test_reduce_scatter_tensor_uneven_avg 已被 @unittest.skip 跳过。因此该问题在当前测试覆盖下是潜伏的。

if (is_atlas_a5 && c10::isIntegralType(tensor.scalar_type(), /includeBool=/false)) {
tensor.div_(getSize(), "trunc");
} else {
tensor.div_(getSize());
}
如果 A5 上所有类型的 div_ 都必须带 rounding_mode,则浮点类型应使用 true division 的正确 rounding_mode(或不带 rounding_mode)。

建议:根据张量 dtype 条件性添加 rounding_mode="trunc":整数类型使用 trunc,浮点类型不添加 rounding_mode(保持原始浮点除法行为)。或者确认 A5 上所有类型都必须带 rounding_mode 后,对浮点类型使用不带 rounding_mode 的 div_(若可用)或显式使用 true division 路径。

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#38813 已完成
阶段 任务名 状态 详情
编译构建 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#11244 [ commitID:bc760bd7 ] 已完成
likedislike