已开启
fix(flash_attn_fp8_fullquant): use flat KV cost for mask-off tiling split #131
死んじのだごめんね创建于 12 天前
fix(flash_attn_fp8_fullquant): use flat KV cost for mask-off tiling split #131
已开启
死んじのだごめんね创建于 12 天前
死んじのだごめんね
12 天前

当前PR是否有AI参与:

[ ] 否

[x] 是
__1. AI Agent 平台: ZCode
__2. AI 模型: GLM
__3. Prompt上下文 : 针对 flash_attn_fp8_fullquant 精度比较器在 mask_mode="off" 且 KV 短于 Q 时崩溃的问题做缺陷排查与修复;复现脚本、修复范围与验收由人工确认

PR功能描述 / 为什么需要这个合入**:

samples/flash_attn_fp8_fullquant/flash_attn_fp8_fullquant_checker.py 的 host 侧切分路径对合法输入崩溃。

mask_mode="off" 是全注意力,仓内 README 写明支持 cross-attention(即允许 KV 比 Q 短),validate_and_resolve 对这种形态不做 kv>=q 校验。但做 tile 切分时,它无条件把每个 batch 的因果偏移(kv-q)喂给 balanced_split_analytic 的因果 ramp 代价模型。当某个 batch 的 KV 比 Q 短 384 行及以上(例如 BNSD s1=1024、kv=8,偏移 -1016)时,_ramp_prefix 的前置守卫直接抛错:

ValueError: Causal_offset=-1016 is too negative for query_tile_rows=128 kv_tile_rows=256

崩溃发生在任何 NPU 计算开始之前的 host 校验/切分阶段,所以 README 支持的这种合法输入整个精度比较流程跑不起来。同输入在 mask_mode="on" 下走的是设计内的 kv>=q 校验报错,说明 off 路径不是有意为之。

修复(checker.py 改动约 10 行逻辑):

  • mask_mode="off" 时,切分偏移改用每个 batch 的 KV 长度本身。这样代价模型在首个 Q tile 就达到平铺容量 ceil(kv/S2),按全注意力的平坦代价均衡切分;"on" 模式的因果 ramp 代价不变。
  • FlashAttnTilingInfo.causal_offsets 之后仍会重新赋值为真实的 kv-q,kernel 侧语义不变;core_task_ranges 只决定 tile 在核间的划分。
  • 顺手把 balanced_split_analytic 里核区间终点的二分搜索提为模块级纯函数 _bisect_boundary,逻辑逐行原样搬移:这段 for→if/else→while→if 嵌套按 CodeCheck 函数深度口径(上限 4)在 origin/master 上本来就是 5 层,该文件进入变更集后被门禁拦下,提取后降为 4 层。

该PR关联的issue

(格式为fixes #<issue号>, 或者resolves #<issue号>):

希望检视人员了解:

  • 改动只影响 checker 的 host 侧 tile 划分;mask_mode="on" 的代价模型与切分结果不变,测试里有 on 模式对照用例。
  • _bisect_boundary 的提取只为过 CodeCheck 深度检查,无行为变化,diff 可逐行对照。
  • 新增测试 monkeypatch 了 NPU 核数查询,纯 CPU 运行,不需要 NPU 环境。

改动类型 / Change Type

测试信息 / Testing

新增 test/flash_attn_fp8_fullquant/test_flash_attn_fp8_fullquant_host_tiling.py,7 个用例全部通过,无需 NPU:

python -m pytest -q test/flash_attn_fp8_fullquant/test_flash_attn_fp8_fullquant_host_tiling.py
.......                                                                  [100%]
7 passed in 5.18s

覆盖:BNSD s1=1024 kv=8/640/641(修复前崩溃的形态与边界)、BNSD B=2 kv=[2048,8]、TND q_len=1000 kv_len=8、off 模式长 KV 对照、on 模式设计内校验对照。

静态检查与仓内门禁同口径的本地检查:

ruff check / ruff format --check        -> All checks passed! / 2 files already formatted
py_compile + 函数嵌套深度(≤4) + 行尾空白  -> 2 个文件 exit 0

检查清单 / Checklist

likedislike
合并受阻
死んじのだごめんね死んじのだごめんね
12 天前 创建了 pull request,commit 7b67881c
CANN-robotCANN-robot成员
12 天前 添加了label:ai-co-authored
CANN-robotCANN-robot成员
12 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
12 天前 评论:

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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-cann/cannbot-dsl ❌ (0/2)(You can also ask: liujun2025, 沈敏, 陆玲辉, 金炯, yang-jiwei) ❌ (0/1)(You can also ask: 陈振宇, 刘佳玮, liujun2025, 沈敏, 陆玲辉)

💡 Tip:

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

CLA Signature Pass

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

likedislike
死んじのだごめんね
死んじのだごめんね
12 天前 评论:

这个 PR 修的是 flash_attn_fp8_fullquant 精度比较器 host 侧切分的一个崩溃:mask_mode="off" 是 README 写明支持的 cross-attention 形态(允许 KV 比 Q 短),但切分时仍套用因果 ramp 代价模型,KV 比 Q 短 384 行以上就对合法输入抛 "Causal_offset too negative",流程在进 NPU 前就断了。修法是 off 模式下把切分偏移改用 KV 长度本身,让代价模型按全注意力的平坦代价均衡切分,on 模式不变;顺带把一处超出 CodeCheck 深度上限的二分搜索提成了纯函数,逻辑逐行搬移。新增的 7 个回归用例纯 CPU 可跑,修复前 4 例失败、修复后全过。麻烦评审重点看两处:off 分支偏移替换那几行,以及 _bisect_boundary 提取的等价性。

likedislike
死んじのだごめんね
死んじのだごめんね
12 天前 评论:

/compile

likedislike
死んじのだごめんね死んじのだごめんね
12 天前 预合并成功(commit_id: 90cd1dd5c8813e67dd365e861be891780ffd8d82)
CANN-robot
CANN-robot成员
12 天前 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-failed
死んじのだごめんね
死んじのだごめんね
12 天前 评论:

/compile

likedislike
CANN-robot
CANN-robot成员
12 天前 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
12 天前 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-failed
死んじのだごめんね
死んじのだごめんね
12 天前 评论:

/compile

likedislike
CANN-robot
CANN-robot成员
12 天前 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
12 天前 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-failed
死んじのだごめんね
死んじのだごめんね
12 天前 评论:

compile

likedislike
CANN-robot
CANN-robot成员
12 天前 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
12 天前 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-failed
死んじのだごめんね
死んじのだごめんね
12 天前 评论:

compile

likedislike
CANN-robot
CANN-robot成员
12 天前 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
12 天前 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
12 天前 添加了label:ci-pipeline-failed
死んじのだごめんね
死んじのだごめんね
11 天前 评论:

compile

likedislike
CANN-robot
CANN-robot成员
11 天前 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
11 天前 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
11 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-failed
死んじのだごめんね
死んじのだごめんね
11 天前 评论:

compile

likedislike
死んじのだごめんね死んじのだごめんね
11 天前 预合并成功(commit_id: e38f6861cefb8c2435aec515680725b9b3052c51)
CANN-robot
CANN-robot成员
11 天前 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
11 天前 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
11 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-failed