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

Signed-off-by: Nils Matteson <nilsmatteson@icloud.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

Nils Matteson committed Jul 22, 2026 at 17:56 UTC 149daf0d723bde1015bdb9695711c01ec60448eb
2 files changed +69
tests/config/test_config_utils.py
+62
@@ -214,3 +214,65 @@ def test_cache_config_hash_ignores_kv_cache_sizing_knobs():
214 base_hash = CacheConfig().compute_hash()
215 assert CacheConfig(kv_cache_memory_bytes=1 << 30).compute_hash() == base_hash
216 assert CacheConfig(gpu_memory_utilization=0.5).compute_hash() == base_hash
217 +
218 +
219 +def test_envs_compile_factors_relocation_invariant(tmp_path):
220 + """Relocating HOME or the XDG roots must not change the compile-cache
221 + env hash.
222 +
223 + Location-derived env vars (VLLM_XLA_CACHE_PATH from XDG_CACHE_HOME,
224 + VLLM_CONFIG_ROOT from XDG_CONFIG_HOME/HOME) carry no information about
225 + compiled artifacts, only about where directories live. When they leak
226 + into compile_factors(), a cache produced under one HOME/XDG layout
227 + silently misses under another - which defeats copying or pre-baking a
228 + compile cache into a container image.
229 + """
230 + import os
231 + import subprocess
232 + import sys
233 +
234 + code = """
235 +import sys
236 +import logging
237 +logging.disable(logging.CRITICAL)
238 +from vllm import envs
239 +from vllm.config.utils import hash_factors
240 +print(hash_factors(envs.compile_factors()))
241 +"""
242 +
243 + def hash_with(extra_env):
244 + env = {**dict(os.environ), "VLLM_LOGGING_LEVEL": "ERROR"}
245 + # Drop explicit overrides so the derived defaults are what is
246 + # exercised, then apply the relocation under test.
247 + for key in ("VLLM_XLA_CACHE_PATH", "VLLM_CONFIG_ROOT", "VLLM_CACHE_ROOT"):
248 + env.pop(key, None)
249 + env.update(extra_env)
250 + result = subprocess.run(
251 + [sys.executable, "-c", code],
252 + capture_output=True,
253 + text=True,
254 + check=True,
255 + env=env,
256 + )
257 + return result.stdout.strip()
258 +
259 + xdg_cache = tmp_path / "relocated-xdg-cache"
260 + xdg_config = tmp_path / "relocated-xdg-config"
261 + new_home = tmp_path / "relocated-home"
262 + for d in (xdg_cache, xdg_config, new_home):
263 + d.mkdir()
264 +
265 + base = hash_with({})
266 + relocated_xdg = hash_with(
267 + {"XDG_CACHE_HOME": str(xdg_cache), "XDG_CONFIG_HOME": str(xdg_config)}
268 + )
269 + relocated_home = hash_with({"HOME": str(new_home)})
270 +
271 + assert relocated_xdg == base, (
272 + "XDG_CACHE_HOME/XDG_CONFIG_HOME relocation changed the compile-cache "
273 + "env hash - a location-only derived var is leaking into the key"
274 + )
275 + assert relocated_home == base, (
276 + "HOME relocation changed the compile-cache env hash - a "
277 + "location-only derived var is leaking into the key"
278 + )
vllm/envs.py
+7
@@ -2114,6 +2114,13 @@ def compile_factors() -> dict[str, object]:
2114 "VLLM_CACHE_ROOT",
2115 # Runtime memory-plan persistence; does not affect compiled graphs.
2116 "VLLM_ENABLE_STARTUP_PLAN",
2117 + # Location-only derived paths: where a cache/config directory lives
2118 + # cannot affect compiled artifacts, and hashing them means relocating
2119 + # HOME or the XDG roots silently invalidates every compile cache
2120 + # (VLLM_CACHE_ROOT above and VLLM_FLASHINFER_AUTOTUNE_CACHE_DIR below
2121 + # are already ignored for the same reason).
2122 + "VLLM_XLA_CACHE_PATH",
2123 + "VLLM_CONFIG_ROOT",
2124 "LD_LIBRARY_PATH",
2125 "VLLM_SERVER_DEV_MODE",
2126 "VLLM_DP_MASTER_IP",