已合并
test: add validation cases for tree_flatten, tree_unflatten and active_fake_mode APIs #40720
liuhaodong-2026创建于 7月11日
test: add validation cases for tree_flatten, tree_unflatten and active_fake_mode APIs #40720
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 liuhaodong-2026 的贡献)7月11日 创建了 pull request,commit 01e4f8bf
7月11日 关联了issue:【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(70)
atomgit-bot
7月11日 评论:
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_flatten和tree_unflatten添加 8 个测试用例,覆盖 list、dict、嵌套结构、tensor 的 flatten 及 unflatten 往返验证(如test_tree_flatten_list、test_tree_flatten_nested、test_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_context、test_active_fake_mode_nested_context等)。 - 测试类均继承自
TestCase:两个文件中的TestVmapPytreeAPIs和TestActiveFakeMode均使用torch.testing._internal.common_utils.TestCase和run_tests,遵循现有测试框架约定。


ascend-robot
7月11日 评论:
7月11日 评论:
atomgit-bot
7月11日 评论:
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 验证用例,不涉及生产代码修改。发现的问题均为测试质量层面的改进建议(断言不够严格、测试覆盖不完整),不影响构建、运行时行为或数据正确性。测试文件可正常导入和运行,核心测试逻辑正确。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


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


20 天前 添加了label:approvedlgtm
20 天前 合入了pull request
ascend-robot
20 天前 评论:
20 天前 评论:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


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


【合入来源】
关联 issue:
【修改方案】
test/functorch/test_vmap_pytree_apis.py,为torch._functorch.vmap.tree_flatten和torch._functorch.vmap.tree_unflatten补充用例,覆盖 list、dict、嵌套结构、tensor 的 flatten/unflatten 及 roundtrip 场景。test/fx/test_guards_apis.py,为torch._guards.active_fake_mode补充用例,覆盖FakeTensorMode上下文内外、嵌套上下文等场景。tree_flatten/tree_unflatten社区用例均通过torch.utils._pytree导入验证,未直接验证torch._functorch.vmap下的别名;active_fake_mode无社区用例),因此自行编写用例并提交到 test 目录。tree_flatten/tree_unflatten是torch.utils._pytree的别名,active_fake_mode为torch._guards模块内的上下文查询函数),不涉及 NPU 适配,不需要代码修改。【资料变更】
不涉及。以上三个 API 均为 PyTorch 私有接口(
torch._functorch、torch._guards),按规范私有接口不需要补充资料。【接口变更】
不涉及。
【功能验证】
测试方法:本地容器环境执行 pytest,命令如下:
TORCH_DEVICE_BACKEND_AUTOLOAD=0 python3 -m pytest
test/functorch/test_vmap_pytree_apis.py
test/fx/test_guards_apis.py -v
新增用例共 13 个(
test_vmap_pytree_apis.py8 个,test_guards_apis.py5 个),已在 v2.7.1、v2.9.0、v2.10.0、v2.11.0、v2.12.0、master 六个版本全部验证通过(13 passed)。涉及张量创建的用例已适配 NPU 设备(通过
torch.accelerator.current_accelerator()获取设备并迁移张量)。测试自验证截图:
【测试用例设计说明】
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】