PAGEMRW
cb7b6eaae4
test: add functional export tests for NPU
Co-authored-by: PAGEMRW<985608880@qq.com>
# message auto-generated for no-merge-commit merge:
!41798 merge test-functional-export-master into master
test: add functional export tests for NPU
Created-by: PAGEMRW
Commit-by: PAGEMRW
Merged-by: ascend-robot
Description: 关联 Issue:#3011
【合入来源】
Issue #3011:补充 TorchDynamo 相关 API 的独立验证用例。
PyTorch 社区现有用例主要在 `torch.export` 等上层流程中间接使用 TorchDynamo 图捕获能力,缺少针对以下两个 API 的独立、聚焦验证:
- `torch._dynamo.functional_export.dynamo_graph_capture_for_export`
- `torch._dynamo.utils.is_compile_supported`
因此,本次新增测试文件:
```text
test/dynamo/test_functional_export.py
```
用于验证上述 API 的核心功能和主要判断分支。
版本适配范围如下:
1. `torch._dynamo.functional_export.dynamo_graph_capture_for_export`:PyTorch 2.10.0 及以上版本;
2. `torch._dynamo.utils.is_compile_supported`:PyTorch 2.7.1、2.8.0、2.9.0、2.10.0 和 2.11.0。
【修改方案】
一、API 功能说明
1. `torch._dynamo.functional_export.dynamo_graph_capture_for_export`
该 API 是 TorchDynamo functional export 流程中的图捕获入口,主要功能包括:
- 接收普通 Python 函数、可调用对象或 `torch.nn.Module`;
- 通过 TorchDynamo 的完整图捕获流程获取计算图;
- 将捕获结果构造为 `torch.fx.GraphModule`;
- 支持位置参数、关键字参数和 PyTree 输入输出结构;
- 保留模块参数、缓冲区和子模块等必要状态;
- 保证捕获图与原始函数或模块的计算语义一致;
- 维护 export 后续流程所需的输入输出结构及图元数据。
1. `torch._dynamo.utils.is_compile_supported`
该 API 用于根据当前 Python 运行环境和指定设备类型,判断 TorchDynamo 编译流程是否具备基础支持条件,主要逻辑包括:
- 调用 `torch._dynamo.eval_frame.is_dynamo_supported()` 检查当前环境是否支持 TorchDynamo;
- 对 `cpu` 返回当前环境的 Dynamo 支持状态;
- 对 `cuda` 等设备同时检查 Dynamo 和 Triton 是否可用;
- 对未注册的设备类型返回 `False`;
- 较新版本支持通过 `torch.device(device_type).type` 规范化设备类型;
- 查询过程不会创建张量、初始化设备或执行实际编译任务。
二、测试用例说明
新增的 `test_functional_export.py` 从图捕获能力、结果一致性、模块兼容性、设备判断分支和 Mock 隔离性等方面验证上述两个 API。
1. `dynamo_graph_capture_for_export` 测试
(1)普通函数图捕获
使用包含基础张量运算的普通 Python 函数进行验证:
- 调用 `dynamo_graph_capture_for_export` 获取图捕获函数;
- 使用示例张量执行图捕获;
- 验证返回对象为 `torch.fx.GraphModule`;
- 分别执行原始函数和捕获后的图;
- 验证两者输出结果一致。
该测试验证 API 能够接收普通可调用对象、完成 TorchDynamo 图捕获并返回语义一致的 FX 图。
(2)`nn.Module` 图捕获
构造包含参数和缓冲区的简单 `torch.nn.Module`,验证:
- 模块前向计算能够被正常捕获;
- 捕获结果为可执行的 `torch.fx.GraphModule`;
- 原始模块与捕获图在相同输入下输出一致;
- 模块参数和缓冲区能够正确参与捕获后的计算。
测试主要关注 API 的外部行为,不依赖 FX 图内部自动生成的节点名称或具体图结构,避免因上游内部实现调整产生非功能性失败。
1. `is_compile_supported` 测试
(1)CPU 支持分支
`test_cpu_supported_when_dynamo_is_supported`:
- Mock `is_dynamo_supported()` 返回 `True`;
- 调用 `is_compile_supported("cpu")`;
- 验证返回值为 `True`;
- 验证底层环境检查函数被调用一次。
`test_cpu_unsupported_when_dynamo_is_unsupported`:
- Mock `is_dynamo_supported()` 返回 `False`;
- 调用 `is_compile_supported("cpu")`;
- 验证返回值为 `False`。
(2)CUDA 与 Triton 联合判断分支
`test_cuda_supported_with_triton`:
- Mock `is_dynamo_supported()` 返回 `True`;
- Mock `has_triton()` 返回 `True`;
- 调用 `is_compile_supported("cuda")`;
- 验证返回值为 `True`;
- 验证两项检查均被正确调用。
`test_cuda_unsupported_without_triton`:
- Mock `is_dynamo_supported()` 返回 `True`;
- Mock `has_triton()` 返回 `False`;
- 调用 `is_compile_supported("cuda")`;
- 验证返回值为 `False`。
(3)未注册设备分支
`test_npu_returns_false`:
- Mock `is_dynamo_supported()` 返回 `True`;
- 调用 `is_compile_supported("npu")`;
- 验证返回值为 `False`。
对应版本的 PyTorch 上游实现未将 `npu` 加入该函数内置的编译设备类型列表,因此该测试用于验证未注册设备进入默认分支并返回 `False`,保证 torch-npu 仓库中的行为与对应上游版本一致。
该结果仅表示 `is_compile_supported` 当前未注册 `npu` 设备类型,不代表 torch-npu 整体不具备编译能力。
1. Mock 隔离性
`is_compile_supported` 的结果会受到 Python 版本、TorchDynamo 可用状态和 Triton 安装情况影响。
测试使用 `unittest.mock.patch` 控制以下函数的返回值:
```text
torch._dynamo.eval_frame.is_dynamo_supported
torch._dynamo.utils.has_triton
```
从而实现:
- 不依赖测试机器是否安装 CUDA、NPU 或 Triton;
- 不依赖当前 Python 环境是否真实支持 TorchDynamo;
- 稳定覆盖各个条件分支;
- 验证底层依赖函数的调用关系;
- 避免不同 CI 环境配置造成测试结果波动。
每个 Mock 仅在对应测试方法执行期间生效,测试结束后自动恢复,不会造成跨用例状态污染。
综上,该测试文件覆盖:
- 普通函数的 TorchDynamo 图捕获;
- `nn.Module` 的图捕获;
- `torch.fx.GraphModule` 返回类型;
- 捕获前后计算结果一致性;
- 模块参数和缓冲区兼容性;
- CPU 支持与不支持分支;
- CUDA 与 Triton 联合判断分支;
- NPU 等未注册设备的默认处理分支;
- Mock 调用关系及测试环境隔离性。
三、NPU 适配说明
本次测试不需要修改 API 实现,也不需要在测试文件中增加 `import torch_npu` 或将测试张量迁移至 NPU。
1. 图捕获流程与具体计算后端解耦
`dynamo_graph_capture_for_export` 的主要职责是调用 TorchDynamo 捕获 Python 函数或模块的计算图,并构造 FX `GraphModule`。
本次测试目标是验证:
- API 能否完成图捕获;
- 返回结果是否为可执行的 `GraphModule`;
- 捕获结果是否与原始程序保持语义一致;
- 模块参数和缓冲区是否能够被正确保留。
这些功能主要位于 Python、TorchDynamo、FakeTensor 和 FX 图处理层,不直接验证 NPU 算子执行能力,因此无需将输入张量修改为 `.npu()`。
1. 避免扩大测试范围
如果强制增加:
```python
import torch_npu
```
或将输入张量修改为:
```python
tensor.npu()
```
测试范围将由 TorchDynamo API 验证扩大为多个组件的集成验证,包括:
- torch-npu 后端加载;
- NPU 设备初始化;
- PrivateUse1 调度;
- NPU 算子注册;
- FakeTensor 或 Meta 支持;
- 实际硬件、驱动和算子环境。
这些内容不属于本次 API 级测试的验证目标。
保留 CPU 张量能够使测试聚焦于图捕获接口本身,并支持在无 NPU 硬件的普通 CI 环境中稳定运行。
1. `is_compile_supported` 不执行硬件计算
该 API 仅检查环境能力和设备类型,不会:
- 创建 CPU、CUDA 或 NPU 张量;
- 初始化 NPU 设备;
- 执行计算算子;
- 分配 NPU 显存;
- 启动实际编译任务或设备通信。
测试又通过 Mock 固定 Dynamo 和 Triton 状态,因此测试结果不依赖实际硬件环境。
1. 不涉及底层 NPU 适配
本次仅新增 UT 测试文件,不涉及:
- ATen 算子实现;
- PrivateUse1 算子注册;
- NPU Kernel;
- ACLNN 接口;
- NPU 内存格式转换;
- HCCL 通信;
- C++ 扩展;
- Python API 实现修改。
因此,本次无需新增 NPU 算子或修改 torch-npu 后端实现。
【资料变更】
不涉及。
本次仅新增 API 级 UT 测试,不新增、删除或修改公开 API,也不改变现有接口的参数、返回值、异常行为或使用方式。
`torch._dynamo.functional_export.dynamo_graph_capture_for_export` 仅在包含该 API 的 PyTorch 2.10.0 及以上版本中新增测试;低版本不增加该 API 的测试或资料声明。
现有 API 支持资料无需调整,因此不需要修改 `docs/zh/native_apis` 等接口资料。
【接口变更】
不涉及。
本次未修改:
- `dynamo_graph_capture_for_export` 的函数签名;
- `dynamo_graph_capture_for_export` 的返回类型;
- `is_compile_supported` 的函数签名;
- `is_compile_supported` 的设备判断逻辑;
- 其他现有 TorchDynamo API 的行为。
本次仅新增独立 UT 测试文件,对现有 API 进行功能看护。
【功能验证】
本次采用常规 Python UT 测试方式,通过 `torch.testing._internal.common_utils.TestCase` 编写测试,并使用 `run_tests()` 统一执行。
测试文件:
```text
test/dynamo/test_functional_export.py
```
执行命令:
```bash
python test/dynamo/test_functional_export.py
```
一、PyTorch 2.7.1、2.8.0 和 2.9.0
上述版本仅验证:
```text
torch._dynamo.utils.is_compile_supported
```
由于这些版本中不存在:
```text
torch._dynamo.functional_export.dynamo_graph_capture_for_export
```
因此低版本测试文件不导入或调用该 API,避免产生 `ImportError`。
共执行 5 个测试用例,日志如下:
```text
test_cpu_supported_when_dynamo_is_supported (test_functional_export_v1.TestIsCompileSupported.test_cpu_supported_when_dynamo_is_supported) ... ok
test_cpu_unsupported_when_dynamo_is_unsupported (test_functional_export_v1.TestIsCompileSupported.test_cpu_unsupported_when_dynamo_is_unsupported) ... ok
test_cuda_supported_with_triton (test_functional_export_v1.TestIsCompileSupported.test_cuda_supported_with_triton) ... ok
test_cuda_unsupported_without_triton (test_functional_export_v1.TestIsCompileSupported.test_cuda_unsupported_without_triton) ... ok
test_npu_returns_false (test_functional_export_v1.TestIsCompileSupported.test_npu_returns_false) ... ok
----------------------------------------------------------------------
Ran 5 tests in 0.008s
OK
```
二、PyTorch 2.10.0 和 2.11.0
上述版本执行完整测试文件,验证:
```text
torch._dynamo.functional_export.dynamo_graph_capture_for_export
torch._dynamo.utils.is_compile_supported
```
测试包括:
- 2 个 `dynamo_graph_capture_for_export` 核心功能用例;
- 5 个 `is_compile_supported` 条件分支用例。
共执行 7 个测试用例,日志如下:
```text
test_capture_module_with_parameter_and_buffer (test_functional_export_v2.TestDynamoGraphCaptureForExport.test_capture_module_with_parameter_and_buffer) ... ok
test_capture_simple_function (test_functional_export_v2.TestDynamoGraphCaptureForExport.test_capture_simple_function) ... ok
test_cpu_supported_when_dynamo_is_supported (test_functional_export_v2.TestIsCompileSupported.test_cpu_supported_when_dynamo_is_supported) ... ok
test_cpu_unsupported_when_dynamo_is_unsupported (test_functional_export_v2.TestIsCompileSupported.test_cpu_unsupported_when_dynamo_is_unsupported) ... ok
test_cuda_supported_with_triton (test_functional_export_v2.TestIsCompileSupported.test_cuda_supported_with_triton) ... ok
test_cuda_unsupported_without_triton (test_functional_export_v2.TestIsCompileSupported.test_cuda_unsupported_without_triton) ... ok
test_npu_returns_false (test_functional_export_v2.TestIsCompileSupported.test_npu_returns_false) ... ok
----------------------------------------------------------------------
Ran 7 tests in 1.278s
OK
```
测试结果表明:
- 所有测试用例均执行通过;
- 捕获结果为 `torch.fx.GraphModule`;
- 捕获图与原始函数或模块的输出一致;
- 模块参数和缓冲区能够正确参与计算;
- `is_compile_supported` 的各条件分支返回结果符合预期;
- Mock 依赖函数的调用次数符合预期;
- 未出现 `FAIL`、`ERROR` 或 `ImportError`。
See merge request: Ascend/pytorch!41798
2026-08-27 10:28:23 +08:00
..
2023-12-28 03:29:15 +00:00
2023-12-28 03:29:15 +00:00
2026-06-02 16:16:09 +08:00
2024-01-05 02:09:57 +00:00
2024-01-08 12:38:09 +00:00
2024-01-08 12:38:09 +00:00
2024-01-08 12:38:09 +00:00
2024-01-05 02:09:57 +00:00
2024-01-09 07:06:09 +00:00
2026-08-21 15:22:04 +08:00
2024-01-05 02:09:57 +00:00
2024-01-08 12:38:09 +00:00
2024-01-05 02:09:57 +00:00
2024-01-08 12:38:09 +00:00
2024-01-09 07:06:09 +00:00
2024-01-08 12:38:09 +00:00
2026-07-07 19:12:18 +08:00
2026-07-23 11:11:17 +08:00
2024-01-10 10:11:42 +00:00
2023-12-28 03:29:15 +00:00
2024-01-10 10:11:42 +00:00
2026-08-27 10:28:23 +08:00
2024-01-08 12:38:09 +00:00
2023-12-28 03:29:15 +00:00
2026-05-07 20:53:34 +08:00
2024-01-08 12:38:09 +00:00
2024-01-09 07:06:09 +00:00
2024-01-08 12:38:09 +00:00
2024-01-05 02:09:57 +00:00
2024-01-08 12:38:09 +00:00
2026-05-21 16:41:18 +08:00
2024-01-08 12:38:09 +00:00
2023-12-28 03:29:15 +00:00
2024-01-08 12:38:09 +00:00
2023-12-28 03:29:15 +00:00
2026-02-04 14:56:25 +08:00
2026-08-25 10:32:31 +08:00
2026-06-23 09:14:24 +08:00
2026-03-13 14:57:25 +08:00
2024-01-09 07:06:09 +00:00
2024-01-05 02:09:57 +00:00
2024-01-10 10:11:42 +00:00
2023-12-28 03:29:15 +00:00
2024-01-08 12:38:09 +00:00
2024-01-08 12:38:09 +00:00
2024-01-05 02:09:57 +00:00
2023-12-28 03:29:15 +00:00
2026-07-16 09:27:23 +08:00
2026-05-21 16:41:18 +08:00
2024-01-08 12:38:09 +00:00
2026-07-29 11:35:20 +08:00
2024-05-11 09:33:09 +00:00
2026-05-21 16:41:18 +08:00
2026-06-22 16:47:27 +08:00
2026-05-26 19:14:45 +08:00
2024-01-08 12:38:09 +00:00
2023-12-28 03:29:15 +00:00
2023-12-28 03:29:15 +00:00