Merged
kv_quant_sparse_flash_attention: kv_quant_sparse_flash_attention 算子性能优化 #10428
kv_quant_sparse_flash_attention: kv_quant_sparse_flash_attention 算子性能优化 #10428
Merged
kdy18482276080Add on Aug 22
kdy18482276080
kdy18482276080Collaborators
Aug 22

描述

kv_quant_sparse_flash_attention 算子性能优化:

  • 优化 tiling 模板配置(template_tiling_key / tiling.cpp / tiling.h)
  • 优化 service_vector_mla 实现,降低 vector 侧开销
  • 调整 kernel 侧逻辑(kernel_mla_arch35 / common_arch35 / op_kernel)
  • 增加性能 profiling 采集结果验证收益

关联的Issue

关联Issue #4498
https://gitcode.com/cann/ops-transformer/issues/4498

测试

  • 算子 UT 用例执行通过
  • NPU 性能 profiling 采集完成,确认性能提升

文档更新

无

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for kdy18482276080's contribution.)
kdy18482276080kdy18482276080Collaborators
Aug 22 Created pull request, commit 80f3b012
atomgit-bot
atomgit-bot
Aug 22 Comments:

变更摘要

该 PR 以补充测试与性能采集为名,实际核心改动是为 kv_quant_sparse_flash_attention(QSFA)算子新增「稀疏 KV 物理地址向量化预计算」能力:host 侧在 QSFAMlaTiling 新增 CalcVectorizeFlag() 判定开关并将 vectorizeFlag_ 纳入 tiling key 与 workspace 计算;kernel 侧新增 IS_VEC_S2PHYADDR 模板参数,在 QSFAVectorService::GetKVPhyAddr(含 SIMD 实现 GetKVPhyAddrVF)中将 PA 场景的稀疏 KV 物理地址预计算写入 workspace,主循环改用 GetRealS2Addr 直接读取预计算表,替代原先逐 token 计算 offset 的路径。

主要改动

  • 新增向量化开关 CalcVectorizeFlag: 在 QSFAMlaTiling 中新增 CalcVectorizeFlag(),仅当 KV 布局为 PA_BSND、blockSize 为 2 的幂且 UB 占用预算(256KB 预留 8KB)满足时置位 vectorizeFlag_,并在 DoOpTiling 中于 GetWorkspaceSize() 之前调用。
  • tiling key 与 workspace 扩展: GenTilingKey() 将 vectorizeFlag_ 并入 GET_TPL_TILING_KEY 参与模板选择;GetWorkspaceSize() 在向量化开启时追加 totalBS1 * sparseBlockCount * sizeof(int64_t) 的物理地址表 workspace。
  • 贯穿新增 IS_VEC_S2PHYADDR 模板参数: 该参数被加入 TEMPLATE_INTF/TEMPLATE_INTF_ARGS/QSFA_CUBE_BLOCK_TRAITS_TYPE_FIELDS 宏、kv_quant_sparse_flash_attention.cpp 的 DispatchKernelDtype310 与 kernel 入口,以及 kv_quant_sparse_flash_attention_template_tiling_key.h 的全部模板参数组合声明。
  • 稀疏 KV 物理地址预计算: QSFAVectorService 新增 GetKVPhyAddr(含 SIMD 实现 GetKVPhyAddrVF/GetKVPhyAddrVFImpl),按 vec core 均分负载、将 blockTable/sparseIdx 拷入 UB 后计算 int64 物理地址并写回 kvPhyAddrGm;ProcessSparseKv 将 topkBS1Idx、s2LoopOffset 等循环不变量外提,新增 GetRealS2Addr 读取预计算地址,CopyInKvSparse 在向量化分支改用 CopyInSingleKv 按物理地址直接拷贝。
  • UB/GM buffer 初始化拆分与调用顺序调整: InitMMResBuf 拆分为 InitMMResBufUb 与 InitMMResBufGm,在 Init() 中先执行 GetKVPhyAddr(内含 tPipe->Reset())再初始化 UB buffer,避免 buffer 池被重置导致重叠,并在 ProcessMainLoop 前新增 ICachePreLoad(6)。
likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 22 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 22 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.
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
attention ✅ wangzhe123456789, 於欣洁 (2/2) ✅ wangzhe123456789, 於欣洁 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
Aug 22 Set guijianwei,hz36amy_00,coder_linx,juyangokok,monologue815,xuanyuandy,crystalhu,yu-xinjie62,huangwei791,jiang-lirui,yang-binrong,Allan_Yu,liudan12,shasha_an,wangzhe123456789,cc-z,shen_weiling,gaoxiang618,songjionghui,wenxin_fight,miaofangzheng,mabing1118,tang-hao-hw-gitcode,haijie_699874,zzy__,yuhao_,L_Euler as approver
154 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 25 Added label: approved
於欣洁
於欣洁Collaborators
Aug 25 Comments:

/lgtm
/approve

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