Open
【API一致性任务】torch.layout 社区已有用例且无需额外 NPU 适配 #3378
zhangjiahao-2026Add on Jul 27
Jul 27 Modify description of issue
Jul 31 Modify description of issue
Jul 31 Change title to "torch.layout 社区已有用例且无需额外 NPU 适配", originally titled "torch.layout 社区有用例 + 无需任何修改".
Jul 31 Change title to "torch.layout 社区已有用例且无需额外 NPU 适配", originally titled "torch.layout 社区有用例 + 无需任何修改".
群青世界
Aug 6 Comments:
Aug 6 Comments:
资料中请检查2-13-0,并且2-9-0和2-10-0均已删除


Aug 6 Modify description of issue
Aug 6 associated Kanban: FrameworkPTAdapter 版本issue看板
Aug 9 Modify description of issue
Aug 9 Modify description of issue
Aug 13 Change title to "【API一致性任务】torch.layout 社区已有用例且无需额外 NPU 适配", originally titled "torch.layout 社区已有用例且无需额外 NPU 适配".
Aug 13 Change title to "【API一致性任务】torch.layout 社区已有用例且无需额外 NPU 适配", originally titled "torch.layout 社区已有用例且无需额外 NPU 适配".
Aug 13 Added label: event: api-consistencybot-triaged
TorchNPU-Bot
Aug 13 Comments:
Aug 13 Comments:
检测到社区任务相关 issue,自动添加标签:event: api-consistency


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


Sep 7 Modify description of issue
Sep 7 Added label: help-wanted
TorchNPU-Bot
Sep 7 Comments:
Sep 7 Comments:
检测到社区任务相关 issue,自动添加标签:event: api-consistency、help-wanted


zhangjiahao-2026
Sep 7 Comments:
Sep 7 Comments:
缺少A5验证
已在 5.4 节补充 A5(Ascend950PR)环境下的验证情况、运行结果及验证结论,torch.layout 的 torch.zeros + torch.strided 路径在 NPU 上覆盖的 7 种 dtype 均验证通过,请查阅。


群青世界
Sep 8 Comments:
Sep 8 Comments:
Ascend 950DT 标记不支持。
本次已补充 A5(Ascend950PR)实机验证并通过。资料仍维持现有支持范围,无需新增或修改资料,也无需提交资料 PR。
查看是否需要docsPR修改支持情况


Sep 8 Associated pull request
Sep 8 Modify description of issue
zhangjiahao-2026
Sep 8 Comments:
Sep 8 Comments:
Ascend 950DT 标记不支持。 本次已补充 A5(Ascend950PR)实机验证并通过。资料仍维持现有支持范围,无需新增或修改资料,也无需提交资料 PR。查看是否需要docsPR修改支持情况
已根据 A5(Ascend950PR)实机验证结果提交资料 PR #46016,并同步更新 Issue 中 2.7.1、2.11.0、2.12.0 和 2.13.0 的资料检查及验证结论,请查阅。


Sep 18 changed custom state from TODO to ACCEPTED
API 一致性说明:
torch.layout已有社区用例,无需额外 NPU 适配关联任务
torch.layout一、API 功能
torch.layout是表示torch.Tensor内存布局的 PyTorch 原生类型,目前处于 beta 状态。常见布局实例包括:
torch.stridedtorch.sparse_cootorch.sparse_csrtorch.sparse_csctorch.sparse_bsrtorch.sparse_bsc常见使用方式如下:
# 读取 Tensor 的布局属性 layout = tensor.layout # 创建指定布局的 Tensor tensor = torch.zeros((2, 3), layout=torch.strided)PyTorch 上游实现中:
torch.layout类型由torch/csrc/Layout.cpp中的THPLayout_init()注册到torch模块;torch.strided、torch.sparse_coo等布局实例由torch/csrc/utils/tensor_layouts.cpp中的initializeLayouts()注册。上述实现属于 PyTorch 框架公共代码,不涉及
torch_npu自定义API 实现。
本任务验证现有社区用例覆盖的
torch.strided路径。稀疏 layout对应的算子支持情况由相应稀疏算子测试验证,不在本任务中扩展。
二、测试用例完备性
2.1 PyTorch 官方社区用例
PyTorch 官方仓库
pytorch/pytorch的下列版本中均已存在相关用例:v2.7.1v2.11.0v2.12.0v2.13.0main用例位置:
社区用例核心代码如下:
@dtypes( torch.bool, torch.uint8, torch.int16, torch.int64, torch.float16, torch.float32, torch.complex64, ) def test_zeros_dtype_layout_device_match(self, device, dtype): layout = torch.strided t = torch.zeros((2, 3), device=device, dtype=dtype, layout=layout) self.assertIs(dtype, t.dtype) self.assertIs(layout, t.layout) self.assertEqual(torch.device(device), t.device)该用例验证以下内容:
torch.zeros能接收layout=torch.strided;layout与传入布局一致;dtype、layout和device能同时正确生效;bool、uint8、int16、int64、float16、float32和complex64七种 dtype。2.2 覆盖范围说明
该社区用例直接覆盖
torch.zeros + torch.strided路径,但没有覆盖:layout参数;因此,本说明只根据现有社区用例判断
torch.zeros + torch.strided路径是否需要额外 NPU 适配,不将测试结果扩大为所有 layout 和相关算子均已完成兼容性验证。
2.3 全入参及正常/异常场景说明
torch.layout本身是 PyTorch 的类型对象,不是具有公开调用签名的函数或算子,因此不存在需要传入的业务参数。
针对新版验收标准中的全入参检查:
None/非None:不适用;torch.layout()不是该 API 的公开使用方式,其构造行为属于PyTorch 框架公共类型逻辑,不涉及 NPU 后端分发。
本任务使用官方社区用例验证公开布局实例
torch.strided在 NPUTensor 创建及
Tensor.layout返回值上的一致性,并覆盖七种 dtype。稀疏 layout 对应的 Tensor 创建方式和算子功能由相应稀疏 API
测试负责,不属于
torch.layout类型对象的入参检查范围。三、API 及测试用例适配
3.1 API 适配
torch.layout类型和布局实例由 PyTorch 框架公共代码注册,不存在需要在
torch_npu中重复实现的设备专属逻辑,因此无需修改torch_npuAPI 实现。3.2 测试用例适配
Ascend for PyTorch 对应分支的
test/test_tensor_creation_ops.py已经包含上述社区用例。测试文件通过以下方式将
TestTensorCreation实例化到privateuse1设备:instantiate_device_type_tests( TestTensorCreation, globals(), only_for="privateuse1", )因此,
test_zeros_dtype_layout_device_match已经可以作为 NPU测试用例执行,无需:
test/测试文件或测试方法;test_upstreampatch;torch_npu私有实现。需要注意:该测试实际创建 NPU Tensor,依赖 Ascend for PyTorch
现有的
privateuse1测试环境,不能套用“完全不依赖torch_npu或 NPU 设备”的说明。四、检索记录
在 PyTorch 官方仓库中检查社区用例:
rg -n \ "test_zeros_dtype_layout_device_match" \ test/test_tensor_creation_ops.py在 Ascend for PyTorch 仓库中检查现有用例和 NPU 实例化逻辑:
rg -n \ "test_zeros_dtype_layout_device_match" \ test/test_tensor_creation_ops.py rg -n \ "instantiate_device_type_tests\\(TestTensorCreation" \ test/test_tensor_creation_ops.py检查是否存在需要修改的
test_upstreampatch:rg -n \ "zeros_dtype_layout_device_match|torch\\.layout" \ test_upstream/ || true检索结果表明:
privateuse1;test_upstreampatch。五、验证结果
5.1 测试命令
5.2 测试日志
5.3 日志说明
七个 dtype 参数化用例均为
PASSED,测试进程最终返回:此前实机运行结果对应 PyTorch/torch_npu 2.9.0 和
Ascend 910B3 环境。本次已补充 A5(Ascend950PR,torch/torch_npu 2.12.0)实机验证。
v2.7.1、v2.11.0、v2.12.0及main已完成用例和适配代码的静态核查。5.4 A5 硬件验证
验证环境:
验证命令:
验证场景:对齐社区用例
test_zeros_dtype_layout_device_match,在 NPU 上使用layout=torch.strided调用torch.zeros((2, 3), ...),覆盖以下 7 种 dtype:torch.booltorch.uint8torch.int16torch.int64torch.float16torch.float32torch.complex64每种 dtype 均校验返回 Tensor 的
dtype、layout、device与入参一致。运行结果:
验证结论:
torch.layout的torch.zeros + torch.strided路径在 A5(Ascend950PR)上 7 种 dtype 均通过。本结论不扩大为所有稀疏 layout 及其相关算子均已完成兼容性验证。六、资料检查
以下 Ascend for PyTorch 资料文件中均已包含
torch.layout:docs/zh/api/native_api/pytorch_2-7-1/Tensor-Attributes.mddocs/zh/api/native_api/pytorch_2-11-0/Tensor-Attributes.mddocs/zh/api/native_api/pytorch_2-12-0/Tensor-Attributes.mddocs/zh/api/native_api/pytorch_2-13-0/Tensor-Attributes.md原资料支持状态如下:
2.7.12.11.02.12.02.13.0pytorch_2-9-0和pytorch_2-10-0资料目录已经删除,不再纳入当前资料检查范围。按照当前任务验收口径,A5(Ascend950PR)实机验证结果对应资料中的
Ascend 950DT产品支持情况。本次验证中,torch.layout的torch.zeros + torch.strided路径在 A5 环境下覆盖的 7 种 dtype 均通过,因此需要将上述四个版本中torch.layout的Ascend 950DT支持状态由“不支持”更新为“支持”。相关资料修改已提交至:
该 PR 仅修改
master分支下四个版本的资料支持状态,不涉及 API 实现、测试用例或test_upstreampatch。七、验证结论
TestTensorCreation.test_zeros_dtype_layout_device_match社区用例;privateuse1(NPU);torch.layout由 PyTorch 框架公共代码注册,不需要补充torch_npu设备专属实现;torch.layout的Ascend 950DT支持状态更新为“支持”,相关修改已提交至 PR #46016。因此,本任务当前:
torch_npuAPI 实现;test_upstreampatch;本结论仅适用于现有社区用例覆盖的
torch.zeros + torch.strided路径,不扩大为所有稀疏 layout 及其相关算子均已完成兼容性验证。