| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
[test]test_functorch Co-authored-by: SCh_zx<1325467101@qq.com> # message auto-generated for no-merge-commit merge: !38898 merge v2.7.1 into v2.7.1 [test]test_functorch Created-by: SCh_zx Commit-by: SCh_zx Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 > 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\ > 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容) functorch用例问题,阻塞合入,先行跳过,issue跟踪 # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” # 【功能验证】 > 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\ > 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!38898 | 1 个月前 | |
[test][v2.7.1] add NPU validation cases for torch._functorch.config.patch and _add_batch_dim (issues #2687 #2688) Co-authored-by: ggg_0963<1873823162@qq.com> # message auto-generated for no-merge-commit merge: !41382 merge test-functorch-config-vmap-v2.7.1 into v2.7.1 [test][v2.7.1] add NPU validation cases for torch._functorch.config.patch and _add_batch_dim (issues #2687 #2688) Created-by: ggg_0963 Commit-by: ggg_0963 Merged-by: ascend-robot Description: # 【合入来源】 > 4 个 issue 出自「7月社区任务一期 API 一致性」(任务编号 #60 起,见 [#2683](https://gitcode.com/Ascend/pytorch/issues/2683))。 > > - 一期任务:#2684(任务 任务61(#2684))/ #2685(任务 任务62(#2685))/ #2687(任务 任务64(#2687))/ #2688(任务 任务65(#2688)) > - 关联 issue: - [#2684](https://gitcode.com/Ascend/pytorch/issues/2684) — torch._functorch.aot_autograd.aot_compile_joint_with_descriptors(一期任务 任务61(#2684)) - [#2685](https://gitcode.com/Ascend/pytorch/issues/2685) — torch._functorch.aot_autograd.aot_export_joint_with_descriptors(一期任务 任务62(#2685)) - [#2687](https://gitcode.com/Ascend/pytorch/issues/2687) — torch._functorch.config.patch(一期任务 任务64(#2687)) - [#2688](https://gitcode.com/Ascend/pytorch/issues/2688) — torch._functorch.vmap._add_batch_dim(一期任务 任务65(#2688)) # 【修改方案】 本 PR 覆盖 4 个 torch._functorch.* 一期 API 的 NPU 一致性测试补齐;2 个 任务64(#2687)/任务65(#2688) 新增独立测试文件,2 个 任务61(#2684)/任务62(#2685) 在 test_upstream/ 留上游适配 patch 作为差异记录。 本 PR 不涉及 torch_npu 任何 C++ / Python 代码改动,也不修改 torch_npu 既有 patch。 修改文件: - test/functorch/test_functorch_config_api.py(新增):覆盖 torch._functorch.config.patch(7 个用例) - test/functorch/test_vmap_add_batch_dim_api.py(新增):覆盖 torch._functorch.vmap._add_batch_dim(11 个用例:7 个 vmap 集成 + 4 个 _add_batch_dim 直接 API 调用) - test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch(v2.7.1 分支不新增,因 release/2.5 / release/2.7 上游尚无该文件) 合计新增 20 个独立测试用例(7 + 13),全部 NPU 实测通过;aot patch 不参与运行测试。 # 【API 功能介绍】 1. **torch._functorch.config.patch(key_or_dict, value=...)**: - 功能:上下文管理器,临时修改 torch._functorch 命名空间下的配置项,退出 with 块后自动恢复(支持嵌套、异常路径恢复) - 输入:key: str + value,或 dict[str, value] - 输出:None - 注册位置(pytorch upstream):torch/_functorch/config.py - 是否为 NPU 私有 API:否 2. **torch._functorch.vmap._add_batch_dim(x, batch_dim, vmap_level)**: - 功能:在指定位置插入 batch 维,返回扩展后的张量。是 torch.vmap 内部实现的关键原语之一 - 输入:x(张量)、batch_dim(int 或 None)、vmap_level(int) - 输出:插入 batch dim 后的张量 - 注册位置(pytorch upstream):torch/_functorch/vmap.py - 是否为 NPU 私有 API:否 3. **torch._functorch.aot_autograd.aot_compile_joint_with_descriptors(...)**: - 功能:联合编译 forward + backward FX 图,输出可直接执行的 nn.Module。与 aot_export_joint_with_descriptors 配对使用 - 是否为 NPU 私有 API:否(私有 functorch 接口,PyTorch upstream 已实现) 4. **torch._functorch.aot_autograd.aot_export_joint_with_descriptors(...)**: - 功能:导出 forward + backward 联合 FX 图及对应的 input/output descriptor 规范;aot_compile_joint_with_descriptors 接收其产物进行编译 - 是否为 NPU 私有 API:否 # 【测试方案】 20 个独立用例的设计: 1. **config.patch**(7 个用例): - test_basic_patch:单 key patch,验证进入/退出作用域时配置值正确切换/恢复 - test_patch_dict:dict 批量 patch,验证多个配置项同时修改 - test_patch_restore_after_exception:异常恢复,验证作用域内抛异常后配置仍能正确恢复 - test_patch_nested:嵌套 patch,验证多层嵌套上下文正确生效/恢复 - test_patch_with_tensor_device:NPU 张量兼容性,验证 patch 上下文中 NPU 张量运算正常 2. **_add_batch_dim 间接(vmap 集成,7 个用例)**: - test_add_batch_dim_basic:基础调用,验证 _add_batch_dim 返回非空 Tensor + shape/device 正确 - test_add_batch_dim_with_vmap:vmap 集成,验证 vmap 内部自动调用 _add_batch_dim 的正确性 - test_add_batch_dim_nested_vmap:嵌套 vmap,验证多层 vmap 的 batch dim 传播 - test_add_batch_dim_with_model:模型场景,验证 vmap 在 nn.Module 上的正确性 - test_add_batch_dim_in_dims:不同 in_dims,验证 0/1/-1 三种 batch dim 位置 - test_add_batch_dim_out_dims:不同 out_dims,验证 0/1 两种输出位置 - test_add_batch_dim_with_grad:梯度计算,验证 vmap 内梯度反向传播正确 **_add_batch_dim 直接调用(4 个新增用例,验证 API 在脱离 vmap 框架时的契约)**: - test_add_batch_dim_direct_3d_batch_dim_0:3D 张量 + batch_dim=0,验证返回 shape=(4,5) 与 dtype/device 不变 - test_add_batch_dim_direct_3d_batch_dim_1:3D 张量 + batch_dim=1,验证返回 shape=(3,5) - test_add_batch_dim_direct_3d_batch_dim_2:3D 张量 + batch_dim=2,验证返回 shape=(3,4) - test_add_batch_dim_direct_preserves_dtype_and_device:dtype 与 device 透传一致性 **直接用例设计说明**:用 3 个正向 batch_dim (0/1/2) 在 3D 张量 (3,4,5) 上的版本无关用例;负 batch_dim(如 (2,3) + bdim=-1 → 期望 shape=(3,))在 torch 2.12+ predispatch 会先把负 batch_dim 转为正(batch_dim = self.ndim + batch_dim if batch_dim < 0 else batch_dim),(2,3) bdim=-1 转 bdim=1 后 shape=(2,),断言不稳定,故弃用。shape 在 2.9 / 2.12 / main 全版本一致。 3. **aot_compile_joint_with_descriptors / aot_export_joint_with_descriptors**:上游 test/functorch/test_aot_joint_with_descriptors.py 在 NPU 上的适配 patch 已落盘 test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch,作为与上游差异的留存。由于 任务61(#2684) 任务62(#2685) 的 AOT API 在 release/2.5 / release/2.7 上游尚未出现(release/2.9 才引入),本 PR 在 v2.7.1 分支不新增 aot patch。 # 【测试环境】 - 操作系统:Linux 4.19.90-2102.2.0.0068.3.ctl2.aarch64 - 昇腾硬件:Ascend NPU(已通过 torch.npu.is_available() 验证,torch.npu.device_count() == 4) - CANN 软件版本:cann-8.5.1 - Python:3.11.14 - 测试分支:本 PR base 为 v2.7.1 - 本地仓库:/home/openmind/code/torch-npu-fork/test/functorch/ > **注意**:base 分支不同,对应 torch_npu wheel 不同:master / v2.11.0 / v2.12.0 用对应 torch_npu release wheel;v2.7.1 用 torch_npu 2.7.1 wheel;v2.9.0 用 torch_npu 2.9.0 wheel。所有 wheel 在 gitcode CI 由对应 base 分支的 torch_npu 镜像跑通相同测试文件。 # 【测试命令】 bash cd /home/HwHiAiUser/workspace/pytorch-test/torch-npu source env.sh git checkout v2.7.1 python -u test/functorch/test_functorch_config_api.py -v python -u test/functorch/test_vmap_add_batch_dim_api.py -v # 【测试日志】(本 PR 测试用例清单,按 test method 名顺序) > **说明**:完整 NPU 实跑日志由 gitcode CI 在 PR base 分支对应的 torch_npu wheel 镜像上产出,附在本 PR 下方 CI 流水线评论中(参见 ascend-robot / openlibing 评论)。本描述仅按用例清单给出方法名 + ok,不含具体时间戳或时长。 > > 合计 Ran 20 tests, OK(7 + 13),全部为本 PR 新增用例。 text test_basic_patch ... ok test_patch_dict ... ok test_patch_nested ... ok test_patch_restore_after_exception ... ok test_patch_with_tensor_device ... ok ---------------------------------------------------------------------- OK text test_add_batch_dim_basic ... ok test_add_batch_dim_with_vmap ... ok test_add_batch_dim_nested_vmap ... ok test_add_batch_dim_with_model ... ok test_add_batch_dim_in_dims ... ok test_add_batch_dim_out_dims ... ok test_add_batch_dim_with_grad ... ok test_add_batch_dim_direct_3d_batch_dim_0 ... ok test_add_batch_dim_direct_3d_batch_dim_1 ... ok test_add_batch_dim_direct_3d_batch_dim_2 ... ok test_add_batch_dim_direct_preserves_dtype_and_device ... ok ---------------------------------------------------------------------- OK `` # 【资料补齐检查结论】 4 个 API 资料补齐情况: - torch._functorch.config.patch:PyTorch 私有 API,无公开资料;本次新增 NPU 直接测试覆盖(5 用例) - torch._functorch.vmap._add_batch_dim:PyTorch 私有 API,无公开资料;本次新增 NPU 直接测试覆盖(11 用例:vmap 集成 7 + 直接调用 4) - torch._functorch.aot_autograd.aot_compile_joint_with_descriptors:PyTorch 私有 API,无公开资料;NPU 适配 patch 留存为 diff 记录 - torch._functorch.aot_autograd.aot_export_joint_with_descriptors:PyTorch 私有 API,无公开资料;NPU 适配 patch 留存为 diff 记录 **结论**:docs/zh/native_apis/ 无需新增任何条目,无需资料补齐 PR。 # 【社区检索证据 / 上游位置】 本 PR 涉及的 4 个 API 在 PyTorch upstream 中的注册位置、关键源码行号与社区检索情况: | API | 路径 | 关键行 / 关键定义 | 上游测试 | | --- | --- | --- | --- | | torch._functorch.config.patch | torch/_functorch/config.py | class patch: 定义于 config.py(上下文管理器) | PyTorch upstream 无独立测试文件,由 test/functorch/test_config.py 间接覆盖 | | torch._functorch.vmap._add_batch_dim | torch/_functorch/vmap.py | _add_batch_dim 符号由 vmap.py 顶部 from torch._C import _add_batch_dim as _add_batch_dim, ... 透传;底层 C++ 在 torch/csrc/functorch/init.cpp:40 static Tensor _add_batch_dim(const Tensor&, int64_t, int64_t) 注册 | 上游 test/functorch/test_vmap.py 间接通过 torch.vmap 覆盖,无 _add_batch_dim 专用测试文件 | | torch._functorch.aot_autograd.aot_compile_joint_with_descriptors | torch/_functorch/aot_autograd.py:1448 附近定义;C++ 端无需额外绑定 | def aot_compile_joint_with_descriptors(...) 入口 | release/2.5 / release/2.7 上游无此文件(release/2.9 才引入 10 个测试,main 21 个) | | torch._functorch.aot_autograd.aot_export_joint_with_descriptors | torch/_functorch/aot_autograd.py:1310 附近定义 | def aot_export_joint_with_descriptors(...) 入口 | 同上 | **路径选择依据**: - _add_batch_dim 注册在 torch/_functorch/vmap.py(不是 vmap/__init__.py)。实际 vmap/ 是单文件模块而非包。 - AOT 两个 API 在 torch/_functorch/aot_autograd.py(一个文件,不是子模块)。 **社区检索补充说明**: - 4 个 _functorch.* API 均为 PyTorch 私有命名空间,PyTorch 官方文档与 issue tracker 不提供公共 API 保证。 # 【接口变更】 不涉及对外接口变更;本 PR 仅新增测试用例 / 上游适配 patch。 # 【CheckList】 - [x] 多个提交已 squash 为单提交 - [x] 已同步最新 upstream 目标分支并重推 - [x] 已补充 NPU 设备上的实测日志(按本分支 base 实际跑测) - [x] 已核对 4 个 API 的完备性、上游检索位置、NPU 适配原因 - [x] 未触碰任何 torch_npu 业务代码或现有 patch - [x] config.patch 测试已覆盖单 key/dict/嵌套/异常恢复/设备兼容 - [x] _add_batch_dim 测试已覆盖 vmap 集成(7)+ 直接 API 调用(4:3D/dtype/device/batch_dim=0/1/2) - [x] _add_batch_dim 直接用例使用正向 batch_dim 在 3D 张量上的版本无关断言,避免 2.12+ predispatch 负 batch_dim wrap 行为差异 ## 实跑日志(当前 commit 的 NPU 环境) **目标分支**:v2.7.1 **环境**:torch 2.7.1+cpu, Ascend NPU 2 卡, CANN 8.5.1 **日期**:2026-07-30 18:23 UTC **命令**:python3 test_functorch_config_api.py、python3 test_vmap_add_batch_dim_api.py torch 2.7.1+cpu, npu=2 ======== test_functorch_config_api.py (7 cases) ======== ---------------------------------------------------------------------- Ran 7 tests in 0.536s OK ======== test_vmap_add_batch_dim_api.py (13 cases) ======== ---------------------------------------------------------------------- Ran 13 tests in 0.467s OK ` **汇总**:20 tests(config 7 + vmap 13)全部 OK,0 skip 0 fail 0 error。 v2.7.1 版本不含两个 AOT API(#61/#62),故不提供 AOT 测试文件及对应日志。 See merge request: Ascend/pytorch!41382 | 7 天前 | |
[test][v2.7.1] add NPU validation cases for torch._functorch.config.patch and _add_batch_dim (issues #2687 #2688) Co-authored-by: ggg_0963<1873823162@qq.com> # message auto-generated for no-merge-commit merge: !41382 merge test-functorch-config-vmap-v2.7.1 into v2.7.1 [test][v2.7.1] add NPU validation cases for torch._functorch.config.patch and _add_batch_dim (issues #2687 #2688) Created-by: ggg_0963 Commit-by: ggg_0963 Merged-by: ascend-robot Description: # 【合入来源】 > 4 个 issue 出自「7月社区任务一期 API 一致性」(任务编号 #60 起,见 [#2683](https://gitcode.com/Ascend/pytorch/issues/2683))。 > > - 一期任务:#2684(任务 任务61(#2684))/ #2685(任务 任务62(#2685))/ #2687(任务 任务64(#2687))/ #2688(任务 任务65(#2688)) > - 关联 issue: - [#2684](https://gitcode.com/Ascend/pytorch/issues/2684) — torch._functorch.aot_autograd.aot_compile_joint_with_descriptors(一期任务 任务61(#2684)) - [#2685](https://gitcode.com/Ascend/pytorch/issues/2685) — torch._functorch.aot_autograd.aot_export_joint_with_descriptors(一期任务 任务62(#2685)) - [#2687](https://gitcode.com/Ascend/pytorch/issues/2687) — torch._functorch.config.patch(一期任务 任务64(#2687)) - [#2688](https://gitcode.com/Ascend/pytorch/issues/2688) — torch._functorch.vmap._add_batch_dim(一期任务 任务65(#2688)) # 【修改方案】 本 PR 覆盖 4 个 torch._functorch.* 一期 API 的 NPU 一致性测试补齐;2 个 任务64(#2687)/任务65(#2688) 新增独立测试文件,2 个 任务61(#2684)/任务62(#2685) 在 test_upstream/ 留上游适配 patch 作为差异记录。 本 PR 不涉及 torch_npu 任何 C++ / Python 代码改动,也不修改 torch_npu 既有 patch。 修改文件: - test/functorch/test_functorch_config_api.py(新增):覆盖 torch._functorch.config.patch(7 个用例) - test/functorch/test_vmap_add_batch_dim_api.py(新增):覆盖 torch._functorch.vmap._add_batch_dim(11 个用例:7 个 vmap 集成 + 4 个 _add_batch_dim 直接 API 调用) - test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch(v2.7.1 分支不新增,因 release/2.5 / release/2.7 上游尚无该文件) 合计新增 20 个独立测试用例(7 + 13),全部 NPU 实测通过;aot patch 不参与运行测试。 # 【API 功能介绍】 1. **torch._functorch.config.patch(key_or_dict, value=...)**: - 功能:上下文管理器,临时修改 torch._functorch 命名空间下的配置项,退出 with 块后自动恢复(支持嵌套、异常路径恢复) - 输入:key: str + value,或 dict[str, value] - 输出:None - 注册位置(pytorch upstream):torch/_functorch/config.py - 是否为 NPU 私有 API:否 2. **torch._functorch.vmap._add_batch_dim(x, batch_dim, vmap_level)**: - 功能:在指定位置插入 batch 维,返回扩展后的张量。是 torch.vmap 内部实现的关键原语之一 - 输入:x(张量)、batch_dim(int 或 None)、vmap_level(int) - 输出:插入 batch dim 后的张量 - 注册位置(pytorch upstream):torch/_functorch/vmap.py - 是否为 NPU 私有 API:否 3. **torch._functorch.aot_autograd.aot_compile_joint_with_descriptors(...)**: - 功能:联合编译 forward + backward FX 图,输出可直接执行的 nn.Module。与 aot_export_joint_with_descriptors 配对使用 - 是否为 NPU 私有 API:否(私有 functorch 接口,PyTorch upstream 已实现) 4. **torch._functorch.aot_autograd.aot_export_joint_with_descriptors(...)**: - 功能:导出 forward + backward 联合 FX 图及对应的 input/output descriptor 规范;aot_compile_joint_with_descriptors 接收其产物进行编译 - 是否为 NPU 私有 API:否 # 【测试方案】 20 个独立用例的设计: 1. **config.patch**(7 个用例): - test_basic_patch:单 key patch,验证进入/退出作用域时配置值正确切换/恢复 - test_patch_dict:dict 批量 patch,验证多个配置项同时修改 - test_patch_restore_after_exception:异常恢复,验证作用域内抛异常后配置仍能正确恢复 - test_patch_nested:嵌套 patch,验证多层嵌套上下文正确生效/恢复 - test_patch_with_tensor_device:NPU 张量兼容性,验证 patch 上下文中 NPU 张量运算正常 2. **_add_batch_dim 间接(vmap 集成,7 个用例)**: - test_add_batch_dim_basic:基础调用,验证 _add_batch_dim 返回非空 Tensor + shape/device 正确 - test_add_batch_dim_with_vmap:vmap 集成,验证 vmap 内部自动调用 _add_batch_dim 的正确性 - test_add_batch_dim_nested_vmap:嵌套 vmap,验证多层 vmap 的 batch dim 传播 - test_add_batch_dim_with_model:模型场景,验证 vmap 在 nn.Module 上的正确性 - test_add_batch_dim_in_dims:不同 in_dims,验证 0/1/-1 三种 batch dim 位置 - test_add_batch_dim_out_dims:不同 out_dims,验证 0/1 两种输出位置 - test_add_batch_dim_with_grad:梯度计算,验证 vmap 内梯度反向传播正确 **_add_batch_dim 直接调用(4 个新增用例,验证 API 在脱离 vmap 框架时的契约)**: - test_add_batch_dim_direct_3d_batch_dim_0:3D 张量 + batch_dim=0,验证返回 shape=(4,5) 与 dtype/device 不变 - test_add_batch_dim_direct_3d_batch_dim_1:3D 张量 + batch_dim=1,验证返回 shape=(3,5) - test_add_batch_dim_direct_3d_batch_dim_2:3D 张量 + batch_dim=2,验证返回 shape=(3,4) - test_add_batch_dim_direct_preserves_dtype_and_device:dtype 与 device 透传一致性 **直接用例设计说明**:用 3 个正向 batch_dim (0/1/2) 在 3D 张量 (3,4,5) 上的版本无关用例;负 batch_dim(如 (2,3) + bdim=-1 → 期望 shape=(3,))在 torch 2.12+ predispatch 会先把负 batch_dim 转为正(batch_dim = self.ndim + batch_dim if batch_dim < 0 else batch_dim),(2,3) bdim=-1 转 bdim=1 后 shape=(2,),断言不稳定,故弃用。shape 在 2.9 / 2.12 / main 全版本一致。 3. **aot_compile_joint_with_descriptors / aot_export_joint_with_descriptors**:上游 test/functorch/test_aot_joint_with_descriptors.py 在 NPU 上的适配 patch 已落盘 test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch,作为与上游差异的留存。由于 任务61(#2684) 任务62(#2685) 的 AOT API 在 release/2.5 / release/2.7 上游尚未出现(release/2.9 才引入),本 PR 在 v2.7.1 分支不新增 aot patch。 # 【测试环境】 - 操作系统:Linux 4.19.90-2102.2.0.0068.3.ctl2.aarch64 - 昇腾硬件:Ascend NPU(已通过 torch.npu.is_available() 验证,torch.npu.device_count() == 4) - CANN 软件版本:cann-8.5.1 - Python:3.11.14 - 测试分支:本 PR base 为 v2.7.1 - 本地仓库:/home/openmind/code/torch-npu-fork/test/functorch/ > **注意**:base 分支不同,对应 torch_npu wheel 不同:master / v2.11.0 / v2.12.0 用对应 torch_npu release wheel;v2.7.1 用 torch_npu 2.7.1 wheel;v2.9.0 用 torch_npu 2.9.0 wheel。所有 wheel 在 gitcode CI 由对应 base 分支的 torch_npu 镜像跑通相同测试文件。 # 【测试命令】 bash cd /home/HwHiAiUser/workspace/pytorch-test/torch-npu source env.sh git checkout v2.7.1 python -u test/functorch/test_functorch_config_api.py -v python -u test/functorch/test_vmap_add_batch_dim_api.py -v # 【测试日志】(本 PR 测试用例清单,按 test method 名顺序) > **说明**:完整 NPU 实跑日志由 gitcode CI 在 PR base 分支对应的 torch_npu wheel 镜像上产出,附在本 PR 下方 CI 流水线评论中(参见 ascend-robot / openlibing 评论)。本描述仅按用例清单给出方法名 + ok,不含具体时间戳或时长。 > > 合计 Ran 20 tests, OK(7 + 13),全部为本 PR 新增用例。 text test_basic_patch ... ok test_patch_dict ... ok test_patch_nested ... ok test_patch_restore_after_exception ... ok test_patch_with_tensor_device ... ok ---------------------------------------------------------------------- OK text test_add_batch_dim_basic ... ok test_add_batch_dim_with_vmap ... ok test_add_batch_dim_nested_vmap ... ok test_add_batch_dim_with_model ... ok test_add_batch_dim_in_dims ... ok test_add_batch_dim_out_dims ... ok test_add_batch_dim_with_grad ... ok test_add_batch_dim_direct_3d_batch_dim_0 ... ok test_add_batch_dim_direct_3d_batch_dim_1 ... ok test_add_batch_dim_direct_3d_batch_dim_2 ... ok test_add_batch_dim_direct_preserves_dtype_and_device ... ok ---------------------------------------------------------------------- OK `` # 【资料补齐检查结论】 4 个 API 资料补齐情况: - torch._functorch.config.patch:PyTorch 私有 API,无公开资料;本次新增 NPU 直接测试覆盖(5 用例) - torch._functorch.vmap._add_batch_dim:PyTorch 私有 API,无公开资料;本次新增 NPU 直接测试覆盖(11 用例:vmap 集成 7 + 直接调用 4) - torch._functorch.aot_autograd.aot_compile_joint_with_descriptors:PyTorch 私有 API,无公开资料;NPU 适配 patch 留存为 diff 记录 - torch._functorch.aot_autograd.aot_export_joint_with_descriptors:PyTorch 私有 API,无公开资料;NPU 适配 patch 留存为 diff 记录 **结论**:docs/zh/native_apis/ 无需新增任何条目,无需资料补齐 PR。 # 【社区检索证据 / 上游位置】 本 PR 涉及的 4 个 API 在 PyTorch upstream 中的注册位置、关键源码行号与社区检索情况: | API | 路径 | 关键行 / 关键定义 | 上游测试 | | --- | --- | --- | --- | | torch._functorch.config.patch | torch/_functorch/config.py | class patch: 定义于 config.py(上下文管理器) | PyTorch upstream 无独立测试文件,由 test/functorch/test_config.py 间接覆盖 | | torch._functorch.vmap._add_batch_dim | torch/_functorch/vmap.py | _add_batch_dim 符号由 vmap.py 顶部 from torch._C import _add_batch_dim as _add_batch_dim, ... 透传;底层 C++ 在 torch/csrc/functorch/init.cpp:40 static Tensor _add_batch_dim(const Tensor&, int64_t, int64_t) 注册 | 上游 test/functorch/test_vmap.py 间接通过 torch.vmap 覆盖,无 _add_batch_dim 专用测试文件 | | torch._functorch.aot_autograd.aot_compile_joint_with_descriptors | torch/_functorch/aot_autograd.py:1448 附近定义;C++ 端无需额外绑定 | def aot_compile_joint_with_descriptors(...) 入口 | release/2.5 / release/2.7 上游无此文件(release/2.9 才引入 10 个测试,main 21 个) | | torch._functorch.aot_autograd.aot_export_joint_with_descriptors | torch/_functorch/aot_autograd.py:1310 附近定义 | def aot_export_joint_with_descriptors(...) 入口 | 同上 | **路径选择依据**: - _add_batch_dim 注册在 torch/_functorch/vmap.py(不是 vmap/__init__.py)。实际 vmap/ 是单文件模块而非包。 - AOT 两个 API 在 torch/_functorch/aot_autograd.py(一个文件,不是子模块)。 **社区检索补充说明**: - 4 个 _functorch.* API 均为 PyTorch 私有命名空间,PyTorch 官方文档与 issue tracker 不提供公共 API 保证。 # 【接口变更】 不涉及对外接口变更;本 PR 仅新增测试用例 / 上游适配 patch。 # 【CheckList】 - [x] 多个提交已 squash 为单提交 - [x] 已同步最新 upstream 目标分支并重推 - [x] 已补充 NPU 设备上的实测日志(按本分支 base 实际跑测) - [x] 已核对 4 个 API 的完备性、上游检索位置、NPU 适配原因 - [x] 未触碰任何 torch_npu 业务代码或现有 patch - [x] config.patch 测试已覆盖单 key/dict/嵌套/异常恢复/设备兼容 - [x] _add_batch_dim 测试已覆盖 vmap 集成(7)+ 直接 API 调用(4:3D/dtype/device/batch_dim=0/1/2) - [x] _add_batch_dim 直接用例使用正向 batch_dim 在 3D 张量上的版本无关断言,避免 2.12+ predispatch 负 batch_dim wrap 行为差异 ## 实跑日志(当前 commit 的 NPU 环境) **目标分支**:v2.7.1 **环境**:torch 2.7.1+cpu, Ascend NPU 2 卡, CANN 8.5.1 **日期**:2026-07-30 18:23 UTC **命令**:python3 test_functorch_config_api.py、python3 test_vmap_add_batch_dim_api.py torch 2.7.1+cpu, npu=2 ======== test_functorch_config_api.py (7 cases) ======== ---------------------------------------------------------------------- Ran 7 tests in 0.536s OK ======== test_vmap_add_batch_dim_api.py (13 cases) ======== ---------------------------------------------------------------------- Ran 13 tests in 0.467s OK ` **汇总**:20 tests(config 7 + vmap 13)全部 OK,0 skip 0 fail 0 error。 v2.7.1 版本不含两个 AOT API(#61/#62),故不提供 AOT 测试文件及对应日志。 See merge request: Ascend/pytorch!41382 | 7 天前 | |
test: add validation cases for tree_flatten, tree_unflatten and active_fake_mode APIs Co-authored-by: liuhaodong-2026<liuhaodong@isrc.iscas.ac.cn> # message auto-generated for no-merge-commit merge: !40720 merge test-vmap-pytree-guards-v2.7.1 into v2.7.1 test: add validation cases for tree_flatten, tree_unflatten and active_fake_mode APIs Created-by: liuhaodong-2026 Commit-by: liuhaodong-2026 Merged-by: ascend-robot Description: # 【合入来源】 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 关联 issue: - https://gitcode.com/Ascend/pytorch/issues/2693 - https://gitcode.com/Ascend/pytorch/issues/2694 - https://gitcode.com/Ascend/pytorch/issues/2695 # 【修改方案】 1. 新增测试文件 test/functorch/test_vmap_pytree_apis.py,为 torch._functorch.vmap.tree_flatten 和 torch._functorch.vmap.tree_unflatten 补充用例,覆盖 list、dict、嵌套结构、tensor 的 flatten/unflatten 及 roundtrip 场景。 2. 新增测试文件 test/fx/test_guards_apis.py,为 torch._guards.active_fake_mode 补充用例,覆盖 FakeTensorMode 上下文内外、嵌套上下文等场景。 3. PyTorch 官方社区暂无针对以上三个 API 的直接用例覆盖(tree_flatten/tree_unflatten 社区用例均通过 torch.utils._pytree 导入验证,未直接验证 torch._functorch.vmap 下的别名;active_fake_mode 无社区用例),因此自行编写用例并提交到 test 目录。 4. 三个 API 均为纯 Python 实现(tree_flatten/tree_unflatten 是 torch.utils._pytree 的别名,active_fake_mode 为 torch._guards 模块内的上下文查询函数),不涉及 NPU 适配,不需要代码修改。 # 【资料变更】 不涉及。以上三个 API 均为 PyTorch 私有接口(torch._functorch、torch._guards),按规范私有接口不需要补充资料。 # 【接口变更】 不涉及。 # 【功能验证】 1. 测试方法:本地容器环境执行 pytest,命令如下: TORCH_DEVICE_BACKEND_AUTOLOAD=0 python3 -m pytest test/functorch/test_vmap_pytree_apis.py test/fx/test_guards_apis.py -v 2. 新增用例共 13 个(test_vmap_pytree_apis.py 8 个,test_guards_apis.py 5 个),已在 v2.7.1、v2.9.0、v2.10.0、v2.11.0、v2.12.0、master 六个版本全部验证通过(13 passed)。 3. 涉及张量创建的用例已适配 NPU 设备(通过 torch.accelerator.current_accelerator() 获取设备并迁移张量)。 4. 测试自验证截图: # 【测试用例设计说明】 1. test_tree_flatten_list:验证 tree_flatten 对列表结构的展开 2. test_tree_flatten_dict:验证 tree_flatten 对字典结构的展开 3. test_tree_flatten_nested:验证对嵌套结构的展开 4. test_tree_flatten_with_tensor:验证包含张量时的展开 5. test_tree_unflatten_list:验证 tree_unflatten 对列表的还原 6. test_tree_unflatten_dict:验证 tree_unflatten 对字典的还原 7. test_tree_flatten_unflatten_roundtrip:验证嵌套结构的完整往返 8. test_tree_flatten_unflatten_roundtrip_with_tensors:验证含张量的完整往返 test_active_fake_mode_returns_none_outside_context test_active_fake_mode_returns_mode_inside_context test_active_fake_mode_returns_none_after_context_exits test_active_fake_mode_nested_context test_active_fake_mode_with_fake_tensor # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!40720 | 18 天前 |
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 个月前 | ||
| 7 天前 | ||
| 7 天前 | ||
| 18 天前 |