Merge blocked
变更摘要
本 PR 为 torch_npu.npu_bert_apply_adam(..., out=(var, m, v)) 增加 Ascend950 的 ApplyAdamV2 ACLNN 通路:在算子配置中为 npu_bert_apply_adam 及其 .out 变体新增 op_api 注册,使公开 Python 接口可以进入新增的 op_api 适配层;适配层按设备分流,非 IsAclnnOnly() 设备继续走原有 acl_op::npu_bert_apply_adam_out,950 等设备调用自动生成的 aclnnApplyAdamV2,并通过 DO_COMPATIBILITY 在缺少 ACLNN 符号时给出明确报错。同时补充了通过公开 Python API 的回归用例,保持公开签名、默认参数、out 调用方式与原地更新语义不变。
主要改动
- 算子配置注册
op_api通路: 在op_plugin/config/op_plugin_functions.yaml中为npu_bert_apply_adam与npu_bert_apply_adam.out各增加op_api: all_version,使两个接口可以分发到新增的 op_api 实现。 - 新增
BertApplyAdamKernelNpuOpApi.cpp适配层: 新增op_api::npu_bert_apply_adam_out,先用DO_COMPATIBILITY检查aclnnApplyAdamV2符号是否可用,再在!c10_npu::IsAclnnOnly()时回退到acl_op::npu_bert_apply_adam_out,保证 910/A2/A3 等设备通路不变。 - Scalar 转 Tensor 及模式映射: 将
lr、beta1、beta2、epsilon、max_grad_norm、global_grad_norm、weight_decay通过copy_scalar_to_device转换为与var相同 dtype/device 的 Tensor,并把adam_mode == 0映射为"adam"、其余映射为"mbart_adam",最后经EXEC_NPU_CMD调用aclnnApplyAdamV2并返回var/m/v别名。 step_size=None的缺省处理: 未提供step_size时以lrTensor 作为默认输入,兼顾 Adam 模式忽略该输入与 mBART 以 lr 作为默认步长,同时满足旧版 tiling 对 optional 输入 shape 的检查。- 保留不带
out的重载报错行为: 新增的op_api::npu_bert_apply_adam仍直接抛出npu_bert_apply_adam is not implemented for Tensor,维持原有非 out 调用报错语义。 - 新增公开 API 回归用例: 在
test/test_base_ops/test_npu_bertapplyadam.py中增加覆盖默认参数、Adam/mBART 与非零模式映射、权重衰减、梯度裁剪、显式/缺省step_size、var/m/v原地更新与返回值地址一致性的用例,并对 mBART 缺省步长用例标注@SupportedDevices(["Ascend950"])。


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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
PR Approval Progress
⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-Ascend/op-plugin | ❌ (0/2)(You can also ask: guoqi1024, shen_weiling, renyujin, huangjingwei, liangsongwei) | ❌ (0/1)(You can also ask: 褚博宁, chujinjin, li_jing_hw, wanglijun55, zyw-hw) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
yourealize, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Linking Issue Notice
@yourealize , 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 .


ascend docs pipeline is running...


/check-pr


The following labels are not ready.
lgtm: Please wait for reviewers to review the code.
approved: Please wait for committers to review the code.
ci-pipeline-passed: Please wait for the CI test to be completed.
The following label exists.
needs-issue: 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.


compile


| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


ascend docs pipeline is running...


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


compile


| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


ascend docs pipeline is running...


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


ascend docs pipeline is running...


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


【合入来源】
为
torch_npu.npu_bert_apply_adam(..., out=(var, m, v))增加 Ascend950 的 ApplyAdamV2 ACLNN 通路。配套 ops-nn PR:https://gitcode.com/cann/ops-nn/pull/14078 。部署到 950 时,需要 CANN/自定义算子包提供该 PR 自动生成的
aclnnApplyAdamV2及aclnnApplyAdamV2GetWorkspaceSize符号。【修改方案】
npu_bert_apply_adam和.out注册op_api: all_version,使公开 Python 接口能够进入新增的 op_api 适配层。IsAclnnOnly()设备(包括 910/A2/A3)仍调用原acl_op::npu_bert_apply_adam_out;即使新 ACLNN 符号可用,也保持原通路。IsAclnnOnly()设备执行aclnnApplyAdamV2。DO_COMPATIBILITY检查符号;950 缺少 ACLNN 接口时给出明确错误,旧设备允许继续使用原 ACL 通路。var相同 dtype/device 的 Tensor,适配自动生成的 ACLNN ABI;adam_mode == 0映射为adam,其余值映射为mbart_adam。step_size=None时使用lrTensor:Adam 模式忽略该输入,mBART 使用 lr 作为默认步长,同时兼容旧版 tiling 对 optional 输入 shape 的检查。var/m/v的原地更新和返回别名;不带out的重载维持原有报错行为。调用关系:
【资料变更】
不涉及独立资料变更;通路和兼容设计记录于本 PR 的修改方案。
【接口变更】
公开 Python 签名、默认参数、
out=(var, m, v)调用方式及原地更新语义保持兼容。跨仓新增对 ops-nn 自动生成的
aclnnApplyAdamV2接口的调用,配套 PR 见上文。【功能验证】
环境:Python 3.12、PyTorch 2.7.1、源码构建的 TorchNPU 2.7.1.post11、CANN 9.2、Ascend950PR_9579。
test/test_base_ops/test_npu_bertapplyadam.py:mbart_adam + step_size=None的 FP16/FP32 用例均通过。npu_bert_apply_adam_out exec aten和aclnnApplyAdamV2 EXEC_NPU_CMD,并确认加载了新构建的 TorchNPU 库及任务专用 ACLNN 接口库。Current device only support aclnn operators, but aclnnApplyAdamV2 or aclnnApplyAdamV2GetWorkspaceSize not found。git diff --check通过。验证范围:上述测试使用本机 CANN 9.2 已安装的 kernel,未重新构建当前 ops-nn 工作树 kernel;910/A2/A3 的原通路通过代码检查保留,未作对应硬件实测。
CI 修正(2026-11-11)
原流水线 8 个 x86/ARM 编译任务全部通过,失败发生在 Ascend910B4 的 Python UT:新增通用数值测试有 4 个子用例断言失败,原有两个用例和 requires_out 用例通过。
本次仅修正测试范围:
依据:对照 legacy TBE kernel 源码,mBART 衰减分支的临时 buffer 会被后续 lrvar 覆盖,与 950 的 weight_decaylr*var 计算不同,不能向旧 ACL kernel 施加 950 专有数值预期。原日志未打印实际数值,且两个 Adam 子用例标签相同,不能据此断言具体是哪一个 step_size 场景失败。
验证:本机 Ascend950 上重新执行完整测试文件,
Ran 5 tests in 0.436s / OK,0 失败、0 错误、0 跳过;16 组 FP16/FP32 数值子用例均通过,执行日志仍确认进入 aclnnApplyAdamV2。runtime、schema 和精度阈值保持本 PR 原实现。旧设备结果由本次重新触发的 CI验证。【CheckList】
AI/Agent 生成声明:本 PR 由 AI 辅助编写,已完成上述本机编译与通路验证。