已合并
sdpa use component impl when the last dim of input is greater than 256 #5578
XianglongZeng创建于 24 天前
sdpa use component impl when the last dim of input is greater than 256 #5578
已合并
XianglongZeng创建于 24 天前
XianglongZeng成员
24 天前

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改方案】

在尾轴大于256的情况下,若用户对精度要求高,export TORCH_NPU_USE_COMPATIBLE_IMPL=1,牺牲一定性能和显存,内部实现和torch_gpu走一致实现,调小算子。若用户对性能要求高,则保持现状,调融合大算子。

【资料变更】

不涉及

【接口变更】

不涉及

【功能验证】

image.png
image.png
image.png

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 XianglongZeng 的贡献)
XXianglongZeng成员
24 天前 创建了 pull request,commit 38483749
atomgit-bot
atomgit-bot
24 天前 评论:

变更摘要

此 PR 主要针对 scaled_dot_product_attention 算子,在输入张量最后一维(尾轴)大于 256 的场景下,当用户通过 torch_npu.npu.use_compatible_impl(True) 启用兼容实现模式时,强制走 math 路径(force_math = true),以调用与 torch_gpu 一致的小算子实现,从而保证高精度;若不启用该模式则保持原有融合大算子实现以优先性能。

主要改动

  • ScaledDotProductAttentionKernelNpuOpApi.cpp 新增尾维大于 256 时的 force_math 判断: 在两处 force_math 赋值逻辑中,新增对 query.size(-1) > 256 || key.size(-1) > 256 || value.size(-1) > 256 的检查,满足条件时强制将 force_math 设为 true,使算子退避到 component/math 实现路径。
  • 新增测试用例 test_sdpa_last_dim_greater_than_256: 在 test_scaled_dot_product_attention.py 中增加了尾维为 257 的测试场景,先调用 use_compatible_impl(True) 启用兼容模式,然后对比 CPU(fp32)与 NPU(fp16)输出精度,验证大尾维场景下兼容实现的正确性。
likedislike
atomgit-bot
atomgit-bot
24 天前 评论:

代码审查

✅ 未发现问题

likedislike
XXianglongZeng成员
24 天前 修改了pull request 的描述
此处折叠了55条消息 查看更多
chengpeng25成员
21 天前 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
21 天前 添加了label:lgtm
ascend-robotascend-robot成员
21 天前 关闭了关联的issue
ascend-robotascend-robot成员
21 天前 合入了pull request
ascend-robot
ascend-robot成员
21 天前 评论:

Pull Request 已合并或已关闭。

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

likedislike