已合并
test:Add test cases for torch.compiler.compile #42091
test:Add test cases for torch.compiler.compile #42091
已合并
文诺tan创建于 7月19日
文诺tan
文诺tan
7月19日

【合入来源】

关联任务issue: https://gitcode.com/Ascend/pytorch/issues/2991

【修改方案】

torch.compiler.compile (API 94)
PyTorch 官方社区暂无针对该 API 的直接独立用例覆盖,因此自行编写用例并提交到 test 目录。
该 API 为纯 Python 实现,不涉及 NPU 适配,不需要代码修改。
新增 test/dynamo/test_torch_compiler_compile.py,验证 torch.compiler.compile 存在性、基本编译功能及模型编译功能
更新 docs/zh/native_apis/ 下所有版本的 torch-compiler.md,将 torch.compiler.compile 支持状态从"否"改为"是"

【资料变更】

涉及。上游 PyTorch 在 test/dynamo/test_compile.py 中有相关测试,但未针对 torch.compiler.compile 公开 API 编写独立测试。本次提交补充了 3 个测试方法,覆盖 API 存在性、函数编译、Module 编译的核心行为,测试文件位于test/dynamo/test_torch_compiler_compile.py更新 docs/zh/native_apis/ 下 pytorch2-7-1 至 pytorch2-12-0 的 torch-compiler.md,将 torch.compiler.compile 标记为已支持。

【接口变更】

不涉及

【功能介绍】

torch.compiler.compile 是 PyTorch 2.x 引入的公开编译 API,用于将函数或 nn.Module 通过 TorchDynamo 进行即时编译优化。支持指定 backend(如 "eager"、"inductor" 等),返回编译后的可调用对象。

【功能验证】

test_torch_compiler_compile.py(torch.compiler.compile)
1.test_torch_compiler_compile_exists:验证torch.compiler.compile接口存在且可调用
2.test_torch_compiler_compile_basic:验证torch.compiler.compile对简单函数的编译功能正常
3.test_torch_compiler_compile_with_model:验证torch.compiler.compile对nn.Module模型的编译功能正常

运行命令:
python -m pytest /workspace/user_data/pytorch-v2.12.0/test/dynamo/test_torch_compiler_compile.py -xvs 2>&1
运行结果:
2.12.0-compile.png

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 文诺tan 的贡献)
文诺tan文诺tan
7月19日 创建了 pull request,commit 739df041
文诺tan文诺tan
7月19日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(94)
atomgit-bot
atomgit-bot
7月19日 评论:

变更摘要

该 PR 为 torch.compiler.compile API 新增了独立的测试用例文件,验证该接口的存在性、基本编译功能以及对 nn.Module 模型的编译支持。此前该 API 缺乏直接的用例覆盖,本次新增的测试填补了这一空白。

主要改动

  • 新增测试文件 test/test_torch_compiler_compile.py:创建独立的测试模块,包含 ToyModel 辅助模型类和 TestTorchCompilerCompile 测试类,共计 46 行代码。
  • 接口存在性验证 (test_torch_compiler_compile_exists):通过 hasattrcallable 断言确认 torch.compiler.compile 属性存在且为可调用对象。
  • 基本编译功能验证 (test_torch_compiler_compile_basic):使用 backend="eager" 对简单函数 fn(x) = x + 1 进行编译,并验证编译后的输出与原函数输出一致。
  • 模型编译功能验证 (test_torch_compiler_compile_with_model):对包含 LinearReLUToyModel 进行 torch.compiler.compile 编译,通过 torch.allclose 断言编译后模型的输出与原始模型输出一致。
likedislike
atomgit-bot
atomgit-bot
7月19日 评论:

代码审查

关闭摘要

审查了 1 个变更文件:

  • test/test_torch_compiler_compile.py(新增,+46 行):发现 1 个 P3 问题(缺少 torch.manual_seed() 导致测试不可复现),这是一个可选的测试质量改进,非必需修复。

各优先级统计

  • P0: 0
  • P1: 0
  • P2: 0
  • P3: 1

整体风险评估

此变更新增了 torch.compiler.compile API 的基础功能测试用例,代码逻辑正确,测试结构清晰,遵循了仓库中已有的测试模式(使用 TestCase + run_tests())。未发现安全性、正确性或可靠性问题。测试使用 backend="eager" 作为最基础的编译后端进行冒烟测试,这是验证 API 可用性的合理选择。CI 基础设施能够正确发现并执行此测试文件。整体风险很低。

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

likedislike
此处折叠了74条消息 查看更多
chenrayray
chenrayray成员
22 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
22 天前 添加了label:approvedlgtm
ascend-robotascend-robot成员
22 天前 删除了label:ci-pipeline-passed
ascend-robotascend-robot成员
22 天前 合入了pull request
ascend-robot
ascend-robot成员
22 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13306 [ commitID:8687dc6d ] 已完成
likedislike