Prhub

#49510 [CI] Isolate cudagraph tests in child processes

原始 PR 作者 AndreasKaratzas 合并时间 2026-07-24 01:16 文件变更 1 提交数 1 评论 0 代码增减 +3 / -24

执行摘要

将 cudagraph 测试隔离到子进程,删除手动 GPU 内存清理

PR body 指出:cudagraph 测试用例复用 pytest 进程生命周期,导致 VRAM 释放依赖非确定性的循环引用收集。通过将每个测试标记为 fork,子进程退出成为确定性的 GPU 内存所有权边界。这稳定了 Cudagraph 测试组。

此 PR 值得阅读以了解测试隔离的最佳实践,尤其当测试涉及 GPU 资源时。设计决策可复用于其他类似测试文件。

讨论亮点

无 review 评论或讨论。

实现拆解

  1. 替换 GPU 内存清理机制:在文件 tests/v1/cudagraph/test_cudagraph_mode.py 中,删除 import weakreffrom tests.utils import wait_for_gpu_memory_to_clear 以及测试函数中对应的 weakref.proxydel llmwait_for_gpu_memory_to_clear 代码块。
  2. 引入子进程隔离:为两个测试函数 test_backend_and_cudagraph_mode_combotest_cudagraph_compilation_combo 添加 @create_new_process_for_each_test("spawn") 装饰器,使每个测试在独立子进程中启动,确保 GPU 内存随进程退出而释放。
  3. 简化清理逻辑:移除异常处理中 try-exceptfinally 子句中对 llm 变量的弱引用处理,因为在子进程退出后,所有 GPU 资源会自动回收。
文件 模块 状态 重要度
tests/v1/cudagraph/test_cudagraph_mode.py 测试 modified 5.27

关键源码片段

tests/v1/cudagraph/test_cudagraph_mode.py test-coverage

唯一修改的文件,核心变更:引入子进程隔离并删除手动 GPU 内存清理代码。

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from contextlib import ExitStackimport pytest# 新导入:子进程隔离装饰器
from tests.utils import create_new_process_for_each_test
from tests.v1.attention.utils import full_cg_backend_configs as backend_configs
from vllm import LLM
from vllm.config import CompilationConfig, CompilationMode
from vllm.platforms import current_platform# 测试 attention backend 与 cudagraph_mode 组合
# (backend_name, cudagraph_mode, supported)
if current_platform.is_rocm():
    combo_cases_1 = [
        ("RocmAttn", "FULL", True),
        ("RocmAttn", "FULL_AND_PIECEWISE", True),
        ("TritonAttn", "FULL", True),
        ("TritonAttn", "FULL_AND_PIECEWISE", True),
    ]
else:
    combo_cases_1 = [
        ("FA3", "FULL", True),
        ("FA3", "FULL_AND_PIECEWISE", True),
        ("FA2", "FULL", True), # Should fallback to FULL_AND_PIECEWISE
        ("FA2", "FULL_AND_PIECEWISE", True),
        ("FlashInfer", "FULL", True), # Should fallback to FULL_AND_PIECEWISE
        ("FlashInfer", "FULL_AND_PIECEWISE", True),
    ]
​
​
@pytest.mark.parametrize("backend_name, cudagraph_mode, supported", combo_cases_1)
# 新增:每个测试在独立子进程中以 "spawn" 模式运行
@create_new_process_for_each_test("spawn")
def test_backend_and_cudagraph_mode_combo(backend_name, cudagraph_mode, supported):
    # ... ( 测试体保持不变,但删除了 weakref 和 GPU 清理代码 )
    ...

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

风险较低。变更仅涉及测试文件,通过子进程隔离替代手动内存清理,理论上更安全。需要确保 create_new_process_for_each_test 装饰器在 CI 环境(如 ROCm)中正常工作,但该助手已在其他测试中使用。

影响范围仅限于 cudagraph 测试组。测试变得更稳定,不再依赖非确定性 GC,有利于 CI 一致性。对用户和生产系统无影响。

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论