Merged
Add generic_block_sparse_attention_metadata Op #10657
lijiamin2025Add on Aug 26
Add generic_block_sparse_attention_metadata Op #10657
Merged
Pull Request has been successfully merged by CANN-robot.
(Thank you for lijiamin2025's contribution.)Aug 26 Created pull request, commit 2d800960
Aug 26 Associated issue: [Requirement|需求建议]: Add generic_block_sparse_attention_metadata Op
Aug 26 Associated issue: [Requirement|需求建议]: Add generic_block_sparse_attention_metadata Op
atomgit-bot
Aug 26 Comments:
Aug 26 Comments:
变更摘要
本 PR 新增算子 generic_block_sparse_attention_metadata(GenericBlockSparseAttentionMetadata),用于为块稀疏注意力(block sparse attention)在 AICPU 侧生成并编码调度元数据,涉及算子内核、调度算法、ACLNN 接口、Torch 扩展封装及配套示例与单测。整体流程为:host 侧通过 aclnnGenericBlockSparseAttentionMetadataGetWorkspaceSize/aclnnGenericBlockSparseAttentionMetadata 完成参数校验并调用 AICPU 算子,内核解析稀疏块索引/计数与 Q 序列长度(支持 TND、BSND、BNSD 布局),经 BuildSchedule 生成 SA 任务与 FD decode/combine 调度,最终由 EncodeMetadata 按固定协议写入 1024 长度 INT32 的 metadata 输出张量。同时本 PR 对 gmm/grouped_matmul 相关文件做了少量格式化与 const 正确性调整。
主要改动
- 新增元数据算子内核及调度逻辑: 新增
generic_block_sparse_attention_metadata_aicpu.cpp(含Compute/Prepare/CheckInputs/ParseQSeqLens/GenerateMetadata等流程)及generic_block_sparse_attention_metadata_scheduler.h(实现BuildSchedule、DecodeTask、EncodeMetadata与 FD 成本模型/核心数计算),并注册REGISTER_CPU_KERNEL,同时新增generic_block_sparse_attention_metadata_aicpu.json配置(engineDNN_VM_AICPU,kernelSolibtransformer_aicpu_kernels.so)。 - 定义元数据内存协议与 Q 序列工具: 新增
generic_block_sparse_attention_metadata.h,定义METADATA_TOTAL_SIZE(1024)、METADATA_MAGIC、METADATA_VERSION及HeaderIndex/DecodeScheduleIndex/CombineScheduleIndex等布局常量;新增generic_block_sparse_attention_metadata_q_seq_utils.h提供BuildTndQSeqLayout、GatherTndValidBlockNums等 TND 序列长度解析辅助函数。 - 新增 ACLNN host 接口与参数校验: 新增
aclnn_generic_block_sparse_attention_metadata.h/.cpp暴露aclnnGenericBlockSparseAttentionMetadata系列接口,配合generic_block_sparse_attention_metadata_check.h对张量 dtype/维度、layout(TND/BSND/BNSD/PAGED_BBND/PAGED_BNBD)、blockShape、maskType/quantType等属性进行校验,并注册底层GenericBlockSparseAttentionMetadataAICPU launch。 - 新增 Torch 扩展封装: 新增
generic_block_sparse_attention_metadata.py(含 op schema、Meta 注册及npu_generic_block_sparse_attention_metadataPrivateUse1 实现)与csrc/generic_block_sparse_attention_metadata.cpp,并在ops/__init__.py中导出,同时torch_extension/setup.py增加对集中式ops/csrc/*.cpp的收集逻辑。 - 新增示例与单测: 新增
examples/test_aclnn_generic_block_sparse_attention_metadata.cpp(覆盖 BSND decode 与 TND seqUsedQ 场景)和tests/ut/test_generic_block_sparse_attention_metadata_scheduler.cpp(覆盖 prefill、decode/combine 调度、容量门限、metadata 编码等用例);并对gmm/grouped_matmul下aclnn_grouped_matmul.cpp、grouped_matmul_util.h(tuningConfigOptional改为const)、grouped_matmul_tiling.cpp做了格式化与类型收窄调整。


Incorrect?
Aug 26 Added label: stat/needs-squash
Aug 26 Added label: cann-cla/yes
131 messages are collapsed here。 See more
赵臣臣
Aug 31 Comments:
Aug 31 Comments:
/approve
/lgtm


mabing
Sep 1 Comments:
Sep 1 Comments:
/lgtm


Sep 1 Added label: lgtm
Sep 1 Closed associated issue
Sep 1 Merged pull request
描述
新增 generic_block_sparse_attention_metadata 算子,该metadata算子需要与主算子配合使用。
主算子实现:https://gitcode.com/cann/ops-transformer/pull/10628
遗留问题:torch接口未评审,无法合入,暂存工作区。
关联的Issue
https://gitcode.com/cann/ops-transformer/issues/4596
测试
已与主算子联调,测试通过。
文档更新
更新了aclnn文档
类型标签