已关闭
flash_attn.cpp 中条件编译路径存在头文件引用不一致风险 #4407
崇理战队创建于  27 天前关闭于  11 天前
崇理战队
27 天前 创建

问题描述

attention/flash_attn/op_kernel/flash_attn.cpp 中,存在以下条件编译代码:

#if __has_include("../../common/op_kernel/arch35/flash_attention_score_common_regbase.h")
#include "../../common/op_kernel/arch35/flash_attention_score_common_regbase.h"
#else
#include "../common/arch35/flash_attention_score_common_regbase.h"
#endif

这种基于 __has_include 的条件编译方式存在以下风险:

  1. 路径依赖脆弱../../common/../common/ 的相对路径依赖于文件在目录结构中的精确位置,一旦目录结构调整(如算子目录重组),可能导致编译失败。
  2. 可读性差:其他开发者难以快速判断最终引用的是哪个路径下的头文件。
  3. 与 ops-transformer 其他算子的 include 风格不一致:大部分算子直接使用 #include "common/..."#include "../common/..."

建议方案

  1. 统一使用 CMake 的 target_include_directories 管理头文件搜索路径,避免在源码中使用相对路径条件编译。
  2. 如果必须保留条件编译逻辑,建议增加注释说明两种路径分别适用的场景。
  3. 考虑将 flash_attention_score_common_regbase.h 提取到公共 include 目录,统一引用路径。

相关文件

  • attention/flash_attn/op_kernel/flash_attn.cpp
  • attention/common/arch35/flash_attention_score_common_regbase.h
likedislike
weihao18成员
27 天前 评论:

/assign @jiang-lirui

likedislike
CANN-robotCANN-robot成员
27 天前 将 jiang-lirui 设为负责人
jiang-lirui成员
27 天前 评论:

感谢你的建议!需要解释的是:这并不是在include两份文件,else分支是为在线编译场景准备的,因打包路径问题,在该场景下源码路径发生了变化;但是我们已经意识到这种写法不太合适,将推动编译工程适配后,修改为实际所见路径。

likedislike
Jjiang-lirui成员
14 天前 将 hblnb 设为负责人
huangbolinhuangbolin成员
14 天前 关联了pull request:修改cann包下算子kernel源码的路径,多加一级op_kernel
huangbolinhuangbolin成员
14 天前 删除了关联的pull request:修改cann包下算子kernel源码的路径,多加一级op_kernel
huangbolinhuangbolin成员
13 天前 关联了pull request:修改cann包下算子kernel源码的路径,多加一级op_kernel
游震成员
13 天前 添加了label:feature
huangbolin
huangbolin成员
13 天前 评论:

已关联pr,目前基于该pr出的包里面的文件结构和代码仓一致,验证功能中

likedislike
Jjiang-lirui成员
13 天前 关联了pull request:refactor(fa): 删除op_kernel头文件的__has_include守卫, 统一使用attention/common路径
CANN-robotCANN-robot成员
11 天前 关闭了 issue
CANN-robotCANN-robot成员
11 天前 添加了label:resolved