已合并
test: Add tests for StringTable.default_factory #43210
test: Add tests for StringTable.default_factory #43210
已合并
Flipped创建于 21 天前
Flipped
Flipped
21 天前

【合入来源】

关联社区 issue:[Usage]: torch.autograd.profiler_util.StringTable.default_factory API适配补齐 #3444

关联社区 issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(59) #2906

  • 需求
  • 问题单
  • issue/工单
  • 重构优化
  • 资料更新

【修改方案】

经检索 PyTorch v2.7.1 官方测试目录,未发现针对 torch.autograd.profiler_util.StringTable.default_factory 的直接测试用例。

现有测试未直接覆盖该属性的默认值、构造参数、属性修改、非法参数以及与 StringTable.__missing__ 的交互行为,因此需要补充直接测试。

本次修改内容如下:

  1. 扩展已有测试文件:

    test/profiler/test_string_table.py
    
  2. 保留原有 torch.autograd.profiler_util.StringTable.pop 测试。

  3. 新增 torch.autograd.profiler_util.StringTable.default_factory 测试,覆盖:

    • 默认构造时 default_factoryNone
    • 显式传入 None
    • 构造时传入可调用对象;
    • 读取和修改 default_factory 属性;
    • 构造时传入不可调用对象抛出 TypeError
    • StringTable.__missing__ 不调用 default_factory
  4. 本次仅补充单元测试,不修改 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.defaultdictdefault_factory 是继承获得的标准库属性,并非 PyTorch 在 StringTable 中单独实现的成员,因此本次不新增 torch-npu 原生 API 资料条目。

该属性属于非计算类属性,不涉及张量及数据类型。

【接口变更】

本次不修改 torch.autograd.profiler_util.StringTable.default_factory 的功能、参数或返回行为,仅补充单元测试,不涉及跨代码仓或客户可见接口变更。

【功能验证】

测试命令:

python test/profiler/test_string_table.py -v

测试结果:

Ran 9 tests in 0.032s

OK

原有 4 个 StringTable.pop 测试和新增 5 个 StringTable.default_factory 测试均执行通过。

代码格式检查:

git diff --check

检查结果无异常。

【CheckList】

  • 代码注释完备,正确记录错误日志
  • 代码实现进行了返回值、空指针等校验
  • PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
  • PR持续集成流水线(CI)执行通过,代码检查无异常
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Flipped 的贡献)
FlippedFlipped
21 天前 创建了 pull request,commit 59cf02a8
atomgit-bot
atomgit-bot
21 天前 评论:

变更摘要

此 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_defaulttest_default_factory_preserves_callable,验证默认构造时 default_factoryNone,以及传入可调用对象后该属性被正确保留。
  • 新增 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 许可证头。
likedislike
atomgit-bot
atomgit-bot
21 天前 评论:

代码审查

审查总结

已审文件:

  • 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

⛔ 需要修改

likedislike
ascend-robotascend-robot成员
21 天前 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
21 天前 评论:

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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

likedislike
ascend-robotascend-robot成员
21 天前 添加了label:needs-issue
ascend-robot
ascend-robot成员
21 天前 评论:

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 .

likedislike
ascend-robot
ascend-robot成员
21 天前 评论:

当前仓库存在以下 保护分支

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,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
ascend-robotascend-robot成员
21 天前 添加了label:ci-pipeline-running
ascend-robotascend-robot成员
21 天前 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
21 天前 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
21 天前 评论:
流水线 PR-pipeline_pytorch#50966 [ commitID:d9fcf383 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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 >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan, Jesse]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
atomgit-bot
atomgit-bot21 天前进行代码检视2
test/profiler/test_string_table.py
@@ -50,0 +83,4 @@
83+ 
84+ def test_default_factory_rejects_non_callable_at_construction(self):
85+ with self.assertRaises(TypeError):
86+ StringTable("invalid")
atomgit-bot
atomgit-bot21 天前评论:

🟠 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 中实现校验逻辑。

likedislike
Flipped
Flipped
20 天前 评论:
likedislike
FlippedFlipped
20 天前 修改了pull request 的描述
ascend-robotascend-robot成员
20 天前 删除了label:needs-issue
FlippedFlipped
20 天前 修改了pull request 的描述
Flipped
Flipped
12 天前 评论:

compile

likedislike
ascend-robotascend-robot成员
12 天前 删除了label:ci-pipeline-passed
ascend-robotascend-robot成员
12 天前 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
12 天前 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
12 天前 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
12 天前 评论:

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

likedislike
ascend-robotascend-robot成员
12 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
12 天前 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
12 天前 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
12 天前 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
12 天前 评论:
流水线 PR-pipeline_pytorch#56382 [ commitID:d9fcf383 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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 >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan, Jesse, WSs_321]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
Jingwei HuangJingwei Huang成员
7 天前 解决了最后一个问题
Jingwei Huang
Jingwei Huang成员
7 天前 评论:

/lgtm

likedislike
li_jing_hw成员
6 天前 评论:

/approve

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