Pull Request已成功合入, 合并人@ascend-robot
(感谢 nwww 的贡献)变更摘要
本 PR 为 torch._C._distributed_c10d.Backend.Options 接口新增 API 一致性测试,新增测试文件 test/distributed/test_backend_options.py,其中定义了 TestBackendOptions 测试类,覆盖构造方法、属性访问、边界值及异常参数等场景,不涉及目标 API 功能实现修改。
主要改动
- 新增测试文件
test/distributed/test_backend_options.py:独立测试文件,聚焦Backend.Options的 API 行为验证,共 165 行,无对外部模块的修改。 - 默认构造行为验证:测试仅传入
backend参数时返回Backend.Options实例,且_timeout默认值为timedelta(minutes=30),覆盖位置参数与关键字参数两种调用方式。 - 边界值测试:验证
backend为空字符串、timeout为timedelta(0)及timedelta(seconds=-1)时的构造行为,确保边界输入不会导致崩溃。 - 无效参数异常覆盖:对
backend传入None、整数、列表、字典等非法类型,对timeout传入非timedelta类型及多余参数、未知关键字参数等场景,均断言抛出TypeError。 - 属性只读性与实例独立性验证:验证
backend属性不可写(赋值抛出AttributeError),_timeout可写但仅接受timedelta类型,且不同实例之间状态互不影响。


代码审查
经过全面审查,这个测试文件 test/distributed/test_backend_options.py 是一个新增的 API 一致性测试文件,用于验证 torch._C._distributed_c10d.Backend.Options 的构造方法、属性访问、边界值和异常参数。
我对每个测试方法进行了逐行审查:
-
test_init_with_default_and_explicit_timeout— 测试默认超时(30分钟)、关键字参数、位置参数的构造行为。所有断言正确匹配 API 预期行为。 -
test_init_with_boundary_values— 测试空字符串后端、零超时、负超时等边界值。这些值被正确存储,测试仅验证存储行为,不涉及业务校验逻辑。 -
test_property_access_and_instance_independence— 测试backend为只读属性(设置时抛出AttributeError)、_timeout可读写、实例间独立性。在 pybind11 中.def_readonly()属性设置时确实抛出AttributeError,与测试预期一致。timedelta的比较使用==(值比较),正确。 -
test_invalid_constructor_arguments— 测试无参构造、无效 backend 类型、无效 timeout 类型、额外位置参数、未知关键字参数均抛出TypeError。这是 pybind11 构造函数参数校验的标准行为,断言正确。 -
test_invalid_timeout_assignment— 测试无效类型赋值给_timeout时抛出TypeError,且赋值失败后_timeout保持默认值 30 分钟不变。pybind11 的 chrono 类型转换器不接受非timedelta类型,TypeError预期正确。
审查结论:该文件未发现任何正确性问题、安全问题、可靠性问题或破坏性变更。测试逻辑完整且与 PR 描述一致。
审查总结:
- 变更文件:1 个(
test/distributed/test_backend_options.py) - 发现问题数:
- P0:0
- P1:0
- P2:0
- P3:0
- 整体风险评估:低风险。这是一个纯测试文件新增,不涉及功能实现修改。测试逻辑正确,覆盖了构造方法、属性访问、边界值和异常参数等场景。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


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. 👍


当前仓库存在以下 保护分支 :
| 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 的基础配置类。
PyTorch v2.7.1 中的构造形式为:
其中:
backend为必选参数,表示分布式通信后端名称;timeout为可选参数,类型为datetime.timedelta;timeout时,默认值为 30 分钟;backend为只读属性;_timeout为可读写属性。PyTorch v2.7.1 的
Backend.Options不包含后续版本增加的:因此,本分支只针对 v2.7.1 实际存在的构造参数及属性进行验证。
2. 修改内容
新增测试类:
共新增以下 5 个测试方法。
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_property_access_and_instance_independence验证:
backend为只读属性;backend赋值时抛出AttributeError;_timeout为可写属性;_timeout不影响其他实例;2.4
test_invalid_constructor_arguments验证:
backend;backend传入None、整数、列表或字典;timeout传入None、整数、字符串、列表或字典;2.5
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可写行为;_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,因此对应分支增加分组元数据属性测试;
【资料变更】
不涉及资料文件修改。
目标 API 位于:
其中
torch._C和_distributed_c10d均属于 PyTorch 内部实现模块,目标 API 属于 PyTorch 私有接口,不是客户可见的公开 API。
根据 Torch-NPU API 一致性任务要求,PyTorch 私有接口无需在公开原生
API 支持清单中补充资料。
同时,任务资料只要求在 master 分支检查和补充;本 API 经核查无需新增
公开资料,因此:
docs/zh/api/native_api;【接口变更】
不涉及。
本 PR 仅新增:
用于验证:
本 PR 未修改目标 API 的:
本 PR 不涉及跨代码仓修改,也不涉及客户可见接口变更。
【功能验证】
1. 验证分支和提交
目标分支:
测试分支:
测试提交:
验证前已确认本地与远端提交一致:
本提交只新增:
提交包含:
2. 测试环境
本次验证使用与目标分支匹配的独立 Python 环境:
+cpu是当前安装的 PyTorch wheel 构建标识。目标 API 不执行 Tensor 运算,也不依赖 CUDA、NPU 或其他设备算子,
因此 CPU 构建不影响该 Python 绑定配置类的行为验证。
3. NPU 适配说明
目标 API 是分布式后端配置对象,不接收 PyTorch Tensor,也不执行:
因此,本测试不创建与目标 API 无关的 NPU Tensor,也不初始化 HCCL
进程组。
验证时设置:
用于避免在独立版本环境中自动加载与目标配置类无关的设备后端。
该设置不会跳过或替代
Backend.Options的构造、属性及异常行为验证。本次验证不依赖 torch_npu、CUDA 或 NPU 算子执行,验证对象为目标 API
的 Python 绑定行为。
4. API 探针
执行目标 API 探针,验证对象类型、默认属性、属性可写性以及
v2.7.1 版本特征。
实际结果应记录为:
以上结果说明:
backend和_timeout正确;backend为只读属性;_timeout支持修改;5. 测试方法检查
实际检测到以下 5 个测试方法:
检查结果:
6. Python 语法检查
执行命令:
执行结果:
7. Git 差异检查
执行命令:
git diff --check \ test-backend-options-v2.7.1^ \ test-backend-options-v2.7.1 \ -- test/distributed/test_backend_options.py执行结果:
8. unittest 验证
执行命令:
cd /tmp PYTHONPATH= \ TORCH_DEVICE_BACKEND_AUTOLOAD=0 \ PYTHONDONTWRITEBYTECODE=1 \ /workspace/venvs/api3151-py271/bin/python \ /tmp/api3207_all_versions_latest/v2_7_1/test_backend_options.py \ -v实际结果:
执行结果:
9. pytest 验证
执行命令:
cd /tmp PYTHONPATH= \ TORCH_DEVICE_BACKEND_AUTOLOAD=0 \ PYTHONDONTWRITEBYTECODE=1 \ /workspace/venvs/api3151-py271/bin/python \ -m pytest -q \ /tmp/api3207_all_versions_latest/v2_7_1/test_backend_options.py实际结果:
执行结果:
10. 验证结果汇总
四版本测试脚本完成后,v2.7.1 对应退出码应填写为实际输出:
最终结果:
完整日志:
11. 四版本验证说明
本任务分别在以下目标版本环境执行测试:
每个版本均使用对应分支、对应 PyTorch 版本和独立运行日志,不使用其他
版本的运行结果代替当前分支验证结果。
四版本汇总文件:
【CheckList】