Pull Request已成功合入, 合并人@ascend-robot
(感谢 xiaoqi-zhou 的贡献)变更摘要
此 PR 主要针对 A5 环境(Ascend950 及以上平台)调整了 ProcessGroupHCCL 中 allreduce、reduce、reduce_scatter 的 AVG 归约操作:当输入张量为整数类型时,改用 div_ 的 trunc(向零截断)舍入模式,避免默认舍入行为带来的数值差异。同时,对分布式通信相关的 5 个测试文件进行了设备限制与导入清理,确保测试仅在已验证的 NPU 设备上运行。需要说明的是,PR 标题中提及的“跳过 ND->NC1HWC0 格式转换”在本次提供的补丁中未包含相关代码变更。
主要改动
- 整数类型 AVG 操作的截断舍入:在
ProcessGroupHCCL::allreduce、reduce、reduce_scatter中,新增is_atlas_a5判断与at::isIntegralType检查,对满足条件的张量调用tensor.div_(getSize(), "trunc")替代原先的tensor.div_(getSize())。 - 测试用例设备限制:在
test_allgather.py、test_allgather_base.py、test_reduce.py、test_reduce_scatter.py、test_reduce_scatter_base.py中,为关键测试方法添加@SupportedDevices(['Ascend910A', 'Ascend910B', 'Ascend910_93'])装饰器,限定仅在上述设备上执行。 - 测试文件导入清理:移除上述测试文件中未使用的导入(如
unittest、os、randint、TestCase、mp),并统一引入SupportedDevices,减少冗余依赖。


代码审查
经过全面审查,以下是本次审查的结论:
审查结论
已审查的文件(共 6 个)
- test/distributed/test_allgather.py — 无需报告的问题
- test/distributed/test_allgather_base.py — 无需报告的问题
- test/distributed/test_reduce.py — 无需报告的问题
- test/distributed/test_reduce_scatter.py — 无需报告的问题
- test/distributed/test_reduce_scatter_base.py — 无需报告的问题
- torch_npu/csrc/distributed/ProcessGroupHCCL.cpp — 无需报告的问题
分析总结
C++ 变更(ProcessGroupHCCL.cpp):在三处 div_ 调用(allreduce、reduce、reduce_scatter 的 AVG 路径)中新增了 A5 平台的整数类型分支,使用 rounding_mode="trunc" 替代默认除法。逻辑正确:
GetSocVersion() >= Ascend950正确识别 A5 芯片;at::isIntegralType(..., false)正确排除 bool 类型;"trunc"是 PyTorch ATendiv_API 支持的合法 rounding_mode 值。
Python 测试变更:在 5 个测试文件中新增 @SupportedDevices(['Ascend910A', 'Ascend910B', 'Ascend910_93']) 装饰器,在 A5 上跳过 ND→NC1HWC0 格式转换相关的测试用例,同时清理了未使用的 import(unittest、os、randint、mp 等)。经逐一验证,被移除的 import 在各自文件中确实未被直接使用,SupportedDevices 所需的 unittest 在其定义模块中已 import,无需调用方再 import。
整体风险评估
低风险。变更范围可控,C++ 代码逻辑清晰无缺陷,Python 测试变更仅涉及设备过滤装饰器和 import 清理,均为安全改进。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论


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
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
xiaoqi-zhou, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Linking Issue Notice
@xiaoqi-zhou , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .


当前仓库存在以下 保护分支 :
| Protected Branch | Version | Release |
|---|---|---|
| master | ||
| v2.7.1 | ||
| v2.9.0 | ||
| v2.10.0 | ||
| v2.11.0 | ||
| v2.12.0 | ||
| v2.9.0-26.1.0 | ||
| v2.10.0-26.1.0 | ||
| v2.7.1-26.1.0 | ||
| v2.12.0-26.1.0 | ||
| v2.11.0-26.1.0 | ||
| ci-test |
评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 | 🕚 | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


ascend docs pipeline is running...


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


🟡 Medium Priority
reduce_scatter() 函数内部有三个代码路径执行 AVG 除法:
same_size为 true 的路径(第 5925–5935 行):本次 diff 已添加 A5 的"trunc"舍入处理 ✓hcclReduceScatterVExist()为 true 的路径(第 6038–6041 行):仍使用tensor.div_(getSize()),未添加 A5 整数截断处理else回退路径调用_reduce_oop()(第 6062 行),其内部_reduce_oop()(第 5177–5181 行)同样未添加 A5 处理
当用户在 A5 环境调用 reduce_scatter() 且输入为整数类型时,若因 tensor shape 不同而走第 2 或第 3 条路径,整数除法将使用默认的 floor 舍入而非 trunc 舍入,导致与第 1 条路径(以及 allreduce、reduce 的已修复路径)产生不一致的计算结果。
| 6041
| - |
|
6041 | + 在 `reduce_scatter()` 的 `hcclReduceScatterVExist()` 路径(第 6038–6041 行)和 `_reduce_oop()` 的 AVG 后处理(第 5177–5181 行)中添加与 `allreduce`/`reduce` 相同的 A5 整数截断逻辑:`bool is_atlas_a5 = c10_npu::GetSocVersion() >= c10_npu::SocVersion::Ascend950;` 然后对整数类型使用 `tensor.div_(getSize(), "trunc")`。 |


ascend docs pipeline is running...


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


compile


ascend docs pipeline is running...


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


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


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




【合入来源】
issue:https://gitcode.com/Ascend/pytorch/issues/2827
【修改方案】
【资料变更】
不涉及
【接口变更】
不涉及
【功能验证】
【CheckList】