已关闭
[Bug-Report|缺陷反馈]: ProcessSoftmaxLse 辅助函数(4 处副本)在 softmaxLseFlag=true 且 softmaxLse=nullptr 时缺前置判空,nullptr 直传 inner(FIA 链路有 inner 层兜底不崩溃,3 个旁支接口依赖自动生成代码待确认) #5554
liuhanwen8创建于  12 天前关闭于  4 天前
liuhanwen8
liuhanwen8
12 天前 创建

Thanks for sending an issue! Please fill in the following template to help quickly solve your problem.

Describe the current behavior / 问题描述 (Mandatory / 必填)

fused_infer_attention_score_inner.cpp :186-:199 定义的共用辅助函数 FusedInferAttentionScoreProcessSoftmaxLse,在 softmaxLseFlag == true 分支直接 placeHolder = softmaxLse,不判空;softmaxLse == nullptr 时 nullptr 被原样下传至 inner GetWorkspaceSize。同一段逻辑共存在 4 份副本,全部缺少判空。

先说结论,避免误判严重级:FIA 主链路(V1–V4,V5 高置信同链路)在 inner 入口存在兜底判空(见“二、行为核实”),nullptr 不会到达 l0/kernel,不会崩溃,本单不是崩溃缺陷,实际主张为以下三点:

  1. helper 层“该拦的没拦在最早处”:组合参数(softmaxLseFlag=true 且 softmaxLse=nullptr)的非法组合应在 helper 层 fail-fast 并给出明确组合语义的报错;当前报错点远离真实出错点(跨包装层),且 inner 层的 OP_CHECK_NULL 文案未覆盖该组合语义;
  2. 三个旁支接口(flash_mla_with_kvcache / flash_attn / quant_flash_attn)转发到 build/autogen 自动生成的 inner(libcust_opapi.so),本仓源码内无法静态确认其对 nullptr softmaxLse 的处理;op_host 侧 SoftmaxLSEChecker 只校验 flag 取值与 lse_out 存在性(flash_attn/op_host/checkers/softmax_lse_checker.cpp :66-:77),存在框架层解引用风险,需框架侧或实机确认;
  3. 同源逻辑 4 份副本(V5 又内联出第 3 种写法)已构成复制粘贴漂移风险,建议收敛为 1 份。

一、缺陷代码与副本分布

master 21983cdf(2026-09-21 逐行核验;与 2026-09-18 基线 12a3dd6617 一致):

// attention/fused_infer_attention_score/op_api/aclnn_fused_infer_attention_score_inner.cpp :186-:199
void FusedInferAttentionScoreProcessSoftmaxLse(bool softmaxLseFlag, const aclTensor *softmaxLse,
                                               const aclTensor *&tempTensor, const aclTensor *&placeHolder)
{
    if (softmaxLseFlag == false) {
        std::vector<int64_t> shape = {0};
        int64_t addr = 0xff;
        tempTensor = aclCreateTensor(shape.data(), shape.size(), aclDataType::ACL_FLOAT, shape.data(), 0,
                                     ACL_FORMAT_ND, shape.data(), shape.size(), static_cast<void *>(&addr));
        placeHolder = tempTensor;
    } else {
        placeHolder = softmaxLse;      // ← 未判空,nullptr 原样下传
    }
}

副本与调用点分布:

# 文件 辅助函数 调用点
1 fused_infer_attention_score/op_api/aclnn_fused_infer_attention_score_inner.cpp FusedInferAttentionScoreProcessSoftmaxLse(:186,共用实现) V1 :58;V2 :108/:191;V3 :135/:222;V4 :120/:205
2 flash_mla_with_kvcache/op_api/aclnn_flash_mla_with_kvcache.cpp FlashMlaWithKvcacheProcessSoftmaxLse(:44) :74
3 flash_attn/op_api/aclnn_flash_attn.cpp FlashAttnProcessSoftmaxLse(:44) :92
4 quant_flash_attn/op_api/aclnn_quant_flash_attn.cpp QuantFlashAttnProcessSoftmaxLse(:49) :108

旁证:aclnn_fused_infer_attention_score_v5.cpp 未复用共用函数,而是在 :132-:140 与 :222-:229 将同逻辑内联重写(同样无判空)——新代码已倾向绕开共用实现,但 4 份旧副本未收敛、未修正。

Describe the expected behavior / 预期结果 (Mandatory / 必填)

  1. helper 增加前置判空(函数返回值改为 aclnnStatus,或在各调用点补校验):
} else {
    if (softmaxLse == nullptr) {
        OP_LOGE(ACLNN_ERR_PARAM_NULLPTR,
                "When softmaxLseFlag is true, softmaxLse must be provided, but got nullptr.");
        return ACLNN_ERR_PARAM_NULLPTR;
    }
    placeHolder = softmaxLse;
}
  1. 收敛 4 份副本(含 V5 内联写法)为 1 份公共实现,避免后续改动继续漂移;
  2. 对三个旁支接口补充实机用例:returnSoftmaxLse=1 且 softmaxLse=nullptr,期望返回 ACLNN_ERR_PARAM_NULLPTR(161001)而非崩溃;若确认自动生成 inner 已兜底,可在本单回填结论后按需关闭。

不涉及(host 侧静态审查,未上板)。全部证据为源码行号引用与代码片段,见问题描述;旁支三接口运行时行为待实机确认。

likedislike
liuhanwen8liuhanwen8
12 天前 修改了issue 的描述
liuhanwen8liuhanwen8
12 天前 修改了issue 的描述
游震成员
12 天前 评论:

您好,感谢反馈!

问题分析

1. helper 缺前置判空,核实属实。 FusedInferAttentionScoreProcessSoftmaxLse(attention/fused_infer_attention_score/op_api/aclnn_fused_infer_attention_score_inner.cpp:186-198)在 softmaxLseFlag == true 分支将 softmaxLse 原样下传,无判空;三个旁支副本(aclnn_flash_mla_with_kvcache.cpp:44-56、aclnn_flash_attn.cpp:44-56、aclnn_quant_flash_attn.cpp:49-61)与 V5 的两处内联写法(aclnn_fused_infer_attention_score_v5.cpp:130-140、:220-230)为同型逻辑,同样无判空。

2. FIA 主链路无崩溃,与您的核实一致。 手写 inner 实现 InnerFusedInferAttentionScoreGetWorkspaceSizeImpl(同文件 :690)入口 :726-728 在 softmaxLseFlag 为真时执行 OP_CHECK_NULL(softmaxLse, ...),nullptr 不会下发 l0,接口返回 161001。您提到的报错语义问题也成立:该宏打印的是通用文案(“Expected a value of type [...] for argument [softmaxLse] but instead found nullptr.”),未携带“flag=true 时 softmaxLse 不可为空”的组合条件。补充一个链路细节:仓内源码中,V1 源文件直连手写 Inner* 转发链(aclnn_fused_infer_attention_score.cpp:60,经 inner.cpp:896 汇入 Impl),而 V2–V5 的转发均经 aclnnInner* 封装(由构建工具在 build/autogen/inner 生成,仓内无源码)——这一段与您对 V5 标注的“高置信”属同类情况,静态确证需结合构建产物;不过只要调用汇入上述手写入口,:726-728 的拦截即有效,与您的行为核实结果一致。

3. 旁支三接口:tiling 层拦截已核实,剩余缺口在 autogen op_api 层。 您提到的 SoftmaxLSEChecker 存在性校验,我们补充核实了其实际生效位置:三个算子的 tiling 入口都会先执行 checker 链(flash_attn_tiling.cpp:60-64、quant_flash_attn_tiling.cpp:60-63、flash_mla_with_kvcache_tiling.cpp:60-63),其中 SoftmaxLSE checker 对“开关开启且 lse_out 为空”的拦截文案带组合语义(flash_attn 的 softmax_lse_checker.cpp:66-77:“When softmaxLSE is enabled, lse_out cannot be empty”;quant_flash_attn 的 CheckMultiPara :68-81、flash_mla_with_kvcache :66-77 同型),且三个算子的 tiling 信息解析对 lse_out shape 为空均有判空保护(flash_attn 解析 :567、quant_flash_attn 解析 :546、flash_mla_with_kvcache 解析 :605)。因此只要调用到达 op_host tiling 层,该参数组合会被干净拒绝(GRAPH_FAILED + 明确报错)而非解引用崩溃。剩余不确定性与您的判断一致:自动生成的 op_api inner 层在进入 tiling 前是否解引用 nullptr,仓内源码无法覆盖,需实机或框架侧确认。

处理方向

  1. 前置判空:您给出的方向可行——在 helper 层补组合校验(函数签名改为返回 aclnnStatus)或在各调用点校验,报错明确指向“softmaxLseFlag=true 且 softmaxLse=nullptr”的组合条件。这不改变 FIA 主链路既有行为(仍返回 161001),收益在于报错位置前移、语义可定位,并与 tiling 层现有文案风格保持一致。
  2. 副本收敛:4 份副本加 V5 两处内联写法的漂移风险确实存在(V5 已是第三种写法)。需要说明的约束是:4 个 helper 分属 4 个独立算子目录、构建单元各自独立,flag 形参类型也有差异(int64_t 与 bool),收敛为一份公共实现需要权衡公共代码归属与跨算子构建依赖;FIA 家族内部先行收敛(V1–V4 已共用 fused_infer_attention_score_inner.h 中的实现,V5 改为复用)成本较低。这一方向可与第 1 点一并评估。
  3. 旁支验证:您提出的用例(returnSoftmaxLse 开启且 softmaxLse=nullptr,期望返回 161001 而非崩溃/UB)是闭合剩余不确定性的直接方式。我们也会继续确认三个旁支接口在自动生成 inner 层的实际行为,有结论后在此同步。

再次感谢您详尽的分析与严谨的核实。

likedislike
游震成员
11 天前 评论:

fused_infer_attention_score @yue-ma
flash_mla_with_kvcache @xtqh
flash_attn @haijie_699874
quant_flash_attn @shen_weiling

likedislike
游游震成员
11 天前 将 xtqh、shen_weiling、haijie_699874、L_Euler 设为负责人
游游震成员
11 天前 移除了负责人 L_Euler
游游震成员
11 天前 将 yue-ma 设为负责人
wym_666成员
11 天前 评论:

/assign

likedislike
CANN-robotCANN-robot成员
11 天前 将 wym_666 设为负责人
Wwym_666成员
10 天前 关联了pull request:在 softmaxLseFlag=true 且 softmaxLse=nullptr 时缺前置判空
Yyue-ma成员
6 天前 移除了负责人 yue-ma
PerrySkywalkerPerrySkywalker成员
5 天前 关联了pull request:fix(attention): FlashAttn/FlashMLA 在算子内部校验 LSE 空输出
PerrySkywalkerPerrySkywalker成员
5 天前 关联了pull request:fix(flash_attn): [9.2.0] 在算子内部校验 LSE 空输出
Wwym_666成员
4 天前 关联了pull request:aclnnFusedInferAttentionScore在 softmaxLseFlag=true 且 softmaxLse=nullptr 时缺前置判空
CANN-robotCANN-robot成员
4 天前 关闭了 issue
CANN-robotCANN-robot成员
4 天前 添加了label:resolved