已合并
torch.nn.functional.layer_norm calls aclnnLayerNorm only on A5 #5473
chengpeng25创建于 7月14日
torch.nn.functional.layer_norm calls aclnnLayerNorm only on A5 #5473
已合并
chengpeng25创建于 7月14日
chengpeng25成员
7月14日

【合入来源】

#283
请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改方案】

A5只走aclnnLayerNorm分支

【资料变更】

不涉及

【接口变更】

不涉及

【功能验证】

开启和关闭兼容性环境变量,A5走的是aclnnLayerNorm
image.png

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 chengpeng25 的贡献)
Cchengpeng25成员
7月14日 创建了 pull request,commit 6328a381
atomgit-bot
atomgit-bot
7月14日 评论:

变更摘要

此 PR 修改了 LayerNormKernelNpuOpApi.cpp 中的算子路由逻辑:在原有 CheckCompatibleImpl() 判断基础上,新增 c10_npu::IsAclnnOnly() 条件,使 A5 等仅支持 ACLNN 的硬件上强制走 aclnnLayerNorm 而非 aclnnFastLayerNorm。同时新增测试用例 test_layer_norm_compatible_impl_switch,覆盖 use_compatible_impl 开关在 True/False 两种模式下的正确性验证。

主要改动

  • LayerNormKernelNpuOpApi.cpp 条件判断扩展: native_layer_norm 中调用 aclnnLayerNorm 的条件从 if (!layer_sc) 改为 if (!layer_sc || c10_npu::IsAclnnOnly()),确保在 AclnnOnly 模式下绕过 aclnnFastLayerNorm 直接使用标准 aclnnLayerNorm
  • 新增测试函数 test_layer_norm_compatible_impl_switch: 在 test_layer_norm.py 中新增 @SkipIfNotGteCANNVersion("9.0.0") 装饰的测试,覆盖 float32/float16 及多种 shape,分别验证 use_compatible_impl(False)use_compatible_impl(True) 两种路径下 layer_norm 输出的正确性。
likedislike
atomgit-bot
atomgit-bot
7月14日 评论:

代码审查

经过对两个变更文件的仔细审查,我没有发现任何需要报告的问题。

审查详情:

op_plugin/ops/opapi/LayerNormKernelNpuOpApi.cpp(第 97 行)

  • 变更:将条件从 if (!layer_sc) 改为 if (!layer_sc || c10_npu::IsAclnnOnly())
  • c10_npu::IsAclnnOnly() 是代码库中广泛使用的标准函数(在 MaxPool2dWithIndicesGeluBackwardApplyAdamNormKernel 等多个文件中均有使用)
  • 逻辑与 PR 目标一致:在 A5(aclnn-only)设备上,始终走 aclnnLayerNorm 分支;在非 A5 设备上,行为完全取决于兼容模式,与原逻辑等效
  • 两条分支均使用 aclnnLayerNormaclnnFastLayerNorm,均为 aclnn API,语义一致,无功能回归风险

test/test_base_ops/test_layer_norm.py(第 116–145 行)

  • 新增的 test_layer_norm_compatible_impl_switch 测试方法:
    • 正确使用了 @SkipIfNotGteCANNVersion("9.0.0") 装饰器,与文件中已有测试保持一致
    • 测试数据涵盖了 float32 和 float16 两种数据类型及多种 shape
    • CPU 参考值计算逻辑正确(float16 场景下先用 float32 计算再转换回 float16,与已有测试 test_layer_norm_float16_format 模式一致)
    • 每次迭代中显式设置 use_compatible_impl(False/True),并在迭代结束时恢复到 True 状态,不会影响后续测试
    • 使用 assertRtolEqual 进行数值比对,对两种精度均有合适容差

总体评估:变更风险低,逻辑正确,与代码库风格一致。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
ascend-robotascend-robot成员
7月14日 添加了label:ascend-cla/yes
此处折叠了77条消息 查看更多
ascend-robotascend-robot成员
7月15日 关闭了关联的issue
ascend-robotascend-robot成员
7月15日 合入了pull request
Cchengpeng25成员
7月15日 修改了pull request 的描述
Cchengpeng25成员
7月15日 修改了pull request 的描述
Cchengpeng25成员
7月15日 修改了pull request 的描述