Repository navigation
fix: persist vLLM's torch.compile cache on the network volume #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
velaraptor-runpod
merged 3 commits into
runpod-workers:main
from
ashwinsreedhar28:fix/compile-cache-on-volume
Oct 6, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7b8958e
fix: persist vLLM's torch.compile cache on the network volume
ashwinsreedhar28 307752b
fix: keep starting when the network volume can't take the compile cache
ashwinsreedhar28 2855118
fix: warn when the compile cache can't move off a full disk
ashwinsreedhar28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| """Keep vLLM's compile cache on the network volume only when the volume can take it. | ||
|
|
||
| The image sets VLLM_CACHE_ROOT=$BASE_PATH/vllm-cache so torch.compile output | ||
| survives cold starts on a network volume. vLLM does not handle write errors on | ||
| most of its compile-cache writes, so a full or read-only volume would fail | ||
| startup where compiling into vLLM's default root used to work. Two guards: | ||
|
|
||
| - ensure_usable(), before launch: if the directory isn't writable or has less | ||
| than MIN_FREE_BYTES free, use vLLM's default root for this start. | ||
| - fall_back(), from main.py when vLLM still runs out of disk mid-start (the | ||
| check runs before the weights download onto the same volume): relaunch once | ||
| with vLLM's default root. | ||
|
|
||
| Pure Python, no vLLM import. | ||
| """ | ||
|
|
||
| import logging | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
|
|
||
| # Free space required on the volume before compiling onto it. A heuristic, not | ||
| # a measurement: running out later in the start is handled by the relaunch. | ||
| MIN_FREE_BYTES = 1 << 30 | ||
|
|
||
|
|
||
| def vllm_default_root(env=os.environ) -> str: | ||
| """Where vLLM puts its cache when VLLM_CACHE_ROOT is unset (vllm/envs.py).""" | ||
| base = env.get("XDG_CACHE_HOME") or os.path.expanduser("~/.cache") | ||
| return os.path.join(base, "vllm") | ||
|
|
||
|
|
||
| def _configured_root(env) -> str | None: | ||
| """VLLM_CACHE_ROOT as vLLM resolves it, or None if unset or already the default.""" | ||
| root = env.get("VLLM_CACHE_ROOT") | ||
| if not root: | ||
| return None | ||
| root = os.path.expanduser(root) | ||
| if os.path.realpath(root) == os.path.realpath(vllm_default_root(env)): | ||
| return None | ||
| return root | ||
|
|
||
|
|
||
| def _device(path: str) -> int: | ||
| """st_dev of the path, or of its nearest existing ancestor.""" | ||
| while not os.path.exists(path): | ||
| parent = os.path.dirname(path) | ||
| if parent == path: | ||
| break | ||
| path = parent | ||
| return os.stat(path).st_dev | ||
|
|
||
|
|
||
| def fall_back(reason: str, env=os.environ, out_of_space: bool = False) -> bool: | ||
| """Point VLLM_CACHE_ROOT at vLLM's default root. | ||
|
|
||
| Returns False (and changes nothing) when that can't help: VLLM_CACHE_ROOT is | ||
| unset or already the default, or, for ``out_of_space``, the default is on | ||
| the same filesystem (no volume attached, so the same disk would run out). | ||
| """ | ||
| root = _configured_root(env) | ||
| fallback = vllm_default_root(env) | ||
| if root is None: | ||
| return False | ||
| if out_of_space: | ||
| try: | ||
| if _device(root) == _device(fallback): | ||
| logging.warning( | ||
| "VLLM_CACHE_ROOT=%s is %s, but the fallback %s is on the same " | ||
| "disk; not moving the compile cache.", | ||
| root, | ||
| reason, | ||
| fallback, | ||
| ) | ||
| return False | ||
| except OSError: | ||
| pass | ||
| logging.warning( | ||
| "VLLM_CACHE_ROOT=%s is %s; compiling into %s for this start instead. " | ||
| "The vllm-cache folder is safe to delete while no worker is starting " | ||
| "(e.g. endpoint scaled to zero); vLLM rebuilds it.", | ||
| root, | ||
| reason, | ||
| fallback, | ||
| ) | ||
| env["VLLM_CACHE_ROOT"] = fallback | ||
| return True | ||
|
|
||
|
|
||
| def ensure_usable(env=os.environ, min_free_bytes: int | None = None) -> str | None: | ||
| """Fall back to vLLM's default root if VLLM_CACHE_ROOT can't take writes. | ||
|
|
||
| Returns why it fell back, or None when VLLM_CACHE_ROOT is unset, already the | ||
| default, or writable with at least ``min_free_bytes`` (default | ||
| MIN_FREE_BYTES) free. | ||
| """ | ||
| if min_free_bytes is None: | ||
| min_free_bytes = MIN_FREE_BYTES | ||
| root = _configured_root(env) | ||
| if root is None: | ||
| return None | ||
| try: | ||
| os.makedirs(root, exist_ok=True) | ||
| # A zero-byte file still fits on a full volume or under an exhausted | ||
| # quota (statvfs may report the storage pool, not the quota); one | ||
| # written and synced block does not. | ||
| with tempfile.NamedTemporaryFile(dir=root, prefix=".write-check-") as probe: | ||
| probe.write(b"\0" * 65536) | ||
| probe.flush() | ||
| os.fsync(probe.fileno()) | ||
| free = shutil.disk_usage(root).free | ||
| except OSError as e: | ||
| reason = f"not writable ({e.strerror or e})" | ||
| else: | ||
| if free >= min_free_bytes: | ||
| return None | ||
| reason = f"nearly full ({free >> 20} MiB free)" | ||
| return reason if fall_back(reason, env, out_of_space=True) else None | ||
| return reason if fall_back(reason, env) else None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.