Merged
FusedAdam算子950实现 #8934
Tower19Add on Aug 19
FusedAdam算子950实现 #8934
Merged
Tower19Add on Aug 19
Merged fromDeleted :masterintocann/ops-nnmaster
Tower19
Tower19
Aug 19

描述

完成FusedAdam算子kernel实现

关联的Issue

待补充

测试

待补充

文档更新

更新了相关readme文档

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for Tower19's contribution.)
Tower19Tower19
Aug 19 Created pull request, commit f35d54a3
atomgit-bot
atomgit-bot
Aug 19 Comments:

变更摘要

本 PR 完成 FusedAdam 融合 Adam 优化器算子在 950(ascend950)平台上的完整实现,包含对外 aclnn 接口、算子定义与 shape/dtype 推导、tiling 切分以及 AscendC kernel。接口 aclnnFusedAdamGetWorkspaceSize/aclnnFusedAdam 将参数更新、动量更新、梯度缩放等操作融合为单个 kernel,支持 params、grads、expAvgs、expAvgSqs、maxExpAvgSqs、stateSteps 等张量列表输入,并支持 amsgrad、maximize、可选 grad_scale 与 found_inf 等特性。

主要改动

  • 新增 aclnn 对外接口:aclnn_fused_adam.cpp/h 实现 aclnnFusedAdamGetWorkspaceSize 与 aclnnFusedAdam,包含参数校验(非空、各张量列表数量与 dtype/shape 一致、lr/beta1/beta2/weightDecay/eps 合法性、amsgrad=true 时必须提供 maxExpAvgSqsRef)、可选输入 gradScaleOptional/foundInfOptional 空 tensor 视为 nullptr、stateSteps 统一 Cast 为 FLOAT、张量列表 contiguous 化并展平为 1 维,计算结束后通过 ViewCopy 将结果写回原始 tensor。
  • 新增算子定义与推导:fused_adam_def.cpp 注册 FusedAdam 算子的 8 个输入(其中 grad_scale、found_inf 为 OPTIONAL)与 lr、beta1、beta2、weight_decay、eps、amsgrad、maximize 属性,并配置 ascend910b、ascend910_93、ascend950 AICore;fused_adam_infershape.cpp 校验各输入列表 shape 一致并推导输出 shape/dtype。
  • 新增 tiling 逻辑:fused_adam_tiling.cpp 依据每个 tensor 的数据量按 32 字节块将任务切分到多个 AICore(CalcTilingData),按 dtype 设置 tiling key(FLOAT/FLOAT16/BF16),获取平台核数与 UB 大小并设置 workspace 与 SetBlockDim;fused_adam_tiling_data.h 定义了 FusedAdamTilingData 结构(含 MAX_TENSOR_CONT_950、MAX_CORE_CONT_950 等常量)。
  • 新增 AscendC kernel 实现:fused_adam_regbase.h 提供模板类 FusedAdamKernelRegbase<T>,按 tiling key 分派 FP32/FP16/BF16 路径,使用双缓冲 ping-pong 流水完成梯度缩放、weight decay、m/v 动量更新、amsgrad 的 max_exp_avg_sqs 维护及 found_inf==1 时提前返回;fused_adam_utils.h 提供 WeightDecayFP32、MaximizeAndWeightDecayFP32、UpdateMVFP32、UpdateMVMaxVFP32、UpdateParamFP32 等 SIMD 计算函数。
  • 新增示例与文档:新增 test_aclnn_fused_adam.cpp 示例,构造两组参数数据通过 aclnnFusedAdam 执行并打印更新结果;PR 描述提到更新了相关 readme 文档。
likedislike
Incorrect?
CANN-robot
CANN-robotCollaborators
Aug 19 Comments:

Hi @Tower19, welcome to submitting your first PR to ops-nn!

PR Merge Steps

1. CLA Signing

If the current PR label includes cann-cla/yes, it means you have signed the CLA and can proceed to the next step. If the label includes cann-cla/no, please sign the CLA first. If you have any questions, please refer to the FAQ.

2. CI Check

Please comment /compile to trigger the CI pipeline check. If the CI run is successful, the PR will be tagged with ci-pipeline-passed and you can proceed to the next step. If the CI run fails, the PR will be tagged with ci-pipeline-failed, please check the CI logs to fix the issues in the PR. If you have any questions, please refer to the FAQ.

3. Code Review

After CI passes, please refer to the PR Approval Progress and proactively @ the committers in the table to review the code. After approval, committers will comment /lgtm and /approve. Once the lgtm and approved labels are successfully added, the PR will be merged automatically.

likedislike
atomgit-bot
atomgit-bot
Aug 19 Comments:

代码审查

关闭总结

已逐一审查全部 9 个变更文件,报告 7 条发现:

发现统计

  • P0:0
  • P1:0
  • P2:4
  • P3:3

各文件审查结果

  • optim/fused_adam/CMakeLists.txt — 发现 1 条(P3):OPTYPE fused_adamw 与本目录实际算子 FusedAdam 不一致,疑为复制粘贴遗留;另有遗留 Debug 日志。
  • optim/fused_adam/examples/test_aclnn_fused_adam.cpp — 发现 1 条(P2):stateSteps host 数据元素数(1 个)与传入 shape 元素数(8/4 个)不符,aclrtMemcpy 发生堆越界读,且步骤值为垃圾数据导致结果不可复现。
  • optim/fused_adam/op_api/aclnn_fused_adam.cpp — 发现 2 条(P3):CheckShape 对 max_exp_avg_sqs shape 不匹配只记日志缺 return false;l0op::Cast 返回值未校验。
  • optim/fused_adam/op_api/aclnn_fused_adam.h — no issues(仅文档注释中残留 SGD 算子的描述,属注释内容,未上报)。
  • optim/fused_adam/op_api/fused_adam.cpp — 发现 1 条(P2):ADD_TO_LAUNCHER_LIST_AICORE 失败后仍返回非空输出,违反项目专项规则"失败必须返回 nullptr 终止",会走坏掉的二阶段触发 AIC Error / Core Dump。
  • optim/fused_adam/op_api/fused_adam.h — no issues。
  • optim/fused_adam/op_graph/CMakeLists.txt — no issues(add_graph_plugin_sources() 无参调用无法从 diff 确定失败模式;遗留 Debug 日志未上报)。
  • optim/fused_adam/op_host/fused_adam_def.cpp — 发现 2 条(P2):Output("params") 之后重复声明 Input("grads"),算子输入数量与 L0 接口(8 个)不一致;max_exp_avg_sqs 声明为必填 DYNAMIC 与 API 可空契约不符,导致文档化的 nullptr 可选用法在 infer-shape 必然失败。
  • optim/fused_adam/op_host/fused_adam_infershape.cpp — 上述 max_exp_avg_sqs 问题的另一侧(第 64-65 行无条件解引用),已在 def 文件条目中一并说明。

总体风险评估

本 PR 为全新算子实现(op_api + op_host + 示例 + 构建配置,kernel 不在本 diff 范围内)。最高风险点集中在 op def 的重复输入声明(可能导致算子注册/构图失败)和 max_exp_avg_sqs 必填/可选契约不一致(导致可选用法必然失败),这两点直接影响算子可用性,建议合入前优先修复;其次 L0 launcher 失败未传播违反平台专项规则,属于运行期崩溃隐患。示例代码的越界读为测试质量问题,P3 项为校验逻辑完善和构建配置核对。综合判断:存在阻塞合入的 P2 级功能性问题,需修复后合入。


审查总结

各文件审查结果

文件 结论 发现数
optim/fused_adam/op_host/fused_adam_tiling.cpp 大量编译错误 + 逻辑错误 12(P0×9、P1×1、P2×1、P3×1)
optim/fused_adam/op_kernel/CMakeLists.txt 空文件,kernel 无构建入口 1(P2)
optim/fused_adam/op_kernel/fused_adam.cpp 最烦人解析 + 未定义参数 1(P0)
optim/fused_adam/op_kernel/fused_adam_regbase.h 类名/类型/成员名大面积不一致,含伪代码函数体 8(P0×7、P1×1)
optim/fused_adam/op_kernel/fused_adam_tiling_data.h 结构体与常量一致,无问题 0(no issues)
optim/fused_adam/op_kernel/fused_adam_utils.h Adam 分母公式错误 1(P1)

总体风险判断

变更不可合并、无法构建。 共报告 24 个问题:P0×17、P1×3、P2×2、P3×1。

  • P0(编译阻断,17 个):tiling 侧类声明缺分号、context_/tempShape/idx/i/scalarType_ 等大量未声明标识符、CalcTilingData/SetTilingData 声明与定义返回类型不一致、FusedAdamCompileInfo 全仓未定义;kernel 侧类名 FusedAdamKernelRegbase 与定义名 FusedAdamKernelRegBase 不一致、FusedAdamWTilingData 类型不存在、blockIdx/LoopNum/len/grad2Buf/maxvBuf 等未声明、Process() 循环递增错误变量、ProcessSingleFP16BF16 整个函数体是不可编译的伪代码。该 PR 实际上是未完成的占位代码,host 与 device 两侧均无法编译。
  • P1(3 个):CheckStateSteps 实例数比较方向写反(正常情形恒失败)、kernel 中 vBuf 与 paramBuf 绑定同一缓冲(param 输出被 v 数据覆盖)、UpdateParamFP32 分母用加法代替除法(Adam 公式错误,早期 step 严重失真)。
  • P2(2 个):usedCoreNum_ 重算后未重新钳制到 80 导致 tiling 数组越界写(需核数>80 平台触发);op_kernel/CMakeLists.txt 为空导致 kernel 无构建目标。
  • P3(1 个):新增算子无任何测试覆盖复杂的 tiling 划分与 kernel 分支。

建议:本 PR 应打回,按上述问题逐项修复(重点是统一类名/成员名、补齐所有未声明标识符、替换伪代码函数体、修正比较方向与 Adam 公式、将 vBuf 与 paramBuf 解绑),并补全 op_kernel 构建配置与基础测试后再提交评审。

类型 数量
🔴 阻塞 21
🟡 建议 1

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 19 Added label: stat/needs-squash
338 messages are collapsed here。 See more
TangPC
TangPCCollaborators
Sep 3 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 3 Added label: lgtm
CANN-robotCANN-robotCollaborators
Sep 3 Closed associated issue
CANN-robotCANN-robotCollaborators
Sep 3 Merged pull request
CANN-robot
CANN-robotCollaborators
Sep 3 Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike