| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
[test][master] add NPU validation cases for torch._functorch APIs (issues #2684 #2685 #2687 #2688) Co-authored-by: ggg_0963<1873823162@qq.com> # message auto-generated for no-merge-commit merge: !41387 merge test-functorch-config-vmap-master into master [test][master] add NPU validation cases for torch._functorch APIs (issues #2684 #2685 #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/functorch/test_aot_joint_with_descriptors_api.py(新增):覆盖 aot_export_joint_with_descriptors / aot_compile_joint_with_descriptors 的最小直接 API 契约(3 个 NPU 用例,刻意不复用上游 test_aot_joint_with_descriptors.py scaffolding) - test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch:**本 PR 不新增**。本 PR base 为 Ascend 仓 master 分支(即 Ascend meta-repo),其 test/functorch/ 目录下未独立收录 test_aot_joint_with_descriptors.py(仅有 test_eager_transforms.py),所以 patch 无落点;该文件在 PyTorch upstream main 分支存在(21 个测试),由 #41383(v2.9.0) / #41385(v2.11.0) / #41386(v2.12.0) 的 patch 覆盖。任务61(#2684) 任务62(#2685) 在 master 上的 AOT 适配由本 PR 的本地 AOT 直接 API 实测(3 用例)补齐。 合计新增 23 个 PR 内独立测试用例(7 + 13 + 3),全部 NPU 实测通过;AOT API 的 NPU 适配在 v2.9+ 分支的 patch + 本地 3 用例直接实测补齐。 # 【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:否 # 【测试方案】 PR 内 23 个独立用例(7 + 13 + 3): 1. **config.patch**(7 个 PR 内用例,新增 test_patch_invalid_key、test_patch_invalid_dict_key): - 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 集成 + 直接调用,9 个 PR 内用例)**: - 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 个新增 PR 内用例,验证 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**: - **PR diff 内 patch**:本 PR 在 master 上不落 patch(Ascend meta-repo master 不独立收录该上游 test file;NPU 适配 patch 由 v2.9+ 分支落 test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch,覆盖 PyTorch upstream release/2.9 的 10 个测试 / release/2.11/2.12 的 18 个测试(含 2.11/2.12 新增 8 条 upstream 测试的 NPU 适配)/ main 的 21 个测试,patch diff 范围以 device_type 顶部声明 + model.to(device_type) / inputs.to(device_type) 适配为准;**release 分支的 patch 已 apply 后在匹配 wheel 环境实跑至全 OK**——release/2.9 + patch 在 torch_npu 2.9.0 wheel 环境 10/10 通过、release/2.11 + patch 在 torch_npu 2.11.0rc3 匹配 wheel 环境 18/18 通过、release/2.12 + patch 在 torch_npu 2.12.0rc1 匹配 wheel 环境 18/18 通过,详见 #41383/#41385/#41386 各 release PR 的【upstream patch 实跑日志】段。master 分支因无对应 test_upstream/ patch 落点,不重复实跑)。 - **本地 AOT 直接 API 实测**(3 用例,最小独立设计):在 master + torch_npu master wheel 环境下,针对两个 AOT API 的端到端契约跑 3 个最小用例。刻意不复用上游 test_aot_joint_with_descriptors.py scaffolding(命名的 nn.Module 子类、assertExpectedInline FX 图文本比对、decomposition_table),改用 nn.Sequential(nn.Linear(2, 1)) 作 eager reference,只断言 API 的可观测契约 + 编译产物端到端 forward 与 eager 结果一致。aot_export_joint_with_descriptors 返回 JointWithDescriptors 暴露 graph_module + _aot_state;aot_compile_joint_with_descriptors 返回 callable,调用约定为 compiled(*params, *inputs)(callable 经 fx_pytree 把 (params, inputs) 拍平为位置参数,与上游 release/2.9+ 测试约定 parallel_model_fn(*dict(model.named_parameters()).values(), *inputs) 一致),NPU 上端到端 forward + assert_close 实测通过。 - **上游测试规模(PyTorch upstream)**:main 含 21 个 def test_* 方法(test_simple_linear_module / test_conv_bn_module / test_module_with_kwargs / test_multiple_outputs_module / test_in_out_specs / test_fx_utils_simple_linear / test_fx_utils_conv_bn_module / test_fx_utils_multiple_outputs / test_fx_utils_node_consistency / test_export_and_compile / test_preserve_annotate_simple / test_preserve_annotate_flex_attention / test_preserve_annotate_function / test_annotate_fn_anchors_nested_functional_call / test_annotate_fn_anchors_pure_tensor_ops / test_annotate_fn_nested_with_module_forward / test_custom_op_stack_trace / test_preserve_annotate_replay_view / test_static_input_indices / test_no_annotation_on_gradient_acc_nodes / test_annotate_invoke_subgraph_simple);release/2.11/2.12 各含 18 个;release/2.9 含 10 个。 # 【测试环境】 - 操作系统: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 为 master - 本地仓库:/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 master # PR 内测试文件 python -u test/functorch/test_functorch_config_api.py -v python -u test/functorch/test_vmap_add_batch_dim_api.py -v python -u test/functorch/test_aot_joint_with_descriptors_api.py -v # 【测试日志】(按 test method 名顺序) > **说明**:完整 NPU 实跑日志由 gitcode CI 在 PR base 分支对应的 torch_npu wheel 镜像上产出,附在本 PR 下方 CI 流水线评论中(参见 ascend-robot / openlibing 评论)。本描述仅按用例清单给出方法名 + ok,不含具体时间戳或时长。 > > 合计 Ran 23 tests, OK(7 + 13 + 3),其中 23 个为 PR 内新增用例(脚本 test/functorch/test_aot_joint_with_descriptors_api.py 已随本 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 text test_export_returns_joint_with_descriptors ... ok test_export_preserves_npu_device ... ok test_compile_runs_and_matches_eager ... ok ---------------------------------------------------------------------- Ran 3 tests 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 记录(v2.9+ 分支,已 apply 后在匹配 wheel 环境实跑至全 OK——release/2.9 1 #41383/#41385/#41386)+ 本地 3 用例直接实测(端到端 forward 与 eager 一致) - torch._functorch.aot_autograd.aot_export_joint_with_descriptors:PyTorch 私有 API,无公开资料;NPU 适配 patch 留存为 diff 记录(v2.9+ 分支,已 apply 后在匹配 wheel 环境实跑至全 OK——release/2.9 1 #41383/#41385/#41386)+ 本地 3 用例直接实测(export 返回 JointWithDescriptors 暴露 graph_module + _aot_state) **结论**:docs/z See merge request: Ascend/pytorch!41387 | 26 天前 | |
test_functorch Co-authored-by: SCh_zx<1325467101@qq.com> # message auto-generated for no-merge-commit merge: !38899 merge master into master 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!38899 | 2 个月前 | |
【API一致性任务】test: add torch._C._functorch.is_batchedtensor validation cases on NPU Co-authored-by: cuiyunhao-2026<1830099122@qq.com> # message auto-generated for no-merge-commit merge: !43533 merge test-is-batchedtensor-npu-master into master 【API一致性任务】test: add torch._C._functorch.is_batchedtensor validation cases on NPU Created-by: cuiyunhao-2026 Commit-by: cuiyunhao-2026 Merged-by: ascend-robot Description: # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font> > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> 关联 Issue:https://gitcode.com/Ascend/pytorch/issues/2751 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 适配 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_batchedtensor 在 test/ 下的引用,仅在 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 包裹后为 True,get_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; - 运行结果:  See merge request: Ascend/pytorch!43533 | 11 天前 | |
[test][master] add NPU validation cases for torch._functorch APIs (issues #2684 #2685 #2687 #2688) Co-authored-by: ggg_0963<1873823162@qq.com> # message auto-generated for no-merge-commit merge: !41387 merge test-functorch-config-vmap-master into master [test][master] add NPU validation cases for torch._functorch APIs (issues #2684 #2685 #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/functorch/test_aot_joint_with_descriptors_api.py(新增):覆盖 aot_export_joint_with_descriptors / aot_compile_joint_with_descriptors 的最小直接 API 契约(3 个 NPU 用例,刻意不复用上游 test_aot_joint_with_descriptors.py scaffolding) - test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch:**本 PR 不新增**。本 PR base 为 Ascend 仓 master 分支(即 Ascend meta-repo),其 test/functorch/ 目录下未独立收录 test_aot_joint_with_descriptors.py(仅有 test_eager_transforms.py),所以 patch 无落点;该文件在 PyTorch upstream main 分支存在(21 个测试),由 #41383(v2.9.0) / #41385(v2.11.0) / #41386(v2.12.0) 的 patch 覆盖。任务61(#2684) 任务62(#2685) 在 master 上的 AOT 适配由本 PR 的本地 AOT 直接 API 实测(3 用例)补齐。 合计新增 23 个 PR 内独立测试用例(7 + 13 + 3),全部 NPU 实测通过;AOT API 的 NPU 适配在 v2.9+ 分支的 patch + 本地 3 用例直接实测补齐。 # 【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:否 # 【测试方案】 PR 内 23 个独立用例(7 + 13 + 3): 1. **config.patch**(7 个 PR 内用例,新增 test_patch_invalid_key、test_patch_invalid_dict_key): - 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 集成 + 直接调用,9 个 PR 内用例)**: - 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 个新增 PR 内用例,验证 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**: - **PR diff 内 patch**:本 PR 在 master 上不落 patch(Ascend meta-repo master 不独立收录该上游 test file;NPU 适配 patch 由 v2.9+ 分支落 test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch,覆盖 PyTorch upstream release/2.9 的 10 个测试 / release/2.11/2.12 的 18 个测试(含 2.11/2.12 新增 8 条 upstream 测试的 NPU 适配)/ main 的 21 个测试,patch diff 范围以 device_type 顶部声明 + model.to(device_type) / inputs.to(device_type) 适配为准;**release 分支的 patch 已 apply 后在匹配 wheel 环境实跑至全 OK**——release/2.9 + patch 在 torch_npu 2.9.0 wheel 环境 10/10 通过、release/2.11 + patch 在 torch_npu 2.11.0rc3 匹配 wheel 环境 18/18 通过、release/2.12 + patch 在 torch_npu 2.12.0rc1 匹配 wheel 环境 18/18 通过,详见 #41383/#41385/#41386 各 release PR 的【upstream patch 实跑日志】段。master 分支因无对应 test_upstream/ patch 落点,不重复实跑)。 - **本地 AOT 直接 API 实测**(3 用例,最小独立设计):在 master + torch_npu master wheel 环境下,针对两个 AOT API 的端到端契约跑 3 个最小用例。刻意不复用上游 test_aot_joint_with_descriptors.py scaffolding(命名的 nn.Module 子类、assertExpectedInline FX 图文本比对、decomposition_table),改用 nn.Sequential(nn.Linear(2, 1)) 作 eager reference,只断言 API 的可观测契约 + 编译产物端到端 forward 与 eager 结果一致。aot_export_joint_with_descriptors 返回 JointWithDescriptors 暴露 graph_module + _aot_state;aot_compile_joint_with_descriptors 返回 callable,调用约定为 compiled(*params, *inputs)(callable 经 fx_pytree 把 (params, inputs) 拍平为位置参数,与上游 release/2.9+ 测试约定 parallel_model_fn(*dict(model.named_parameters()).values(), *inputs) 一致),NPU 上端到端 forward + assert_close 实测通过。 - **上游测试规模(PyTorch upstream)**:main 含 21 个 def test_* 方法(test_simple_linear_module / test_conv_bn_module / test_module_with_kwargs / test_multiple_outputs_module / test_in_out_specs / test_fx_utils_simple_linear / test_fx_utils_conv_bn_module / test_fx_utils_multiple_outputs / test_fx_utils_node_consistency / test_export_and_compile / test_preserve_annotate_simple / test_preserve_annotate_flex_attention / test_preserve_annotate_function / test_annotate_fn_anchors_nested_functional_call / test_annotate_fn_anchors_pure_tensor_ops / test_annotate_fn_nested_with_module_forward / test_custom_op_stack_trace / test_preserve_annotate_replay_view / test_static_input_indices / test_no_annotation_on_gradient_acc_nodes / test_annotate_invoke_subgraph_simple);release/2.11/2.12 各含 18 个;release/2.9 含 10 个。 # 【测试环境】 - 操作系统: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 为 master - 本地仓库:/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 master # PR 内测试文件 python -u test/functorch/test_functorch_config_api.py -v python -u test/functorch/test_vmap_add_batch_dim_api.py -v python -u test/functorch/test_aot_joint_with_descriptors_api.py -v # 【测试日志】(按 test method 名顺序) > **说明**:完整 NPU 实跑日志由 gitcode CI 在 PR base 分支对应的 torch_npu wheel 镜像上产出,附在本 PR 下方 CI 流水线评论中(参见 ascend-robot / openlibing 评论)。本描述仅按用例清单给出方法名 + ok,不含具体时间戳或时长。 > > 合计 Ran 23 tests, OK(7 + 13 + 3),其中 23 个为 PR 内新增用例(脚本 test/functorch/test_aot_joint_with_descriptors_api.py 已随本 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 text test_export_returns_joint_with_descriptors ... ok test_export_preserves_npu_device ... ok test_compile_runs_and_matches_eager ... ok ---------------------------------------------------------------------- Ran 3 tests 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 记录(v2.9+ 分支,已 apply 后在匹配 wheel 环境实跑至全 OK——release/2.9 1 #41383/#41385/#41386)+ 本地 3 用例直接实测(端到端 forward 与 eager 一致) - torch._functorch.aot_autograd.aot_export_joint_with_descriptors:PyTorch 私有 API,无公开资料;NPU 适配 patch 留存为 diff 记录(v2.9+ 分支,已 apply 后在匹配 wheel 环境实跑至全 OK——release/2.9 1 #41383/#41385/#41386)+ 本地 3 用例直接实测(export 返回 JointWithDescriptors 暴露 graph_module + _aot_state) **结论**:docs/z See merge request: Ascend/pytorch!41387 | 26 天前 | |
[test][master] add NPU validation cases for torch._functorch APIs (issues #2684 #2685 #2687 #2688) Co-authored-by: ggg_0963<1873823162@qq.com> # message auto-generated for no-merge-commit merge: !41387 merge test-functorch-config-vmap-master into master [test][master] add NPU validation cases for torch._functorch APIs (issues #2684 #2685 #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/functorch/test_aot_joint_with_descriptors_api.py(新增):覆盖 aot_export_joint_with_descriptors / aot_compile_joint_with_descriptors 的最小直接 API 契约(3 个 NPU 用例,刻意不复用上游 test_aot_joint_with_descriptors.py scaffolding) - test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch:**本 PR 不新增**。本 PR base 为 Ascend 仓 master 分支(即 Ascend meta-repo),其 test/functorch/ 目录下未独立收录 test_aot_joint_with_descriptors.py(仅有 test_eager_transforms.py),所以 patch 无落点;该文件在 PyTorch upstream main 分支存在(21 个测试),由 #41383(v2.9.0) / #41385(v2.11.0) / #41386(v2.12.0) 的 patch 覆盖。任务61(#2684) 任务62(#2685) 在 master 上的 AOT 适配由本 PR 的本地 AOT 直接 API 实测(3 用例)补齐。 合计新增 23 个 PR 内独立测试用例(7 + 13 + 3),全部 NPU 实测通过;AOT API 的 NPU 适配在 v2.9+ 分支的 patch + 本地 3 用例直接实测补齐。 # 【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:否 # 【测试方案】 PR 内 23 个独立用例(7 + 13 + 3): 1. **config.patch**(7 个 PR 内用例,新增 test_patch_invalid_key、test_patch_invalid_dict_key): - 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 集成 + 直接调用,9 个 PR 内用例)**: - 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 个新增 PR 内用例,验证 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**: - **PR diff 内 patch**:本 PR 在 master 上不落 patch(Ascend meta-repo master 不独立收录该上游 test file;NPU 适配 patch 由 v2.9+ 分支落 test_upstream/test/functorch/test_aot_joint_with_descriptors.py.patch,覆盖 PyTorch upstream release/2.9 的 10 个测试 / release/2.11/2.12 的 18 个测试(含 2.11/2.12 新增 8 条 upstream 测试的 NPU 适配)/ main 的 21 个测试,patch diff 范围以 device_type 顶部声明 + model.to(device_type) / inputs.to(device_type) 适配为准;**release 分支的 patch 已 apply 后在匹配 wheel 环境实跑至全 OK**——release/2.9 + patch 在 torch_npu 2.9.0 wheel 环境 10/10 通过、release/2.11 + patch 在 torch_npu 2.11.0rc3 匹配 wheel 环境 18/18 通过、release/2.12 + patch 在 torch_npu 2.12.0rc1 匹配 wheel 环境 18/18 通过,详见 #41383/#41385/#41386 各 release PR 的【upstream patch 实跑日志】段。master 分支因无对应 test_upstream/ patch 落点,不重复实跑)。 - **本地 AOT 直接 API 实测**(3 用例,最小独立设计):在 master + torch_npu master wheel 环境下,针对两个 AOT API 的端到端契约跑 3 个最小用例。刻意不复用上游 test_aot_joint_with_descriptors.py scaffolding(命名的 nn.Module 子类、assertExpectedInline FX 图文本比对、decomposition_table),改用 nn.Sequential(nn.Linear(2, 1)) 作 eager reference,只断言 API 的可观测契约 + 编译产物端到端 forward 与 eager 结果一致。aot_export_joint_with_descriptors 返回 JointWithDescriptors 暴露 graph_module + _aot_state;aot_compile_joint_with_descriptors 返回 callable,调用约定为 compiled(*params, *inputs)(callable 经 fx_pytree 把 (params, inputs) 拍平为位置参数,与上游 release/2.9+ 测试约定 parallel_model_fn(*dict(model.named_parameters()).values(), *inputs) 一致),NPU 上端到端 forward + assert_close 实测通过。 - **上游测试规模(PyTorch upstream)**:main 含 21 个 def test_* 方法(test_simple_linear_module / test_conv_bn_module / test_module_with_kwargs / test_multiple_outputs_module / test_in_out_specs / test_fx_utils_simple_linear / test_fx_utils_conv_bn_module / test_fx_utils_multiple_outputs / test_fx_utils_node_consistency / test_export_and_compile / test_preserve_annotate_simple / test_preserve_annotate_flex_attention / test_preserve_annotate_function / test_annotate_fn_anchors_nested_functional_call / test_annotate_fn_anchors_pure_tensor_ops / test_annotate_fn_nested_with_module_forward / test_custom_op_stack_trace / test_preserve_annotate_replay_view / test_static_input_indices / test_no_annotation_on_gradient_acc_nodes / test_annotate_invoke_subgraph_simple);release/2.11/2.12 各含 18 个;release/2.9 含 10 个。 # 【测试环境】 - 操作系统: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 为 master - 本地仓库:/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 master # PR 内测试文件 python -u test/functorch/test_functorch_config_api.py -v python -u test/functorch/test_vmap_add_batch_dim_api.py -v python -u test/functorch/test_aot_joint_with_descriptors_api.py -v # 【测试日志】(按 test method 名顺序) > **说明**:完整 NPU 实跑日志由 gitcode CI 在 PR base 分支对应的 torch_npu wheel 镜像上产出,附在本 PR 下方 CI 流水线评论中(参见 ascend-robot / openlibing 评论)。本描述仅按用例清单给出方法名 + ok,不含具体时间戳或时长。 > > 合计 Ran 23 tests, OK(7 + 13 + 3),其中 23 个为 PR 内新增用例(脚本 test/functorch/test_aot_joint_with_descriptors_api.py 已随本 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 text test_export_returns_joint_with_descriptors ... ok test_export_preserves_npu_device ... ok test_compile_runs_and_matches_eager ... ok ---------------------------------------------------------------------- Ran 3 tests 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 记录(v2.9+ 分支,已 apply 后在匹配 wheel 环境实跑至全 OK——release/2.9 1 #41383/#41385/#41386)+ 本地 3 用例直接实测(端到端 forward 与 eager 一致) - torch._functorch.aot_autograd.aot_export_joint_with_descriptors:PyTorch 私有 API,无公开资料;NPU 适配 patch 留存为 diff 记录(v2.9+ 分支,已 apply 后在匹配 wheel 环境实跑至全 OK——release/2.9 1 #41383/#41385/#41386)+ 本地 3 用例直接实测(export 返回 JointWithDescriptors 暴露 graph_module + _aot_state) **结论**:docs/z See merge request: Ascend/pytorch!41387 | 26 天前 | |
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: !40725 merge test-vmap-pytree-guards-master into master 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!40725 | 1 个月前 |
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 26 天前 | ||
| 2 个月前 | ||
| 11 天前 | ||
| 26 天前 | ||
| 26 天前 | ||
| 1 个月前 |