Prhub

#47573 [Bugfix] Exclude location-derived path vars from torch.compile cache factors

原始 PR 作者 matteso1 合并时间 2026-07-23 07:56 文件变更 2 提交数 3 评论 3 代码增减 +69 / -0

执行摘要

修复路径变量泄漏导致编译缓存失效

根据 PR 描述,VLLM_XLA_CACHE_PATH(派生自 XDG_CACHE_HOME)和 VLLM_CONFIG_ROOT(派生自 XDG_CONFIG_HOME/HOME)被哈希到 torch.compile 缓存键中。这些路径不包含编译产物的信息,但它们的变化会导致缓存键变更,从而在复制或预构建编译缓存到容器镜像时失效(容器 HOME/XDG 布局可能与构建环境不同)。具体而言,在 H100 上重现的 bug 显示:恢复编译缓存到不同的 HOME/XDG 布局时,会产生不同的哈希目录,导致完全重编译(约 18.4s),且无任何警告。

建议合并:该 PR 定位准确、修复 narrow、有硬件验证和回归测试,且遵循已有先例,无风险。

讨论亮点

该 PR 的审查评论较少,只有一个来自 claude[bot] 的自动评论(由于是 fork,自动审查被禁用),以及 simon-mo 的批准。在 PR 评论中,作者 matteso1 提到已标记编译区域代码拥有者 @ProExpertProg,并说明了修复的 narrow 范围和硬件验证结果。

实现拆解

  1. vllm/envs.pycompile_factors() 函数中,向 ignored_factors 集合添加两个条目VLLM_XLA_CACHE_PATHVLLM_CONFIG_ROOT,并附带注释说明理由。这样这两个变量在计算缓存键时被排除。
  2. tests/config/test_config_utils.py 中添加新的回归测试 test_envs_compile_factors_relocation_invariant:该测试使用子进程在重新定位的 XDG_CACHE_HOME/XDG_CONFIG_HOME 和重新定位的 HOME 环境下计算 compile_factors 的哈希值,并断言哈希值与基线一致。测试前会清理显式的 VLLM_XLA_CACHE_PATHVLLM_CONFIG_ROOT 覆盖,确保只测试派生默认值。
文件 模块 状态 重要度
vllm/envs.py 环境配置 modified 5.11
tests/config/test_config_utils.py 配置测试 modified 6.44

关键符号

compile_factors test_envs_compile_factors_relocation_invariant

关键源码片段

vllm/envs.py core-logic

核心修复文件:在 `compile_factors()` 的 `ignored_factors` 集合中添加 `VLLM_XLA_CACHE_PATH` 和 `VLLM_CONFIG_ROOT`,防止它们影响编译缓存键。

# vllm/envs.py (line 2099-2152)
def compile_factors() -> dict[str, object]:
    """Return env vars used for torch.compile cache keys.    Start with every known vLLM env var; drop entries in `ignored_factors`;
    hash everything else. This keeps the cache key aligned across workers."""
​
    ignored_factors: set[str] = {
        "MAX_JOBS",
        "VLLM_RPC_BASE_PATH",
        "VLLM_USE_MODELSCOPE",
        "VLLM_RINGBUFFER_WARNING_INTERVAL",
        "VLLM_DEBUG_DUMP_PATH",
        "VLLM_PORT",
        "VLLM_CACHE_ROOT",
        # Runtime memory-plan persistence; does not affect compiled graphs.
        "VLLM_ENABLE_STARTUP_PLAN",
        # Location-only derived paths: where a cache/config directory lives
        # cannot affect compiled artifacts, and hashing them means relocating
        # HOME or the XDG roots silently invalidates every compile cache
        # (VLLM_CACHE_ROOT above and VLLM_FLASHINFER_AUTOTUNE_CACHE_DIR below
        # are already ignored for the same reason).
        "VLLM_XLA_CACHE_PATH", # Added: derived from XDG_CACHE_HOME
        "VLLM_CONFIG_ROOT", # Added: derived from XDG_CONFIG_HOME/HOME
        "LD_LIBRARY_PATH",
        # ... remaining ignored factors ...
        "VLLM_FLASHINFER_AUTOTUNE_CACHE_DIR",
        # ...
    }
    # ... rest of function ...
tests/config/test_config_utils.py test-coverage

新增回归测试文件:验证在 XDG_CACHE_HOME、XDG_CONFIG_HOME 和 HOME 重新定位时,compile_factors 哈希值保持不变。

# tests/config/test_config_utils.py (line 219-278)
def test_envs_compile_factors_relocation_invariant(tmp_path):
    """Relocating HOME or the XDG roots must not change the compile-cache
    env hash.    Location-derived env vars (VLLM_XLA_CACHE_PATH from XDG_CACHE_HOME,
    VLLM_CONFIG_ROOT from XDG_CONFIG_HOME/HOME) carry no information about
    compiled artifacts, only about where directories live. When they leak
    into compile_factors(), a cache produced under one HOME/XDG layout
    silently misses under another - which defeats copying or pre-baking a
    compile cache into a container image.
    """
    import os
    import subprocess
    import sys
​
    code = """
import sys
import logging
logging.disable(logging.CRITICAL)
from vllm import envs
from vllm.config.utils import hash_factors
print(hash_factors(envs.compile_factors()))
"""
​
    def hash_with(extra_env):
        env = {**dict(os.environ), "VLLM_LOGGING_LEVEL": "ERROR"}
        # Drop explicit overrides so the derived defaults are what is
        # exercised, then apply the relocation under test.
        for key in ("VLLM_XLA_CACHE_PATH", "VLLM_CONFIG_ROOT", "VLLM_CACHE_ROOT"):
            env.pop(key, None)
        env.update(extra_env)
        result = subprocess.run(
            [sys.executable, "-c", code],
            capture_output=True,
            text=True,
            check=True,
            env=env,
        )
        return result.stdout.strip()
​
    xdg_cache = tmp_path / "relocated-xdg-cache"
    xdg_config = tmp_path / "relocated-xdg-config"
    new_home = tmp_path / "relocated-home"
    for d in (xdg_cache, xdg_config, new_home):
        d.mkdir()
​
    base = hash_with({})
    relocated_xdg = hash_with(
        {"XDG_CACHE_HOME": str(xdg_cache), "XDG_CONFIG_HOME": str(xdg_config)}
    )
    relocated_home = hash_with({"HOME": str(new_home)})
​
    assert relocated_xdg == base, (
        "XDG_CACHE_HOME/XDG_CONFIG_HOME relocation changed the compile-cache "
        "env hash - a location-only derived var is leaking into the key"
    )
    assert relocated_home == base, (
        "HOME relocation changed the compile-cache env hash - a "
        "location-only derived var is leaking into the key"
    )

评论区精华

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

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

风险与影响

  1. 无回归风险:变更仅是在已有忽略集合中添加两项,且遵循之前添加 VLLM_CACHE_ROOTVLLM_FLASHINFER_AUTOTUNE_CACHE_DIR 的先例,风险极低。
  2. 测试覆盖:新增的回归测试能有效验证修复,未发现遗漏场景。
  3. 兼容性:忽略这些变量不会影响任何功能逻辑,仅影响缓存键计算。
  1. 用户影响:修复了在容器化部署或不同 HOME/XDG 布局下编译缓存失效的问题,显著改善冷启动时间(从 18.4s 编译降为缓存命中后的即时加载)。
  2. 系统影响:无;仅是缓存键计算逻辑调整。
  3. 团队影响:低,变更小且局部化。

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论