Pull Request has been successfully merged by CANN-robot.
(Thank you for chen-weipeng12's contribution.)变更摘要
该 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()预留一致。


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
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


compile


| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


流水线分析报告 — 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 提交者修复(见各根因的具体建议)
- 参考链接: 本地执行 pre-commit 指导
失败根因分析
根因 #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 命名, 核对证据直接读取)


compile


| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


/approve


/lgtm


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, andactCoreNumis the AIC count (coreNum_ = aicNum_ = GetCoreNumAic()), so per-tile mode reserves 4096B/core while the kernel addresses 8192B/core: every core withaiCoreIdx >= actCoreNum / 2accesses beyond the workspace end (about4096 * actCoreNumbytes 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 andexpectWorkspacesin 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).