已合并
add test for add_module and apply api in torch.jit.ScriptModule #38690
bobebest创建于 6月16日
add test for add_module and apply api in torch.jit.ScriptModule #38690
已合并
bobebest创建于 6月16日
bobebest
6月16日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)
https://gitcode.com/Ascend/pytorch/issues/1977

【修改方案】

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

torch.jit.ScriptModule.add_module和torch.jit.ScriptModule.apply:已在 PyTorch 官方社区搜索 ,无针对该 API 的专门测试用例,因此自行编写测试用例。
Torch.jit.ScriptModule各属性介绍:把一个普通的 Python 模型(nn.Module)编译成 TorchScript 模型(ScriptModule),让它可以脱离 Python、在 NPU/C++/ 推理引擎上跑。

API介绍****

torch.jit.ScriptModule.add_module是 PyTorch TorchScript 中用于动态添加子模块的核心方法,该子模块可以使用给定的名称作为属性进行访问;
语法:add_module(name, module)
参数:
name(str):子模块的名称,可以是任意字符串
module(torch.nn.Module 或 None):要添加的子模块实例;传入 None 时相当于删除该名称对应的子模块
**返回值:**None
核心特性:
1、TorchScript 静态性约束:只有 add_module 能让 TorchScript 识别并追踪新加入的子模块、参数和缓冲区;
2、动态网络构建:适用于运行时才决定网络结构的场景(如循环添加多层、条件分支添加模块);
3、状态管理:加入的子模块会被纳入 parameters()、state_dict()、to(device)、train()/eval() 等管理体系。

torch.jit.ScriptModule.apply是 PyTorch TorchScript 静态图模块 的递归遍历函数,递归遍历模块自身 + 所有子模块,对每一个模块执行传入的函数。
**语法:**apply(fn)
参数:
fn(Callable[[torch.nn.Module], None]):一个函数,接受一个 Module 实例作为输入,返回 None。该函数会被递归地应用到模块自身和每一个子模块上
返回值:self(即调用 apply 的 ScriptModule 实例本身)
核心特性如下
1、递归执行:深度优先遍历所有子模块
2、原地修改:直接修改模块,不返回新模型
3、顺序固定:先处理自身,再处理子模块
4、TorchScript 安全:可在静态图中正常使用
5、生命周期统一管理:参数、设备、模式全部生效
具体测试内容如下:

测试维度 具体内容
功能正确性 torch.jit.ScriptModule.add_module 能否正确添加子模块;torch.jit.ScriptModule.apply 能否正确修改参数
递归行为 torch.jit.ScriptModule.apply 是否递归访问所有嵌套子模块
类型一致性 添加的子模块是否被转换为 ScriptModule 类型
设备适配 所有参数和张量在 NPU 设备上的 device type 是否正确
前向推理 修改后的模型能否正常执行前向传播
返回值语义 torch.jit.ScriptModule.apply 是否返回自身以支持链式调用

【资料变更】

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

【接口变更】

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

【功能验证】

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

【CheckList】

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

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 bobebest 的贡献)
Bbobebest
6月16日 创建了 pull request,commit 1634106e
Bbobebest
6月16日 关联了issue:[Usage]: test目录下torch.jit.ScriptModule.add_module和torch.jit.ScriptModule.apply的测试用例缺失
ascend-robotascend-robot成员
6月16日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
6月16日 评论:

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

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

likedislike
此处折叠了240条消息 查看更多
ascend-robotascend-robot成员
7月10日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月10日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
7月10日 评论:
流水线 PR-pipeline_pytorch#43030 [ commitID:71e9dff7 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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, hhz0]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
ascend-robotascend-robot成员
7月10日 合入了pull request
ascend-robot
ascend-robot成员
7月10日 评论:
流水线 pytorch_gitcode_PR_multiVersion#12360 [ commitID:71e9dff7 ] 已完成
likedislike