Pull Request已成功合入, 合并人@ascend-robot
(感谢 qiaoyaodan 的贡献)变更摘要
此 PR 在 transfer_to_npu 模块中新增了 _patch_triton_nvidia_driver() 函数,用于在 NPU 环境下禁用 Triton 框架对 NVIDIA CudaDriver 的激活,避免 Triton 误将 CUDA 驱动注册为活跃后端。同时补充了单元测试,验证在未安装 triton / triton_ascend 的场景下,该 patch 逻辑具备容错能力且不会影响 transfer_to_npu 的核心功能。
主要改动
- 新增
_patch_triton_nvidia_driver()函数: 在torch_npu/contrib/transfer_to_npu.py中定义该函数,通过 monkey-patch 将triton.backends.nvidia.driver.CudaDriver.is_active强制返回False,使 Triton 不再激活 NVIDIA GPU 驱动;若导入失败则静默跳过,保证兼容性。 - 将 patch 调用集成到
_init()流程: 在_init()中,_patch_triton_nvidia_driver()在_patch_has_triton等属性设置之前被调用,确保 Triton 驱动屏蔽在 transfer_to_npu 初始化阶段即生效。 - 新增
test_transfer_to_npu_works_without_triton测试用例: 在test/contrib/test_transfer_to_npu.py中添加测试,模拟 triton 未安装的环境,验证_patch_triton_nvidia_driver()可被安全调用(幂等)、transfer_to_npu仍可正常 import,且torch.cuda.is_available()、tensor.cuda()、eager 计算、nn.Module.cuda()等核心 cuda→npu 转换功能均不受影响。


代码审查
我已对两个变更文件完成了全面审查。以下是最终结论:
审查总结
共发现 2 个问题:
| 优先级 | 数量 | 说明 |
|---|---|---|
| P2 | 1 | _patch_triton_nvidia_driver 静默吞掉 is_active 赋值失败的 AttributeError,无告警日志 |
| P3 | 1 | 测试仅覆盖 triton 未安装路径,缺少 triton 已安装时 patch 生效的测试覆盖 |
各文件审查结论:
torch_npu/contrib/transfer_to_npu.py— 发现 1 个 P2 问题(_patch_triton_nvidia_driver异常静默处理)test/contrib/test_transfer_to_npu.py— 发现 1 个 P3 问题(测试覆盖缺口)
整体风险评估:低风险。 该 PR 是一个增量式修复,新增的 _patch_triton_nvidia_driver() 函数在核心逻辑上没有错误(staticmethod(lambda: False) 能正确使 triton 认为 NVIDIA driver 未激活)。主要隐患在于当 is_active 不可写时 patch 静默失败,缺少可观测性——建议在 AttributeError 分支增加一条 warnings.warn。测试方面,"triton 已安装"路径未被 CI 覆盖,建议利用 mock 补充一个验证 patch 生效的测试用例。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


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 | ✅ zichun_ye, huangjingwei (2/2) | ✅ zichun_ye (1/1) |
| torch_npu/contrib/transfer_to_npu.py | ✅ zichun_ye, huangjingwei (2/2) | ✅ zichun_ye (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
qiaoyaodan888, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Linking Issue Notice
@qiaoyaodan888 , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .


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


ascend docs pipeline is running...


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


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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_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 : 停止流水线


/lgtm


/approve


The MR can not be merged, because of this MR is work in process.
If you want to solve this problem, you can click here to do it in the FAQs.


/retry


ascend docs pipeline is running...


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




【合入来源】
【修改方案】
解决 迁移v2.10.0的aoti的社区用例,有三分之一的用例都报错RuntimeError: 2 active drivers ([<class 'triton.backends.ascend.driver.NPUDriver'>, <class 'triton.backends.nvidia.driver.CudaDriver'>]). 的问题。
报错原因:跑社区用例的时候需要import transfer_to_npu,把社区用例里面CUDA出现的地方替换成NPU,cuda的is_available也被替换成npu的is_available;3.6版本ta的_discover_backends会把所有backend都扫描进来包括nvidia和ascend,导致返回了两个可以用的backends从而报错
修改方法:打了一个patch,把TA强行打包的nvidia backend 让他不使能
【资料变更】
【接口变更】
【功能验证】
【CheckList】