Pull Request已成功合入, 合并人@ascend-robot
(感谢 nwww 的贡献)变更摘要
该 PR 为 NPU 平台新增了 torch._C._distributed_c10d.Backend.Options API 的单元测试文件,补充社区 PyTorch 中对该 API 缺乏直接验证的覆盖盲区。新增文件 test/distributed/test_backend_options.py(共 180 行),包含 6 个测试用例,覆盖 Backend.Options 的构造、属性读写、边界值及异常输入校验。
主要改动
- 新增
TestBackendOptions测试类:在test/distributed/test_backend_options.py中新增测试类,对Backend.Options进行系统化验证,所有测试用例继承自torch.testing._internal.common_utils.TestCase。 - 构造函数参数验证:
test_init_with_default_and_explicit_timeout验证默认超时(timedelta(minutes=30))、关键字参数和位置参数三种构造方式;test_init_with_boundary_values覆盖空字符串 backend、零超时和负超时等边界情况。 - 属性读写与实例独立性验证:
test_group_metadata_properties验证global_ranks_in_group和group_name属性的读写;test_property_access_and_instance_independence验证backend属性为只读(赋值抛出AttributeError)、_timeout可修改以及不同实例互不影响。 - 非法输入类型校验:
test_invalid_constructor_arguments覆盖传入None、整数、列表、字典等非法 backend 和 timeout 类型时抛出TypeError,以及多余参数和未知关键字参数的错误处理;test_invalid_timeout_assignment验证对_timeout赋非法值同样抛出TypeError。


代码审查
审查结论
经过对 test/distributed/test_backend_options.py(新增 180 行)的逐行审查,我未发现任何需要报告的问题。
审查覆盖范围
| 维度 | 结论 |
|---|---|
| 逻辑正确性 | 所有断言与 Backend.Options API 的预期行为一致:默认超时 timedelta(minutes=30)、backend 只读、_timeout 可写、边界值(空字符串、零超时、负超时)均按 API 现有合约接受、非法参数类型抛出 TypeError。 |
| 安全性 | 纯本地测试文件,无外部输入,无敏感信息。 |
| 导入与依赖 | from torch._C._distributed_c10d import Backend 是 C 扩展模块的权威导入路径,与项目内其他文件一致。 |
| 测试结构 | 类与方法命名遵循 unittest 规范,subTest 使用正确,assertRaises 上下文管理器用法正确,run_tests() 入口正确。 |
| 规范 | 许可证头完整,文件级 docstring 清晰说明了测试目的。 |
各文件审查结果
test/distributed/test_backend_options.py:未发现问题。
综合风险评估
低风险。该变更为纯测试新增,覆盖了 Backend.Options 的构造、属性访问、实例独立性和非法参数处理,不涉及任何运行时行为变更。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


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 | ✅ 楚浩田, chenrayray (2/2) | ✅ 楚浩田 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
nannan-2026, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Linking Issue Notice
@nannan-2026 , 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.9.0 | ||
| v2.12.0-26.1.0 | ||
| v2.11.0 | ||
| v2.7.1 | ||
| v2.12.0 | ||
| v2.9.0-26.1.0 | ||
| v2.11.0-26.1.0 | ||
| v2.10.0 | ||
| v2.7.1-26.1.0 | ||
| v2.10.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_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...


ascend docs pipeline is running...


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


✅ 跳过 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_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 : 停止流水线


/lgtm


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_pre-commit | ✅ | >>> |
| check_error | ✅ | >>> | |
| lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | 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 : 停止流水线




【合入来源】
关联社区 Issue:
https://gitcode.com/Ascend/pytorch/issues/3207
【修改方案】
本 PR 为 PyTorch 原生 API 一致性测试补齐,不涉及目标 API 功能实现修改。
目标 API:
新增测试文件:
1. API 功能说明
torch._C._distributed_c10d.Backend.Options是 PyTorch 分布式通信后端Options 的基础配置类。
构造形式为:
其中:
backend为必选参数,表示分布式通信后端名称;timeout为可选参数,类型为datetime.timedelta;timeout时,默认值为 30 分钟;backend为只读属性;_timeout为可读写属性;global_ranks_in_group和group_name。2. 修改内容
新增测试类:
共新增以下 6 个测试方法:
2.1
test_init_with_default_and_explicit_timeout验证:
backend;Backend.Options;backend属性与传入值一致;_timeout为 30 分钟;timeout支持位置参数;timeout支持关键字参数;_timeout一致。2.2
test_init_with_boundary_values验证:
backend为空字符串;timeout为零;timeout为负数;2.3
test_group_metadata_properties验证 PyTorch v2.12.0 中的分组元数据属性:
global_ranks_in_group默认值为空列表;group_name默认值为空字符串;global_ranks_in_group支持写入 rank 列表;group_name支持写入分组名称;2.4
test_property_access_and_instance_independence验证:
backend为只读属性;backend赋值时抛出AttributeError;_timeout为可写属性;_timeout不影响其他实例;2.5
test_invalid_constructor_arguments验证:
backend;backend传入None、整数、列表或字典;timeout传入None、整数、字符串、列表或字典;2.6
test_invalid_timeout_assignment验证:
_timeout写入None、整数、字符串、列表或字典时抛出TypeError;_timeout值保持不变。3. 测试实现规范
新增测试符合以下要求:
TestCase和run_tests;self.assert*系列方法验证;self.assertRaises验证;try-except屏蔽测试失败;test/distributed/test_backend_options.py。4. PyTorch 社区用例情况及新增必要性
已检查 PyTorch 官方社区测试代码。
官方源码和测试中存在以下间接相关内容:
C10dBackend.Options作为类型标注;ProcessGroupNCCL.Options。但是,现有社区测试未针对以下精确 API 提供直接、完整的 Python 行为
验证:
现有上游测试未充分覆盖:
backend必选参数;timeout可选参数;backend只读行为;_timeout可写行为;global_ranks_in_group的默认值及读写行为;group_name的默认值及读写行为;_timeout非法赋值后的状态保持行为。现有继承关系、类型标注和间接使用不能替代目标 API 的直接一致性验证。
因此,有必要在 Ascend for PyTorch 的
test目录新增独立专项测试。5. 各版本差异说明
各分支测试范围根据对应 PyTorch 版本中
Backend.Options的实际接口能力进行区分:backend和_timeoutglobal_ranks_in_group和group_name具体说明:
global_ranks_in_group和group_name,因此不包含test_group_metadata_properties;global_ranks_in_group和group_name,因此增加分组元数据属性测试;
【资料变更】
不涉及资料文件修改。
已检查 Ascend for PyTorch master 分支下的原生 API 资料目录:
目标 API 位于:
其中
torch._C和_distributed_c10d均属于 PyTorch 内部实现模块,目标 API 属于 PyTorch 私有接口,不是客户可见的公开 API。
根据 Torch-NPU API 一致性任务要求,PyTorch 私有接口不需要在公开原生
API 支持清单中补充资料。
因此,本任务无需新增 Docs PR,也不修改资料目录。
【接口变更】
不涉及。
本 PR 仅新增:
用于验证:
本 PR 未修改目标 API 的:
本 PR 不涉及跨代码仓修改,也不涉及客户可见接口变更。
【功能验证】
1. 验证分支和提交
目标分支:
测试分支:
测试提交:
验证前已确认本地与远端提交一致:
本提交仅新增:
2. 测试环境
本次验证使用与目标分支匹配的独立 Python 环境:
其中:
是当前安装的 PyTorch wheel 构建标识。
目标 API 不执行 CUDA 算子,本测试也不依赖 CUDA 设备。
3. NPU 适配说明
目标 API 是分布式后端配置对象,不接收 PyTorch Tensor,也不执行:
因此,本测试不创建与目标 API 无关的 NPU Tensor,也不初始化 HCCL
进程组。
验证时设置:
用于避免在独立版本环境中自动加载与目标配置类无关的设备后端。
该设置不会跳过或替代
Backend.Options的任何接口行为验证。本次版本验证不依赖 torch_npu、CUDA 或 NPU 算子执行,验证对象为目标
API 的 Python 绑定及属性行为。
4. API 探针
实际环境探针结果:
属性行为探针结果:
以上结果说明:
backend和_timeout正确;backend为只读属性;_timeout支持修改;global_ranks_in_group支持读写;group_name支持读写;5. 测试方法检查
实际检测到以下 6 个测试方法:
检查结果:
6. Python 语法检查
执行命令:
执行结果:
7. Git 差异检查
执行命令:
git diff --check \ test-backend-options-v2.12.0^ \ test-backend-options-v2.12.0 \ -- test/distributed/test_backend_options.py执行结果:
8. unittest 验证
执行命令:
cd /tmp PYTHONPATH= \ TORCH_DEVICE_BACKEND_AUTOLOAD=0 \ PYTHONDONTWRITEBYTECODE=1 \ /workspace/venvs/api3205-v2_12_0/bin/python \ /tmp/api3207_all_versions_latest/v2_12_0/test_backend_options.py \ -v实际结果:
执行结果:
9. pytest 验证
执行命令:
cd /tmp PYTHONPATH= \ TORCH_DEVICE_BACKEND_AUTOLOAD=0 \ PYTHONDONTWRITEBYTECODE=1 \ /workspace/venvs/api3205-v2_12_0/bin/python \ -m pytest -q \ /tmp/api3207_all_versions_latest/v2_12_0/test_backend_options.py实际结果:
执行结果:
10. 验证结果汇总
最终结果:
完整日志:
11. 四版本验证说明
本任务分别在以下目标版本环境执行测试:
每个版本均使用对应分支、对应 PyTorch 版本和独立运行日志,不使用其他
版本的运行结果代替当前分支验证结果。
四版本汇总文件:
【CheckList】