Pull Request已成功合入, 合并人@ascend-robot
(感谢 Flipped 的贡献)变更摘要
此 PR 在 test/profiler/test_string_table.py 中为 torch.autograd.profiler_util.StringTable.default_factory 补充单元测试,覆盖该属性的默认值、构造参数、读写行为、非法参数校验以及与 StringTable.__missing__ 的交互。同时重构了测试文件的导入方式(将 StringTable 提升为模块级导入)并添加了 BSD-3 许可证头,不涉及 API 功能实现变更。
主要改动
- 新增
default_factory默认值测试:添加test_default_factory_is_none_by_default和test_default_factory_preserves_callable,验证默认构造时default_factory为None,以及传入可调用对象后该属性被正确保留。 - 新增
default_factory属性读写测试:添加test_default_factory_is_writable,验证default_factory属性可被赋值为可调用对象或None。 - 新增非法参数校验测试:添加
test_default_factory_rejects_non_callable_at_construction,验证构造时传入不可调用对象(如字符串"invalid")会抛出TypeError。 - 新增
__missing__交互行为测试:添加test_default_factory_is_not_used_by_string_table_missing,验证StringTable.__missing__在键缺失时不调用default_factory,而是直接返回键本身。 - 重构导入结构与文档字符串:将
from torch.autograd.profiler_util import StringTable从各测试方法内部提升为模块级导入,更新模块文档字符串以涵盖新增的default_factory测试,并添加 BSD-3 许可证头。


代码审查
审查总结
已审文件:
test/profiler/test_string_table.py— 发现 1 个问题
各优先级发现数量:
- P0: 0
- P1: 1(
test_default_factory_rejects_non_callable_at_construction断言了不存在的 TypeError 行为,该测试会在执行时失败) - P2: 0
- P3: 0
整体风险评估:此变更存在一个会导致测试失败的正确性问题。test_default_factory_rejects_non_callable_at_construction 测试假设 StringTable("invalid") 在构造时抛出 TypeError,但 CPython 的 defaultdict.__init__ 不在构造时校验 default_factory 的可调用性,且 StringTable.__missing__ 被覆写为直接返回键名本身,不会触发 default_factory 调用,因此该测试会因 AssertionError: TypeError not raised 而失败。建议在合入前删除或修正该测试。其余新增测试(default_factory 默认值、保留可调用对象、可写性、__missing__ 不调用 default_factory)以及导入重构均正确无误。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 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 | ✅ li_jing_hw, huangjingwei (2/2) | ✅ li_jing_hw (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
qq_65095566, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Linking Issue Notice
@qq_65095566 , 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.12.0 | ||
| v2.11.0 | ||
| v2.7.1 | ||
| v2.7.1-26.1.0 | ||
| v2.12.0-26.1.0 | ||
| v2.9.0 | ||
| v2.11.0-26.1.0 | ||
| v2.9.0-26.1.0 | ||
| v2.10.0-26.1.0 | ||
| v2.10.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 : 停止流水线


🟠 High Priority
变更行:第 84–86 行新增的 test_default_factory_rejects_non_callable_at_construction 测试断言 StringTable("invalid") 在构造时抛出 TypeError。
受影响的契约/行为:StringTable 继承自 collections.defaultdict,其 __init__ 未覆写,直接使用 CPython 的 defaultdict.__init__。CPython 的 defaultdict.__init__ 不在构造时校验 default_factory 的可调用性——它只是存储该值。TypeError("first argument must be callable or None") 仅由 defaultdict.__missing__ 在访问缺失键时抛出。而 StringTable.__missing__ 已被覆写为直接返回键名本身(字符串驻留),不会调用 default_factory。
失败模式:执行此测试时,StringTable("invalid") 不会抛出任何异常,assertRaises(TypeError) 将失败,报 AssertionError: TypeError not raised。
建议:删除该测试,或将其改为验证实际行为:StringTable 在构造时接受非可调用对象而不抛异常(因为 missing 不使用 default_factory)。如果目标是在构造时校验,请先在 StringTable.init 中实现校验逻辑。


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


/lgtm


/approve




【合入来源】
关联社区 issue:[Usage]: torch.autograd.profiler_util.StringTable.default_factory API适配补齐 #3444
关联社区 issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(59) #2906
【修改方案】
经检索 PyTorch v2.7.1 官方测试目录,未发现针对
torch.autograd.profiler_util.StringTable.default_factory的直接测试用例。现有测试未直接覆盖该属性的默认值、构造参数、属性修改、非法参数以及与
StringTable.__missing__的交互行为,因此需要补充直接测试。本次修改内容如下:
扩展已有测试文件:
保留原有
torch.autograd.profiler_util.StringTable.pop测试。新增
torch.autograd.profiler_util.StringTable.default_factory测试,覆盖:default_factory为None;None;default_factory属性;TypeError;StringTable.__missing__不调用default_factory。本次仅补充单元测试,不修改 API 功能实现。
【资料变更】
不涉及
经检查,torch-npu master 分支
docs/zh/api/native_api下 PyTorch 2.7.1、2.9.0、2.10.0、2.11.0 和 2.12.0 对应资料中均未收录torch.autograd.profiler_util.StringTable.default_factory。PyTorch 官方文档中也未提供该属性的独立公开文档条目。
StringTable继承自collections.defaultdict,default_factory是继承获得的标准库属性,并非 PyTorch 在StringTable中单独实现的成员,因此本次不新增 torch-npu 原生 API 资料条目。该属性属于非计算类属性,不涉及张量及数据类型。
【接口变更】
本次不修改
torch.autograd.profiler_util.StringTable.default_factory的功能、参数或返回行为,仅补充单元测试,不涉及跨代码仓或客户可见接口变更。【功能验证】
测试命令:
python test/profiler/test_string_table.py -v测试结果:
原有 4 个
StringTable.pop测试和新增 5 个StringTable.default_factory测试均执行通过。代码格式检查:
检查结果无异常。
【CheckList】