已合并
test: add math SDP fp16 bf16 reduction API validation #41428
zhaoziyi-2026创建于 7月13日
test: add math SDP fp16 bf16 reduction API validation #41428
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 zhaoziyi-2026 的贡献)7月13日 创建了 pull request,commit d44d6e3c
7月13日 关联了issue:【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(45)
atomgit-bot
7月13日 评论:
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用例:验证公共 wrappertorch.backends.cuda.allow_fp16_bf16_reduction_math_sdp与底层 getter 操作的是同一标志位,且 wrapper 调用返回None。 - 新增
test_math_sdp_allow_fp16_bf16_reduction_invalid_value用例:验证传入非布尔值(如1、None)时,setter 抛出RuntimeError("expects a bool"),且当前标志位状态不受影响。 - 文件头部新增模块文档字符串:为
test/npu/test_torch_backends.py添加了描述 NPU SDP 开关和 math SDP fp16/bf16 reduction 标志位测试的模块级注释。


ascend-robot
7月13日 评论:
7月13日 评论:
atomgit-bot
7月13日 评论:
7月13日 评论:
此处折叠了82条消息 查看更多
chenrayray
7月24日 评论:
7月24日 评论:
/lgtm


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


【合入来源】
关联 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是 PyTorchtorch._C内部状态 setter,用于设置 math SDP 是否允许 fp16 / bf16 reduction。经检查,PyTorch 官方未提供该 API 的直接专项测试,torch-npu 当前test目录中也未发现该 API 的专项测试。因此本 PR 按指导规范在 torch-nputest目录补充自写测试。任务 API 功能、上游社区用例情况与本 PR 处理方式如下:
torch._C._set_math_sdp_allow_fp16_bf16_reductiontorch._C内部状态 setter,用于设置 math SDP 是否允许 fp16 / bf16 reduction。test_upstream/test/test_transformers.py.patch中涉及上层 wrappertorch.backends.cuda.allow_fp16_bf16_reduction_math_sdp,但不是目标_Csetter 的直接专项测试。test/npu/test_torch_backends.py中补充用例,验证底层 setter / getter 状态切换、上层 backend wrapper 联动和异常输入路径。主要开发思路:
torch._C._set_math_sdp_allow_fp16_bf16_reduction和配套 gettertorch._C._get_math_sdp_allow_fp16_bf16_reduction在当前 torch-npu 环境中均存在。_Csetter 的直接专项测试。test_upstream/test/test_transformers.py.patch,其中覆盖的是上层 wrapper 调用和 SDPA 输出行为,不是本任务目标 API 的直接验证。test_upstreampatch。test/npu/test_torch_backends.py覆盖flash_sdp、mem_efficient_sdp、math_sdp等 backend 开关,因此本 PR 将新增用例放入该文件。修改测试文件:
新增测试方法:
各测试核心验证点如下:
test_math_sdp_allow_fp16_bf16_reduction_settertorch._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_wrappertorch.backends.cuda.allow_fp16_bf16_reduction_math_sdp(True / False)控制的是同一个底层状态,不依赖 wrapper 返回值。test_math_sdp_allow_fp16_bf16_reduction_invalid_value【资料变更】
不涉及资料 PR。
已检查 PyTorch 官方公开文档与 Ascend native API 文档,未发现
torch._C._set_math_sdp_allow_fp16_bf16_reduction独立条目。该 API 属于torch._C私有接口,按指导文档不需要补充资料,因此不新增docs.md,不提交资料 PR。【接口变更】
不涉及。
本 PR 不修改 API 实现,不改变现有接口行为,不新增 torch-npu 对外接口。
【功能验证】
运行环境:
执行命令:
cd /root python /root/pytorch/test/npu/test_torch_backends.py -k math_sdp_allow_fp16_bf16_reduction -v执行结果:
最终验证结论:
torch._C._set_math_sdp_allow_fp16_bf16_reduction的正常状态切换、backend wrapper 联动和异常输入路径。【CheckList】