已合并
test: adapt Tensor.indices upstream case for NPU #42805
Flipped创建于 7月26日
test: adapt Tensor.indices upstream case for NPU #42805
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Flipped 的贡献)7月26日 关联了issue:【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(83),[社区任务]: Tensor.indices API一致性验证补齐
7月26日 关联了issue:【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(83),[社区任务]: Tensor.indices API一致性验证补齐
atomgit-bot
7月26日 评论:
7月26日 评论:
变更摘要
此 PR 主要针对 NPU(Ascend 设备)适配 test/test_sparse.py 中 TestSparse.test_basic 的上游用例。由于 NPU 的 Sparse COO coalesce() 不支持 float64 和 complex128 数据类型,原始测试在到达 Tensor.indices 断言前即失败。通过为 NPU 设备单独指定 torch.float 数据类型来覆盖该测试场景。
主要改动
- 新增
dtypesIfPRIVATEUSE1导入: 从torch.testing._internal.common_device_type中导入dtypesIfPRIVATEUSE1,用于为 NPU(privateuse1后端)指定专用数据类型装饰器。 - 新增
transfer_to_npu导入: 添加from torch_npu.contrib import transfer_to_npu,用于将上游用例适配到 NPU 设备。 - 为
test_basic添加@dtypesIfPRIVATEUSE1装饰器: 在test/test_sparse.py的TestSparse.test_basic方法上新增@dtypesIfPRIVATEUSE1(torch.float)装饰器,使 NPU 设备使用float32而非不支持的float64/complex128来运行Tensor.indices相关测试。


ascend-robot
7月26日 评论:
7月26日 评论:
atomgit-bot
7月26日 评论:
7月26日 评论:
7月26日 添加了label:ascend-cla/yes
ascend-robot
7月26日 评论:
7月26日 评论:
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. 👍


ascend-robot
7月26日 评论:
7月26日 评论:
当前仓库存在以下 保护分支 :
| 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 命令关闭


7月26日 添加了label:ci-pipeline-running
7月26日 修改了pull request 的描述
ascend-robot
7月26日 评论:
7月26日 评论:
ascend docs pipeline is running...


7月26日 添加了label:docs-ci-pipeline-running
ascend-robot
7月26日 评论:
7月26日 评论:
✅ 跳过 docs ci 检查,没有需要检查的文档文件


7月26日 删除了label:docs-ci-pipeline-running
7月26日 添加了label:docs-ci-pipeline-success
7月26日 删除了label:ci-pipeline-running
7月26日 添加了label:ci-pipeline-passed
ascend-robot
7月26日 评论:
7月26日 评论:
流水线 PR-pipeline_pytorch#49645 [ commitID:02bca4ac ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 : 停止流水线


7月26日 修改了pull request 的描述
Flipped
7月29日 评论:
7月29日 评论:
compile


7月29日 删除了label:ci-pipeline-passed
7月29日 添加了label:ci-pipeline-running
7月29日 删除了label:ci-pipeline-running
7月29日 添加了label:ci-pipeline-passed
ascend-robot
7月29日 评论:
7月29日 评论:
流水线 PR-pipeline_pytorch#50958 [ commitID:02bca4ac ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 : 停止流水线


8月5日 添加了label:approved
chenrayray
8月5日 评论:
8月5日 评论:
/lgtm


8月5日 添加了label:lgtm
8月5日 删除了label:ci-pipeline-passed
8月5日 合入了pull request
ascend-robot
8月5日 评论:
8月5日 评论:
流水线 pytorch_gitcode_PR_multiVersion#13686 [ commitID:02bca4ac ] 已完成


【合入来源】
issue: [社区任务]: Tensor.indices API一致性验证补齐 #3317
issue: 【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(83) #2706
【修改方案】
1. API基础功能及用例覆盖
Tensor.indices用于获取已合并 Sparse COO Tensor 的索引张量:Tensor._indices一致;PyTorch v2.11.0 官方用例位置:
该用例覆盖未合并 Tensor 的异常场景,以及已合并 Tensor 的索引返回结果,因此无需新增自定义用例。
2. v2.11.0原始用例情况
v2.11.0 原本不存在:
官方用例默认使用:
@dtypes(torch.double, torch.cdouble)在 NPU 上会生成
float64和complex128用例。当前 NPU 的 Sparse COOcoalesce()不支持上述数据类型,导致测试尚未执行到Tensor.indices断言即失败。原始用例运行结果:
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.11.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.11.0 工作树后,运行官方目标用例。
cd /workspace CHECK_DIR="/workspace/pytorch-v2.11.0-patch-check" PATCH="/workspace/torch-npu-v2.11.0/test_upstream/test/test_sparse.py.patch" git -C /workspace/pytorch-v2.11.0 worktree remove \ "${CHECK_DIR}" --force 2>/dev/null || true rm -rf "${CHECK_DIR}" git -C /workspace/pytorch-v2.11.0 worktree prune git -C /workspace/pytorch-v2.11.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.11.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.11.0-patch-check/" "test/test_sparse.py" ) namespace = runpy.run_path( test_file, run_name="tensor_indices_v211", ) 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】