已合并
test: add validation cases for tree_flatten, tree_unflatten and active_fake_mode APIs #40720
test: add validation cases for tree_flatten, tree_unflatten and active_fake_mode APIs #40720
已合并
liuhaodong-2026创建于 7月11日
7月11日

【合入来源】

关联 issue:

【修改方案】

  1. 新增测试文件 test/functorch/test_vmap_pytree_apis.py,为 torch._functorch.vmap.tree_flattentorch._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_unflattentorch.utils._pytree 的别名,active_fake_modetorch._guards 模块内的上下文查询函数),不涉及 NPU 适配,不需要代码修改。

【资料变更】

不涉及。以上三个 API 均为 PyTorch 私有接口(torch._functorchtorch._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. 测试自验证截图:image.png

【测试用例设计说明】

  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】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 liuhaodong-2026 的贡献)
Lliuhaodong-2026
7月11日 创建了 pull request,commit 01e4f8bf
Lliuhaodong-2026
7月11日 关联了issue:【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(70)
atomgit-bot
atomgit-bot
7月11日 评论:

变更摘要

本 PR 为两个 PyTorch 私有 API 模块新增了测试用例文件,主要覆盖 torch._functorch.vmap 下的 tree_flatten/tree_unflatten 以及 torch._guards 下的 active_fake_mode 共三个接口。这些接口在社区中缺乏直接的用例验证,本次通过新增独立的测试文件补全了对 list、dict、嵌套结构、tensor 的 flatten/unflatten 往返测试,以及 FakeTensorMode 上下文内外、嵌套上下文等场景的覆盖。所有改动均为纯测试代码,不涉及任何源代码修改。

主要改动

  • 新增 test/functorch/test_vmap_pytree_apis.py:为 torch._functorch.vmap.tree_flattentree_unflatten 添加 8 个测试用例,覆盖 list、dict、嵌套结构、tensor 的 flatten 及 unflatten 往返验证(如 test_tree_flatten_listtest_tree_flatten_nestedtest_tree_flatten_unflatten_roundtrip_with_tensors 等)。
  • 新增 test/fx/test_guards_apis.py:为 torch._guards.active_fake_mode 添加 5 个测试用例,覆盖 FakeTensorMode 上下文外返回 None、上下文内返回实例、退出后恢复 None、嵌套上下文以及 fake tensor 场景(如 test_active_fake_mode_returns_none_outside_contexttest_active_fake_mode_nested_context 等)。
  • 测试类均继承自 TestCase:两个文件中的 TestVmapPytreeAPIsTestActiveFakeMode 均使用 torch.testing._internal.common_utils.TestCaserun_tests,遵循现有测试框架约定。
likedislike
atomgit-bot
atomgit-bot
7月11日 评论:

代码审查

审查总结

本次审查覆盖了以下 2 个变更文件:

  • test/functorch/test_vmap_pytree_apis.py:已审查,发现 2 个 P3 问题(测试断言不完整、模块级变量写法不一致)。
  • test/fx/test_guards_apis.py:已审查,发现 2 个 P3 问题(嵌套上下文测试断言过于宽松、fake tensor 测试未验证实际行为且存在未使用变量)。

按优先级统计

  • P0:0
  • P1:0
  • P2:0
  • P3:4

整体风险评估:低风险。这些测试文件均为新增的 API 验证用例,不涉及生产代码修改。发现的问题均为测试质量层面的改进建议(断言不够严格、测试覆盖不完整),不影响构建、运行时行为或数据正确性。测试文件可正常导入和运行,核心测试逻辑正确。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
此处折叠了159条消息 查看更多
梁松伟
梁松伟成员
20 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
20 天前 添加了label:approvedlgtm
ascend-robotascend-robot成员
20 天前 合入了pull request
ascend-robot
ascend-robot成员
20 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
20 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13478 [ commitID:eb525835 ] 已完成
likedislike