已合并
【API一致性任务】test: add torch._C._functorch.is_batchedtensor validation cases on NPU #43533
cuiyunhao-2026创建于 8月1日
【API一致性任务】test: add torch._C._functorch.is_batchedtensor validation cases on NPU #43533
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 cuiyunhao-2026 的贡献)8月1日 创建了 pull request,commit eb25ba32
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(纯新增,+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_vmap与test_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。


atomgit-bot
8月1日 评论:
8月1日 评论:
8月1日 添加了label:ascend-cla/yes
此处折叠了575条消息 查看更多
8 天前 删除了label:docs-ci-pipeline-running
8 天前 添加了label:docs-ci-pipeline-success
8 天前 删除了label:ci-pipeline-running
8 天前 添加了label:ci-pipeline-passed
AtlasAccount
8 天前 评论:
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 | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


【合入来源】
关联 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;