已合并
fix(jit): fix test for jit api: torch.jit.script、torch.jit.trace、torch.jit.save、torch.jit.load #32381
dinglaiping创建于 3月25日
fix(jit): fix test for jit api: torch.jit.script、torch.jit.trace、torch.jit.save、torch.jit.load #32381
已合并
dinglaiping创建于 3月25日
已删除 :fixtest-jit-api-2.7.1合入到Ascend/pytorchv2.7.1
dinglaiping成员
3月25日

【合入来源】

https://gitcode.com/Ascend/pytorch/issues/1597
请勿携带内部流程信息(需求链接、问题单、内部issue等)

  • issue/工单

【修改方案】

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

2.7.1及以上版本提示additional_module_tests、new_module_tests和module_tests找不到,要把additional_module_tests换成get_all_nn_module_tests并且删除new_module_tests和module_tests,最后在调用的时候使用get_all_nn_module_tests()。

【资料变更】

不涉及

【接口变更】

不涉及

【功能验证】

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

整体结论:
本次验证基于torch-npu 官方test_jit.py用例全量复用的方式,在完成最小化的文件、代码、环境适配后,于 NPU环境下完成torch.jit.script、torch.jit.trace、torch.jit.save、torch.jit.load四个核心 API 的全量验证,所有指定的测试方法均执行成功。验证结果表明,torch-npu 官方 JIT 用例可完整覆盖四个 API 的核心功能,使用官网用例完成 NPU 适配验证已足够;四个 API 在 NPU环境下的基础能力、协同逻辑均正常可用,完全满足 NPU 场景下模型编译优化、序列化部署、跨设备迁移、推理执行的实际业务需求。

  1. torch.jit.script:官方用例中函数重复脚本化操作正常,脚本化模型的序列化 / 反序列化无数据丢失,NPU 设备下编译后模型的参数、设备属性与原模型保持一致,编译优化逻辑正常生效;
  2. torch.jit.trace:官方用例中追踪化过程可完整保留模型的 train/eval 状态,字典类型可正常作为示例输入完成追踪,NPU 设备下追踪化模型的前向传播执行正常,输出结果与原模型一致;
  3. torch.jit.save:官方用例中模型可成功保存至 zip 压缩包格式,保存过程中可完整保留 NPU 设备信息与模型全状态(参数、缓冲区、计算图),序列化过程无数据遗漏或损坏;
  4. torch.jit.load:官方用例中可成功还原模型保存时的 NPU 设备信息,模型中自定义方法可正常导入并调用,zip 压缩包格式的模型可正确解析,加载后模型可直接在 NPU 上推理,执行结果与保存前模型一致。

运行日志:
root@hostname-fqv42:~/torchnpuapi/torch-npu/2.7.1/pytorch/test# python -m unittest test_jit.TestJit.test_restore_device test_jit.TestJit.test_import_method test_jit.TestJit.test_torch_load_zipfile_check test_jit.TestScript.test_repeated_script_on_function test_jit.TestJit.test_script_fn_pkl test_jit.TestJit.test_trace_retains_train test_jit.TestFrontend.test_dictionary_as_example_inputs_for_jit_trace -v
monkeytype is not installed. Skipping tests for Profile-Directed Typing
test_restore_device (test_jit.TestJit.test_restore_device) ... ok
test_import_method (test_jit.TestJit.test_import_method) ... ok
test_torch_load_zipfile_check (test_jit.TestJit.test_torch_load_zipfile_check) ... ok
test_repeated_script_on_function (test_jit.TestScript.test_repeated_script_on_function) ... ok
test_script_fn_pkl (test_jit.TestJit.test_script_fn_pkl) ... ok
test_trace_retains_train (test_jit.TestJit.test_trace_retains_train) ... ok
test_dictionary_as_example_inputs_for_jit_trace (test_jit.TestFrontend.test_dictionary_as_example_inputs_for_jit_trace) ... ok


Ran 7 tests in 0.104s

OK
root@hostname-fqv42:~/torchnpuapi/torch-npu/2.7.1/pytorch/test#

【CheckList】

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

  • 代码注释完备,正确记录错误日志
  • 代码实现进行了返回值、空指针等校验
  • PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
  • PR持续集成流水线(CI)执行通过,代码检查无异常
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 dinglaiping 的贡献)
Ddinglaiping成员
3月25日 创建了 pull request,commit e8e49829
Ddinglaiping成员
3月25日 关联了issue:[Bug]: 验证torch.jit.script、torch.jit.trace、torch.jit.save、torch.jit.load这4个API的时候发现官网test目录下test_jit.py文件存在错误
ascend-robot
ascend-robot成员
3月25日 评论:

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 liwei386, AACAES, yuhaiyan (3/2) liwei386 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robotascend-robot成员
3月25日 添加了label:ascend-cla/yes
dinglaiping成员
3月25日 评论:

compile

likedislike
ascend-robotascend-robot成员
3月25日 添加了label:ci-pipeline-running
ascend-robotascend-robot成员
3月25日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
3月25日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
3月25日 评论:
流水线 PR-pipeline_pytorch#12425 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
Build_ARM_inductor 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 >>>
UT_X86_Part_02 >>>
UT_ARM_A3_Part_01 >>>
UT_ARM_A3_Part_02 >>>
UT_DIST_X86_Part_01 🛑 >>>
UT_DIST_X86_Part_02 🛑 >>>
UT_DIST_X86_Part_03 🛑 >>>
UT_DIST_X86_Part_04 🛑 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
likedislike
Ddinglaiping成员
3月27日 修改标题为 “fix(jit): fix test for jit api: torch.jit.script、torch.jit.trace、torch.jit.save、torch.jit.load”,原标题为“fix(nn): fix test for jit api: torch.jit.script、torch.jit.trace、torch.jit.save、torch.jit.load”
Ddinglaiping成员
3月27日 修改了pull request 的描述
AACAES成员
3月27日 评论:

/lgtm

likedislike
haiyan8
haiyan8
3月27日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
3月27日 添加了label:lgtm
liwei386成员
3月27日 评论:

/approve

likedislike
ascend-robotascend-robot成员
3月27日 添加了label:approved
ascend-robot
ascend-robot成员
3月27日 评论:

Review Guide

This pull-request passes review.
Committers who wrote a comment of /approve are: liwei386.
Reviewers who wrote a comment of /lgtm are: liwei386, AACAES, yuhaiyan.

likedislike
ascend-robotascend-robot成员
3月27日 合入了pull request