已关闭
feat/fix/docs/style/refactor/adaptor/chore/test(backend): pr describe #98
SovLyn创建于 7月12日关闭于 7月24日
feat/fix/docs/style/refactor/adaptor/chore/test(backend): pr describe #98
已关闭
SovLyn创建于 7月12日关闭于 7月24日
已删除 :master合入到Ascend/MindSpeed-Opsmaster
SovLyn成员
7月12日

What this PR does / why we need it?

Adds the Triton-Ascend implementation for mamba3_siso_bwd_kernel, including the production API, arch32 kernel, arch35 fail-fast stub, PR-local test references, ATK wrapper/generator/config, unit tests, a self-check script, and reviewer-facing documentation.

Does this PR introduce any user-facing change?

Yes. It adds a new MindSpeed-Ops Triton backend implementation and reviewer-facing docs for mamba3_siso_bwd_kernel.

How was this patch tested?

  • python tests/atk_tests/triton/mamba3_siso_fwd_kernel/self_check_mamba3_siso_bwd.py --quick
  • python tests/atk_tests/triton/mamba3_siso_fwd_kernel/self_check_mamba3_siso_bwd.py
  • pytest -q tests/unit_tests/triton/test_mamba3_siso_bwd.py
    The self-check uses PR-local torch_npu.profiler.kernel_details.csv active-window timing and PR-local baseline/reference files only.
likedislike
当前Pull Request已关闭, 关闭人@SovLyn
atomgit-bot
atomgit-bot
7月12日 评论:

变更摘要

该 PR 为 MindSpeed-Ops 的 Triton-Ascend 后端新增了 mamba3_siso_bwd_kernel 的完整实现,包括生产级 API、arch32 上的三个 Triton kernel、arch35 的 fail-fast 桩代码、ATK 封装/生成器/配置、单元测试、自检脚本以及面向审阅者的文档。核心功能覆盖 Mamba-3 反向传播中的旋转编码梯度、偏置梯度和状态梯度计算,支持 GQA(分组查询注意力)和变长序列。

主要改动

  • 新增生产 APImindspeed_ops/api/triton/mamba3_siso_bwd.py 中导出 compute_dqkthetacompute_ddt_dtrap_dinput_states 两个公开函数,通过 @input_guardis_arch35() 守卫,在 arch35 上直接抛出 NotImplementedError,在 arch32 上委托给内核实现。

  • 新增三个 Triton kernelmindspeed_ops/arch32/triton/mamba3/mamba3_siso_bwd_impl.py 中实现了 mamba3_siso_bwd_kernel_rotary_bias_angles(旋转编码与偏置的梯度计算,采用偶/奇列分离避免 NPU 上 tl.split/tl.join)、mamba3_siso_bwd_kernel_dk_state_post(通过 tl.atomic_addd_ok_state 贡献累加到 dKdK_biasdAngles)和 mamba3_siso_bwd_kernel_ddt_dtrap_dinput_states(计算 dDTdTrap 及输入状态梯度),并包含对应的 host dispatch 函数。

  • 新增 Triton 工具函数mindspeed_ops/arch32/triton/mamba3/mamba3_utils.py 提供 cos_approxsin_approx(小角度旋转编码的多项式近似)、sigmoid_approxsilu 四个 JIT 辅助函数,供 kernel 内部调用。

  • 新增 arch35 桩代码mindspeed_ops/arch35/triton/mamba3/mamba3_siso_bwd_impl.py 为占位实现,API 层在检测到 arch35 时直接报错,确保架构不支持时明确失败而非静默回退。

  • 新增测试与自检体系:包含 tests/unit_tests/triton/test_mamba3_siso_bwd.py(pytest 精度单测)、tests/atk_tests/triton/mamba3_siso_bwd/self_check_mamba3_siso_bwd.py(静态无 fallback 检查 + 实时精度自证报告)、reference_impl.py(torch 参考实现)、triton_mamba3_siso_bwd.py(ATK baseline/candidate 封装)、generate_mamba3_siso_bwd.py(ATK 用例生成器)和 mamba3_siso_bwd.yaml(ATK 配置),覆盖 fp16/bf16 多组 shape 组合。

likedislike
不准确?
atomgit-bot
atomgit-bot
7月12日 评论:

代码审查

All files have been reviewed. Let me now provide the closing summary.


审查总结

已审查文件(共 12 个)

文件 审查结果
docs/triton/mamba3_siso_bwd.md 无问题(文档一致,无安全隐患)
mindspeed_ops/api/triton/mamba3_siso_bwd.py 无问题(arch35 guard 和参数转发逻辑正确)
mindspeed_ops/arch32/triton/mamba3/mamba3_siso_bwd_impl.py P3: d_ok_state 路径未经测试
mindspeed_ops/arch32/triton/mamba3/mamba3_utils.py P3: silu 函数死代码
mindspeed_ops/arch35/triton/mamba3/__init__.py 无问题(仅包声明)
mindspeed_ops/arch35/triton/mamba3/mamba3_siso_bwd_impl.py 无问题(arch35 stub,行为正确)
tests/atk_tests/triton/mamba3_siso_bwd/generate_mamba3_siso_bwd.py P3: CASES 包含不支持的 headdim_qk=128
tests/atk_tests/triton/mamba3_siso_bwd/mamba3_siso_bwd.yaml 无问题(ATK 配置键名正确)
tests/atk_tests/triton/mamba3_siso_bwd/reference_impl.py 无问题(reference 逻辑与生产实现一致)
tests/atk_tests/triton/mamba3_siso_bwd/self_check_mamba3_siso_bwd.py P2: tensor 名称标签错位 + dscale/dgamma 未测试;P3: 死 import;P3: 容差与文档不一致
tests/atk_tests/triton/mamba3_siso_bwd/triton_mamba3_siso_bwd.py 无问题(ATK wrapper 正确调用生产 API)
tests/unit_tests/triton/test_mamba3_siso_bwd.py P2: tensor 名称标签错位 + dscale/dgamma 未测试

按优先级统计

  • P0: 0
  • P1: 0
  • P2: 2(tensor 名称标签错位导致 dscale/dgamma 未测试 × 2 个文件,实际为同一根因)
  • P3: 5(silu 死代码、死 import、容差不一致、headdim_qk=128 在 ATK generator、d_ok_state 路径未测试)

整体风险判断

中等风险。核心的生产 kernel 实现(rotary、dk_state_post、ddt_dtrap)逻辑正确,与 reference 一致。主要问题集中在测试基础设施:compute_dqktheta 返回的 dscale 和 dgamma 两个核心输出在单元测试和自证脚本中完全未测试(受 actual[:5] 切片与名称列表错位影响),且自证报告中展示的 "dscale"/"dgamma" 精度数据实际来自 dk_bias/dangles 的比较结果,存在虚假信心。建议优先修复测试代码中的名称列表对齐问题并补齐 dscale/dgamma 的精度校验。

类型 数量
🔴 阻塞 0
🟡 建议 5

💬 仅评论

likedislike
不准确?
ascend-robotascend-robot成员
7月12日 添加了label:ascend-cla/no
ascend-robot
ascend-robot成员
7月12日 评论:

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

⚠️ 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
docs ❌ (0/2)(You can also ask: LinShua, 朱彦儒, 刘哲续, gitcode_lhp, zjchenn) ❌ (0/1)(You can also ask: 华郁秀, 刘荣, 孙银磊, 朱彦儒, 郑加利)
repo-Ascend/MindSpeed-Ops ❌ (0/2)(You can also ask: zheliuyu, 华郁秀, 郑加利, guihaowen666, 周蓓蓉) ❌ (0/1)(You can also ask: 雷珍珍, bigdog1206, 郑加利, 孙银磊, 王晓歆)

💡 Tip:

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

CLA Signature Guide

@SovLyn , thanks for your pull request.

The following commit(s) have not signed Contributor License Agreement (CLA).

Commit Reason
935a53f1 feat: add mamba3_siso_bwd triton... it's an invalid email! please check it.

If you need to sign CLA, you can click here to sign the CLA.

If you need to check if the email is set up correctly, you can click here to do it in the FAQs.

After signing the CLA or updating the email, you must comment /check-cla to check the CLA status again.

likedislike
atomgit-bot
atomgit-bot7月12日进行代码检视1
tests/atk_tests/triton/mamba3_siso_bwd/generate_mamba3_siso_bwd.py
@@ -0,0 +39,4 @@
39+ seqlen = random.choice([32, 64, 96, 128, 160])
40+ nheads_qk = random.choice([1, 2, 4])
41+ nheads = random.choice([h for h in [1, 2, 4, 8] if h % nheads_qk == 0])
42+ headdim_qk = random.choice([64, 128])
atomgit-bot
atomgit-bot7月12日评论:

🟡 Medium Priority

changed line: generate_mamba3_siso_bwd.py 第 42 行 headdim_qk = random.choice([64, 128])

affected behavior: 当 _case_pool 耗尽后,after_case_config 的 else 分支通过随机采样生成用例,其中 headdim_qk 有 50% 概率选到 128。

failure mode: 与 _build_pool 中未过滤 headdim_qk=128 的问题相同——文档明确声明 headdim_qk=128 在 NPU 上因 UB 溢出暂不支持。随机生成的 headdim_qk=128 用例同样会导致 kernel 崩溃或错误结果。

suggested fix: 将第 42 行改为 headdim_qk = 64,与 _build_pool 的修复保持一致。

建议:将第 42 行改为 headdim_qk = 64,与文档中"当前 PR 覆盖 headdim_qk=64"的声明一致。

likedislike
不准确?
atomgit-bot
atomgit-bot7月12日进行代码检视1
tests/atk_tests/triton/mamba3_siso_bwd/mamba3_siso_bwd.yaml
@@ -0,0 +160,4 @@
160+ valid:
161+ values: [ 32, 64 ]
162+ invalid:
163+ values: [ 32, 64 ]
atomgit-bot
atomgit-bot7月12日评论:

🟡 Medium Priority

变更内容:mamba3_siso_bwd.yaml 仅配置了 torch_mamba3_siso_bwd_dqktheta / triton_mamba3_siso_bwd_dqktheta 测试用例(通过 nametriton_namegenerate 字段),但 triton_mamba3_siso_bwd.py 中还注册了 torch_mamba3_siso_bwd_ddttriton_mamba3_siso_bwd_ddt 两个测试类,且 generate_mamba3_siso_bwd.py 中已实现对应的 generate_mamba3_siso_bwd_ddt 生成器。缺少对应的 YAML 配置意味着 ATK 框架无法通过 YAML 驱动的方式运行 ddt 测试,该路径的 ATK 测试覆盖为空。虽然 ddt 路径在单元测试和 self_check 脚本中有覆盖,但 ATK 框架的 YAML 驱动测试是项目的标准测试流程。

建议:为 torch_mamba3_siso_bwd_ddt / triton_mamba3_siso_bwd_ddt 添加对应的 YAML 配置文件(可新建 mamba3_siso_bwd_ddt.yaml 或在现有 YAML 中增加 ddt 测试段),引用 generate_mamba3_siso_bwd_ddt 生成器,并定义 dscale、dgamma、dt、trap 四个输入张量的规格。

likedislike
不准确?
atomgit-bot
atomgit-bot7月12日进行代码检视1
tests/unit_tests/triton/test_mamba3_siso_bwd.py
@@ -0,0 +229,4 @@
229+ actual[:5],
230+ expected[:5],
231+ )):
232+ assert_close(f"compute_dqktheta_{name}", exp, act, 1e-3, err_atol=1e-3)
atomgit-bot
atomgit-bot7月12日评论:

🟡 Medium Priority

changed line: tests/unit_tests/triton/test_mamba3_siso_bwd.py 第 227-232 行,test_compute_dqktheta 只校验了 actual[:5](dq, dk, dangles, dscale, dgamma),跳过了 dq_bias(索引 2)和 dk_bias(索引 3)。

affected behavior: compute_dqktheta 返回 7 个 tensor,但单元测试和 self_check 脚本均未验证 dq_bias 和 dk_bias 的精度。这两个 tensor 由 kernel 内 even/odd 累加并 partial sum 输出(impl 第 620-621 行),其计算路径涉及 bias 加载、GQA 循环内的逐 head 累加和 store,若 kernel 中 bias 索引或 stride 有误,现有测试无法发现。

failure mode: 若 kernel 内 dq_bias/dk_bias 计算有误(如 stride 错误、GQA 分组索引偏移),用户在生产环境中得到的 bias 梯度将是错误的,但所有测试仍会 PASS。

suggested fix: 在 test_compute_dqktheta 中添加对 actual[2](dq_bias)和 actual[3](dk_bias)的 assert_close 校验;在 self_check_mamba3_siso_bwd.py_run_dqktheta_case 中同样加入 dq_bias 和 dk_bias 的精度对比。

建议:在 test_compute_dqktheta 和 self_check 的 _run_dqktheta_case 中补齐 dq_bias 和 dk_bias 的精度校验

likedislike
不准确?
atomgit-bot
atomgit-bot7月12日进行代码检视1
tests/atk_tests/triton/mamba3_siso_bwd/generate_mamba3_siso_bwd.py
@@ -0,0 +24,4 @@
24+ 
25+ @classmethod
26+ def _build_pool(cls):
27+ return [case for case in cls.CASES if case[3] % case[2] == 0]
atomgit-bot
atomgit-bot7月12日评论:

🟡 Medium Priority

changed line: generate_mamba3_siso_bwd.py 第 17 行 [64, 128] 以及第 27 行 _build_pool 仅过滤 nheads % nheads_qk == 0,未过滤 headdim_qk == 128

affected behavior: Mamba3SisoBwdDqkthetaGeneratorCASES_build_pool 会生成 headdim_qk=128 的用例。YAML 中 dtype_numbers=10 配合 2 种 dtype 共产生 20 个用例,按生成顺序前 20 个中有约一半是 headdim_qk=128

failure mode: 文档(docs/triton/mamba3_siso_bwd.md 第 207 行及 self_check_mamba3_siso_bwd.py 第 306 行)明确声明 headdim_qk=128 在 NPU 上因 UB 溢出暂不支持,当前 PR 仅覆盖 headdim_qk=64。ATK 框架执行这些用例时,kernel 会因 NPU UB 溢出而崩溃或产生错误结果,导致 ATK 测试大量失败。

trigger condition: 当 ATK 框架按 mamba3_siso_bwd.yaml 配置运行 generate_mamba3_siso_bwd_dqktheta 生成器时必然触发。

suggested fix: 在 _build_pool 中增加 case[4] == 64 过滤条件,同步在 after_case_config 的 else 分支中将 random.choice([64, 128]) 改为 64

建议:在 _build_pool 的列表推导中增加 case[4] == 64 过滤条件,并在 else 分支中将 headdim_qk 固定为 64。具体修改:第 27 行改为 return [case for case in cls.CASES if case[3] % case[2] == 0 and case[4] == 64];第 42 行改为 headdim_qk = 64

likedislike
不准确?
atomgit-bot
atomgit-bot7月12日进行代码检视1
tests/unit_tests/triton/test_mamba3_siso_bwd.py
@@ -0,0 +229,4 @@
229+ actual[:5],
230+ expected[:5],
231+ )):
232+ assert_close(f"compute_dqktheta_{name}", exp, act, 1e-3, err_atol=1e-3)
atomgit-bot
atomgit-bot7月12日评论:

🟡 Medium Priority

changed line: tests/unit_tests/triton/test_mamba3_siso_bwd.py 第 227-232 行和 tests/atk_tests/triton/mamba3_siso_bwd/self_check_mamba3_siso_bwd.py 第 211-213 行。

compute_dqktheta 返回 7 个 tensor:(dq, dk, dq_bias, dk_bias, dangles, dscale, dgamma)。但测试代码使用 actual[:5] 取前 5 个,与名称列表 ["dq", "dk", "dangles", "dscale", "dgamma"] 做 zip,导致:

  1. 名称标签错位:索引 2 的 dq_bias 被标记为 "dangles",索引 3 的 dk_bias 被标记为 "dscale",索引 4 的 dangles 被标记为 "dgamma"
  2. dscale 和 dgamma 完全未测试:这两个核心输出(索引 5、6)不在 actual[:5] 范围内,从未参与精度校验
  3. 自证报告误导docs/triton/mamba3_siso_bwd.md 第 155-200 行展示的 "dscale" 和 "dgamma" 精度数据,实际来自 dk_bias 和 dangles 的比较结果,而非真正的 dscale/dgamma

affected behavior: dscale 和 dgamma 是 kernel 的关键输出,它们直接作为 compute_ddt_dtrap_dinput_states 的输入。若 kernel 中 dscale/dgamma 计算有误(如 GQA reduction 错误、stride 偏移),所有测试仍会 PASS,但下游 dDT/dTrap 计算将基于错误的 dscale/dgamma。

failure mode: dscale/dgamma 的错误会传播到 ddt/dtrap 计算,但测试无法发现。自证报告中的 "dscale"/"dgamma" 行实际展示的是 dk_bias/dangles 精度,造成虚假信心。

suggested fix: 修正为 actualexpected 的完整 7 元组对比,名称列表改为 ["dq", "dk", "dq_bias", "dk_bias", "dangles", "dscale", "dgamma"]

建议:修正名称列表与实际返回值对齐,补齐 dscale 和 dgamma 的精度校验

改动建议
232
+ for i, (name, act, exp) in enumerate(zip(
233
+ ["dq", "dk", "dq_bias", "dk_bias", "dangles", "dscale", "dgamma"],
234
+ actual,
235
+ expected,
236
+ )):
232
237
  assert_close(f"compute_dqktheta_{name}", exp, act, 1e-3, err_atol=1e-3)
应用建议
likedislike
不准确?
SSovLyn成员
7月24日 关闭了 pull request