Merged
新增 MsaIndexScore 算子 (A2/A3) #9503
新增 MsaIndexScore 算子 (A2/A3) #9503
Merged
oscillatedAdd on Aug 3
oscillated
oscillatedCollaborators
Aug 3

描述

新增 MsaIndexScore 算子及其接口(A2/A3),改进M3模型中MSA模块的性能。

关联的Issue

关联Issue #4117

测试

example、torch_extension接口功能自验

image.png

输出精度比对 v.s. triton小算子实现

image.png

输出性能批跑

image.png

文档更新

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for oscillated's contribution.)
oscillatedoscillatedCollaborators
Aug 3 Created pull request, commit d8e3f8a8
atomgit-bot
atomgit-bot
Aug 3 Comments:

变更摘要

此 PR 新增了 msa_index_score 算子,用于在昇腾 Atlas A2/A3 平台上实现 MSA(Multi-head Sparse Attention)的 Index Score 计算。算子采用 AIC + AIV 异构流水线设计:AIC 侧通过 BlockMmad 完成 paged QK^T 矩阵乘法并产出 S tile 到 GM workspace,AIV 侧从 workspace 读取 S tile 后执行因果 mask、逐 sparse block 行归约取最大值、施加 per-token per-head scale,最终将 score 写回。由于 Atlas A2 无 L0C→UB 直通路径,S 中间结果必须经 GM 中转,AIC/AIV 通过 cross-core flag 实现流水同步。算子支持 bf16/fp16 输入、varlen 多 batch、Decode/投机解码、长序列多 S-tile 轮转以及因果裁剪等特性,输出为 fp32 张量,末维按 16 对齐。

主要改动

  • 算子原型注册:在 msa_index_score_def.cpp 中注册 MsaIndexScore 算子,定义 7 个输入(query、key_cache、block_table、scale、actual_seq_lengths_query、actual_seq_lengths_key、start_loc)、1 个输出(score)及 block_size、fill_invalid_tail 属性,支持 910b/910_93 平台动态编译与动态形状。
  • 形状与数据类型推导:在 msa_index_score_infershape.cpp 中实现 InferShapeMsaIndexScore,输出 shape 为 [numQHeads, totalQ, round_up(maxBlocks, 16)],并强制输出 DT_FLOAT 以满足下游 topk 精度要求。
  • Tiling 与多核调度:在 msa_index_score_tiling.cpp / .h 中定义 MsaIndexScoreTilingData 结构体(包含 batch、totalQ、headDim、stride 等 21 个字段),并实现参数校验、workspace 分配及按 dtype 分支设置 tiling key。
  • AIC/AIV 异构 Kernel:在 msa_index_score_kernel.h 中实现 MsaIndexScoreKernel,AIC 使用 BlockMmad 按 block_table 间接寻址完成分页 QK^T;AIV 调用 MsaSegRowMaxEpilogue 完成 mask、128→64→8→1 三级归约取 max 及 scale 写入。
  • AIV Epilogue 归约流水:在 msa_index_score_epilogue.h 中实现 MsaSegRowMaxEpilogue,手工布局 UB 缓冲区,对 S tile 按 [rows*BLOCKS, BLOCK_SIZE] 视图做行归约,并对边界 block 使用位掩码 Duplicate 精确填充 -inf。
  • varlen 任务枚举器:在 msa_index_score_task.h 中实现 MsaTaskScheduler 与 MsaTask,按 batch 内 (token, head) 扁平索引切分 M-tile,计算因果裁剪边界 fullEndBlk / visibleEndBlk,供 AIC 与 AIV 共享以保证 cross-core flag 握手配对。
  • 公共常量与 kernel 入口:在 msa_index_score_common.h 中定义 MSA_BLOCK_SIZE=128、MSA_ROW_TILE_M=128 等编译期常量;在 msa_index_score.cpp 入口中按 TILING_KEY 宏分发 bf16/fp16 kernel 并调用 GET_TILING_DATA_WITH_STRUCT 解析 tiling 数据。
  • 测试与构建配置:新增 C++ 端到端示例 test_aclnn_msa_index_score.cpp(覆盖 11 个场景)、Python CPU golden 参考 msa_index_score_golden.py,并在 test_config.yaml 中将 msa_index_score 模块加入 attention 测试构建。
likedislike
Incorrect?
oscillatedoscillatedCollaborators
Aug 3 Set as a draft.
CANN-robotCANN-robotCollaborators
Aug 3 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 3 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.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
*/*/README.md ✅ 蔡雯雯, 黄俊健, 蒋礼锐 (3/2) ✅ 蔡雯雯 (1/1)
*/*/docs/acl*.md ✅ 蒋礼锐, 黄俊健, 蔡雯雯 (3/2) ✅ 蔡雯雯 (1/1)
*/*/op_host/*_def.cpp ✅ 王永光, 蒋礼锐, 黄俊健 (3/2) ✅ 王永光 (1/1)
attention ✅ 黄俊健, gaoxiang618, 蒋礼锐, 於欣洁, shasha_an (5/2) ✅ gaoxiang618, 蒋礼锐, 於欣洁, 黄俊健 (4/1)
sig-ops-transformer ✅ 蒋礼锐, 黄俊健 (2/2) ✅ 蒋礼锐, 黄俊健 (2/1)
tests ✅ 黄俊健, gaoxiang618, 蒋礼锐 (3/2) ✅ 黄俊健, gaoxiang618, 蒋礼锐 (3/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
348 messages are collapsed here。 See more
jiang-lirui
jiang-liruiCollaborators
Aug 20 Comments:

/approve
/lgtm

likedislike
CANN-robot
CANN-robotCollaborators
Aug 20 Comments:

The following users do not have permission to comment /lgtm or /approve on any module in this PR:
唐超

likedislike
CANN-robotCANN-robotCollaborators
Aug 20 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 20 Closed associated issue
CANN-robotCANN-robotCollaborators
Aug 20 Merged pull request