Skip to content

fix(checkpoint_db): resolve cached Hugging Face checkpoints without runtime uv - #458

Open
kane-vln wants to merge 1 commit into
mainfrom
kane-vln/bug-inference-requires-uv-at-run-time-and-fails
Open

kane-vln wants to merge 1 commit into
mainfrom
kane-vln/bug-inference-requires-uv-at-run-time-and-fails

Conversation

@kane-vln

@kane-vln kane-vln commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #416 (related: #417).

Previously, CheckpointFileHf._download and CheckpointDirHf._download in cosmos_framework/utils/checkpoint_db.py unconditionally spawned uv run ... hf download --format=json, even when HF_HUB_OFFLINE=1 was set and every required checkpoint (Cosmos3-Edge, Cosmos-Guardrail1, processor, Wan2.2_VAE.pth) was already present in the local Hugging Face cache. In runtime containers or Python environments without uv on PATH, inference failed immediately with FileNotFoundError: [Errno 2] No such file or directory: 'uv'.

Key Changes

  • Live Hugging Face environment resolution (cosmos_framework/utils/checkpoint_db.py):
    • Dynamically evaluates HF_HUB_OFFLINE / TRANSFORMERS_OFFLINE for offline routing, cache-root precedence (HF_HUB_CACHE > HUGGINGFACE_HUB_CACHE > <HF_HOME>/hub > <XDG_CACHE_HOME>/huggingface/hub > ~/.cache/huggingface/hub), endpoint precedence (HUGGINGFACE_CO_STAGING / HF_ENDPOINT), and live token environment variables (HF_HUB_DISABLE_IMPLICIT_TOKEN / HF_TOKEN / HUGGING_FACE_HUB_TOKEN) per call without mutating os.environ. Two upstream huggingface_hub SDK import-time limitations remain and are documented in the helper docstrings: if huggingface_hub was initially imported with HF_HUB_OFFLINE=1, its in-process HTTP sessions keep OfflineAdapter mounted; and when no token environment variable is set, huggingface_hub.utils.get_token() falls back to huggingface_hub.constants.HF_TOKEN_PATH as resolved at import time.
    • Lazy-imports huggingface_hub and requests inside helper functions so cosmos_framework.utils.checkpoint_db keeps minimal top-level import overhead.
  • Strict path, revision, ref, filter, tree-manifest, and pre-/post-transfer symlink validation:
    • Rejects empty segments, leading /, \, drive prefixes, . / .. traversal, invalid CACHEDIR.TAG entries, symlinked .locks / models--* / refs/ / trees/ / blobs/ / .no_exist/ / snapshots/ / snapshots/<sha> containers or entries, and file symlinks resolving outside the repository's cache (blobs/ or snapshots/<sha>/), validating existing cache write destinations (including negative-cache .no_exist marker paths) before online transfers execute while preserving legitimate relative Hugging Face blob pointer paths, ordinary existing .no_exist marker files, and trailing-slash directory patterns.
    • Validates trees/<commit_hash>.json (format_version == 1, relative file entries, integer sizes, non-empty blob_id) and guards against zero-match include filters.
  • In-process offline & pinned-SHA resolution:
    • Resolves explicit offline requests (HF_HUB_OFFLINE=1 / TRANSFORMERS_OFFLINE=1) completely in-process with zero uv or network calls, enforcing trees/<sha>.json completeness when available and preserving legacy cache OR-filter, *.safetensors.index.json shard completeness, singleton literal include, and selected dangling/external symlink checks.
    • Short-circuits online pinned 40-char SHA hits in-process when completeness is provable (CheckpointFileHf, validated trees/<sha>.json, or singleton literal include).
  • Two-stage online preflight, uv-optional transfer fallback, & exact snapshot verification:
    • Separates online commit resolution (HfApi.repo_info(..., files_metadata=False) / get_hf_file_metadata with 40-hex X-Repo-Commit validation) from commit-pinned inventory listing (HfApi.list_repo_files(..., revision=commit_hash)) so partial caches (config.json-only) cannot pass on HTTP 401 or transient connection errors, and once a mutable ref resolves to a newer SHA, connectivity fallback only accepts provably complete snapshots of that resolved SHA rather than stale local refs.
    • Discovers uv from the active Python environment (sys.executable / sys.prefix scripts dir -> uv.find_uv_bin() -> PATH), uses a same-invocation startup marker immediately before huggingface_hub.cli.hf:main so uv bootstrap failures fall back to in-process huggingface_hub (snapshot_download / hf_hub_download) without retrying real hf errors, and verifies both pre-transfer cache destinations and post-transfer materialization at the exact expected snapshots/<commit_hash> path before updating refs/<revision> atomically.
  • Regression suite (cosmos_framework/utils/checkpoint_db_test.py):
    • Adds 27 [pytest.mark.L0, pytest.mark.CPU] unit and staged hf_cli integration tests isolated from huggingface_hub import order and host uv installations, including online pre-transfer symlink-destination and staged 404 EntryNotFound .no_exist regressions against both the real Hub SDK and the locked hf CLI.

Upstream Sync Note

  • cosmos_framework/utils/checkpoint_db.py is mapped in .file_mapping.json (imaginaire/utils/checkpoint_db.py) and should be mirrored/coordinated with upstream sync ownership as appropriate.

Verification

  • ruff check cosmos_framework/utils/checkpoint_db.py cosmos_framework/utils/checkpoint_db_test.py
  • ruff format --check cosmos_framework/utils/checkpoint_db.py cosmos_framework/utils/checkpoint_db_test.py
  • pyrefly check -c pyrefly.toml cosmos_framework/utils/checkpoint_db.py cosmos_framework/utils/checkpoint_db_test.py (pyrefly==0.55.0, 0 errors)
  • Standalone minimal --noconftest CPU runner with uv package installed: 27 passed (0 skipped)
  • pytest cosmos_framework/utils/checkpoint_db_test.py tests/hf_cli_resources_test.py (29 passed, 0 skipped with staged IMAGINAIRE_HF_CLI_ENVIRONMENT)
  • pytest --skip-mapped-non-gpu-tests cosmos_framework/utils/checkpoint_db_test.py (27 passed)
  • Cross-module suite (cosmos_framework/utils/checkpoint_db_test.py, tests/hf_cli_resources_test.py, cosmos_framework/utils/safe_unpickle_test.py in both forward and reverse orders: 51 passed)

@kane-vln
kane-vln force-pushed the kane-vln/bug-inference-requires-uv-at-run-time-and-fails branch from f9df960 to 456719b Compare December 4, 2026 12:07
@kane-vln
kane-vln marked this pull request as ready for review December 5, 2026 05:08

@lfengad lfengad 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.

Could you confirm an online cold-cache download through CheckpointDirHf.download() in the supported processor runtime, with its normal Python environment and staged hf_cli configuration? On a cache miss the new code imports huggingface_hub and resolves metadata in the calling interpreter before invoking the locked CLI. Previously the CLI environment handled that lookup. This matters for callers that use the HF class directly. A test or CI job that exercises this path with an empty cache, together with the caller-side SDK version, would settle my compatibility question before approval.

lfengad
lfengad previously approved these changes Dec 5, 2026

@lfengad lfengad 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.

I checked the dependency lock and the integration tests after my earlier question. The caller environment pins huggingface-hub 0.36.2. The staged test exercises caller-side metadata lookup followed by a real CLI transfer of a missing weights file, and the SDK fallback test downloads a previously uncached revision against the mock Hub. That addresses my compatibility question for the standard locked runtime. No additional run is needed for this review.

@yaoxu-crypto
yaoxu-crypto self-requested a review December 5, 2026 17:07
@kane-vln
kane-vln force-pushed the kane-vln/bug-inference-requires-uv-at-run-time-and-fails branch from 2ad6249 to 32947fb Compare December 6, 2026 07:53
…and verify inventory sizes

Resolves cached Hugging Face checkpoints in-process without requiring uv at run time, with cache/symlink validation and online fallback, and preserves expected file sizes from trees/<commit_hash>.json so both offline resolution and pinned-revision online cache shortcuts reject truncated files and mixed partial snapshots repair in a single online download.

Signed-off-by: Kane <153349620+kane-vln@users.noreply.github.com>
@kane-vln
kane-vln force-pushed the kane-vln/bug-inference-requires-uv-at-run-time-and-fails branch from 32947fb to 0f4e388 Compare December 6, 2026 07:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Inference requires uv at run time and fails with FileNotFoundError, even with every checkpoint cached

2 participants