已合并
test: add math SDP fp16 bf16 reduction API validation #41428
test: add math SDP fp16 bf16 reduction API validation #41428
已合并
zhaoziyi-2026创建于 7月13日
zhaoziyi-2026
7月13日

【合入来源】

关联 issue:

【修改方案】

本 PR 在已有 backend 测试文件 test/npu/test_torch_backends.py 中补充 torch._C._set_math_sdp_allow_fp16_bf16_reduction 的 API 一致性验证用例。

torch._C._set_math_sdp_allow_fp16_bf16_reduction 是 PyTorch torch._C 内部状态 setter,用于设置 math SDP 是否允许 fp16 / bf16 reduction。经检查,PyTorch 官方未提供该 API 的直接专项测试,torch-npu 当前 test 目录中也未发现该 API 的专项测试。因此本 PR 按指导规范在 torch-npu test 目录补充自写测试。

任务 API 功能、上游社区用例情况与本 PR 处理方式如下:

API 功能说明 上游社区用例 / 覆盖情况 本 PR 处理
torch._C._set_math_sdp_allow_fp16_bf16_reduction PyTorch torch._C 内部状态 setter,用于设置 math SDP 是否允许 fp16 / bf16 reduction。 未检索到 PyTorch 官方直接专项测试;2.7.1 现有 test_upstream/test/test_transformers.py.patch 中涉及上层 wrapper torch.backends.cuda.allow_fp16_bf16_reduction_math_sdp,但不是目标 _C setter 的直接专项测试。 在已有 backend 测试文件 test/npu/test_torch_backends.py 中补充用例,验证底层 setter / getter 状态切换、上层 backend wrapper 联动和异常输入路径。

主要开发思路:

  1. 先确认 torch._C._set_math_sdp_allow_fp16_bf16_reduction 和配套 getter torch._C._get_math_sdp_allow_fp16_bf16_reduction 在当前 torch-npu 环境中均存在。
  2. 复查 PyTorch 官方测试和 torch-npu 当前测试,未发现目标 _C setter 的直接专项测试。
  3. 复查 2.7.1 现有 test_upstream/test/test_transformers.py.patch,其中覆盖的是上层 wrapper 调用和 SDPA 输出行为,不是本任务目标 API 的直接验证。
  4. 该 API 不涉及 Tensor 输入,不存在需要迁移 Tensor 到 NPU 的适配点,因此不需要 test_upstream patch。
  5. 该 API 属于 math SDP 全局状态控制接口,当前仓库已有 test/npu/test_torch_backends.py 覆盖 flash_sdpmem_efficient_sdpmath_sdp 等 backend 开关,因此本 PR 将新增用例放入该文件。
  6. 本 PR 仅补充测试,不修改 API 实现,不改变现有接口行为。

修改测试文件:

test/npu/test_torch_backends.py

新增测试方法:

test_math_sdp_allow_fp16_bf16_reduction_setter
test_math_sdp_allow_fp16_bf16_reduction_backend_wrapper
test_math_sdp_allow_fp16_bf16_reduction_invalid_value

各测试核心验证点如下:

测试方法 核心验证点
test_math_sdp_allow_fp16_bf16_reduction_setter 验证 torch._C._set_math_sdp_allow_fp16_bf16_reduction(True / False) 可正确更新 torch._C._get_math_sdp_allow_fp16_bf16_reduction() 返回值。
test_math_sdp_allow_fp16_bf16_reduction_backend_wrapper 验证上层 wrapper torch.backends.cuda.allow_fp16_bf16_reduction_math_sdp(True / False) 控制的是同一个底层状态,不依赖 wrapper 返回值。
test_math_sdp_allow_fp16_bf16_reduction_invalid_value 验证传入非 bool 值时会抛出异常,且当前 math SDP flag 不被错误输入改变。

【资料变更】

不涉及资料 PR。

已检查 PyTorch 官方公开文档与 Ascend native API 文档,未发现 torch._C._set_math_sdp_allow_fp16_bf16_reduction 独立条目。该 API 属于 torch._C 私有接口,按指导文档不需要补充资料,因此不新增 docs.md,不提交资料 PR。

【接口变更】

不涉及。

本 PR 不修改 API 实现,不改变现有接口行为,不新增 torch-npu 对外接口。

【功能验证】

运行环境:

操作系统:Ubuntu 22.04
昇腾硬件信息:910B
CANN软件版本:9.0.0
安装的软件版本:torch 2.7.1,torch-npu 2.7.1

执行命令:

cd /root
python /root/pytorch/test/npu/test_torch_backends.py -k math_sdp_allow_fp16_bf16_reduction -v

执行结果:

test_math_sdp_allow_fp16_bf16_reduction_backend_wrapper (__main__.TorchBackendsApiTestCase) ... ok
test_math_sdp_allow_fp16_bf16_reduction_invalid_value (__main__.TorchBackendsApiTestCase) ... ok
test_math_sdp_allow_fp16_bf16_reduction_setter (__main__.TorchBackendsApiTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.823s

OK

最终验证结论:

  1. 本 PR 补充 3 个 API 一致性测试,覆盖 torch._C._set_math_sdp_allow_fp16_bf16_reduction 的正常状态切换、backend wrapper 联动和异常输入路径。
  2. 补充测试不仅验证 API 可调用,也验证 setter / getter 状态一致性、wrapper 对同一底层 flag 的控制能力,以及错误输入不会污染全局状态。
  3. 该 API 不涉及 Tensor 输入,不需要 NPU Tensor 迁移适配。
  4. 补充测试已在 2.7.1 环境验证通过。
  5. 目标 API 当前行为符合预期,未发现需要 API 功能补齐的问题。
  6. 本 PR 不修改 API 实现,不涉及接口变更。

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 zhaoziyi-2026 的贡献)
Zzhaoziyi-2026
7月13日 创建了 pull request,commit d44d6e3c
Zzhaoziyi-2026
7月13日 关联了issue:【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(45)
atomgit-bot
atomgit-bot
7月13日 评论:

变更摘要

本 PR 在已有后端测试文件 test/npu/test_torch_backends.py 中新增了针对 torch._C._set_math_sdp_allow_fp16_bf16_reduction 及其配套 getter 的 API 一致性验证用例,覆盖 setter/getter 基本行为、与 torch.backends.cuda.allow_fp16_bf16_reduction_math_sdp 公共 wrapper 的联动,以及非法输入的错误处理。

主要改动

  • 新增 test_math_sdp_allow_fp16_bf16_reduction_setter 用例:验证 torch._C._set_math_sdp_allow_fp16_bf16_reduction 设置 True/False 后,torch._C._get_math_sdp_allow_fp16_bf16_reduction 能正确返回对应状态,并通过 addCleanup 恢复原始值。
  • 新增 test_math_sdp_allow_fp16_bf16_reduction_backend_wrapper 用例:验证公共 wrapper torch.backends.cuda.allow_fp16_bf16_reduction_math_sdp 与底层 getter 操作的是同一标志位,且 wrapper 调用返回 None
  • 新增 test_math_sdp_allow_fp16_bf16_reduction_invalid_value 用例:验证传入非布尔值(如 1None)时,setter 抛出 RuntimeError("expects a bool"),且当前标志位状态不受影响。
  • 文件头部新增模块文档字符串:为 test/npu/test_torch_backends.py 添加了描述 NPU SDP 开关和 math SDP fp16/bf16 reduction 标志位测试的模块级注释。
likedislike
atomgit-bot
atomgit-bot
7月13日 评论:

代码审查

✅ 未发现问题

likedislike
此处折叠了82条消息 查看更多
chenrayray
chenrayray成员
7月24日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
7月24日 添加了label:lgtm
ascend-robotascend-robot成员
7月24日 删除了label:ci-pipeline-passed
ascend-robotascend-robot成员
7月24日 合入了pull request
ascend-robot
ascend-robot成员
7月24日 评论:
流水线 pytorch_gitcode_PR_multiVersion#13111 [ commitID:f41d7186 ] 已完成
likedislike