Merged
FIA 伪量化 PA 场景支持 kv 及 kv_scale 0 轴 1 轴非连续 #10263
FIA 伪量化 PA 场景支持 kv 及 kv_scale 0 轴 1 轴非连续 #10263
Merged
anakinSkywalkerAdd on Aug 19
anakinSkywalker
Aug 19

描述

推理场景(FusedInferAttentionScore)的伪量化 PA(PageAttention)目前要求 KV cache 及其反量化参数必须以连续布局传入。实际部署中,KV cache 是多轮写入的长驻tensor,随着预算调度、显存整理,用户侧很容易拿到带 padding 的非连续视图;反量化 scale/offset 也常是同一块物理内存上切出来的非连续切片。此前这些非连续输入要么被框架强制拷贝成连续(多一次全量数据搬运),要么直接不可用,与长 cache、大 batch 的推理成本诉求相悖。

本 PR 让 FIA 伪量化 PA 场景接受非连续输入,用户传入的 stride 会被完整保留并送达到 kernel 侧用于寻址,免除强制连续化带来的拷贝:

  • KV cache(INT8/INT4):支持 BNBD、BBH、NZ 三种 PA 布局在 0/1 轴非连续,kernel 按 stride 跳过 padding 直接寻址
  • 反量化 scale/offset:在 per-token-pa(mode4)/ per-token-head-pa(mode5)两种模式下支持非连续,且 scale 与 offset 各自持有独立 stride 通道,二者可以不同布局
  • 场景约束:非 PA 场景、mode0-3 的 scale/offset 无 Bn 轴,非连续输入会被 checker 明确拦截并给出可读的报错,不会静默走错数据

其中 INT4 以 INT32 打包存储,口径转换阶段 stride 会被重算成连续值导致非连续信息丢失,本 PR 一并修复,保证 INT4 的非连续 stride 与 shape 同口径送达。

关联的Issue

关联 issue #4720

测试

  • 新增 STC 用例 35 个:覆盖 INT8/INT4/FP8E4M3 三种 dtype × mode0-5 × BNBD/BBH/NZ 布局,含 FlashDecode 长序列场景,以及 scale 与 offset 各自独立非连续的交叉验证用例
  • 全量 STC 结果:33 passed / 2 failed(2 个失败为测试框架对特定 error case 报错形态的既有识别缺陷,与本特性无关,历史分支上即如此)
  • 所有参与精度比对的用例 max_err=0(非连续 actual 与连续 reference 逐位一致)
  • 补充验证“连续 KV + 非连续 scale/offset”组合:stride 送达 tiling 层、checker 按 mode/轴规则放行,精度逐位一致
  • 连续基线回归:原有连续路径行为无变化

文档更新

无

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for anakinSkywalker's contribution.)
anakinSkywalkeranakinSkywalker
Aug 19 Created pull request, commit 804d058c
atomgit-bot
atomgit-bot
Aug 19 Comments:

变更摘要

本 PR 为 FIA(Fused Infer Attention)伪量化(anti-quant)场景的 PA(Page Attention)路径增加对 kv 及 kv_scale(dequant scale)张量在 0 轴/1 轴非连续(stride 非默认)布局的支持。整体改动分三层:Host 侧在 tiling 阶段把 key/value 及 k/v descale 的 bnStride/n2Stride 通过 kScaleStrides、vScaleStrides 等新字段下发,并新增 CheckStrideForAntiquant 校验器限定非连续仅允许出现在 0/1 维(BBH 布局仅限 0 维);内核侧在 RunInfo、CVSharedParams、AntiquantTaskParamBaseAPI 与 ConstInfo 中传递这些 stride,并在 CopyKV、CopyKVPageAttention、LoadAntiquant 以及 key/value 核心偏移计算中按非零 stride 动态计算地址,实现 PA 场景下 key、value 与 scale 的 0/1 轴非连续读取。

主要改动

  • 新增 stride 下发字段与接口:flash_attention_score_tiling_regbase_arch35.h 为 MultiCoreParamsRegbase 增加 kScaleStrides、vScaleStrides 字段及 set_/get_ 方法,util_regbase.h 在 RunInfo 宏与 CVSharedParams<true, true> 中新增 kScaleBnStride、kScaleN2Stride、vScaleBnStride、vScaleN2Stride,用于承载非连续 scale 的 stride 信息。

  • Host 侧 tiling 与合法性校验:fused_infer_attention_score_tiling_impl.cpp 的 SetFATilingData 将 fiaInfo.kScaleStrides/vScaleStrides 的 stride 写入 inputParams;dequant_checker.cpp 新增 CheckStrideForAntiquant 并接入伪量化特征校验链,非 PA 场景要求 key/value/descale 连续,PA 场景仅允许 0/1 维非连续(BBH 仅允许 0 维),否则报错拒绝。

  • 内核参数透传:flash_attention_score_antiquant_kernel.h 将 keyStrides、valueStrides、kScaleStrides、vScaleStrides 的 bnStride/n2Stride 写入 constInfo;flash_attention_score_antiquant_block_vec.h 的 SetAntiqParamCommon 增加 isKey 参数以区分 key/value 各自的 kv 与 scale stride,并在 scaleN2Stride 非 0 时用其计算 antiqParamOffset。

  • 非连续寻址计算:flash_attention_score_antiquant_processor.h 的 AntiquantTaskParamBaseAPI 新增 kvBnStride、kvN2Stride、scaleBnStride、scaleN2Stride;LoadAntiquant 的 scale 偏移与 CopyKV/CopyKVPageAttention 在 NZ/BBH/BNBD 布局下按自定义 stride 计算 headStride、blockStride、batchStride,CopyKV 增加 kvN2Stride 参数以支持非连续的 head 间步长。

  • key/value 核心偏移拆分:infer_flash_attention_kvcache_fused_infer.h 的 GetKeyCoreOffsetParam、CalcValueCoreOffsetBSH/TND/BNSD 在对应 stride 非 0 时改用自定义 headStride/batchStride,并在 SyncKeyCoreOffset、ComputeOffsetNonPrefixAIC、ComputeOffsetForAntiquant 中依据 stride 是否非 0(或 dSize != dSizeV)拆分 keyOffset 与 valueOffset 分别计算。

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 19 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Aug 19 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 19 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
*/*/op_host/*_def.cpp ✅ 王永光, 刘丹 (2/2) ✅ 王永光 (1/1)
attention ✅ shen_weiling, 刘丹, shasha_an (3/2) ✅ 刘丹, shasha_an (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
246 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 31 Added label: lgtm
CANN-robot
CANN-robotCollaborators
Aug 31 Comments:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
wangyongguangwangyongguangCollaborators
Aug 31 Resolved final issue
CANN-robotCANN-robotCollaborators
Aug 31 Closed associated issue
CANN-robotCANN-robotCollaborators
Aug 31 Merged pull request