Skip to content

build: export uv cache via a BuildKit cache mount; add opt-in STRIP_UV_CACHE - #4340

Merged
kajalj22 merged 13 commits into
mainfrom
kajalj/uv-cache-mount
Oct 26, 2026
Merged

kajalj22 merged 13 commits into
mainfrom
kajalj/uv-cache-mount

Conversation

@kajalj22

@kajalj22 kajalj22 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Moves the uv package-download cache onto a BuildKit cache mount (--mount=type=cache) instead of a plain directory, mirroring the pattern already used for the TRT-LLM ccache/wheel-cache in this Dockerfile. Content on a cache mount is never part of any image layer.
  • Since a cache mount is a different filesystem than the install targets, UV_LINK_MODE=hardlink installs materialize a plain same-filesystem copy of the cache at UV_CACHE_DIR (/opt/nemo_rl_cache/uv) at the start of each uv-writing RUN (dependency install, TRT-LLM build, Dynamo install, worker-venv prefetch phase 2, Gym prefetch), and sync new downloads back to the cache mount via a bash EXIT trap before that RUN's layer commits.
  • Adds a uv-cache-export/uv-cache stage pair that republishes the cache mount's contents as its own small registry image, keyed by sha256(BASE_IMAGE-UV_VERSION-HYBRID_EP_MULTINODE) (mirroring the TRT-LLM cache tag scheme), so the next build can still warm-start from it. _build_container.yml's "Resolve uv cache configuration"/"Resolve uv cache seed" steps compute and existence-check that tag before wiring it in, and "Update uv cache image" refreshes it on main after each build.
  • A new STRIP_UV_CACHE build-arg (default off) controls whether the materialized cache ships in the final image. Off by default, so main/nightly builds keep the cache in UV_CACHE_DIR, preserving the RUNTIME_UID's ability to run uv run --extra <backend> fully offline. Size-sensitive builds (e.g. nemotron, built elsewhere) can pass --build-arg STRIP_UV_CACHE=1 to delete the materialized cache from every uv-writing layer instead.

Goal

Stop the uv download cache from being duplicated across every layer of the image by construction (the actual root cause of the nightly image bloat), while giving callers an explicit choice between keeping the cache in the final image (default, needed for offline installs) or stripping it out for a smaller image — with zero change to install behavior (hardlink sharing across venvs), the registry-backed warm-start cache for future builds, or GitHub Actions' separate BuildKit registry layer-cache (cache-from/cache-to).

Measured image size impact

H100 release image, compressed registry size (docker buildx imagetools inspect --raw, summed layer + config sizes):

Build Size (bytes) Size (GB)
Default (STRIP_UV_CACHE off, uv cache kept) 46,216,845,028 ~46.22 GB
--build-arg STRIP_UV_CACHE=1 31,000,346,462 ~31.00 GB
Delta −15,216,498,566 −15.22 GB (−32.9%)
  • Default-path size is unchanged vs current main (46,216,845,028 vs 46,238,403,941 bytes on main), confirming this PR introduces no regression in the default (cache-kept) path.
  • STRIP_UV_CACHE=1 removes the uv download cache (prebuilt wheel downloads + unpacked archive payloads) from every image layer, since it's only needed during the dependency-install build step, not at runtime — cutting the shipped image by about a third. This option is intended for size-sensitive builds that don't need fully-offline uv run --extra <backend>.

Test plan

  • docker buildx build --target uv-cache -f docker/Dockerfile . builds successfully standalone
  • docker buildx build --target release -f docker/Dockerfile . (default args) — confirm UV_CACHE_DIR is present and owned by RUNTIME_UID, and uv run --extra <backend> works offline as the runtime user
  • docker buildx build --target release --build-arg STRIP_UV_CACHE=1 -f docker/Dockerfile . — confirm UV_CACHE_DIR is empty/absent in the resulting image
  • Confirm hardlink sharing still works: compare docker run --rm <image> du -sh /opt/nemo_rl_venv /opt/ray_venvs/* before/after this PR
  • CI: build-container / build-container-gb200 pass with UV_BUILD_CACHE=enabled
  • tests/unit/tools/test_docker_hybridep_build.py::test_docker_images_build_deepep_with_multinode_hybridep passes
  • CI on main: Update uv cache image step succeeds and refreshes the keyed :uv-cache-<arch>-<key> tag
  • A subsequent build warm-starts from the refreshed seed (check uv sync logs for cache hits)

🤖 Generated with Claude Code

The uv cache is written to a plain persisted directory in the hermetic
stage, so it rides all the way into the shipped release image (~9GB
amd64 / ~5GB arm64 of dead build-time cache with zero runtime use).

Move it to a BuildKit cache mount, same pattern TRT-LLM's ccache/wheel
cache already use: the cache never lands in any image layer, and a new
uv-cache-export stage (mirroring trtllm-ccache-export) republishes it
as its own scratch image for the next build's warm start. Update the
uv-cache refresh job accordingly -- it now builds the uv-cache target
directly instead of docker cp-ing /root/.cache/uv out of the shipped
release image.

Signed-off-by: Kajal Jain <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Sep 29, 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.

@kajalj22 kajalj22 added the CI:L0 Run doctests and unit tests label Sep 29, 2026
@github-actions github-actions Bot added the CI Relating to CI label Sep 29, 2026
@kajalj22

Copy link
Copy Markdown
Contributor Author

/ok to test eebf511

@kajalj22

Copy link
Copy Markdown
Contributor Author

/review

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The separate cache-export target is a reasonable approach, but the cache-mount conversion is incomplete and breaks the existing hardlink-based image layout. There is also a deterministic regression in the Megatron lockfile workflow caused by the duplicate UV_VERSION default. These should be fixed before merging. Review was static; container builds and CI tests could not be executed in this read-only environment.

Review generated by codex

Comment thread docker/Dockerfile Resolved
Comment thread docker/Dockerfile Resolved
Comment thread docker/Dockerfile Resolved
Follow-up to the review findings on this PR (hardlink breakage across
the mount boundary, cache not shared with the TRT-LLM/Dynamo/Gym RUNs,
and a duplicate ARG UV_VERSION default breaking prepare-megatron-lock).

- Retarget the uv cache mount to /root/.uv-cache-store (self-seeded via
  from=uv-cache-seed, mirroring trtllm-wheel-cache) and materialize a
  plain /root/.cache/uv copy at the start of every RUN that invokes uv
  (dependency install, TRT-LLM build, Dynamo install, worker-venv
  prefetch phase 2, Gym prefetch), synced back and deleted via a bash
  EXIT trap before each RUN's layer commits. This keeps hardlink-based
  installs working exactly as before (same filesystem throughout each
  RUN) while still never shipping the download cache itself.
- Drop the now-unnecessary CACHE_KEY/.cache-key comparison machinery --
  BASE_IMAGE/UV_VERSION/TARGETARCH are now part of the cache mount id
  itself, so a version or base-image bump naturally gets a fresh cache
  instead of relying on a manually-computed key.
- Fix the duplicate ARG UV_VERSION=0.11.28 default in uv-cache-export by
  redeclaring it bare (inheriting base's value), matching the existing
  pattern the hermetic stage already uses.
- Move uv-cache-seed resolution into _build_container.yml with an
  existence check (docker buildx imagetools inspect), mirroring
  resolve_seed() for the TRT-LLM caches, instead of unconditionally
  wiring an image ref that may not exist yet.

Still not build-tested (no docker daemon available) -- this is exactly
what the PR's test plan checklist covers before merge.

Signed-off-by: Kajal Jain <[email protected]>
@kajalj22

Copy link
Copy Markdown
Contributor Author

/ok to test 42a1544

@kajalj22

Copy link
Copy Markdown
Contributor Author

/review

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The cache export is now separated from the release image, and the temporary in-layer cache addresses cross-mount hardlinking. Two issues remain: registry seeds bypass the previous compatibility check, and removing CACHE_KEY breaks an existing unit test. Review was static; Docker builds and tests were not executed.

Review generated by codex

Comment thread docker/Dockerfile Resolved
Comment thread docker/Dockerfile Resolved
Addresses the second automated review round on this PR plus the
L0_Unit_Tests_Other CI failure from the last push.

- Key the uv registry cache seed tag by BASE_IMAGE/UV_VERSION/
  HYBRID_EP_MULTINODE (sha256, mirroring the TRT-LLM ccache/wheel-cache
  tags), via a new "Resolve uv cache configuration" step in
  _build_container.yml. Previously the seed was a flat, unversioned
  :uv-cache tag, so a base-image bump would create a fresh local cache
  mount (new id) but still import a registry seed built against the
  old base image -- exactly the staleness the removed CACHE_KEY check
  used to catch, just via a different mechanism.
- Promote HYBRID_EP_MULTINODE from a hardcoded `hermetic`-stage ENV to
  a global ARG (default 1), redeclared bare wherever the cache mount
  id needs it (hermetic already had BASE_IMAGE/UV_VERSION; now release
  and uv-cache-export do too). The ENV line itself stays a literal
  `ENV HYBRID_EP_MULTINODE=1` -- tests/unit/tools/test_docker_hybridep_build.py
  asserts that exact line.
- That same test asserted the now-removed `CACHE_KEY=` line existed
  and referenced BASE_IMAGE/UV_VERSION/HYBRID_EP_MULTINODE. Updated it
  to check the equivalent invariant on the new mount `id=` line
  instead, preserving the single-node-vs-multi-node DeepEP wheel
  safety check it exists for. Verified locally (no pytest needed --
  the test only reads the Dockerfile as text).

Signed-off-by: Kajal Jain <[email protected]>
@kajalj22

Copy link
Copy Markdown
Contributor Author

/ok to test 9d743b5

@kajalj22

Copy link
Copy Markdown
Contributor Author

/review

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The cache export is now separated from the release image, and the materialized working cache preserves same-filesystem hardlink installs. One configuration mismatch remains: overriding HYBRID_EP_MULTINODE makes the build and exporter use different cache mounts. The updated text-based regression test does not cover this behavior. Review was static; Docker builds and tests were not executed.

Review generated by codex

Comment thread docker/Dockerfile Resolved
Addresses an unaddressed review comment (discussion on
docker/Dockerfile:645): the hermetic stage's ENV HYBRID_EP_MULTINODE=1
was a hardcoded literal, which takes precedence over the ARG for
${...} expansion in that stage and everything derived from it
(hermetic, release). uv-cache-export (FROM base, no such ENV)
correctly expanded the real ARG value. So a non-default
--build-arg HYBRID_EP_MULTINODE=0 build would populate its cache
mount under id ...-1 (from the 5 writer RUNs) but export/tag under
...-0 -- silently exporting an empty/stale cache under a key that
never matches what was actually built. No current CI path sets this
build-arg (both sides already agree at the "1" default), but it's a
real bug, not a hypothetical.

Fix: ENV HYBRID_EP_MULTINODE=${HYBRID_EP_MULTINODE} (pass-through),
matching how BASE_IMAGE/UV_VERSION are already handled. Updated
tests/unit/tools/test_docker_hybridep_build.py's literal-line
assertion to match (docker/Dockerfile.ngc_pytorch, which has no
cache-mount-id mechanism, keeps the old literal). Also hardened the
test's cache-mount check: it previously used next() to inspect only
the FIRST of 6 identical --mount=type=cache,id=uv-cache-... lines, so
a future copy-paste drift in any of the other 5 wouldn't have been
caught -- now every occurrence is checked and their id= segments must
all be identical. Verified locally (no pytest needed, pure text
assertions against the real Dockerfile).

Signed-off-by: Kajal Jain <[email protected]>
@kajalj22

Copy link
Copy Markdown
Contributor Author

/ok to test 24d82e7

@kajalj22

Copy link
Copy Markdown
Contributor Author

/review

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No merge-blocking defects found in the revised change. The uv cache mount identity is consistent across writers and export, the registry seed is keyed by architecture and build configuration, and same-layer hardlink installs are preserved before cache cleanup. The redundant UV_VERSION default is also avoided.

Reviewed the changed files and relevant installation/prefetch callers against the base tree. Validation was static only; Docker builds and tests were not executed. End-to-end checks should still confirm release image size, retained hardlink sharing, and warm-start behavior on a fresh builder.

Review generated by codex

@kajalj22
kajalj22 marked this pull request as ready for review September 30, 2026 19:20
chtruong814
chtruong814 previously approved these changes Sep 30, 2026
Pulls in 594 commits from main, including PR #4456 which independently
moved the uv cache to /opt/nemo_rl_cache/uv for RUNTIME_UID offline
installs. Reconciles the two designs: the BuildKit cache mount now
materializes into UV_CACHE_DIR instead of /root/.cache/uv, and a new
STRIP_UV_CACHE build-arg (default off) lets size-sensitive builds opt
into deleting the materialized cache from the final image while
keeping it for main/nightly.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Signed-off-by: Kajal Jain <[email protected]>
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Signed-off-by: Kajal Jain <[email protected]>
@kajalj22

Copy link
Copy Markdown
Contributor Author

/ok to test e8e82c2

@kajalj22

Copy link
Copy Markdown
Contributor Author

/review reset=true

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 19, 2026 21:19

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review: PR #4340

The separate cache-export target and configuration-keyed registry seed are useful improvements. The earlier non-default HybridEP mismatch is fixed by the ARG-to-ENV passthrough.

A fresh review of all four changed files found one stripped-build size regression requiring correction and one regression-test suggestion, detailed inline.

Validation was static only: Docker builds, pytest, and pre-commit were not executed. Findings carry this reviewer's source verification only; suggested edits and acceptance checks require author verification.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex

Comment thread docker/Dockerfile Resolved
@kajalj22

Copy link
Copy Markdown
Contributor Author

/ok to test 2320d60

@kajalj22

Copy link
Copy Markdown
Contributor Author

/review reset=true

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review: PR #4340

No action needed — no high-confidence actionable findings were established in this fresh static review.

The latest review findings are resolved in the current source:

  • The default-retention test now rejects nonempty argument defaults and checks the helper’s cache deletion, including a nonempty-match assertion.
  • The shared shell helper has the required copyright and license header.

Inspected all six changed files and traced the directly affected cache lifecycle, build/export workflow, installation, prefetch, and regression-test paths. Earlier ownership-handoff, mount-identity, and nightly-dependency corrections remain present.

Validation was source-only. Docker builds, pytest, pre-commit, and workflow execution were not performed; this result carries only this reviewer’s source verification.

Review generated by codex

Review generated by codex. This was a full review. Request a full review with /review reset=true.

@kajalj22 kajalj22 added the CI:L1 Run doctests, unit tests, and functional tests label Oct 22, 2026
chtruong814
chtruong814 previously approved these changes Oct 22, 2026
@terrykong terrykong removed the CI:L0 Run doctests and unit tests label Oct 23, 2026
terrykong
terrykong previously approved these changes Oct 23, 2026
@terrykong
terrykong enabled auto-merge (squash) October 23, 2026 17:44
@terrykong

Copy link
Copy Markdown
Collaborator

/ok to test 5aaa616

Signed-off-by: Kajal Jain <[email protected]>

# Conflicts:
#	docker/Dockerfile
@kajalj22
kajalj22 dismissed stale reviews from terrykong and chtruong814 via cb5a03e October 26, 2026 03:19
@kajalj22

Copy link
Copy Markdown
Contributor Author

/ok to test cb5a03e

@kajalj22
kajalj22 enabled auto-merge (squash) October 26, 2026 03:26
@kajalj22

Copy link
Copy Markdown
Contributor Author

/review

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review: PR #4340

No action needed — no qualifying findings in this follow-up.

The previously verified fixes remain present: the retention regression test rejects nonempty defaults and checks helper deletions, and the shared helper retains its license header and guarded stripping behavior.

Compared all six changed files with the previous reviewed head. Five are byte-identical; the Dockerfile adds system-library installation inside the existing Gym-prefetch gate. Three delegated inspections and parent verification covered the affected prefetch, cache, workflow and regression-test contracts without establishing an introduced regression.

Validation was source-only. Docker builds, pytest, pre-commit and workflows were not executed. Gym submodule source was unavailable, so its import-time behavior was not independently verified.

Review generated by codex

Review generated by codex. This was a findings-only review. Request a full review with /review reset=true.

@kajalj22
kajalj22 disabled auto-merge October 26, 2026 17:09
@kajalj22
kajalj22 merged commit 26d52d9 into main Oct 26, 2026
255 of 260 checks passed
@kajalj22
kajalj22 deleted the kajalj/uv-cache-mount branch October 26, 2026 17:10
jthomson04 added a commit to jthomson04/RL that referenced this pull request Oct 26, 2026
Resolves the docker/Dockerfile conflict with NVIDIA-NeMo#4340 (uv cache mounts). The
BUILD_DYNAMO RUN keeps this branch's form: docker/dynamo/install.sh now only
downloads etcd and nats-server, so it is not a uv-writing RUN and takes no
uv cache mount.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: jthomson04 <[email protected]>
kajalj22 added a commit that referenced this pull request Oct 28, 2026
main (#4340) replaced the update-uv-cache jobs with an in-build cache mount
and seed export, so this branch drops its workflow change and takes main's.

- Prune right after `uv_cache_restore force`, before any sync; the RUN's
  `uv_cache_persist prune` trap mirrors the result into the cache mount.
- Dynamo keeps its throwaway UV_CACHE_DIR, so its RUN no longer needs the
  cache mount or the restore/persist helpers.
- Placement test and docs updated for the new flow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Kajal Jain <[email protected]>
kajalj22 added a commit that referenced this pull request Oct 28, 2026
main (#4340) replaced the update-uv-cache jobs, so retarget the test at
the new mechanism: both builds use the uv-cache-test tag prefix, and the
"Update uv cache image" step also runs on this PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Kajal Jain <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests CI Relating to CI Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants