已合并
test(jit):add test fot ignore #39838
wei-pengfei22创建于 7月2日
test(jit):add test fot ignore #39838
已合并
wei-pengfei22创建于 7月2日
7月2日

【合入来源】

[Usage]: torch.jit.ignore API的测试用例完善与验证
请勿携带内部流程信息(需求链接、问题单、内部issue等)

  • issue/工单

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

本次变更为重新编写部分测试, 补齐API torch.jit.ignore 的测试覆盖。
重新编写torch.jit.ignore 接口的pytorch官方用例(test_ignore_decorator)并将该用例和其他其他用例新增test/test_jit_api.py文件中

测试用例设计:

测试方法 验证点
test_ignore_decorator 验证带drop_on_export参数的忽略装饰器功能,编译会触发对应告警,导出后忽略方法代码被移除,调用该方法会触发JIT报错
test_ignored_props 验证JIT忽略属性配置生效,忽略属性可在被忽略方法内正常调用,模型编译正常无异常
test_torch_ignore_conversion_to_none 验证忽略方法有无返回值标注均可正常编译,不影响模型主前向推理逻辑与计算结果
test_comment_ignore_indent 验证代码注释缩进不规范场景下,模型可正常编译,无JIT解析报错
test_ignored_method_binding 验证被忽略的类方法可正常绑定实例,能读写模块成员变量,与导出方法共享实例状态
test_no_self_arg_ignore_function 验证类内被忽略方法缺失self参数时,模型JIT编译会精准抛出参数异常报错

NPU适配:
torch.jit.ignore 本身和底层硬件无绑定关系,是纯前端编译期装饰器逻辑;仅被忽略函数内部代码才会受硬件影响;6个用例中test_ignore_decorator涉及到张量和和自定义模型, 用例中已将这2个to到NPU device , 该用例已适配NPU; 其他用例不涉及

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

已经在 https://gitcode.com/Ascend/pytorch/tree/v2.7.1/docs/zh/native_apis 的文档中进行查找与验证。该目录下的pytorch.2-7.1至pytorch.2-12-0文件夹下的torch-jit.md已经对"torch.jit.ignore"全部标注"是"。无需更改。

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

不涉及

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图
测试命令

cd pytorch/test
python3 -m unittest test_jit_api.TestJitIgnoreNPU.test_ignore_decorator test_jit_api.TestJitIgnoreNPU.test_ignored_props test_jit_api.TestJitIgnoreNPU.test_torch_ignore_conversion_to_none test_jit_api.TestJitIgnoreNPU.test_comment_ignore_indent test_jit_api.TestJitIgnoreNPU.test_ignored_method_binding test_jit_api.TestJitIgnoreNPU.test_no_self_arg_ignore_function

验证结果

cd /workspace/user_data/0701/271/pytorch/test

root@d9ea95a58de0:/workspace/user_data/0701/271/pytorch/test# python3 -m unittest test_jit_api.TestJitIgnoreNPU.test_ignore_decorator test_jit_api.TestJitIgnoreNPU.test_ignored_props test_jit_api.TestJitIgnoreNPU.test_torch_ignore_conversion_to_none test_jit_api.TestJitIgnoreNPU.test_comment_ignore_indent test_jit_api.TestJitIgnoreNPU.test_ignored_method_binding test_jit_api.TestJitIgnoreNPU.test_no_self_arg_ignore_function
/usr/local/python3.12.13/lib/python3.12/site-packages/torch/jit/_serialization.py:89: DeprecationWarning: `torch.jit.save` is deprecated. Please switch to `torch.export`.
  warnings.warn(
/usr/local/python3.12.13/lib/python3.12/site-packages/torch/jit/_serialization.py:176: DeprecationWarning: `torch.jit.load` is deprecated. Please switch to `torch.export`.
  warnings.warn(
./usr/local/python3.12.13/lib/python3.12/site-packages/torch/jit/_script.py:1488: DeprecationWarning: `torch.jit.script` is deprecated. Please switch to `torch.compile` or `torch.export`.
  warnings.warn(
.....
----------------------------------------------------------------------
Ran 6 tests in 1.018s

OK

验证结论
6个测试用例均通过,API 在 NPU 环境下可正常调用,状态设置与查询行为符合预期。

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

  • 代码注释完备,正确记录错误日志
  • 代码实现进行了返回值、空指针等校验
  • PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
  • PR持续集成流水线(CI)执行通过,代码检查无异常
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 wei-pengfei22 的贡献)
Wwei-pengfei22
7月2日 创建了 pull request,commit a140d23c
Wwei-pengfei22
7月2日 关联了issue:[Usage]: torch.jit.ignore API的测试用例完善与验证
ascend-robotascend-robot成员
7月2日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
7月2日 评论:

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, huangjingwei (2/2) chenrayray (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

wei-pengfei22, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robot
ascend-robot成员
7月2日 评论:

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

Protected Branch Version Release
master
v2.7.1
v2.9.0
v2.10.0
v2.11.0
v2.12.0
v2.9.0-26.1.0
v2.7.1-26.1.0
v2.11.0-26.1.0
v2.10.0-26.1.0
v2.12.0-26.1.0
ci-test
sync-pr28113--to-v2.9.0

评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
ascend-robotascend-robot成员
7月2日 添加了label:ci-pipeline-running
atomgit-bot
atomgit-bot
7月2日 评论:

变更摘要

本次变更为 torch.jit.ignore API 补充测试覆盖,在 test/test_jit_api.py 文件中新增测试类 TestJitIgnoreNPU,包含 7 个测试用例,覆盖忽略装饰器的告警触发、导出移除、属性配置、返回值标注、方法绑定等核心场景。

主要改动

  • 新增 TestJitIgnoreNPU 测试类:继承 JitTestCase,新增 warningsionumpyJitTestCase 的导入,为 torch.jit.ignore 相关功能提供集中测试入口。
  • test_ignore_decorator 用例:验证带 drop_on_export=True 参数的 @torch.jit.ignore 装饰器在编译时触发 "TorchScript will now drop the function" 告警,导出后忽略方法被移除且调用会触发 AttributeError,同时验证导出前后前向计算结果一致。
  • test_ignored_props 用例:验证 __jit_ignored_attributes__ 配置的忽略属性可在 @torch.jit.ignore 标注的方法内正常访问,模型可成功编译为 ScriptModule
  • test_torch_ignore_conversion_to_none 用例:验证被忽略方法无论是否显式标注 -> None 返回值类型,均能正常编译且不影响 forward 的计算结果。
  • test_no_self_arg_ignore_function 用例:验证缺少 self 参数的忽略函数在编译时会抛出包含 "does not have a self argument" 的异常。
likedislike
ascend-robot
ascend-robot成员
7月2日 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
7月2日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
7月2日 评论:

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

likedislike
ascend-robotascend-robot成员
7月2日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月2日 添加了label:docs-ci-pipeline-success
atomgit-bot
atomgit-bot
7月2日 评论:

代码审查

经过全面审查,我对该 diff 中的每项变更都进行了仔细分析,包括:

  • 逐行检查所有新增测试方法的逻辑正确性
  • 验证 nn.Bufferdevice_typeJitTestCase 等依赖项的可用性
  • 检查警告捕获、异常断言、张量比较等边界条件
  • 与上游 test/test_jit.py 中的对应测试进行交叉比对
  • 检查 Python 特有的问题(可变默认参数、is vs ==、异常吞噬、资源泄漏等)
  • 验证导入语句的完整性和正确性

结论:该 diff 未引入任何可报告的问题。 所有测试用例逻辑正确,实现了 PR 描述中声明的验证目标(test_ignore_decoratortest_ignored_propstest_torch_ignore_conversion_to_nonetest_comment_ignore_indenttest_ignored_method_bindingtest_no_self_arg_ignore_function),且与上游 PyTorch 测试风格保持一致。


审查总结

  • 已审查文件test/test_jit_api.py — 无问题
  • 发现问题数量:0(P0: 0, P1: 0, P2: 0, P3: 0)
  • 整体风险判定:低风险。变更仅涉及新增测试用例,不修改任何生产代码,测试逻辑正确且与上游保持一致。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
ascend-robotascend-robot成员
7月2日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月2日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
7月2日 评论:
流水线 PR-pipeline_pytorch#40610 [ commitID:42350275 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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_X86_Part_01 >>>
UT_X86_Part_02 >>>
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]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
Jingwei Huang
Jingwei Huang成员
7月2日 评论:

/lgtm

likedislike
chenrayray
chenrayray成员
7月6日 评论:

/approve

likedislike
ascend-robotascend-robot成员
7月6日 添加了label:approvedlgtm
ascend-robotascend-robot成员
7月6日 删除了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
7月6日 评论:

The following label is not ready.

ci-pipeline-passed: The ci-pipeline-passed label is expired. Please compile again.

likedislike
wei-pengfei22
7月6日 评论:

/compile

likedislike
wei-pengfei22
7月7日 评论:

compile

likedislike
ascend-robotascend-robot成员
7月7日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
7月7日 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
7月7日 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
7月7日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
7月7日 评论:

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

likedislike
ascend-robotascend-robot成员
7月7日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月7日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
7月7日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月7日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
7月7日 评论:
流水线 PR-pipeline_pytorch#41740 [ commitID:42350275 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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_X86_Part_01 >>>
UT_X86_Part_02 >>>
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]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
ascend-robot
ascend-robot成员
7月7日 评论:
流水线 PR-pipeline_pytorch#41740 [ commitID:42350275 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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_X86_Part_01 >>>
UT_X86_Part_02 >>>
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]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
ascend-robotascend-robot成员
7月7日 合入了pull request
ascend-robot
ascend-robot成员
7月7日 评论:
流水线 pytorch_gitcode_PR_multiVersion#11993 [ commitID:42350275 ] 已完成
likedislike