已合并
test(distributed): add ProcessGroupMPI.create tests #42948
lgxxx创建于 22 天前
test(distributed): add ProcessGroupMPI.create tests #42948
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 lgxxx 的贡献)ascend-robot
22 天前 评论:
22 天前 评论:
atomgit-bot
22 天前 评论:
22 天前 评论:
变更摘要
本 PR 新增了 test/distributed/test_c10d_mpi.py 测试文件,为 torch.distributed.distributed_c10d.ProcessGroupMPI.create 静态工厂方法及 MPI 后端的进程组初始化流程补充 Python 层验证用例。社区此前仅有 C++ 覆盖,缺少直接面向该 API 的 Python 测试。新增的测试涵盖 MPI 可用性检查与导入守卫、ProcessGroupMPI.create 工厂创建及属性验证、非成员行为、以及通过 dist.init_process_group 初始化后的 allreduce/broadcast/barrier 等集合通信操作。
主要改动
- 新增
TestProcessGroupMPIAvailability类:验证dist.is_mpi_available()和dist.is_available()返回布尔值、MPI 不可用时dist.init_process_group(backend="mpi")抛出RuntimeError、以及ProcessGroupMPI的导入受可用性守卫控制。 - 新增
TestProcessGroupMPICreate类:验证ProcessGroupMPI.create返回实例的name()、size()、rank()属性是否正确;覆盖单 rank、全量 world size ranks、以及当前 rank 不在指定列表中时返回None的非成员场景。 - 新增
TestProcessGroupMPIInitGroup类:通过dist.init_process_group(backend="mpi")初始化后验证get_backend、get_rank、get_world_size、is_initialized等 API,并覆盖all_reduce、broadcast、barrier三个集合通信操作的端到端测试。 - 新增辅助函数:
_get_process_group_mpi()实现惰性导入并安全返回None,_is_mpi_usable()组合检查dist.is_available()和dist.is_mpi_available(),各测试用例依赖这些函数进行灵活跳过(skip)以适配无 MPI 运行时的环境。


atomgit-bot
22 天前 评论:
22 天前 评论:
代码审查
审查总结
共发现 3 个问题:
| 优先级 | 数量 | 说明 |
|---|---|---|
| P1 | 1 | dist.get_world_size()/dist.get_rank() 在默认进程组未初始化时被调用,MPI 可用时将直接崩溃 |
| P3 | 2 | 环境变量未恢复的测试隔离性缺陷;多处硬编码 [0] 导致非 rank 0 进程测试失败 |
整体风险判断:中等。P1 问题在 MPI 环境可用时会直接导致 TestProcessGroupMPICreate 中 2 个测试用例崩溃;当前测试环境无 MPI 支持所以未被触发。建议在合入前修复 P1 问题(补充 setUp/tearDown 或改用环境变量),P3 项作为可选改进。
已审查文件:
test/distributed/test_c10d_mpi.py:发现 3 个问题(1 × P1, 2 × P3)
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 0 |
⛔ 需要修改


此处折叠了131条消息 查看更多
li_jing_hw
6 天前 评论:
6 天前 评论:
/approve


6 天前 添加了label:approvedlgtm
6 天前 删除了label:ci-pipeline-passed
6 天前 合入了pull request
ascend-robot
6 天前 评论:
6 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13944 [ commitID:7ac28e4c ] 已完成


【合入来源】
Issue: https://gitcode.com/Ascend/pytorch/issues/3208
【修改方案】
Add Python validation tests for
torch.distributed.distributed_c10d.ProcessGroupMPI.createintest/distributed/test_c10d_mpi.py.The PyTorch community has C++ coverage for the MPI process group but no direct Python tests for this API. The tests cover MPI availability and import guards, factory creation and properties, non-member behavior, process-group initialization, and allreduce/broadcast/barrier operations.
【资料变更】
不涉及。This is a backend control API and no native API documentation update is required.
【接口变更】
不涉及。
【功能验证】
The test was run with an absolute path outside the source tree:
python -m pytest test/distributed/test_c10d_mpi.py -qResult:
4 passed, 12 skipped. The skipped cases require an MPI runtime and a PyTorch build compiled with MPI support; this is expected in the current environment. ProcessGroupMPI is a backend/control API, so the test does not require NPU tensors.CI validation is pending.
【CheckList】
NPU 功能验证补充
以下为当前 PR head commit 对应的单卡 NPU 实测环境、命令和结果:
【合入来源】
Issue: https://gitcode.com/Ascend/pytorch/issues/3208
【修改方案】
Add Python validation tests for
torch.distributed.distributed_c10d.ProcessGroupMPI.createintest/distributed/test_c10d_mpi.py.The PyTorch community has C++ coverage for the MPI process group but no direct Python tests for this API. The tests cover MPI availability and import guards, factory creation and properties, non-member behavior, process-group initialization, and allreduce/broadcast/barrier operations.
【资料变更】
不涉及。This is a backend control API and no native API documentation update is required.
【接口变更】
不涉及。
【功能验证】
The test was run with an absolute path outside the source tree:
python -m pytest test/distributed/test_c10d_mpi.py -qResult:
4 passed, 14 skipped. The skipped cases require an MPI runtime and a PyTorch build compiled with MPI support; this is expected in the current environment. ProcessGroupMPI is a backend/control API, so the test does not require NPU tensors.【CheckList】