Pull Request has been successfully merged by CANN-robot.
(Thank you for oscillated's contribution.)变更摘要
此 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 测试构建。


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


/approve
/lgtm


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


描述
新增 MsaIndexScore 算子及其接口(A2/A3),改进M3模型中MSA模块的性能。
关联的Issue
关联Issue #4117
测试
example、torch_extension接口功能自验
输出精度比对 v.s. triton小算子实现
输出性能批跑
文档更新
类型标签