Merged
fix(kv_quant_sparse_flash_attention): gate valid-size ring stride by quant mode #13458
fix(kv_quant_sparse_flash_attention): gate valid-size ring stride by quant mode #13458
Merged
chen-weipeng12Add on Oct 8
chen-weipeng12
Oct 8

Supersedes the closed #13448 (same fix, minimal diff: the file existing clang-format alignment is left untouched).

Problem

kv_quant_sparse_flash_attention (arch22, 910B) reads/writes past the end of its workspace in per-tile mode (key_quant_mode = 2). Observed as a fixed output index range that is wrong while the values change from run to run.

Root cause

InitWorkspaceGlobalTensor() sets the per-core base of the valid-size ring with a fixed 4-unit stride for every quantization mode:

kvValidSizeGm_.SetGlobalBuffer(
    (__gm__ int32_t *)(workspace + qsfaOffset + (aiCoreIdx * 4) * 128 * 4 * sizeof(int32_t)));

while the host side GetWorkspaceSize() reserves the extra ring only for TQ4:

workspaceSize_ += VALID_SIZE_RING_BYTES * (AIV_PER_AIC * actCoreNum);   // all modes
if (qsfaInfo_->keyQuantMode == KEY_QUANT_MODE_TQ4) {
    workspaceSize_ += VALID_SIZE_RING_BYTES * (AIV_PER_AIC * actCoreNum);
}

VALID_SIZE_RING_BYTES = 4 * 128 * 4 = 2048B, AIV_PER_AIC = 2, and actCoreNum is the AIC count (coreNum_ = aicNum_ = GetCoreNumAic()), so per-tile mode reserves 4096B/core while the kernel addresses 8192B/core: every core with aiCoreIdx >= actCoreNum / 2 accesses beyond the workspace end (about 4096 * actCoreNum bytes in total, ~128KB for 32 cores). In the non-Flash-Decode layout the ring is the last region of the workspace, so the overflow lands in whatever GM buffer follows it.

The stride was widened to make room for the TQ4 per-token FP16 scale ring (staged in the upper half of the per-core block) but was not gated by quantization mode.

Fix

Derive the per-core ring units from keyQuantMode: 4 for TQ4, 2 for every other mode. The host reservation is untouched, so the workspace size and expectWorkspaces in the tiling UT are unchanged, and the TQ4 addressing is bit-identical to before.

Verification

Static only so far: the addressing now matches the host reservation in both modes. The file pre-existing clang-format style is deliberately left untouched to keep this change minimal (5 lines).

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for chen-weipeng12's contribution.)
chen-weipeng12chen-weipeng12
Oct 8 Created pull request, commit b59c53e4
atomgit-bot
atomgit-bot
Oct 8 Comments:

变更摘要

该 PR 修复 kv_quant_sparse_flash_attention(arch22,910B)在 per-tile 模式(key_quant_mode = 2)下工作区越界读写的问题。根因是 kernel 侧 InitWorkspaceGlobalTensor() 为每种量化模式都用固定的 4 份单位步长设置每核有效 size 环的基址,而 host 侧 GetWorkspaceSize() 只为 KEY_QUANT_MODE_TQ4 额外预留该环,导致 aiCoreIdx 较大的核访问超出工作区末尾。修复方式是让每核环单位数与 keyQuantMode 关联:TQ4 为 4,其余模式为 2,从而与 host 预留保持一致;host 侧预留不变,TQ4 寻址保持等价。

主要改动

  • 按量化模式推导每核有效 size 环单位数: 在 InitWorkspaceGlobalTensor() 中新增 qsfaValidSizeRingUnits,依据 kvSfaKernelConstInfo.keyQuantMode 取 QUANT_MODE::TQ4 时的 4、其余模式的 2。
  • 修正 kvValidSizeGm_ 基址步长: kvValidSizeGm_.SetGlobalBuffer 的偏移由固定 (aiCoreIdx * 4) * 128 * 4 * sizeof(int32_t) 改为 (aiCoreIdx * qsfaValidSizeRingUnits) * 128 * 4 * sizeof(int32_t),使非 TQ4 模式的每核寻址与 host 侧预留一致,避免超出工作区末尾的访问。
  • 补充注释说明分区数含义: 将原注释更新为区分 TQ4 需 4 份(2 份 AIV 有效 size 分区 + 等大的 TQ4 逐 token FP16 scale 区)与其余模式只需 2 份,并注明与 GetWorkspaceSize() 预留一致。
likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Oct 8 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Oct 8 Comments:

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
Oct 8 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 ✅ songjionghui, wangzhe123456789 (2/2) ✅ songjionghui (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robotCANN-robotCollaborators
Oct 8 Set guijianwei,hz36amy_00,yangziqi_007,coder_linx,juyangokok,monologue815,xuanyuandy,crystalhu,yu-xinjie62,huangwei791,jiang-lirui,yang-binrong,Allan_Yu,huang-wei-chen,liudan12,shasha_an,wangzhe123456789,Wei_NaChuan,zhangshuoleiokok,luwei_1104,cc-z,kdy18482276080,xuchengyang,shen_weiling,gaoxiang618,songjionghui,lining_210,wenxin_fight,miaofangzheng,mabing1118,tang-hao-hw-gitcode,haijie_699874,zzy__,Julius47,yuhao_,L_Euler,wangxun21 as approver
CANN-robotCANN-robotCollaborators
Oct 8 Set guijianwei,hz36amy_00,yangziqi_007,coder_linx,juyangokok,monologue815,xuanyuandy,crystalhu,yu-xinjie62,huangwei791,jiang-lirui,yang-binrong,Allan_Yu,huang-wei-chen,liudan12,shasha_an,wangzhe123456789,Wei_NaChuan,zhangshuoleiokok,luwei_1104,cc-z,kdy18482276080,xuchengyang,shen_weiling,gaoxiang618,songjionghui,lining_210,wenxin_fight,miaofangzheng,mabing1118,tang-hao-hw-gitcode,haijie_699874,zzy__,Julius47,yuhao_,L_Euler,wangxun21 as approver
chen-weipeng12
chen-weipeng12
Oct 8 Comments:

compile

likedislike
chen-weipeng12chen-weipeng12
Oct 8 Pre merge succeeded (commit_id: 2febf307e098c07a30d13140d871fa827aa039b9)
CANN-robot
CANN-robotCollaborators
Oct 8 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
Oct 8 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 8 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 8 Added label: ci-pipeline-failed
CANN-robot
CANN-robotCollaborators
Oct 8 Comments:

流水线分析报告 — ops-transformer run#9948

流水线: https://gitcode.com/CANN/ops-transformer/actions/runs/6041972d459b43868a20da4a030ced27 | PR#13458 | 2026-10-08 22:06:56 ~ 2026-10-08 22:11:59 | 失败 Job 1 个

定界结论: 代码问题 / 代码检查错误 — clang-format............................
  • 失败类型: 代码问题
  • 根因: pre-commit 检查项不通过: clang-format(本地执行 pre-commit run clang-format 修复)
  • 处置建议: 通知 PR 提交者修复(见各根因的具体建议)

失败根因分析

根因 #1: 代码问题 / 代码检查错误 — clang-format............................
  • 判据: pre-commit 检查项不通过: clang-format(本地执行 pre-commit run clang-format 修复)
  • 定位步骤: 3_precommit.log
  • 影响 Job: CodeCheck_precommit
  • 根因证据(日志原文):
[2026-10-08 22:10:23.295+08:00] clang-format.............................................................Failed
[2026-10-08 22:10:23.297+08:00] - hook id: clang-format
[2026-10-08 22:10:23.299+08:00] - files were modified by this hook
[2026-10-08 22:10:23.301+08:00] Formatting [1/1] attention/kv_quant_sparse_flash_attention/op_kernel/arch22/kv_quant_sparse_flash_attention_kernel_mla.h
[2026-10-08 22:10:23.344+08:00] ::error::precommit.py 异常退出: exit code=1, signal=null
  • 补充定位: 不符合 clang-format 规范的文件仅 1 个: attention/kv_quant_sparse_flash_attention/op_kernel/arch22/kv_quant_sparse_flash_attention_kernel_mla.h(本次 PR 触碰的头文件)。该 hook 以 "files were modified by this hook" 方式失败, 即此文件格式与仓库 .clang-format 规则不一致, 由提交者本地格式化后重新提交即可。同 Job 其余检查项(check for merge conflicts / detect private key / codespell / Duplicate Header / OAT Compliance)均 Passed, 排除其他代码检查问题。

由门禁失败定界分析自动生成 | repo: ops-transformer | run: 6041972d459b43868a20da4a030ced27 | PR#13458
失败 Job 日志已落盘: /tmp/opencode/analyze_logs/6041972d459b43868a20da4a030ced27/(按 Job id 命名, 核对证据直接读取)

likedislike
chen-weipeng12chen-weipeng12
Oct 8 Pre merge succeeded (commit_id: a4e2c6059885cf524f1de8aeadfa20a4402ae828)
chen-weipeng12chen-weipeng12
Oct 8 Force-pushed 3 commits:21b2065a-2 commits from branch master,1d9c4c6b-fix(kv_quant_sparse_flash_attention): gate valid-size ring stride by quant mode
chen-weipeng12chen-weipeng12
Oct 8 Pre merge succeeded (commit_id: 2239d7cce986e03bc589e13157d345993d044f57)
CANN-robotCANN-robotCollaborators
Oct 8 Removed label: cann-cla/yes
CANN-robotCANN-robotCollaborators
Oct 8 Added label: cann-cla/yes
chen-weipeng12
chen-weipeng12
Oct 9 Comments:

compile

likedislike
chen-weipeng12chen-weipeng12
Oct 9 Pre merge succeeded (commit_id: 6179d7cf72546d5906712e0235d24746288ccff6)
CANN-robot
CANN-robotCollaborators
Oct 9 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
Oct 9 Removed label: ci-pipeline-failed
CANN-robotCANN-robotCollaborators
Oct 9 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 9 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 9 Added label: ci-pipeline-failed
CANN-robot
CANN-robotCollaborators
Oct 9 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
Oct 9 Removed label: ci-pipeline-failed
CANN-robotCANN-robotCollaborators
Oct 9 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 9 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 9 Added label: ci-pipeline-passed
songjionghui
songjionghuiCollaborators
Oct 9 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Oct 9 Added label: approved
wangzhe123456789
wangzhe123456789Collaborators
Oct 9 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Oct 9 Added label: lgtm
CANN-robotCANN-robotCollaborators
Oct 9 Merged pull request