已关闭
【API一致性任务】: Tensor.indices API一致性验证补齐 #3317
Flipped创建于 7月24日关闭于 14 天前
7月24日 修改了issue 的描述
此处折叠了7条事件消息 查看更多
7月28日 添加了label:bot-triaged
TorchNPU-Bot
7月28日 评论:
7月28日 评论:
检测到当前 issue 已关联 PR !42541,自动添加标签:bot-triaged


22 天前 修改标题为 “[API一致性任务]: Tensor.indices API一致性验证补齐”,原标题为“[社区任务]: Tensor.indices API一致性验证补齐”
22 天前 关联了看板:FrameworkPTAdapter 版本issue看板
22 天前 添加了label:event: api-consistency
TorchNPU-Bot
22 天前 评论:
22 天前 评论:
检测到社区任务相关 issue,自动添加标签:event: api-consistency


16 天前 修改标题为 “【API一致性任务】: Tensor.indices API一致性验证补齐”,原标题为“[API一致性任务]: Tensor.indices API一致性验证补齐”
16 天前 修改标题为 “【API一致性任务】: Tensor.indices API一致性验证补齐”,原标题为“[API一致性任务]: Tensor.indices API一致性验证补齐”
16 天前 添加了label:help-wanted
TorchNPU-Bot
16 天前 评论:
16 天前 评论:
检测到社区任务相关 issue,自动添加标签:event: api-consistency、help-wanted


14 天前 添加了label:resolved
任务 issue 来源:【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(83) #2706
环境信息
使用场景及问题
当前需确认以下 API 在 NPU 环境下的测试覆盖和适配情况:
经检查,PyTorch 官方已有直接测试用例:
该用例在 NPU 上需要进行数据类型适配,因此本任务走 1.1 路线:PyTorch 官方有用例,并且需要进行 NPU 适配。
按照当前版本要求,本次处理:
v2.9.0 和 v2.10.0 已进入维护阶段,暂不提交测试 patch;master 不需要提交 patch。
一、API功能说明
Tensor.indices用于获取已合并 Sparse COO Tensor 的索引张量:Tensor._indices一致;二、官方用例覆盖情况
PyTorch 官方直接测试用例及准确行号范围如下:
该用例直接覆盖以下场景:
if not coalesced: with self.assertRaisesRegex( RuntimeError, "Cannot get indices on an uncoalesced tensor", ): x.indices() else: self.assertEqual(x.indices(), x._indices())因此,无需在 torch-npu 的
test/目录新增自定义测试用例,只需适配现有官方用例。三、NPU适配分析
官方用例默认使用:
@dtypes(torch.double, torch.cdouble)在 NPU 上会生成
float64和complex128测试实例。当前 NPU 的 Sparse COOcoalesce()不支持上述 values 数据类型,导致用例在执行到Tensor.indices断言前失败。本次为 PrivateUse1/NPU 单独指定支持的
torch.float。PyTorch v2.7.1 修改为:
@coalescedonoff @dtypes(torch.double, torch.cdouble) @dtypesIfPRIVATEUSE1(torch.float) def test_basic(self, device, dtype, coalesced):PyTorch v2.11.0 和 v2.12.0 保留原有 MPS 配置,并增加:
@coalescedonoff @dtypes(torch.double, torch.cdouble) @dtypesIfMPS(torch.float32, torch.complex64) @dtypesIfPRIVATEUSE1(torch.float) def test_basic(self, device, dtype, coalesced):修改后:
torch.double和torch.cdouble;torch.float;Tensor.indices接口实现。四、各版本patch处理
v2.7.1
v2.7.1 已存在:
原 patch 未适配
TestSparse.test_basic的 NPU 数据类型,本次在已有 patch 中增加:@dtypesIfPRIVATEUSE1(torch.float)同时修正原 patch 中两处设备判断与实际设备不一致的问题。
原代码已经将内部测试设备修改为
npu:0,但外层仍使用:if torch.cuda.is_available():本次修改为:
if torch.npu.is_available():确保设备可用性判断与实际测试设备一致。
对应处理路线为 1.1.2:修改已有 patch。
v2.11.0
v2.11.0 原本不存在:
本次新增该 patch,并完成以下适配:
dtypesIfPRIVATEUSE1;TestSparse.test_basic增加@dtypesIfPRIVATEUSE1(torch.float);import torch_npu;transfer_to_npu;# noqa: F401处理未直接引用的导入;transfer_to_npu放在现有torch、torch.testing相关导入之后。最终保留的导入为:
from torch_npu.contrib import transfer_to_npu # noqa: F401transfer_to_npu用于完成 PyTorch 官方测试到 NPU 的转换,依赖导入副作用,因此不能删除。单独的import torch_npu不再需要。对应处理路线为 1.1.1:新增 patch。
v2.12.0
v2.12.0 原本不存在:
本次新增该 patch,采用与 v2.11.0 相同的适配方式:
dtypesIfPRIVATEUSE1;TestSparse.test_basic增加@dtypesIfPRIVATEUSE1(torch.float);import torch_npu;transfer_to_npu;# noqa: F401;transfer_to_npu放在现有torch、torch.testing相关导入之后。最终保留的导入为:
from torch_npu.contrib import transfer_to_npu # noqa: F401对应处理路线为 1.1.1:新增 patch。
五、功能验证
最终 patch 分别应用到对应 PyTorch 干净工作树后,重新运行官方目标用例。
v2.7.1
v2.11.0
原始用例使用
float64和complex128,在 Sparse COOcoalesce()阶段失败。适配后结果如下:
v2.12.0
三个目标版本修改后的官方用例均在 NPU 环境下执行通过,能够正常覆盖:
Tensor.indices与Tensor._indices的一致性。六、资料检查
已基于 torch-npu 最新 master 分支检查资料目录:
本次检查以下版本:
各版本的
torch-Tensor.md中均已存在唯一的Tensor.indices条目,具体位置如下:五个版本的资料均登记为:
Tensor.indices属于非计算类 API,与输入数据类型无关。现有资料未填写fp16、fp32等具体数据类型,符合非计算类 API 的资料填写要求。本次验证环境为 Ascend 910B3,不属于资料中注明暂不支持的 Ascend 950DT,资料支持状态与实际验证结果一致。
PyTorch 2.6 和 2.8 按当前规则无需刷新。
因此,本任务无需修改资料,也无需单独提交 master 资料 PR。
七、PR信息
v2.7.1:
https://gitcode.com/Ascend/pytorch/pull/42541
v2.11.0:
https://gitcode.com/Ascend/pytorch/pull/42805
v2.12.0:
https://gitcode.com/Ascend/pytorch/pull/42806
八、结论
Tensor.indices已有 PyTorch 官方直接测试用例覆盖。TestSparse.test_basic的准确行号范围。float64和complex128在 NPU Sparse COOcoalesce()场景下不受支持。torch.float。test_sparse.py.patch。test_sparse.py.patch。import torch_npu。transfer_to_npu,并使用# noqa: F401。git diff重新生成。test_basic_npu_float32均重新验证通过。v的规范源分支名。Tensor.indices接口实现。欢迎加入社区,感谢您对社区的贡献 🎉!