Skip to content

perf: Dblanaru/persistent compile caches - #4715

Merged
terrykong merged 8 commits into
mainfrom
dblanaru/persistent-compile-caches
Nov 10, 2026
Merged

terrykong merged 8 commits into
mainfrom
dblanaru/persistent-compile-caches

Conversation

@DanBlanaru

@DanBlanaru DanBlanaru commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Title: perf: persistent JIT compile caches for vLLM, Megatron and external vLLM pools

What does this PR do ?

Keeps the JIT compile caches (vLLM, TorchInductor, Triton, FlashInfer, FlashAttention-4, CUDA) in a shared folder between jobs, one cache per worker type: the vLLM generation engines, the Megatron trainer, and each external vLLM pool. A node copies the caches it needs to local storage before compiling, and new files are written back during and at the end of the job. Everything is off unless the new variables are set.

Issues

None filed. Today every job recompiles: the caches live in the container and disappear with it. On the Nemotron 3 Ultra RL smoke (64 nodes), policy engine-core start-up is 175 s cold and 39 s warm.

Usage

Two variables in the environment the Ray nodes start with, plus two background commands in SETUP_COMMAND. Full example in docs/guides/persistent-jit-caches.md.

export NRL_COMPILE_CACHE_SHARED_DIR=/lustre/.../compile_cache   # kept between jobs
export NRL_COMPILE_CACHE_LOCAL_DIR=/tmp/nemo_rl_compile_cache    # per node
# in SETUP_COMMAND (runs on every node before ray start):
#   copy both caches early:  bash $SYNC seed-type vllm; bash $SYNC seed-type megatron  (in the background)
#   write-back:              bash $SYNC sidecar-types 60 "$(dirname "$0")/ENDED" vllm megatron &

External vLLM pool: register_external_vllm_pool ... --cache-dir /lustre/.../compile_cache/genrm.

Optional: NRL_COMPILE_CACHE_VLLM_DIR / NRL_COMPILE_CACHE_MEGATRON_DIR (one type elsewhere), NRL_COMPILE_CACHE_FORCE_FRESH=1 (start empty, still write back), NRL_COMPILE_CACHE_MIN_AGE_MIN (default 1), NRL_COMPILE_CACHE_SYNC_SCRIPT (another copy script, e.g. for object storage; interface in the guide).

How it works

  • Layout: <shared>/<type> is copied to <local>/<type>, and each compiler points at <local>/<type>/{inductor,triton,flashinfer,cuda,fa4_cute} (compile_cache.env_overrides). vLLM's own cache is <local>/vllm/vllm.
  • vLLM engines: before creating an engine, the NeMo-RL vLLM process runs one Ray task on each node of the engine's placement group and waits for them (compile_cache.prepare_vllm_engine). Each task copies the vllm cache onto its node once, and fills the engine's own VLLM_CACHE_ROOT (<local>/vllm_engines/<seed>) from it. Engines on one node keep separate folders, because vLLM writes its cache index without locking. This covers engines that span several nodes without patching vLLM's worker code.
  • Megatron trainer: the worker waits for its node's megatron copy before anything compiles. Its kernels are compiled during setup.
  • Copying: nemo_rl/utils/cache_sync.sh seed-type <type> copies once per node under a node-local flock. A launcher can start it before ray start so the copy overlaps Ray start-up, and the workers then find it done.
  • Write-back (sidecar-types, run by the launcher on every node):
    • It publishes files that are new since the copy started or since the last pass, and unchanged for a minute.
    • Files already in the shared folder are never replaced. Nodes take turns through a lock folder.
    • The final pass, after the job's ENDED marker, waits up to 2 minutes for the lock and reports what it couldn't publish.
    • Engine folders are merged into <local>/vllm/vllm first, so the shared cache gets one copy instead of one per engine.
  • Errors: a failed copy or write-back only warns, and the job compiles what is missing. A missing script, rsync or flock, or only one of the two variables set, fails at start.

vLLM versions

  • vllm#52142 backport: vLLM up to 0.31.0 stores absolute artifact paths in its compile-cache index, so an engine can't load a cache another engine wrote. With persistent caches on, _patch_vllm_compile_cache_relative_artifacts in patches.py applies the upstream fix (first released in 0.31.1) on every node of an engine. It raises if vLLM's source no longer matches. Remove it once vLLM >= 0.31.1.
  • Not included: vLLM 0.29.0 still hashes VLLM_RAY_BUNDLE_INDICES into the compile-cache key. vllm#61211 fixed this upstream, but it isn't in main's pin. Until it is, each engine reuses only its own entries from an earlier job with the same placement, and engines don't share compiled graphs.

Commits

  1. perf(external-vllm): persistent compile caches for external vLLM pools: cache_sync.sh and --cache-dir for pools; every node of a replica is seeded before ray start.
  2. perf(vllm,megatron): persistent per-type compile caches for NeMo RL workers: compile_cache.py, wired into the vLLM and Megatron workers.
  3. fix(external-vllm): wait longer for a replica's head node when it seeds a cache.
  4. perf(cache): one root per cache type, per-node engine seeding, review fixes: review comments C4, C5, C9, C11, C12, C14, C15, C16, C17, C18.
  5. fix(cache): backport vllm#52142 on every node of an engine.
  6. fix(cache): Megatron workers wait for their cache copy.

Validation

Nemotron 3 Ultra RLVR smoke on 64 nodes with 4 GPUs each: 32 trainer, 20 generation (10 TP8 engines across 2 nodes each), 6 Gym and 6 judge nodes; 1 step. These ran this PR's code ported to the recipe's NeMo-RL branch (vLLM 0.25.1, with vllm#61211 applied as a patch), with the cache wiped before the cold run.

Seconds Cold (1669809) Warm (1670262)
Policy engine-core init 175 39
Create policy engine (vLLM process) 199 51
GenRM / NL2Bash / safety judge engine init 194 / 161 / 91 126 / 85 / 61
Setup done (from allocation start) 516 402
First weight sync done (from allocation start) 549 434
  • Warm runs load everything from the cache: every engine loaded its compiled graphs ("Directly load the compiled graph"), and no vLLM files were written back.
  • Copies are short at this scale: with 58 nodes copying at once, the vLLM cache (1.7 GB, ~2.7k files) took 8-9 s per node and the Megatron cache (1.9 GB) about 30 s. Both finished before Ray was up (run 1673531).
  • The Megatron cache keeps growing. Each warm run still writes back ~4k Megatron files (~130 MB). These are new compiles, not cache misses: Inductor specializes a small fused kernel (squared ReLU times a per-token weight, over [tokens, 5120]) on the token count, which changes with every batch. So the cache grows by roughly that much per run until it is reset, as described in the guide.

Tests

  • tests/unit/utils/test_cache_sync.py:
    • write-back publishes once and skips young files;
    • the marker is set at copy start (fake slow rsync);
    • a failed copy only warns;
    • seed-type with the engine merge;
    • missing flock.
  • tests/unit/utils/test_compile_cache.py:
    • off when unset, raises when half set;
    • the derived folders;
    • a missing script raises;
    • copies once per node from the per-type folder;
    • the vllm#52142 backport against a fake vLLM file (applies once, raises on unknown source).
  • tests/unit/tools/test_external_gym_vllm.py: updated for the new seed command.

Run: the 11 tests above pass (CPU, torch 2.13, --noconftest). ruff check and ruff format --check are clean.

Not run:

  • test_external_gym_vllm.py: it needs the full stack; I ran its pool checks separately.
  • GPU or functional tests, and a docs build.
  • An end-to-end job on main's vLLM 0.29.0. The vLLM anchor is the same in 0.29.0 and 0.25.1.

AI assistance (Claude) was used to write the change, tests and this description; I reviewed every changed line and ran the tests and jobs listed.

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented Oct 13, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Oct 13, 2026
@DanBlanaru DanBlanaru changed the title Dblanaru/persistent compile caches (oerf): Dblanaru/persistent compile caches Oct 13, 2026
@DanBlanaru DanBlanaru changed the title (oerf): Dblanaru/persistent compile caches (perf): Dblanaru/persistent compile caches Oct 13, 2026
@DanBlanaru
DanBlanaru force-pushed the dblanaru/persistent-compile-caches branch from 3da271d to d5b40fb Compare October 15, 2026 17:08
@DanBlanaru DanBlanaru changed the title (perf): Dblanaru/persistent compile caches perf: Dblanaru/persistent compile caches Oct 16, 2026
@DanBlanaru
DanBlanaru force-pushed the dblanaru/persistent-compile-caches branch from d5b40fb to b278235 Compare October 27, 2026 23:43
@terrykong terrykong added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Oct 29, 2026

@terrykong terrykong left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for the detailed measurements. The write-back design here (add only new files, never overwrite, one writer at a time) is a real improvement over earlier attempts at a shared compile cache, and the cache-key fix sent upstream (vllm#61211) is the right root-cause fix.

Please rebase first: the PR conflicts with main.

We'd like to keep the shared-filesystem design. The comments below are about making it safe at scale, easy to turn on for any node (including nodes that join later), and documented:

  1. (C4) Replace NRL_VLLM_CACHE_ROOT_PER_ENGINE with main's per-engine folder, filled from one shared policy/vllm folder.
  2. (C17) Forward the cache variable names through the existing extra_env_vars path instead of rebuilding VLLM_RAY_EXTRA_ENV_VARS_TO_COPY by hand.
  3. (C5) Trainer background copy: a bad script path turns the cache off silently (the lost-kernel bug goes away with C16).
  4. (C16) Store each cache folder as a few compressed archives instead of thousands of small files, and let every node write back without a lock: a node claims new files with a small intent file before uploading, so the rest skip them.
  5. (C9) Default each compiler's folder from NRL_LOCAL_COMPILE_CACHE.
  6. (C18) Fail clearly when flock is missing.
  7. (C15) A vLLM engine that spans several nodes only gets a seeded cache on the actor's node; needs the start-up hook the description mentions.
  8. (C11) Clearer names under one NRL_COMPILE_CACHE_ prefix, and move cache_sync.sh next to compile_cache.py so its path needs no setting.
  9. (C12) Add the guide, including the interface a Kubernetes/S3 setup can implement.
  10. (C14) License header and copyright year.

PR description: it describes five commits, including a VLLM_RAY_BUNDLE_INDICES patch, a worker start-up hook, serve_vllm_on_ray.py --apply-patches-only, docs/guides/persistent-jit-caches.md and test_vllm_compile_cache.py. The branch has three commits and none of those. Please update it.

Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4715-shared-cache-load.html

Generated by Claude Code

Comment thread nemo_rl/utils/compile_cache.py Resolved
Comment thread nemo_rl/utils/compile_cache.py Resolved
Comment thread tools/external_gym_vllm/README.md Resolved
Comment thread tools/external_gym_vllm/cache_sync.sh Resolved
Comment thread nemo_rl/utils/compile_cache.py Resolved
Comment thread tools/external_gym_vllm/cache_sync.sh Resolved
Comment thread tools/external_gym_vllm/cache_sync.sh Resolved
@DanBlanaru
DanBlanaru force-pushed the dblanaru/persistent-compile-caches branch 2 times, most recently from 331c6dd to 3f05c9c Compare November 3, 2026 15:07
@DanBlanaru

Copy link
Copy Markdown
Contributor Author

Re: C16: kept rsync with the cheap fixes, as discussed on Slack; shipping the cache in the image is the longer-term plan.

DanBlanaru and others added 7 commits November 9, 2026 08:49
External vLLM pools (judges, reward models) compile their kernels into the
container's ~/.cache and /tmp, so every job compiles them again. A pool with
<POOL>_CACHE_DIR (on the shared filesystem) now copies that directory to
node-local disk on every node before Ray starts, points vLLM, TorchInductor,
Triton and FlashInfer at the copy (re-applied after the pool's ENV_VARS), and
writes new entries back in the background. New tools/external_gym_vllm/
cache_sync.sh (seed, writeback, sidecar): write-back only adds files that are
new and unchanged for a minute, never replaces one, and takes turns on a lock.
CACHE_FORCE_FRESH=1 starts empty but still writes back.

Large multi-node job with three judge pools (vLLM 0.25.1): judges ready
about 417 -> 345 s after job start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DanBlanaru <[email protected]>
…orkers

The vLLM and Megatron workers compile their kernels into the container's
~/.cache and /tmp, so every job compiles them again. With NRL_CACHE_SYNC,
NRL_COMPILE_CACHE_ROOT and NRL_LOCAL_COMPILE_CACHE set, each worker type uses
its own cache, copied onto a node once (cache_sync.sh seed-once, under a
node-local lock): the vLLM actor waits for this node's copy of the policy
cache before creating its engine, and the Megatron worker copies its cache in
the background (its kernels are first compiled at the first training step).
NRL_<PREFIX>_<VAR> points each type's TorchInductor, Triton, FlashInfer, CUDA
and FlashAttention-4 caches at its copy; NRL_VLLM_CACHE_ROOT_PER_ENGINE=0
keeps VLLM_CACHE_ROOT the same across jobs. A launcher starts the policy copy
in its node setup, so it overlaps Ray start-up, and writes back with
cache_sync.sh sidecar-types. Nothing changes unless configured.

Large multi-node job, warm (vLLM 0.25.1, our fork): setup 426 s with the
policy copy started at node setup vs 432 s with the workers copying in the
background, and 667 s when every node copies both caches before Ray starts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DanBlanaru <[email protected]>
…ds a cache

With <POOL>_CACHE_DIR set, every node of a replica copies the pool's compile
cache before Ray starts, and the head node publishes its IP only after its
copy. The other nodes waited 120 s for that IP; on a warm multi-node job with
a 1.8 GB cache the head finished ~3 min after the second node, which gave up
and ended the job. Wait up to 15 min while a cache is configured.

The write-back loops also stop if `sleep` fails (e.g. the container is gone)
instead of spinning.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DanBlanaru <[email protected]>
… fixes

- Two variables turn the caches on (NRL_COMPILE_CACHE_SHARED_DIR,
  NRL_COMPILE_CACHE_LOCAL_DIR); compiler folders default under
  <local>/<type>; NRL_COMPILE_CACHE_<TYPE>_DIR overrides one type's
  shared folder. Types are vllm and megatron.
- cache_sync.sh moves next to compile_cache.py, which finds it there;
  a missing script, flock or rsync fails at start.
- vLLM engines keep their own folder (main's per-engine name), filled
  from the node's copy; write-back merges them into vllm/vllm.
- Before an engine compiles, one Ray task per node of its placement
  group seeds that node, so multi-node engines are covered.
- The cache variables reach vLLM's workers through _apply_vllm_patches.
- Copy-in failures only warn; the seed mark is dated at the copy's
  start; write-back remembers what it sent; the last pass waits for
  the lock.
- docs/guides/persistent-jit-caches.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DanBlanaru <[email protected]>
vLLM before 0.31.1 loads compiled artifacts from the absolute path stored
in its cache index, so an engine could not load a cache another engine
wrote into its own folder (Ultra warm run: "No such file or directory"
under another engine's vllm_engines/<n>). The per-node task now applies
the upstream fix to the node's vLLM before the engine starts. Remove once
vLLM >= 0.31.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DanBlanaru <[email protected]>
The Megatron workers compile kernels during setup, not at the first
training step, so a background copy could land after them: those kernels
missed the cache and their recompiled copies were written back as new
files (Ultra warm run: 4,871 new Megatron files). The workers now wait for
the copy, which the launcher can start before Ray so the wait is short.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DanBlanaru <[email protected]>
The backport's RuntimeError only fired in a job with persistent caches on;
test_relative_artifact_backport checks a hand-written file, so it kept
passing when vLLM changed. Add an anchor test next to the other source
patches' (marked vllm) that applies it to the installed
compilation/compiler_interface.py. new_snippet becomes a plain string so
write_unpatched_copy can read it out of the patch source.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DanBlanaru <[email protected]>
@DanBlanaru
DanBlanaru force-pushed the dblanaru/persistent-compile-caches branch from 3f05c9c to 65e4072 Compare November 9, 2026 16:51
terrykong
terrykong previously approved these changes Nov 9, 2026
@terrykong
terrykong enabled auto-merge (squash) November 9, 2026 17:38
@terrykong

Copy link
Copy Markdown
Collaborator

/ok to test 65e4072

@terrykong
terrykong disabled auto-merge November 9, 2026 22:44
@terrykong

terrykong commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

disabling automerge as we're discussing internally the viability of offering this in the image to greatly simplify the mechanism

(edit: mistakenly disabled, i've re-enabled i think the plan is still to land this one and circle back with a docker image solution)

test_worker_initialization_preserves_only_restored_rng runs
MegatronPolicyWorkerImpl.__init__ against a hand-built namespace, so the
new compile_cache.seed("megatron") call raised NameError. Provide it and
check the constructor seeds the Megatron cache once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DanBlanaru <[email protected]>
Comment thread nemo_rl/utils/compile_cache.py Resolved
@terrykong

Copy link
Copy Markdown
Collaborator

/ok to test f490151

@terrykong
terrykong merged commit 8248b6a into main Nov 10, 2026
94 checks passed
@terrykong
terrykong deleted the dblanaru/persistent-compile-caches branch November 10, 2026 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants