已合并
test: adapt Tensor.indices upstream case for NPU #42805
test: adapt Tensor.indices upstream case for NPU #42805
已合并
Flipped创建于 7月26日
Flipped
Flipped
7月26日

【合入来源】

issue: [社区任务]: Tensor.indices API一致性验证补齐 #3317
issue: 【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(83) #2706

【修改方案】

1. API基础功能及用例覆盖

Tensor.indices 用于获取已合并 Sparse COO Tensor 的索引张量:

  • 已合并 Tensor 调用后,返回结果应与 Tensor._indices 一致;
  • 未合并 Tensor 调用时,应抛出异常。

PyTorch v2.11.0 官方用例位置:

test/test_sparse.py::TestSparse.test_basic
test/test_sparse.py:L356-L390

该用例覆盖未合并 Tensor 的异常场景,以及已合并 Tensor 的索引返回结果,因此无需新增自定义用例。

2. v2.11.0原始用例情况

v2.11.0 原本不存在:

test_upstream/test/test_sparse.py.patch

官方用例默认使用:

@dtypes(torch.double, torch.cdouble)

在 NPU 上会生成 float64complex128 用例。当前 NPU 的 Sparse COO coalesce() 不支持上述数据类型,导致测试尚未执行到 Tensor.indices 断言即失败。

原始用例运行结果:

TestSparseNPU.test_basic_npu_complex128 ... ERROR
TestSparseNPU.test_basic_npu_float64 ... ERROR

Ran 2 tests

FAILED (errors=2)

3. 本次修改内容

新增:

test_upstream/test/test_sparse.py.patch

为 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
  • 该导入放在现有 torchtorch.testing 相关导入之后,符合标准库、第三方库、自定义扩展模块的导入顺序。

最终保留的导入为:

from torch_npu.contrib import transfer_to_npu  # noqa: F401

修改后:

  • CPU 继续使用 torch.doubletorch.cdouble
  • MPS 保持原有数据类型配置;
  • NPU 使用支持的 torch.float
  • 保留官方用例原有验证逻辑;
  • 不修改 Tensor.indices 接口实现。

本次从干净的 PyTorch v2.11.0 工作树修改 test/test_sparse.py,并通过:

git diff -- test/test_sparse.py

重新生成 patch,未手工编辑 patch 文件。

【资料变更】

不涉及资料修改。

已基于 torch-npu 最新 master 分支检查资料目录:

docs/zh/api/native_api

PyTorch 2.7.1、2.9.0、2.10.0、2.11.0 和 2.12.0 对应的 torch-Tensor.md 中均已存在唯一的 Tensor.indices 条目,支持状态均登记为:

是否支持:是,暂不支持 Ascend 950DT

Tensor.indices 属于非计算类 API,与数据类型无关。现有资料未填写 fp16fp32 等具体数据类型,符合当前资料填写要求。

【接口变更】

不涉及。

本次仅适配 PyTorch 官方测试用例,不修改客户可见接口及其功能实现。

【功能验证】

运行环境

操作系统:Ubuntu 22.04.5 LTS
机器架构:aarch64
昇腾硬件:Ascend 910B3 NPU
npu-smi版本:25.2.0
CANN版本:9.1.0-beta.1
Python版本:3.11
torch版本:2.11.0+cpu
torch-npu版本:2.11.0rc1

将最终 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

验证结果:

test_basic_npu_float32 (tensor_indices_v211.TestSparseNPU.test_basic_npu_float32) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.607s

OK

最终 patch 可以在干净工作树中正常应用,适配后的官方目标用例已在 NPU 环境执行通过,可以覆盖 Tensor.indices 的核心功能。

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Flipped 的贡献)
FlippedFlipped
7月26日 创建了 pull request,commit a4426fd6
FlippedFlipped
7月26日 关联了issue:【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(83),[社区任务]: Tensor.indices API一致性验证补齐
atomgit-bot
atomgit-bot
7月26日 评论:

变更摘要

此 PR 主要针对 NPU(Ascend 设备)适配 test/test_sparse.pyTestSparse.test_basic 的上游用例。由于 NPU 的 Sparse COO coalesce() 不支持 float64complex128 数据类型,原始测试在到达 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.pyTestSparse.test_basic 方法上新增 @dtypesIfPRIVATEUSE1(torch.float) 装饰器,使 NPU 设备使用 float32 而非不支持的 float64/complex128 来运行 Tensor.indices 相关测试。
likedislike
atomgit-bot
atomgit-bot
7月26日 评论:

代码审查

✅ 未发现问题

likedislike
ascend-robotascend-robot成员
7月26日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

likedislike
ascend-robot
ascend-robot成员
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,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
ascend-robotascend-robot成员
7月26日 添加了label:ci-pipeline-running
FlippedFlipped
7月26日 修改了pull request 的描述
ascend-robot
ascend-robot成员
7月26日 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
7月26日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
7月26日 评论:

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

likedislike
ascend-robotascend-robot成员
7月26日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月26日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
7月26日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月26日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
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 >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
FlippedFlipped
7月26日 修改了pull request 的描述
Flipped
Flipped
7月29日 评论:

compile

likedislike
ascend-robotascend-robot成员
7月29日 删除了label:ci-pipeline-passed
ascend-robotascend-robot成员
7月29日 添加了label:ci-pipeline-running
ascend-robotascend-robot成员
7月29日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月29日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
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 >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan, Jesse]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
htchu成员
8月5日 评论:

/approve

likedislike
ascend-robotascend-robot成员
8月5日 添加了label:approved
chenrayray
chenrayray成员
8月5日 评论:

/lgtm

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