已合并
【API一致性任务】test: add torch._C._functorch.is_batchedtensor validation cases on NPU #43535
cuiyunhao-2026创建于 8月1日
【API一致性任务】test: add torch._C._functorch.is_batchedtensor validation cases on NPU #43535
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 cuiyunhao-2026 的贡献)8月1日 创建了 pull request,commit 5cacc002
ascend-robot
8月1日 评论:
8月1日 评论:
atomgit-bot
8月1日 评论:
8月1日 评论:
变更摘要
本 PR 为昇腾 NPU 补齐 PyTorch 原生 API torch._C._functorch.is_batchedtensor 的测试覆盖:新增测试文件 test/functorch/test_functorch_api.py,通过 torch.accelerator.current_accelerator() 解析 device_type(无加速器时回退 cpu),以设备无关的方式验证该分发层接口在普通张量、torch.vmap 内/外、嵌套 vmap、手动批处理维包裹/解包、多种 dtype 以及非张量输入等场景下的行为。该文件为纯新增(+98 行),不修改任何现有实现。
主要改动
- 新增测试类
TestFunctorchIsBatchedTensor:在test/functorch/test_functorch_api.py中新增继承自TestCase的测试类,从torch._C._functorch导入_add_batch_dim、_vmap_increment_nesting、_vmap_decrement_nesting、get_unwrapped、is_batchedtensor等接口,覆盖该 API 的完整输入空间。 - vmap 作用域语义验证:
test_is_batchedtensor_inside_vmap、test_is_batchedtensor_nested_vmap断言 vmap 传入的张量为True(含嵌套 vmap),test_is_batchedtensor_outside_vmap断言 vmap 返回结果为False,验证批处理状态随作用域变化。 - 手动批处理维包裹验证:
test_is_batchedtensor_manual_batch_dim通过_vmap_increment_nesting建立嵌套层级后,用_add_batch_dim包裹的张量判定为True,get_unwrapped解包后恢复False,并以finally调用_vmap_decrement_nesting确保清理。 - dtype 无关性与非法输入验证:
test_is_batchedtensor_various_dtypes遍历torch.float32/float16/int32/bool,断言结果只取决于批处理维;test_is_batchedtensor_non_tensor_input断言对 int 输入抛出TypeError。


不准确?
atomgit-bot
8月1日 评论:
8月1日 评论:
8月1日 添加了label:ascend-cla/yes
此处折叠了557条消息 查看更多
11 天前 添加了label:ci-pipeline-passed
AtlasAccount
11 天前 评论:
11 天前 评论:
流水线 PR-pipeline_pytorch#64756 [ commitID:05935778 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


ascend-robot
11 天前 评论:
11 天前 评论:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


ascend-robot
11 天前 评论:
11 天前 评论:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


ascend-robot
11 天前 评论:
11 天前 评论:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


【合入来源】
关联 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 经复核属于硬件相关用例,予以保留:
torch.vmap批处理分发作用域内,本用例通过device_type = torch.accelerator.current_accelerator().type将真实张量运行在 NPU 设备上,验证 vmap/batched tensor 在昇腾硬件上的分发行为(普通张量 / vmap 内部 / 嵌套 vmap / 手动_add_batch_dim/ 多 dtype 等);test/自写用例,非test_upstreamNPU 适配 patch,符合"不再接收 NPU 适配 patch"的要求。1. 上游社区用例情况分析
在 PyTorch 上游仓库(github.com/pytorch/pytorch)全量检索
is_batchedtensor在test/下的引用,仅在test/functorch/test_eager_transforms.py:4982一处出现,且位于 helperconstruct_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包裹后为True,get_unwrapped解包后恢复False;False;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._functorchAPI 可归集于此文件,便于扩展)。文件遵循 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. 多版本分支合入
【资料变更】
不涉及。
依据:
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 自动具备,无跨代码仓或客户面可见接口变更。【功能验证】
python test/functorch/test_functorch_api.py;