Closed
【API 一致性任务】torch.nn.attention.flex_attention.or_masks 社区已有用例且无需额外 NPU 适配 #4082
zhangjiahao-2026Add on Aug 16Closed at Oct 10
Aug 16 associated Kanban: FrameworkPTAdapter 版本issue看板
Aug 17 Added label: help-wantedevent: api-consistencybot-triaged
TorchNPU-Bot
Aug 17 Comments:
Aug 17 Comments:
检测到社区任务相关 issue,自动添加标签:event: api-consistency、help-wanted


Aug 28 Modify description of issue
Sep 2 Modify description of issue
Sep 4 Modify description of issue
群青世界
Sep 7 Comments:
Sep 7 Comments:
缺少A5验证


Sep 9 Modify description of issue
zhangjiahao-2026
Sep 9 Comments:
Sep 9 Comments:


Oct 10 Close issue
Oct 10 changed custom state from TODO to CLOSED
Oct 10 Added label: resolved
API 一致性说明:
torch.nn.attention.flex_attention.or_masks社区已有用例且无需额外 NPU 适配关联任务
torch.nn.attention.flex_attention.or_masks一、API 功能
torch.nn.attention.flex_attention.or_masks(*mask_mods)用于组合一个或多个mask_mod,返回一个新的掩码函数。返回函数接收b、h、q_idx和kv_idx四个索引参数,对所有输入mask_mod的结果执行逐元素逻辑或。def causal_mask(b, h, q_idx, kv_idx): return q_idx >= kv_idx def batch_mask(b, h, q_idx, kv_idx): return b == 1 combined_mask = or_masks(causal_mask, batch_mask) result = combined_mask(b, h, q_idx, kv_idx)该 API 只有一个变长位置参数
*mask_mods。所有输入必须为 callable;单个mask_mod时返回该 mask 的结果,多个mask_mod时返回所有结果的逻辑或,非 callable 输入会触发
RuntimeError。当前实现允许零个mask_mod,返回函数的结果为 bool 标量
False。该 API 为纯 Python 组合接口,不包含独立的设备 kernel,也不直接执行
FlexAttention 计算。
二、测试用例完备性
2.1 PyTorch 官方社区用例
PyTorch 官方仓库以下版本的
test/inductor/test_flex_attention.py::test_mask_mod_combiners均包含torch.nn.attention.flex_attention.or_masks验证:or_masks验证位置v2.7.1v2.11.0v2.12.0v2.13.0main用例中的验证如下:
def causal_mask(b, h, q, kv): return q >= kv def neg_causal_mask(b, h, q, kv): return q < kv block_mask1 = create_block_mask( or_masks(causal_mask, neg_causal_mask), 1, 1, local_s, local_s, device=device, ) block_mask2 = create_block_mask( noop_mask, 1, 1, local_s, local_s, device=device, ) self.assertEqual(block_mask1.sparsity(), block_mask2.sparsity())causal_mask与neg_causal_mask为互补掩码,执行逻辑或后应与noop_mask等价。官方用例通过比较生成的BlockMask.sparsity()验证了or_masks的基本逻辑或语义。官方用例未直接覆盖单个 mask、多个不同 mask、零个 mask 和非 callable 入参,
因此直接 API 验证不够充分。根据最新 API 一致性规则,该 API 属于不涉及硬件
实现的纯 Python 接口,无需新增 NPU 测试文件,可在 Issue 中完成直接运行验证。
2.2 PyTorch 官方实现
各目标版本中的
or_masks实现逻辑一致:v2.7.1torch/nn/attention/flex_attention.py:L707-L720v2.11.0torch/nn/attention/flex_attention.py:L1087-L1100v2.12.0torch/nn/attention/flex_attention.py:L1361-L1374v2.13.0torch/nn/attention/flex_attention.py:L1630-L1643maintorch/nn/attention/flex_attention.py:L1759-L1772核心实现如下:
def or_masks(*mask_mods: _mask_mod_signature) -> _mask_mod_signature: """Returns a mask_mod that's the union of provided mask_mods""" if not all(callable(arg) for arg in mask_mods): raise RuntimeError(f"All inputs should be callable mask_mods: {mask_mods}") def or_mask(b, h, q_idx, kv_idx): result = b.new_zeros((), dtype=torch.bool) for mask in mask_mods: result = result | mask(b, h, q_idx, kv_idx) return result return or_mask该实现使用
b.new_zeros在输入 Tensor 所在设备创建 bool 标量,随后调用各个mask 并执行逻辑或。实现中没有 CUDA、NPU 或其他设备专属分支,也没有需要在
torch_npu中单独注册的 API 实现。2.3 NPU 适配判断
使用 NPU Tensor 调用
or_masks返回的函数时,初始 bool 标量、mask 比较结果和最终逻辑或结果均位于 NPU 上。API 主体不依赖 NPU 专属 kernel,不需要增加
NPU 设备参数、修改断言或覆盖 CUDA/Triton 平台判断。
因此,
torch.nn.attention.flex_attention.or_masks无需额外 NPU 适配。三、API 及测试用例适配
3.1 API 适配
现有实现已经满足以下一致性要求:
torch.bool;RuntimeError。因此,无需修改
torch_npuAPI 实现。3.2 测试用例适配
PyTorch 官方已有
test_mask_mod_combiners。该测试文件整体受 Inductor、CUDA/Triton 和
supported_platform等平台条件影响。为单独验证or_masks而解除整个测试模块的平台门禁,会影响同文件中的其他 Inductor 用例,超出本 API
的最小适配范围。
同时,
or_masks为纯 Python API。按照最新规则,本任务无需新增test/测试文件,无需修改官方用例,也无需新增或修改
test_upstreampatch。3.3 现有 test_upstream patch 检查
已检查相关分支中的
test_upstream/test/inductor/test_flex_attention.diff或对应 patch。
v2.7.1曾存在一个覆盖整个上游测试文件的适配 diff,其中导入transfer_to_npu、设置_dynamo.use_jit_script,并覆盖torch.cuda.get_device_capability。这些全局修改会影响同文件中的其他FlexAttention/Inductor 用例,适配范围超过
or_masks的验证需要。v2.11.0和v2.12.0未发现必须为or_masks单独保留的相关 patch。根据最新规则,本任务不新增或修改 NPU 适配 patch。
四、检索记录
在 PyTorch 官方仓库检查 API 实现和社区用例:
git grep -n \ "def or_masks\|or_masks(causal_mask, neg_causal_mask)\|def test_mask_mod_combiners" \ v2.7.1 -- \ torch/nn/attention/flex_attention.py \ test/inductor/test_flex_attention.py git grep -n \ "def or_masks\|or_masks(causal_mask, neg_causal_mask)\|def test_mask_mod_combiners" \ v2.11.0 -- \ torch/nn/attention/flex_attention.py \ test/inductor/test_flex_attention.py git grep -n \ "def or_masks\|or_masks(causal_mask, neg_causal_mask)\|def test_mask_mod_combiners" \ v2.12.0 -- \ torch/nn/attention/flex_attention.py \ test/inductor/test_flex_attention.py git grep -n \ "def or_masks\|or_masks(causal_mask, neg_causal_mask)\|def test_mask_mod_combiners" \ v2.13.0 -- \ torch/nn/attention/flex_attention.py \ test/inductor/test_flex_attention.py git grep -n \ "def or_masks\|or_masks(causal_mask, neg_causal_mask)\|def test_mask_mod_combiners" \ main -- \ torch/nn/attention/flex_attention.py \ test/inductor/test_flex_attention.py在 Ascend for PyTorch 仓库检查 NPU 专属实现、现有测试和 patch:
rg -n \ "torch.nn.attention.flex_attention.or_masks|def or_masks|or_masks\(" \ torch_npu test test_upstream rg -n \ "transfer_to_npu|use_jit_script|get_device_capability|or_masks" \ test_upstream/test/inductor检索结果表明:PyTorch 官方各目标版本均包含
or_masks实现和基本逻辑或验证;
or_masks不存在 NPU 专属实现,也不需要新增测试文件或test_upstreampatch。五、验证结果
5.1 实测环境
v2.7.12.7.1+cpu2.7.1v2.11.02.11.0+cpu2.11.0v2.12.02.12.0+cpu2.12.0mastermaster分支构建版本master分支构建版本5.2 NPU 直接验证
运行命令:
临时验证用例覆盖:
mask_mod返回True和False;mask_mod对b、h、q_idx、kv_idx的逻辑或结果;mask_mod返回 NPU bool 标量False;None、整数和字符串等非 callable 输入触发预期RuntimeError;torch.bool,设备类型为npu。v2.7.1
v2.11.0
v2.12.0
master
5.3 运行结果说明
四个目标版本均执行 4 个验证场景,结果为
Ran 4 tests和OK。v2.11.0、v2.12.0和master运行过程中出现的NPUCachingAllocator.cppwarning 是当前 CANN 与 SoC 组合处理 32 字节 padding时产生的运行提示,不影响测试结果。
因此,
torch.nn.attention.flex_attention.or_masks的功能、返回类型、返回设备和异常行为在 NPU 环境中与 PyTorch 实现一致。
5.4 A5硬件补充验证
验证设备:
验证环境:
验证命令:
验证场景:
mask_mod返回True和False;mask_mod执行逻辑或;mask_mod返回位于NPU上的bool标量False;RuntimeError;torch.bool,设备类型为npu。运行结果:
验证结论:
torch.nn.attention.flex_attention.or_masks在上述A5环境中运行正常,单mask、多mask、零mask和异常入参场景均验证通过,返回类型及返回设备符合PyTorch实现。六、资料检查
已检查 Ascend for PyTorch
master分支中的以下资料:docs/zh/api/native_api/pytorch_2-7-1/torch-nn-attention.mddocs/zh/api/native_api/pytorch_2-11-0/torch-nn-attention.mddocs/zh/api/native_api/pytorch_2-12-0/torch-nn-attention.mddocs/zh/api/native_api/pytorch_2-13-0/torch-nn-attention.md上述资料原先未收录
torch.nn.attention.flex_attention.or_masks,因此通过资料PR #43375 补充支持情况。
该API为非计算类纯Python接口,与具体数据类型无关,不填写数据类型限制。
支持状态标记为支持 。
pytorch_2-9-0和pytorch_2-10-0资料目录已经删除,不纳入当前资料补齐范围。七、验证结论
torch.nn.attention.flex_attention.or_masks实现;test_mask_mod_combiners已验证两个互补 mask 的基本逻辑或语义;BlockMask.sparsity()的间接验证,直接 API 覆盖不充分;or_masks为纯 Python 组合接口,不包含 NPU 专属 kernel 或设备分支;torch_npuAPI 实现;test_upstreampatch;