Closed
[Bug] FlexAttention deferred epilogue fusion skips cold-start compilation validation #4708
stonexxxAdd on  Sep 9Closed at  Sep 29
stonexxx
stonexxxCollaborators
Sep 9 Create

问题现象

在 torch_npu: 2.13.0、NPU: Ascend 950 环境中,FlexAttention 输出与 cos 发生 epilogue fusion 时,融合 kernel 需要的 UB 超过可用容量:

ub overflow, requires 2658304 bits while 1769472 bits available

对应测试:

test_flex_attention213.py::TestFlexAttentionPRIVATEUSE1::test_epilogue_fused_npu

FlexAttention template 已设置 _npu_deferred_epilogue_compile_only=True,预期 scheduler 在接受融合前先编译 fused kernel;如果编译失败,应拒绝融合并回退为独立的 attention 和 pointwise cos kernel。

触发条件

  1. FlexAttention template 带 _npu_deferred_epilogue_compile_only=True。
  2. 该 template 与 pointwise epilogue 进入 speculative fusion 检查。
  3. 检查发生在 AsyncCompile 进程池冷启动、ready future 尚未完成时。
  4. fused kernel 存在编译错误,例如 UB overflow。

定位过程与根因

探针确认 lowering 和 scheduler 获取的是同一个 TritonTemplateBuffer,且 deferred 标记在 scheduler 中仍为 True,因此不是标记传播问题。

AsyncCompile.use_process_pool() 在首次调用时会提交进程池 ready task,并立即返回 ready_future.done()。ready task 尚未完成时返回 False。现有 speculative compile 逻辑在该分支只执行 PyCodeCache.load(),未调用 Triton 编译,并用 future=None 表示结果。后续 callback 只在 future 非空时等待编译,因此直接返回成功,错误接受了 fused kernel。

预热进程池后的对照探针显示:use_process_pool() 返回 True,创建真实 LambdaFuture 后,callback 能捕获 UB overflow 并返回 False。这证明问题是冷启动同步分支跳过了真实编译。

期望行为

仅对带 _npu_deferred_epilogue_compile_only 标记的 template,无论进程池是否已经 ready,都必须执行真实 Triton 编译检查。同步编译异常或异步 future 异常都应使 scheduler 拒绝 fusion,并回退到未融合 kernel。

普通 fusion benchmark 行为保持不变。

验证状态

  • 新增同步返回、异步 future、同步异常三类 scheduler contract 回归测试,全部通过。
  • 完整 scheduling contract 测试:11 passed。
  • 真实 FlexAttention 用例在空 cache 下捕获 speculative fused-kernel UB overflow,正确回退到独立 poi_fused_cos,计算和更新后的测试断言通过。
likedislike
TorchNPU-BotTorchNPU-BotCollaborators
Sep 9 Added label: triage-review
ascend-robotascend-robotCollaborators
Sep 9 Added label: bug
TorchNPU-Bot
TorchNPU-BotCollaborators
Sep 9 Comments:

issue待分派,添加triage-review标签

likedislike
stonexxxstonexxxCollaborators
Sep 9 Associated pull request
TorchNPU-BotTorchNPU-BotCollaborators
Sep 9 Added label: bot-triaged; Removed label: triage-review
TorchNPU-Bot
TorchNPU-BotCollaborators
Sep 9 Comments:

检测到当前 issue 已关联 PR,自动添加标签:bot-triaged

likedislike
chenrayray
chenrayrayCollaborators
Sep 29 Comments:

因为本单相关实施 PR 已全部结束(1 个已合入、0 个已关闭),按照本轮 issue 整理规则,先关闭此单。

核对的 PR:

如仍有问题或需要继续推进,请重新打开,并补充当前 PyTorch / TorchNPU / CANN 版本、硬件环境、复现步骤及相关日志;需求类请补充尚未完成的具体内容。

likedislike
chenrayraychenrayrayCollaborators
Sep 29 Close issue
chenrayraychenrayrayCollaborators
Sep 29 changed custom state from TODO to DONE
ascend-robotascend-robotCollaborators
Sep 29 Added label: resolved