Open
[test] norm_multiply_dropout前向用例与aclnn/torch示例 #234
[test] norm_multiply_dropout前向用例与aclnn/torch示例 #234
Open
penghuiyangAdd on 5 days ago
penghuiyangCollaborators
5 days ago

合入来源

问题/功能描述

norm_multiply_dropout 算子迁移(系列 PR 4/6):新增前向算子测试用例与 aclnn/torch 调用示例。

本系列 6 个 PR 基于同一基线独立拆分,每个 PR 的 diff 仅包含自身变更(均 <1000 行),建议按 P1→P6 顺序合入;目录 CMakeLists 挂载在 P5 生效,SOC 白名单与全部文档在 P6 生效。

修改方案描述

  • 新增前向 pytest 用例(含 @pytest.mark.ci 标记、set_device 默认 0):非法参数校验、dropout_ratio/eps 合法与非法边界、泛化 shape/dtype(dtype 白名单按平台自动适配)、dropout>0 前向+backward 冒烟
  • 新增 aclnn 两段式接口调用示例(float16 输入,mask 按 dropout_ratio 分支生成)与 torch 接口示例,均可通过 run_examples.sh --ops norm_multiply_dropout 自动发现执行

是否涉及UT/ST

开发自检

likedislike
Merge blocked
atomgit-bot
atomgit-bot
5 days ago Comments:

变更摘要

本 PR 为 norm_multiply_dropout 算子迁移系列的第 5 个 PR(P5),聚焦前向算子的测试与示例补齐:新增基于 pytest 的前向用例,覆盖非法参数校验、dropout_ratio/eps 合法与非法边界、dtype 与 shape 泛化组合,以及 dropout_ratio > 0 的前向冒烟验证;新增 aclnn 两段式接口调用示例与 torch 接口示例,便于 run_examples.sh --ops norm_multiply_dropout 自动发现执行;同时在 docs/zh/ops_rec_api.md 索引中补充该算子前反向说明与目录结构。本 PR 不含算子实现与构建接入改动,构建接入统一由 P6 完成。

主要改动

  • 前向 pytest 用例体系: 新增 test_norm_multiply_dropout_forward.py,实现 CPU 公式杆、NPU 融合算子、NPU 小算子三方“双标杆”比对(compute_mare/compute_mere/compute_rmse 及 compare_data_with_double_pole),并对 output/mean/var 三个输出分别校验。
  • dtype 白名单与平台适配: 以 SUPPORT_DTYPES 定义支持类型,通过 _is_ascend_950() 在 Ascend950 平台剔除 float32,其余平台补充 float32;torch_compiled 在 Python 低于 3.12 时才走 torch.compile。
  • 参数校验与边界用例: 通过 INVALID_PARAMS 参数化覆盖不支持的 dim0/dim1、非法 dropout_ratio、非法 eps、float64 及 weight/bias 维度不一致场景,均预期抛 RuntimeError;另补充非 2 维输入、维度不匹配、dim1 非 512/1024、eps 与 dropout_ratio 越界等独立用例,并验证 dropout_ratio 取 0.0/1.0 边界时可正常执行(1.0 时输出恒为 0)。
  • mask 语义与 dropout 行为校验: 用例中确认 dropout_ratio > 0 时第 4 输出 mask 为 uint8 位图且元素数为 batch * col / 8,并断言 dropout 仅影响 output 而不影响 mean/var;test_norm_multiply_dropout_graph 以图模式 fullgraph=True 调用高层接口。
  • aclnn 与 torch 调用示例: 新增 test_aclnn_norm_multiply_dropout_forward.cpp,演示 aclnnNormMultiplyDropoutGetWorkspaceSize + aclnnNormMultiplyDropout 两段式调用,x/u/weight/bias/output 用 float16、mean/var 用 float32,mask 元素数按 (batch*col+127)/128*128/8 计算并在 dropout_ratio <= 1e-10 时退化为 {1} 占位;新增 test_torch_norm_multiply_dropout_forward.py 示例,分别演示高层接口 ops_rec.ascendc.activation.norm_multiply_dropout 与底层 torch.ops.ops_rec.norm_multiply_dropout_forward 的用法差异。
  • 测试标记与发现配置: 新增 test/pytest/pytest.ini 声明 ci 与 graph 标记;主要用例均标注 @pytest.mark.ci,满足 CI 流水线识别要求。

说明:docs/zh/ops_rec_api.md 的索引文档改动未在任务中提供 patch,仅依据 PR 描述说明其补充了 norm_multiply_dropout 前反向算子说明与目录结构。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
5 days ago Comments:

代码审查

审查结论

发现清单(7 条)

P1(1 条)

  • test/pytest/test_norm_multiply_dropout_forward.py:175-179 — golden 的 mean/var 直接取分段求和结果,未除以归一化维度;既与同文件 236-237 行小算子标杆(真实 mean/var)自相矛盾,又让 compare_data_with_double_pole 的比值断言恒成立,导致 output/mean/var 三条比对都无法发现 kernel 数值错误。

P2(1 条)

  • test/pytest/test_norm_multiply_dropout_forward.py:352-359 — 泛化用例 dim0 最大 1e6、col 1024、且 fp32 输入会升到 float64 做 golden,单 case 峰值主机内存十余 GB、设备侧数 GB,典型环境会 OOM 或超时。

P3(5 条)

  • test_norm_multiply_dropout_forward.py:379-388 — 15 个新增用例(含全部非法参数校验、dropout_ratio 边界)缺 @pytest.mark.ci,CI 按标记选择时不会执行。
  • test_norm_multiply_dropout_forward.py:143-145 — two_level_sum_with_grouped 小维度分支缺 keepdim=True,与分段分支形状不一致,dim1<64 时 golden 广播错位/报错。
  • test_norm_multiply_dropout_forward.py:53-56 — torch_compiled 在 Python≥3.12 静默返回未编译模型,标了 graph 的用例退化为 eager,图模式回归无法发现。
  • examples/aclnn/test_aclnn_..._forward.cpp:113-128 — 错误分支裸 return,不释放已申请的 device 内存/stream、不 aclFinalize,与成功路径的完整释放不一致。
  • examples/aclnn/test_aclnn_..._forward.cpp:52-53 — CreateAclTensor 的 hostData 应改为 const 引用(可选改进)。

逐文件复核

  • examples/aclnn/test_aclnn_norm_multiply_dropout_forward.cpp — 已复核(2 条 P3:错误路径资源泄漏、const 引用);mask 对齐公式、strides 推导、字节数计算与 shape/host 数据一致,未发现越界或窄化问题。
  • examples/torch/test_torch_norm_multiply_dropout_forward.py — 已复核,no issues(两个接口返回值解包与参数个数正确)。
  • test/pytest/pytest.ini — 已复核,no issues(ci/graph 标记注册与用例使用一致)。
  • test/pytest/test_norm_multiply_dropout_forward.py — 已复核(1 P1 + 1 P2 + 3 P3)。

总体风险判断

整体为示例 + 用例 + ini 的新增,对生产代码无直接影响,合入风险可控;但测试侧存在一处高优先级缺陷——golden 未做归一化,使这套“双标杆”用例实际上不具备判别力,会给出虚假的正确性保证,建议与本 PR 一并修正后再合入。

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

⛔ 需要修改

likedislike
Incorrect?
ascend-robotascend-robotCollaborators
5 days ago Added label: ascend-cla/no
ascend-robot
ascend-robotCollaborators
5 days ago Comments:

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/ops-rec ❌ (0/2)(You can also ask: zhoucy998, Z_Ling, 文一晴, huhaoliang26, imhandsome1) ❌ (0/1)(You can also ask: 李雄, 黄诗磊, imhandsome1, FngSeven, tqy1998)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

huiyangpeng, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robot
ascend-robotCollaborators
5 days ago Comments:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robotCollaborators
5 days ago Added label: docs-ci-pipeline-running
ascend-robot
ascend-robotCollaborators
5 days ago Comments:

❌ 文档门禁未通过!

检查项 检查结果 详情
markdownlint ✅ 已通过 查看详情
link-validity-check ❌ 未通过 查看详情
resource-existence-check ✅ 已通过 查看详情
tag-closed-check ✅ 已通过 查看详情

💡 本次检查出 2 项错误,详情内容如下:

序号 错误详情
1 [文件路径]:docs/zh/ops_rec_api.md
[检查类型]:link-validity-check
[错误位置]:第 44 行
[错误信息]:本地路径链接无法访问
[错误内容]:../../ascendc/activation/norm_multiply_dropout/norm_multiply_dropout_forward/docs/torch_norm_multipl...
2 [文件路径]:docs/zh/ops_rec_api.md
[检查类型]:link-validity-check
[错误位置]:第 45 行
[错误信息]:本地路径链接无法访问
[错误内容]:../../ascendc/activation/norm_multiply_dropout/norm_multiply_dropout_backward/docs/torch_norm_multip...
likedislike
ascend-robotascend-robotCollaborators
5 days ago Removed label: docs-ci-pipeline-running
ascend-robotascend-robotCollaborators
5 days ago Added label: docs-ci-pipeline-failed
atomgit-bot
atomgit-bot5 days agoStart code review2
ascendc/activation/norm_multiply_dropout/norm_multiply_dropout_forward/test/pytest/test_norm_multiply_dropout_forward.py
@@ -0,0 +356,4 @@
356+ """泛化用例(迁移自原始 test_norm_multiply_double_pole 参数化维度):
357+ 大 dim0 规模遍历 × col × dtype 组合,dropout_ratio=0 双标杆比对。
358+ """
359+ run_case(dim0, col, dtype)
atomgit-bot
atomgit-bot5 days agoComments:

🟡 Medium Priority

变更行(test_norm_multiply_dropout_forward.py:352-355):dim0 参数包含 927750、DIM0_MAX(1e6),并交叉 col ∈ [512, 1024]、dtype ∈ SUPPORT_DTYPES(非 950 平台含 float32)。

影响链与失败模式:

  • 结果:典型 CI/开发机(主机内存 32~64 GB、单卡 32 GB HBM)会 MemoryError 或触发 OOM killer;即便不 OOM,42 个参数组合在大规模 float64 计算下也极易超时。该用例又未打 @pytest.mark.ci,问题只会在开发者本地全量跑 pytest 时暴露,表现为“莫名 OOM/超时”而非明确断言失败。

建议:把 dim0 ≥ 262144 的极大组合单独拆分为少量用例(固定 col=512),或在 fixture 中依据设备/主机可用内存 skip,避免 1e6×1024 在 float64 golden 下产生十余 GB 峰值内存导致 OOM/超时。

likedislike
Incorrect?
penghuiyangCollaborators
4 days ago Comments:

非问题。在当前支持的设备平台上均验证通过

atomgit-bot
atomgit-bot5 days agoStart code review1
ascendc/activation/norm_multiply_dropout/norm_multiply_dropout_forward/test/pytest/test_norm_multiply_dropout_forward.py
@@ -0,0 +176,4 @@
176+ var = two_level_sum_with_grouped((x - mean).pow(2))
177+ 
178+ std = torch.sqrt(var + EPS)
179+ x_hat = (x - mean) / std # (..., C)
atomgit-bot
atomgit-bot5 days agoComments:

🟠 High Priority

变更行(test_norm_multiply_dropout_forward.py:175-176):

求和计算

mean = two_level_sum_with_grouped(x)
var = two_level_sum_with_grouped((x - mean).pow(2))
two_level_sum_with_grouped(135-163 行)沿最后一维只做分段求和,没有任何除以 n 的归一化,但调用侧把结果当作 mean/var 使用:178-179 行 std = sqrt(var + EPS)、x_hat = (x - mean) / std。

影响链:

  1. 数学语义错误:真实 layer-norm 需要 mean = sum/n、var = sum((x-mean)^2)/n;当前 golden 的 mean 量级是真实均值的 512/1024 倍(x~U(-1,1) 时 sum 量级约 ±13,均值量级约 ±0.04),std 也不是标准差而是平方偏差和,因此 y_golden 与算子真实结果完全不同。
  2. 同文件 236-237 行的「小算子标杆」用的是 x.float().mean(dim=1) / .var(dim=1, unbiased=False)(真实均值/方差),与 golden 相互矛盾;243-244 行 compare_data_with_double_pole(msg, mean_fused.cpu(), mean_npu.cpu(), mean_golden.reshape(-1)) 把 sum 当作 golden 去和真实 mean 比较,同一断言里两个参考量本身相差约 dim1 倍,二者必有一个错——按名字与本文件用法可确认错的是 golden。
  3. 失败模式:compare_data_with_double_pole 只比较 mare_fused / mare_npu(以及 mere/rmse 的比值)是否 ≤ 阈值。由于 golden 偏差远大于算子误差,mare_fused ≈ mare_npu ≈ 1,比值恒 ≈1 必然 ≤ 5,断言恒通过;即 mean/var/output 三条比对都无法发现 kernel 的真实数值错误(例如 kernel 直接输出 0 也照样通过),用例给出虚假的正确性保证。

建议:把两次分段求和的结果除以归一化维长度(x.shape[-1],即本算子的 dim1),使 golden 的 mean/var 与 236-237 行的小算子标杆(mean/var(dim=1))语义一致;否则 mean/var/y 三条双标杆断言比值恒约等于 1,永远无法发现 kernel 数值错误。

likedislike
Incorrect?
PpenghuiyangCollaborators
5 days ago Force-pushed 1 commits:0f3d6c96-[test] add norm_multiply_dropout forward pytest cases and aclnn/torch examples with api index doc
ascend-robotascend-robotCollaborators
5 days ago Removed label: ascend-cla/no
AtlasAccount
AtlasAccountCollaborators
5 days ago Comments:

已有相同的PR在排队列表中,不再重复排队

likedislike
ascend-robotascend-robotCollaborators
5 days ago Added label: ascend-cla/yes
ascend-robot
ascend-robotCollaborators
5 days ago Comments:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robotCollaborators
5 days ago Removed label: docs-ci-pipeline-failed
ascend-robotascend-robotCollaborators
5 days ago Added label: docs-ci-pipeline-running
ascend-robot
ascend-robotCollaborators
5 days ago Comments:

❌ 文档门禁未通过!

检查项 检查结果 详情
markdownlint ✅ 已通过 查看详情
link-validity-check ❌ 未通过 查看详情
resource-existence-check ✅ 已通过 查看详情
tag-closed-check ✅ 已通过 查看详情

💡 本次检查出 2 项错误,详情内容如下:

序号 错误详情
1 [文件路径]:docs/zh/ops_rec_api.md
[检查类型]:link-validity-check
[错误位置]:第 44 行
[错误信息]:本地路径链接无法访问
[错误内容]:../../ascendc/activation/norm_multiply_dropout/norm_multiply_dropout_forward/docs/torch_norm_multipl...
2 [文件路径]:docs/zh/ops_rec_api.md
[检查类型]:link-validity-check
[错误位置]:第 45 行
[错误信息]:本地路径链接无法访问
[错误内容]:../../ascendc/activation/norm_multiply_dropout/norm_multiply_dropout_backward/docs/torch_norm_multip...
likedislike
ascend-robotascend-robotCollaborators
5 days ago Removed label: docs-ci-pipeline-running
5 event messages are collapsed here。 See more
ascend-robotascend-robotCollaborators
5 days ago Added label: ascend-cla/yes
AtlasAccount
AtlasAccountCollaborators
5 days ago Comments:
流水线 PR-pipeline_ops-rec#1089 [ commitID:96cf81e5 ] 已终止运行
⚠️ 流水线执行结果已过期,当前 PR 最新 commit 为 ce277ae8,请重新运行流水线
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 PR-build-SAST-check ✅ COMPLETED >>>
Build-2.7.1_arm ✅ COMPLETED >>>
Build-2.10.0_arm ✅ COMPLETED >>>
Build-2.7.1_X86 ✅ COMPLETED >>>
Build-2.10.0_X86 ✅ COMPLETED >>>
恶意代码检查 Antipoison ✅ COMPLETED >>>
编码安全与规范检查 pre-commit ✅ COMPLETED >>>
clang-tidy 🟨 CANCELED >>>
开源片段检查 SCA ✅ COMPLETED >>>
开发者测试 presmoke ✅ COMPLETED >>>
流水线 PR-pipeline_ops-rec 🟨 CANCELED >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员评论有效
  • compile : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
PpenghuiyangCollaborators
5 days ago Change title to "5 [test] norm_multiply_dropout前向用例/示例与索引文档", originally titled "[test] norm_multiply_dropout前向用例/示例与索引文档".
PpenghuiyangCollaborators
5 days ago Modify description of pull request
PpenghuiyangCollaborators
5 days ago Change title to "[test] norm_multiply_dropout前向用例与aclnn/torch示例", originally titled "5 [test] norm_multiply_dropout前向用例/示例与索引文档".
ascend-robot
ascend-robotCollaborators
5 days ago Comments:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robotCollaborators
5 days ago Removed label: docs-ci-pipeline-failed
ascend-robotascend-robotCollaborators
5 days ago Added label: docs-ci-pipeline-running
ascend-robot
ascend-robotCollaborators
5 days ago Comments:

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

likedislike
ascend-robotascend-robotCollaborators
5 days ago Removed label: docs-ci-pipeline-running
6 event messages are collapsed here。 See more
AtlasAccountAtlasAccountCollaborators
5 days ago Added label: ci-pipeline-passed
AtlasAccount
AtlasAccountCollaborators
5 days ago Comments:
流水线 PR-pipeline_ops-rec#1098 [ commitID:ce277ae8 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 PR-build-SAST-check ✅ COMPLETED >>>
Build-2.7.1_arm ✅ COMPLETED >>>
Build-2.10.0_arm ✅ COMPLETED >>>
Build-2.7.1_X86 ✅ COMPLETED >>>
Build-2.10.0_X86 ✅ COMPLETED >>>
恶意代码检查 Antipoison ✅ COMPLETED >>>
编码安全与规范检查 pre-commit ✅ COMPLETED >>>
clang-tidy ✅ COMPLETED >>>
开源片段检查 SCA ✅ COMPLETED >>>
开发者测试 presmoke ✅ COMPLETED >>>
流水线 PR-pipeline_ops-rec ✅ COMPLETED >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员评论有效
  • compile : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
zengxiongCollaborators5 days agoStart code review1
ascendc/activation/norm_multiply_dropout/norm_multiply_dropout_forward/test/pytest/test_norm_multiply_dropout_forward.py
@@ -0,0 +50,4 @@
50+ SUPPORT_DTYPES.append(torch.float32)
51+ 
52+ 
53+def torch_compiled(model, **kwargs):
zengxiong5 days agoComments:

[P2] torch_compiled 在 Python≥3.12 静默退化为 eager,图模式用例假通过(模式 1.12,ops-rec #74 同型复发)

问题
L53-56 def torch_compiled(model, **kwargs): if sys.version_info < (3, 12, 0): return torch.compile(...); return model——py≥3.12 时返回未编译原函数。test_norm_multiply_dropout_graph(L324 @pytest.mark.graph)经该 helper 调用,编译被静默跳过后用例仍 PASS,dynamo/Meta 注册(#233 的 NormMultiplyDropoutForwardInterMeta)零验证。

影响
本系列新增的 Meta/inductor 支持(前向 InterMeta、plugin schema)在 py≥3.12 环境(新 torch 标配)完全没有回归保护;后续改动破坏入图能力不会有任何用例失败。#235 反向 test_norm_multiply_dropout_backward.py L59-62 同款 helper 同病(两 PR 合并计 4 处图用例)。

建议
py≥3.12 分支改为 pytest.skip("torch.compile gate for py>=3.12") 显式跳过(让"未验证"可见),或直接启用编译;并在编译路径加"确实入图"证据断言,如 torch._dynamo.utils.counters["frames"]["ok"] > 0。

likedislike
zengxiongCollaborators5 days agoStart code review1
ascendc/activation/norm_multiply_dropout/norm_multiply_dropout_forward/test/pytest/test_norm_multiply_dropout_forward.py
@@ -0,0 +384,4 @@
384+ wb_dim = weight_and_bias_dim if weight_and_bias_dim is not None else col
385+ weight = torch.rand([wb_dim], device="npu", dtype=dtype)
386+ bias = torch.rand([wb_dim], device="npu", dtype=dtype)
387+ with pytest.raises(RuntimeError):
zengxiong5 days agoComments:

[P2] 13 处 pytest.raises(RuntimeError) 全部无 match=,负例校验归属不可证(模式 1.9)

问题
本文件 13 处负例(INVALID_PARAMS 10 组 + dim1/eps/ratio/dtype/mask 单点用例)均为裸 with pytest.raises(RuntimeError):,全文 match= 零命中。op_host TilingFunc 有 eps/dropout_ratio/dim0/dim1/dtype/shape 一致性/ mask numel 等多条独立校验。

影响
任何 RuntimeError(更早触发的另一条校验、aclnn 加载失败、甚至 #234 aclnn 用例数据问题)都会让负例"通过"。实例:INVALID_PARAMS 里 (1, 512, 1e-5, -0.2, ...) 意图验证 dropout_ratio 下界,若 dim0=1 先触发别的校验分支,测试同样绿——新增的 mask numel 校验(#230 L176-182)在该文件中无任何带 match 的针对性负例。

建议
逐条补 match=re.escape("...")(OPS_LOG_E 消息片段即可,如 match="dropout_ratio must in range"、match="mask numel must be"),并为 mask numel 校验补正/负各一条用例(numels/8 与 numels/8±1、ratio=0 时 numel=1 占位)。

likedislike