Pull Request已成功合入, 合并人@ascend-robot
(感谢 Flipped 的贡献)变更摘要
此 PR 为 test_sparse.py 新增 NPU 适配补丁,主要解决 NPU 不支持 Sparse COO Tensor 在 coalesce() 场景下使用 float64 / complex128 的问题。通过在 test_basic 测试方法上添加 @dtypesIfPRIVATEUSE1(torch.float) 装饰器,使 Tensor.indices 相关用例在 NPU 上使用 float32 数据类型执行,同时引入必要的 NPU 相关导入。
主要改动
- 新增
@dtypesIfPRIVATEUSE1装饰器适配 NPU 数据类型: 在test_basic方法上添加@dtypesIfPRIVATEUSE1(torch.float),使 NPU 设备使用float32而非默认的float64/complex128,避免因数据类型不支持导致Tensor.indices核心断言无法执行。 - 引入 NPU 相关导入: 新增
from torch_npu.contrib import transfer_to_npu导入,确保 NPU 设备后端正确注册;同时从common_device_type导入dtypesIfPRIVATEUSE1装饰器。 - 调整
import torch位置: 将import torch从文件顶部移至其他标准库导入之后(from packaging import version下方),以适配补丁的上下文结构。


Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| test_upstream | ✅ 楚浩田, chenrayray (2/2) | ✅ 楚浩田 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
qq_65095566, thanks for your pull request. All authors of the commits have signed the CLA. 👍


当前仓库存在以下 保护分支 :
| Protected Branch | Version | Release |
|---|---|---|
| master | ||
| v2.9.0 | ||
| v2.10.0 | ||
| v2.7.1 | ||
| v2.12.0 | ||
| v2.11.0 | ||
| v2.12.0-26.1.0 | ||
| v2.11.0-26.1.0 | ||
| v2.10.0-26.1.0 | ||
| v2.9.0-26.1.0 | ||
| v2.7.1-26.1.0 | ||
| ci-test |
评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


ascend docs pipeline is running...


✅ 跳过 docs ci 检查,没有需要检查的文档文件


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | ✅ | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| check_error | ✅ | >>> | |
| CodeCheck_lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_ARM_A3_Part_01 | 🛑 | >>> |
| UT_ARM_A3_Part_02 | 🛑 | >>> | |
| UT_ARM_A2_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Part_02 | ✅ | >>> | |
| UT_ARM_A2_Part_03 | ✅ | >>> | |
| UT_inductor_Part_01 | 🛑 | >>> | |
| UT_inductor_Part_02 | 🛑 | >>> | |
| UT_inductor_Part_03 | 🛑 | >>> | |
| UT_inductor_Part_04 | 🛑 | >>> | |
| UT_DIST_ARM_Part_01 | 🛑 | >>> | |
| UT_DIST_ARM_Part_02 | 🛑 | >>> | |
| UT_DIST_ARM_Part_03 | 🛑 | >>> | |
| UT_DIST_ARM_Part_04 | 🛑 | >>> | |
| UT_ARM_A2_Select_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Select_Part_02 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


compile


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | ✅ | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| check_error | ✅ | >>> | |
| CodeCheck_lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_ARM_A3_Part_01 | 🛑 | >>> |
| UT_ARM_A3_Part_02 | 🛑 | >>> | |
| UT_ARM_A2_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Part_02 | ✅ | >>> | |
| UT_ARM_A2_Part_03 | ✅ | >>> | |
| UT_inductor_Part_01 | 🛑 | >>> | |
| UT_inductor_Part_02 | 🛑 | >>> | |
| UT_inductor_Part_03 | 🛑 | >>> | |
| UT_inductor_Part_04 | 🛑 | >>> | |
| UT_DIST_ARM_Part_01 | 🛑 | >>> | |
| UT_DIST_ARM_Part_02 | 🛑 | >>> | |
| UT_DIST_ARM_Part_03 | 🛑 | >>> | |
| UT_DIST_ARM_Part_04 | 🛑 | >>> | |
| UT_ARM_A2_Select_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Select_Part_02 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


/lgtm




【合入来源】
issue: [社区任务]: Tensor.indices API一致性验证补齐 #3317
issue: 【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(83) #2706
【修改方案】
1. API基础功能及用例覆盖
Tensor.indices用于获取已合并 Sparse COO Tensor 的索引张量:Tensor._indices一致;PyTorch v2.12.0 官方用例位置:
该用例覆盖未合并 Tensor 的异常场景,以及已合并 Tensor 的索引返回结果,因此无需新增自定义用例。
2. v2.12.0原始用例情况
v2.12.0 原本不存在:
官方用例默认使用:
@dtypes(torch.double, torch.cdouble)NPU 当前不支持在 Sparse COO Tensor 的
coalesce()场景中使用对应的float64和complex128,导致官方用例无法执行到Tensor.indices核心断言,需要对 NPU 测试数据类型进行适配。3. 本次修改内容
新增:
为 PrivateUse1/NPU 导入数据类型装饰器,并增加支持的数据类型:
@coalescedonoff @dtypes(torch.double, torch.cdouble) @dtypesIfMPS(torch.float32, torch.complex64) @dtypesIfPRIVATEUSE1(torch.float) def test_basic(self, device, dtype, coalesced):导入处理如下:
import torch_npu未被直接使用,且导入torch_npu.contrib.transfer_to_npu时会加载对应包,因此删除该重复导入;from torch_npu.contrib import transfer_to_npu用于完成 PyTorch 官方测试到 NPU 的转换,依赖导入副作用,因此予以保留;transfer_to_npu未在文件中直接引用,按照仓库惯例增加# noqa: F401;torch、torch.testing相关导入之后,符合标准库、第三方库、自定义扩展模块的导入顺序。最终保留的导入为:
from torch_npu.contrib import transfer_to_npu # noqa: F401修改后:
torch.double和torch.cdouble;torch.float;Tensor.indices接口实现。本次从干净的 PyTorch v2.12.0 工作树修改
test/test_sparse.py,并通过:git diff -- test/test_sparse.py重新生成 patch,未手工编辑 patch 文件。
【资料变更】
不涉及资料修改。
已基于 torch-npu 最新 master 分支检查资料目录:
PyTorch 2.7.1、2.9.0、2.10.0、2.11.0 和 2.12.0 对应的
torch-Tensor.md中均已存在唯一的Tensor.indices条目,支持状态均登记为:Tensor.indices属于非计算类 API,与数据类型无关。现有资料未填写fp16、fp32等具体数据类型,符合当前资料填写要求。【接口变更】
不涉及。
本次仅适配 PyTorch 官方测试用例,不修改客户可见接口及其功能实现。
【功能验证】
运行环境
将最终 patch 应用到干净的 PyTorch v2.12.0 工作树后,运行官方目标用例。
cd /workspace CHECK_DIR="/workspace/pytorch-v2.12.0-patch-check" PATCH="/workspace/torch-npu-v2.12.0/test_upstream/test/test_sparse.py.patch" git -C /workspace/pytorch-v2.12.0 worktree remove \ "${CHECK_DIR}" --force 2>/dev/null || true rm -rf "${CHECK_DIR}" git -C /workspace/pytorch-v2.12.0 worktree prune git -C /workspace/pytorch-v2.12.0 worktree add \ --detach "${CHECK_DIR}" HEAD git -C "${CHECK_DIR}" apply --check "${PATCH}" git -C "${CHECK_DIR}" apply "${PATCH}" git -C "${CHECK_DIR}" diff --check source /workspace/envs/torch-npu-2.12.0/bin/activate source /usr/local/Ascend/ascend-toolkit/set_env.sh unset PYTHONPATH export PYTHONPATH="${CHECK_DIR}/test" export PYTHONNOUSERSITE=1 export PYTORCH_PRINT_REPRO_ON_FAILURE=0 python - <<'PY' import runpy import unittest import torch import torch_npu torch.npu.get_device_capability = lambda *args, **kwargs: (0, 0) torch.cuda.get_device_capability = lambda *args, **kwargs: (0, 0) test_file = ( "/workspace/pytorch-v2.12.0-patch-check/" "test/test_sparse.py" ) namespace = runpy.run_path( test_file, run_name="tensor_indices_v212", ) test_class = namespace["TestSparseNPU"] suite = unittest.TestSuite([ test_class("test_basic_npu_float32"), ]) result = unittest.TextTestRunner(verbosity=2).run(suite) raise SystemExit(0 if result.wasSuccessful() else 1) PY验证结果:
最终 patch 可以在干净工作树中正常应用,适配后的官方目标用例已在 NPU 环境执行通过,可以覆盖
Tensor.indices的核心功能。【CheckList】