已合并
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
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 bobebest 的贡献)ascend-robot
6月16日 评论:
6月16日 评论:
6月16日 添加了label:ascend-cla/yes
ascend-robot
6月16日 评论:
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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


此处折叠了240条消息 查看更多
7月10日 删除了label:ci-pipeline-running
7月10日 添加了label:ci-pipeline-passed
ascend-robot
7月10日 评论:
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 | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


7月10日 合入了pull request
ascend-robot
7月10日 评论:
7月10日 评论:
流水线 pytorch_gitcode_PR_multiVersion#12360 [ commitID:71e9dff7 ] 已完成


【合入来源】
【修改方案】
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是否递归访问所有嵌套子模块torch.jit.ScriptModule.apply是否返回自身以支持链式调用【资料变更】
【接口变更】
【功能验证】
【CheckList】