已合并
【API一致性任务】test: add torch._C._functorch.is_batchedtensor validation cases on NPU #43533
【API一致性任务】test: add torch._C._functorch.is_batchedtensor validation cases on NPU #43533
已合并
cuiyunhao-2026创建于 8月1日
cuiyunhao-2026
8月1日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

关联 Issue:https://gitcode.com/Ascend/pytorch/issues/2751

【修改方案】

适配 API:torch._C._functorch.is_batchedtensor(torch-npu 尚未提供独立验证用例,本 PR 自写用例补齐,按规范 1.3)。

0. 按导师最新要求的保留判定

导师公告:资料 PR 已全部打回,且不再接收 NPU 适配 patch 与非硬件相关 API 的新增用例。本 PR 经复核属于硬件相关用例,予以保留:

  • 该 API 用于判断张量是否处于 torch.vmap 批处理分发作用域内,本用例通过 device_type = torch.accelerator.current_accelerator().type真实张量运行在 NPU 设备上,验证 vmap/batched tensor 在昇腾硬件上的分发行为(普通张量 / vmap 内部 / 嵌套 vmap / 手动 _add_batch_dim / 多 dtype 等);
  • 用例直接操作 NPU 上的张量与 functorch 分发层,属于规范 1.2②「涉及硬件接口,在 test 目录新增用例」的保留范围,不属于被关闭的非硬件相关用例
  • 本 PR 为 test/ 自写用例,非 test_upstream NPU 适配 patch,符合"不再接收 NPU 适配 patch"的要求。

1. 上游社区用例情况分析

在 PyTorch 上游仓库(github.com/pytorch/pytorch)全量检索 is_batchedtensortest/ 下的引用,仅在 test/functorch/test_eager_transforms.py:4982 一处出现,且位于 helper construct_sum_pyop() 内部,作为 vmap 自定义规则的控制流分支,从未被直接 assert 验证。

结论:该 API 在 PyTorch 上游无独立社区用例,属规范 1.3(社区无直接用例,NPU 侧自写用例) 场景,按规范在 test/ 目录新增自写用例文件,不做 test_upstream patch、不做 API 补齐。

2. NPU 适配方案

torch._C._functorch.is_batchedtensor 由 PyTorch functorch 分发层提供,torch-npu 基于原生 PyTorch 自动具备,在 NPU 上行为与原生一致,无需修改 API 实现。本 PR 仅补充用例,覆盖其完整输入空间:

  • 普通张量返回 False
  • torch.vmap 内部张量返回 True(含嵌套 vmap);
  • 手动 _add_batch_dim 包裹后为 Trueget_unwrapped 解包后恢复 False
  • vmap 作用域外返回 False
  • 多种 dtype(float32 / float16 / int32 / bool)结果仅取决于批处理维,与 dtype 无关;
  • 非张量输入(int)抛出 TypeError

张量通过 device_type = torch.accelerator.current_accelerator().type 落到 NPU 设备,符合“张量必须运行在 NPU 上”规范(昇腾环境导入 torch 自动导入 torch_npu,current_accelerator() 返回 npu)。

3. 提交方式(test 目录自写用例,规范 1.3)

按规范 1.3,在 test/ 目录新增 test/functorch/test_functorch_api.py(同类 torch._C._functorch API 可归集于此文件,便于扩展)。文件遵循 torch-npu 测试规范:Huawei BSD-3 版权头、模块 docstring 说明可扩展与同类归集、从 torch.testing._internal.common_utils 导入 run_tests/TestCase(不直接 import unittest)、导入顺序 torch → torch.testing → functorch、类前后空 2 行、方法间空 1 行、self.assert* 断言、无 print / 无 try-except / 无裸 assert。

按规范 1.3,提交到 2.7.1 / 2.11.0 / 2.12.0 以及 master 四个分支(与 test 目录条款一致)。各分支基线一致、新增文件内容相同,单文件通用。2.9.0 / 2.10.0 已转入维护阶段,不作补齐要求。

4. API 补齐

不需要。torch._C._functorch.is_batchedtensor 由 PyTorch functorch 分发层提供,torch-npu 基于原生 PyTorch 自动具备,无需额外实现,故不提交 API 代码。

5. 多版本分支合入

目标分支 PR 说明
v2.7.1 https://gitcode.com/Ascend/pytorch/pull/43535 test 自写用例
v2.11.0 https://gitcode.com/Ascend/pytorch/pull/43534 test 自写用例
v2.12.0 https://gitcode.com/Ascend/pytorch/pull/43536 test 自写用例
master https://gitcode.com/Ascend/pytorch/pull/43533 test 自写用例
  • 按规范 1.3(test 目录用例),master 需要提交(与 test_upstream patch 场景不同,后者 master 不需要)。
  • 2.9.0 / 2.10.0 已转入维护阶段,不作补齐要求。

【资料变更】

不涉及。

依据:torch._C._functorch.is_batchedtensor 为 PyTorch 私有(内部 C++)接口,按规范 2.3「PyTorch 私有接口不需要补充资料」。经核查 docs/zh/native_apis/ 未收录该 API,故不在 PR 中补资料。

【接口变更】

不涉及。

依据:torch._C._functorch.is_batchedtensor 由 PyTorch functorch 分发层提供,torch-npu 基于原生 PyTorch 自动具备,无跨代码仓或客户面可见接口变更。

【功能验证】

  • 离线校验:用例已在真实 Ascend NPU(910B4)环境执行,全部通过;
  • 运行命令:python test/functorch/test_functorch_api.py
  • 运行结果:
    image.png
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 cuiyunhao-2026 的贡献)
cuiyunhao-2026cuiyunhao-2026
8月1日 创建了 pull request,commit eb25ba32
atomgit-bot
atomgit-bot
8月1日 评论:

变更摘要

本 PR 为昇腾 NPU 补充 PyTorch 原生 API torch._C._functorch.is_batchedtensor 的验证用例:新增测试文件 test/functorch/test_functorch_api.py(纯新增,+98 行),通过 torch.accelerator.current_accelerator() 解析 device_type,使用例可在 CPU 与 NPU 上通用运行,覆盖该 API 的完整输入空间。此改动仅涉及测试代码,不改变任何运行时行为。

主要改动

  • 新增测试类 TestFunctorchIsBatchedTensor: 在 test/functorch/test_functorch_api.py 中定义 7 个测试方法,逐一验证 is_batchedtensor 在不同场景下的返回值。
  • vmap 场景覆盖: test_is_batchedtensor_inside_vmaptest_is_batchedtensor_nested_vmap 验证 vmap 函数内部传入张量(含嵌套 torch.vmap)返回 True,而 test_is_batchedtensor_outside_vmap 验证 vmap 返回结果不再是批处理张量、返回 False
  • 手动批处理维度覆盖: test_is_batchedtensor_manual_batch_dim 使用 _vmap_increment_nesting_add_batch_dim 手动包裹张量后返回 True,经 get_unwrapped 解包后恢复 False,并通过 _vmap_decrement_nesting 清理嵌套层级。
  • 边界输入覆盖: test_is_batchedtensor_various_dtypes 遍历 torch.float32/torch.float16/torch.int32/torch.bool 确认结果只取决于批处理维与 dtype 无关;test_is_batchedtensor_non_tensor_input 验证非张量输入(int)抛出 TypeError
likedislike
atomgit-bot
atomgit-bot
8月1日 评论:

代码审查

✅ 未发现问题

likedislike
ascend-robotascend-robot成员
8月1日 添加了label:ascend-cla/yes
此处折叠了575条消息 查看更多
ascend-robotascend-robot成员
8 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
8 天前 添加了label:docs-ci-pipeline-success
AtlasAccountAtlasAccount成员
8 天前 删除了label:ci-pipeline-running
AtlasAccountAtlasAccount成员
8 天前 添加了label:ci-pipeline-passed
AtlasAccount
AtlasAccount成员
8 天前 评论:
流水线 PR-pipeline_pytorch#64763 [ commitID:71c12040 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 🛑 >>>
Build_ARM 🛑 >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
Build_X86_213 🛑 >>>
Build_ARM_213 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 codecheck_pre-commit >>>
check_error >>>
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 🛑 >>>
UT_ARM_A2_Part_213 🛑 >>>
UT_inductor_Part_213 🛑 >>>
UT_DIST_ARM_Part_213 🛑 >>>
UT_ARM_A2_Select_Part_213 🛑 >>>
流水线 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, WSs_321, SCh_zx]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike