Merged
docs: add torch.nn.attention.flex_attention.or_masks support status #43375
docs: add torch.nn.attention.flex_attention.or_masks support status #43375
Merged
zhangjiahao-2026Add on Jul 30
zhangjiahao-2026
Jul 30

【合入来源】

关联任务issue: https://gitcode.com/Ascend/pytorch/issues/3293

【修改方案】

在 master 资料中为公开非计算类 API torch.nn.attention.flex_attention.or_masks 补齐支持情况。

修改文件:

  • docs/zh/api/native_api/pytorch_2-7-1/torch-nn-attention.md
  • docs/zh/api/native_api/pytorch_2-11-0/torch-nn-attention.md
  • docs/zh/api/native_api/pytorch_2-12-0/torch-nn-attention.md
  • docs/zh/api/native_api/pytorch_2-13-0/torch-nn-attention.md

具体修改内容:

  1. 在PyTorch 2.7.1、2.11.0、2.12.0和2.13.0资料中新增torch.nn.attention.flex_attention.or_masks条目;
  2. 按照PyTorch官方资料的层级和顺序,将该API归入BlockMask Utilities,并放置在BlockMask之前;
  3. Atlas A2训练系列产品、Atlas A3训练系列产品和Ascend 950DT均标记为支持;
  4. 该API为非计算类纯Python接口,与具体数据类型无关,不填写数据类型限制。

本PR仅补充目标API的资料,不批量修改同一文件中其他历史API的资料格式。

【资料变更】

资料检查结果:此前 master 下的原生 API 资料未收录
torch.nn.attention.flex_attention.or_masks,因此需要补充。

本次在以下版本目录的 torch-nn-attention.md 中新增支持情况:

  • PyTorch 2.7.1
  • PyTorch 2.11.0
  • PyTorch 2.12.0
  • PyTorch 2.13.0

该API为公开非计算类纯Python接口,与张量数据类型无关,不填写数据类型限制。文档条目按照PyTorch官方资料的层级和顺序归入BlockMask Utilities。

【接口变更】

不涉及

【功能验证】

torch.nn.attention.flex_attention.or_masks(*mask_mods)用于组合一个或多个
mask_mod,返回一个新的掩码函数,并对所有输入mask的返回结果执行逻辑或。

PyTorch官方社区在以下用例中已有or_masks基本逻辑或语义验证:

test/inductor/test_flex_attention.py::test_mask_mod_combiners

or_masks为纯Python组合接口,不包含NPU专属kernel或设备分支,不需要修改
Torch-NPU功能实现,也不需要新增测试文件或test_upstream patch。

API功能、社区用例情况、NPU适配分析和各版本验证结果见说明Issue:

https://gitcode.com/Ascend/pytorch/issues/4082

A5硬件验证信息如下。

验证设备:
QQ20260831-105536.png

验证环境:

 torch:2.12.0+cpu
 torch_npu:2.12.0
 Python:3.12.13

验证命令:

python /tmp/test_or_masks_a5.py

验证场景:

  1. 单个mask_mod返回True和False;
  2. 多个mask_mod执行逻辑或;
  3. 零个mask_mod返回位于NPU上的bool标量False;
  4. 非callable输入触发预期RuntimeError;
  5. 返回值dtype为torch.bool,设备类型为npu。

运行截图:
QQ20260831-105848.png

验证结论:

torch.nn.attention.flex_attention.or_masks在上述A5环境中运行正常,单mask、
多mask、零mask和异常入参场景均验证通过,返回类型及返回设备符合PyTorch实现。

【CheckList】

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

likedislike
Pull Request has been successfully merged by ascend-robot.
(Thank you for zhangjiahao-2026's contribution.)
zhangjiahao-2026zhangjiahao-2026
Jul 30 Created pull request, commit 61466a16
zhangjiahao-2026zhangjiahao-2026
Jul 30 Associated issue: 【社区任务】7月社区任务第三期-Ascend for PyTorch API 一致性开发(180)
atomgit-bot
atomgit-bot
Jul 30 Comments:

变更摘要

本次 PR 是一个纯资料更新,为五个版本的中文 API 支持表补齐 torch.nn.attention.flex_attention.or_masks 的支持状态。该接口是 FlexAttention 的 mask 组合工具函数(纯 Python,非计算类 API),用于将多个 mask_mod 组合为逻辑或(union)结果。所有五个版本均标记为"支持",无限制说明,无数据类型要求。

主要改动

  • 在 docs/zh/api/native_api/pytorch_2-7-1/torch-nn.md 中新增支持条目:添加 torch.nn.attention.flex_attention.or_masks 行,标记支持为"是",限制与说明和数据类型均为"-"
  • 在 docs/zh/api/native_api/pytorch_2-9-0/torch-nn.md 中新增支持条目:同上,补齐该版本对 or_masks 的支持状态
  • 在 docs/zh/api/native_api/pytorch_2-10-0/torch-nn.md 中新增支持条目:同上,补齐该版本的支持信息
  • 在 docs/zh/api/native_api/pytorch_2-11-0/torch-nn.md 中新增支持条目:同上,补齐该版本的支持信息
  • 在 docs/zh/api/native_api/pytorch_2-12-0/torch-nn.md 中新增支持条目:同上,补齐该版本的支持信息
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Jul 30 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
663 messages are collapsed here。 See more
群青世界群青世界Collaborators
Oct 10 Resolved final issue
ascend-robotascend-robotCollaborators
Oct 10 Closed associated issue
ascend-robotascend-robotCollaborators
Oct 10 Merged pull request
ascend-robot
ascend-robotCollaborators
Oct 10 Comments:

Pull Request 已合并或已关闭。

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

likedislike
AtlasAccount
AtlasAccountCollaborators
Oct 10 Comments:
流水线 pytorch_gitcode_PR_multiVersion-onetrack#315 [ commitID:276060f0 ] 已完成
likedislike