Merged
[feat][ops]fa_support_32_64_128_256_dim_1 #247
cwh19980101Add on 3 days ago
[feat][ops]fa_support_32_64_128_256_dim_1 #247
Merged
cwh19980101Add on 3 days ago
cwh19980101Collaborators
3 days ago

合入来源

问题/功能描述

任意mask的FA算子支持32/64/128/256 embedding
母PR:https://gitcode.com/Ascend/ops-rec/pull/244

修改方案描述

本地化catlass组件,支持动态embed32/64/128/256 embed,分为128/256档
128档:处理32/64/128 embed
256档:处理256 embed, n-chunk,256档由于UB不够,使用2 chunk,每次处理128 embed,共同组成256 embed结果。

是否涉及UT/ST

开发自检

likedislike
Pull Request has been successfully merged by ascend-robot.
(Thank you for cwh19980101's contribution.)
Ccwh19980101Collaborators
3 days ago Associated milestone: ops-rec 26.3.0
Ccwh19980101Collaborators
3 days ago Associated issue: [Feature]: FA支持32/64/128/256embedding
atomgit-bot
atomgit-bot
3 days ago Comments:

变更摘要

本 PR 将 fa_infer_with_arbitrary_func 的 embed(head dim)支持从固定 128 扩展为 32/64/128/256 四档:host 侧 tiling 与 torch extension 校验放开维度限制,并通过 ASCENDC_TPL_SEL_PARAM 按 embed 选择不同的二进制 key,kernel 侧以 binId 模板参数分成三条独立实例化路径(128 档沿用原实现、32/64 档输出半轴运行时化、256 档走新增的 Emb256 n-chunk 实现),同时补充对应 pytest 维度用例。

主要改动

  • 对外维度校验放开:TilingFunc 用 EMBED_SUPPORTED 替换原有的 embed != 128 判断,fa_infer_with_arbitrary_func_plugin.cpp 用 HEAD_DIM_SUPPORTED 与 checkHeadDim 允许 q/k/v 最后一维取 32/64/128/256,并新增 q/k/v 三者维度必须一致的检查。
  • 按 embed 选择编译 key:新增 fa_infer_with_arbitrary_func_key.h,通过 ASCENDC_TPL_ARGS_DECL / ASCENDC_TPL_SEL 声明 binId(0/1/2),host 侧按 embed == 128 -> 1、embed < 128 -> 2、embed > 128 -> 0 调用 ASCENDC_TPL_SEL_PARAM。
  • kernel 入口模板化分派:fa_infer_with_arbitrary_func.cpp 中 kernel 函数改为 template <int binId>,binId == 1 走原 FAInferTla,binId == 2 走 FAInferTla 的 RUNTIME_O=true 版本,其余走新增的 Emb256::FAInferTla256。
  • 新增 256 档实现:新文件 fa_infer_embed256_kernel.h 定义 Emb256 命名空间下的 FAInferKernel 与 FAInferTla256,以 CHUNK_WIDTH = 128 将 embed 切成 N_CHUNK 个 chunk,bmm2 双 slot 每 slot 32KB,并用 CHUNK_BARRIER / CHUNK_ACK 跨核 barrier 保证 chunk 间流水排空。
  • 输出行宽运行时化与排空事件:FAInferKernel 新增 RUNTIME_O 模板参数与 OWidth(),EpilogueRescaleO 通过 std::conditional_t 在 BlockEpilogueFARescaleORuntime 与原 BlockEpilogue 间选择;新增 FA_DRAIN_RES_EVENT = 15,在尾部循环用 mm1ResReal / notLastTwoLoop 切换排空事件,并在提前返回与结尾补充 PipeBarrier<PIPE_ALL>。
  • 测试覆盖与可复现性:pytest 的 D 参数由 [128] 扩展为 [32, 64, 128, 256],并新增 RANDOM_SEED 与 autouse fixture _reset_random_seed 固定随机种子以保证 fp16 比对结果可重现。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
3 days ago Comments:

代码审查

审查总结

发现统计:P0 0 / P1 0 / P2 2 / P3 3

逐个文件核查结论:

文件 结论
op_host/fa_infer_with_arbitrary_func.cpp 无问题(embed 白名单 + binId 映射与 key.h、kernel 分支三者一致;faInfo.headSize 在 fa_tiling.h 中仅用于 set_embed,无 128 写死假设)
op_kernel/CMakeLists.txt 无问题(OP_TYPE/KERNEL_FILE 收窄只影响源码选择,头文件仍被入口文件包含;因 CANN 侧 npu_op_kernel_sources 实现不在可访问范围内,未对签名合法性下结论)
op_kernel/fa_infer_embed256_kernel.h 2 项:P2 L359(arb-func 索引导卫用 actualQSeqSize,尾部 drain 迭代越界读 full/mask_block_cnt);P3 L783(rescale epilogue 以容量档 256 当 D_BASE_SIZE,多占 32KB UB 且失去常量步长快路径)
op_kernel/fa_infer_with_arbitrary_func.cpp 无问题(三路 binId 分派与 RUNTIME_O 模板实参对应正确)
op_kernel/fa_infer_with_arbitrary_func_kernel.h 无问题(OWidth() 三处使用点(AIC 输出布局/PV actualShape/AIV rescale 布局)与 GM 输出列偏移一致;mm1ResReal 与 FA_DRAIN_RES_EVENT 的置位/消费配对已按 2-slot ring 逐任务推演,尾块 drain 均对应不存在的等待者,跨 chunk 通过非末 chunk 不 drain 保持预置 credit,逻辑自洽)
op_kernel/fa_infer_with_arbitrary_func_kernel_utils.h 无问题(FA_DRAIN_RES_EVENT=15 与既有 0~10、emb256 的 11/12 无冲突)
op_kernel/fa_infer_with_arbitrary_func_key.h 无问题(TPL 声明/SEL 列表与 host binId 取值集合一致)
test/pytest/test_fa_infer_with_arbitrary_func.py 3 项:P2 L384(新增档位未进入 @pytest.mark.ci 用例,binId 0/2 路径无 CI 覆盖);P3 L384(组合数 256→1024,CPU 参考耗时膨胀约 4 倍);P3 L396(注释声称的 fp16 重测回退不存在)
torch_extension/fa_infer_with_arbitrary_func_plugin.cpp 无问题(HEAD_DIM_SUPPORTED 校验顺序正确,先逐张量档位校验再做 q/k/v 一致性校验,无残留 EXPECTED_HEAD_DIM 引用)

整体风险判断:中等偏低。 改动主体(embed 32/64 走 runtime rescale、embed 256 走 n-chunk 双 slot + 跨 chunk 屏障、host 侧 TPL key 分派)设计自洽,host/plugin/kernel/key 四处的档位契约一致;主要实质风险集中在 emb256 新文件与主 kernel 的一处守卫不一致(越界 GM 读),以及新档位完全没有 CI 覆盖——即“新代码路径出错不会被流水线发现”。

类型 数量
🔴 阻塞 0
🟡 建议 2

💬 仅评论

likedislike
Incorrect?
Ccwh19980101Collaborators
3 days ago Modify description of pull request
73 messages are collapsed here。 See more
lihao_huaweiCollaborators
3 days ago Comments:

/approve

likedislike
ascend-robotascend-robotCollaborators
3 days ago Added label: approved
ascend-robotascend-robotCollaborators
3 days ago Merged pull request
ascend-robot
ascend-robotCollaborators
3 days ago Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robotCollaborators
3 days ago Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike