已合并
add test_nn upstream npu patch #35671
dinglaiping创建于 5月14日
add test_nn upstream npu patch #35671
已合并
从已删除 :addpatchfiles-2.11.0合入到Ascend/pytorchv2.11.0
Pull Request已成功合入, 合并人@ascend-robot
(感谢 dinglaiping 的贡献)5月14日 创建了 pull request,commit 2ea23429
ascend-robot
5月14日 评论:
5月14日 评论:
ascend-robot
5月14日 评论:
5月14日 评论:
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_upstream | ✅ 李伟, huangjingwei (2/2) | ✅ 李伟 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies 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. 👍


5月14日 添加了label:ascend-cla/yes
dinglaiping
5月14日 评论:
5月14日 评论:
compile


ascend-robot
5月14日 评论:
5月14日 评论:
当前仓库存在以下 保护分支 :
| Protected Branch | Version | Release |
|---|---|---|
| master | ||
| v2.7.1 | ||
| v2.9.0 | ||
| v2.11.0 | ||
| v2.10.0 | ||
| v2.12.0 |
评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


5月14日 添加了label:ci-pipeline-running
ascend-robot
5月14日 评论:
5月14日 评论:
流水线 PR-pipeline_pytorch#22183 运行中
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | 🕚 | >>> |
| Build_ARM | 🕚 | >>> | |
| Build_LibTorch_x86 | 🕚 | >>> | |
| Build_LibTorch_ARM | 🕚 | >>> | |
| Build_X86_torchair | 🕚 | >>> | |
| Build_ARM_torchair | 🕚 | >>> | |
| patch_test | 🕚 | >>> | |
| 恶意代码检查 | 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_ARM_A2_Part_01 | 🕚 | >>> | |
| UT_ARM_A2_Part_02 | 🕚 | >>> | |
| UT_ARM_A2_Part_03 | 🕚 | >>> | |
| UT_inductor_Part_01_pool | 🕚 | >>> | |
| UT_inductor_Part_02_pool | 🕚 | >>> | |
| UT_inductor_Part_03_pool | 🕚 | >>> | |
| UT_inductor_Part_04_pool | 🕚 | >>> | |
| UT_DIST_ARM_Part_01 | 🕚 | >>> | |
| UT_DIST_ARM_Part_02 | 🕚 | >>> | |
| UT_DIST_ARM_Part_03 | 🕚 | >>> | |
| UT_DIST_ARM_Part_04 | 🕚 | >>> | |
| 流水线 | PR-pipeline_pytorch | 🕚 | >>> |


ascend-robot
5月14日 评论:
5月14日 评论:
Ascend docs pipeline is running...


5月14日 添加了label:docs-ci-pipeline-running
ascend-robot
5月14日 评论:
5月14日 评论:
5月14日 删除了label:docs-ci-pipeline-running
5月14日 添加了label:docs-ci-pipeline-success
ascend-robot
5月14日 评论:
5月14日 评论:
Ascend docs pipeline is running...


5月14日 删除了label:docs-ci-pipeline-success
5月14日 添加了label:docs-ci-pipeline-running
ascend-robot
5月14日 评论:
5月14日 评论:
5月14日 删除了label:docs-ci-pipeline-running
5月14日 添加了label:docs-ci-pipeline-success
5月14日 删除了label:ci-pipeline-running
ascend-robot
5月14日 评论:
5月14日 评论:
流水线 PR-pipeline_pytorch#22183 已完成
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | ✅ | >>> | |
| 恶意代码检查 | 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_ARM_A2_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Part_02 | ✅ | >>> | |
| UT_ARM_A2_Part_03 | ✅ | >>> | |
| UT_inductor_Part_01_pool | 🛑 | >>> | |
| UT_inductor_Part_02_pool | 🛑 | >>> | |
| UT_inductor_Part_03_pool | 🛑 | >>> | |
| UT_inductor_Part_04_pool | 🛑 | >>> | |
| UT_DIST_ARM_Part_01 | 🛑 | >>> | |
| UT_DIST_ARM_Part_02 | 🛑 | >>> | |
| UT_DIST_ARM_Part_03 | 🛑 | >>> | |
| UT_DIST_ARM_Part_04 | 🛑 | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |


5月14日 添加了label:ci-pipeline-passed
5月15日 修改了pull request 的描述
Jingwei Huang
5月18日 评论:
5月18日 评论:
/lgtm


5月18日 添加了label:approvedlgtm
ascend-robot
5月18日 评论:
5月18日 评论:
Review Guide
This pull-request passes review.
Committers who wrote a comment of /approve are: 李伟.
Reviewers who wrote a comment of /lgtm are: 李伟, huangjingwei.


5月18日 合入了pull request
【合入来源】
【修改方案】
2.9 2.10 2.11版本版本新增 test/test_nn.py 的 upstream NPU 适配 patch。此前这些版本在 torch-npu 中没有对应的 test_nn.py.patch,因此 test_nn.py 中部分 nn.Module 基础能力测试、CUDA-only 测试以及 CUDA 设备假设逻辑无法在 NPU 后端上有效覆盖或运行。
本次新增的 patch 主要包含以下修改:
新增:
import torch_npu
from torch_npu.contrib import transfer_to_npu
这样可以在 upstream 测试中启用 torch-npu 相关兼容逻辑,使部分 CUDA 风格的测试代码能够迁移到 NPU 后端执行。
新增:
if (acc := torch.accelerator.current_accelerator()) and acc.type == "npu":
torch.cuda.get_device_capability = lambda *args, **kwargs: (0, 0)
torch.cuda.is_tf32_supported = lambda: False
原因是 test_nn.py 中存在 CUDA capability 和 TF32 相关判断。NPU 环境下这些 CUDA/TF32 能力不适用,如果不做兼容,
测试可能会错误进入 CUDA/TF32 分支,或者因访问 CUDA 专有能力接口导致异常。这里在 NPU 环境下显式关闭 TF32 能力判
断,避免生成或执行不适合 NPU 的测试路径。
本次把大量原本默认在 CPU 上构造的 module、parameter、buffer 和 tensor 显式迁移到 NPU,例如:
nn.Linear(...).npu()
nn.Conv2d(...).npu()
nn.BatchNorm2d(...).npu()
torch.rand(..., device="npu")
torch.empty(..., device="npu")
nn.Parameter(torch.randn(..., device="npu"))
Buffer(torch.rand(..., device="npu"))
覆盖的测试类型包括:
这样可以确保这些基础 nn.Module 行为不仅在 CPU tensor 上被验证,也能覆盖 NPU tensor/module 在参数注册、buffer
管理、容器模块、state_dict 和属性赋值等场景下的行为。
移除了部分 @onlyCUDA 限制,涉及:
这些测试本身并不是只适用于 CUDA,去掉 @onlyCUDA 后可以让 PyTorch device-type 测试框架生成 NPU 版本,从而补充
NPU 后端覆盖。
将原来的 CUDA 判断:
if self.device_type == "cuda":
input = input.cuda()
mask = mask.cuda()
改为:
input = input.to(device)
mask = mask.to(device)
这样测试逻辑不再绑定 CUDA,可以在 NPU device-type 测试中使用相同逻辑运行。
例如将:
self.assertTrue(mw._base[0][0].device.type == "cuda")
修改为:
self.assertTrue(mw._base[0][0].device.type == torch.device(device).type)
这样断言表达的是“module 参数迁移到了目标 device”,而不是固定要求目标设备必须是 CUDA,从而适配 NPU 后端。
修改原因
2.9、2.10、2.11 原本没有 test_nn.py 的 torch-npu upstream patch,导致以下问题:
本次 patch 的目标是:在尽量保持 upstream 测试语义不变的前提下,只调整设备放置和设备判断逻辑,让这些用例能够真
实覆盖 NPU 后端。
影响范围
【资料变更】
“不涉及”
【接口变更】
“不涉及”
【功能验证】
执行修改过的用例命令是:
日志如下
【CheckList】