Skip to content

hf: serialize cached fetches and resolve checkpoint shards from the index - #985

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-sa-q7a-hf-cache
Oct 29, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-sa-q7a-hf-cache

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

HF 캐시의 동시 fetch를 직렬화하고, checkpoint shard를 index의 weight_map에서 찾도록 고쳤습니다.

What and why

Two processes fetching the same file could resume from the same partial download. Fetch, pre-hash and offline marker reads now take one per-destination cross-process lock, held from the state check through verify and publish, so a second caller finds the finished file. A cache destination named *.lock is refused before any sidecar or parent folder is created, so a payload can never replace a lock file.

A checkpoint's required shards were not read from its index, so shards in subfolders (for example tensors/model-00001-of-00021.safetensors) were missed and offline resolve could fail on them. The shard list now comes from the validated, non-empty weight_map, deduplicated. Resolve refuses, naming the path, a shard missing from the listing, or in offline mode missing its body or verified marker. A malformed index, a non-string shard value or an unsafe relative path is refused at the same boundary. Existing directory symlinks still resolve.

The model picker's download estimate and header read use the same shard list once the index is cached, so unused shards in the listing no longer count as downloads or send a cached checkpoint back to ranged header reads. Without a cached index the estimate stays the upper bound over every listed shard. The header copy of a checkpoint keeps each shard's subfolder. The top-level .json and .jinja rule has one owner, used by both the candidate list and the manifest.

Release notes: Fixed: concurrent downloads of the same Hugging Face file no longer collide, and checkpoints whose shards live in subfolders resolve from the cache.

Code shape

  • No second copy. The shard list and the lock live in crates/hf/src/fetch.rs and crates/hf/src/lib.rs, which every caller already goes through. Folder::weight_map in crates/safetensors/src/lib.rs is the one parser of the index, shared by Folder::open and resolve. The existing cache_path, exact, digest and marker publishing helpers are reused.
  • Model code holds only architecture facts. No model file changed; the common HF owner gained no per-model branch.
  • Dead code is gone. Nothing was made unused.
  • No silent failure. Every case above is a named error.
  • Comments state what is true now.
  • No relaxed gate. No band, tolerance or lint moved. The caller fixture in crates/gpu-gates/src/model_file/fetch.rs now writes a valid weight_map index instead of arbitrary bytes.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line): the two copies of the top-level config rule in crates/hf/src/lib.rs (checkpoint_files and checkpoint_manifest) are now one checkpoint_config.

What I ran

Tests of the cached-index, config and lock changes, on commit 02666d84b79fcce20f756afebdd55dac5e327fcc (base 90c9f3055b75b99c6e5972e2297b16c922562500), on an Apple silicon Mac:

CARGO_BUILD_JOBS=4 cargo test -q -p bloomery-hf --lib availability_ -- --nocapture
CARGO_BUILD_JOBS=4 cargo test -q -p bloomery-hf --lib checkpoint_manifest_ -- --nocapture
CARGO_BUILD_JOBS=4 cargo test -q -p bloomery-hf --lib fetch_refuses_reserved_lock_destinations -- --nocapture
cargo check --target x86_64-unknown-linux-gnu -p bloomery-gpu-gates --features hf --lib
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 151 filtered out
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 151 filtered out
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 152 filtered out
Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.69s

Output of each test with its fix taken out follows. First, with the cached-index branch of row_files replaced by the candidate list:

catalog::tests::availability_uses_the_cached_checkpoint_manifest panicked at crates/hf/src/catalog.rs:1532:9:
  left: Present { download_bytes: 1700 }
 right: Present { download_bytes: 0 }

Next, with the .lock check disabled:

a cache entry cannot replace a lock: ".../reserved-lock/cache/a/b/weights/x.safetensors.lock"

Last, with the .jinja rule dropped from the shared config check:

called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }

Earlier tests of the fetch lock and the shard list, on commit 5609a89b339b99f744a4bdcc69e69893684d8278 (base c7b6420554d8c903a583824e147099c6f715e5a9), on a DGX Spark (NVIDIA GB10, driver 595.84):

CARGO_BUILD_JOBS=4 cargo test -p bloomery-hf --lib fetch::tests -- --test-threads=4
bash tools/gate.sh -p bloomery-gpu-gates --features hf --lib -- model_file::fetch::tests --nocapture --test-threads=1
test result: ok. 46 passed; 0 failed; 0 ignored; 0 measured; 101 filtered out
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 195 filtered out

The new tests there are concurrent_fetches_share_one_download (two processes overlap on one destination; one download, matching digest and marker) and checkpoint_manifest_requires_its_shards_online_and_offline (subfolder shards selected, unneeded shards skipped, missing body, marker or listing refused).

Real binary on a DGX Spark, commit dcd4cb72bd2316d556e11c800d639ea6b7cced2c (base fe680eed0feb81b8924bacdcac825096a77a3210), resolving the already cached nvidia/Qwen3.8-Flash-Next-FP4-Local checkpoint offline, with no downloads. The cache holds the 30 files the index lists, 21 of them shards under tensors/. Card line from nvidia-smi --query-gpu=name,driver_version,memory.total --format=csv,noheader: NVIDIA GB10, 595.84, [N/A]. The release build used cargo oxide build --arch sm_121a -- --release -p bloomery-gpu-gates --features glm5next,clef,mimo2,solar2,minimaxm3,flint,gemma-embedding2,hf,checkpoint --bin bloomery-serve, then:

HF_HUB_OFFLINE=1 BLOOMERY_DRAFT=off bloomery-serve \
  --hf nvidia/Qwen3.8-Flash-Next-FP4-Local@b9ce18a6887e18c089fdea8509a33e63429a3300 \
  --plan --place a --ctx-size 4096 --no-mmproj
Cargo build succeeded
CACHE-RESOLVE existing-files=30 downloads=0
hf offline repo=nvidia/Qwen3.8-Flash-Next-FP4-Local@b9ce18a6887e18c089fdea8509a33e63429a3300 reason=HF_HUB_OFFLINE is set
hf checkpoint repo=nvidia/Qwen3.8-Flash-Next-FP4-Local@b9ce18a6887e18c089fdea8509a33e63429a3300 files=30 bytes=102258927843
hf file file=tensors/model-00001-of-00021.safetensors bytes=5000643504 state=offline-cached from=5000643504
hf file file=tensors/model-00021-of-00021.safetensors bytes=2700016864 state=offline-cached from=2700016864
CACHE-RESOLVE PASS downloads=0

The --plan run loads no tensors and starts no server.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 29, 2026

@github-actions github-actions Bot 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.

Verdict: CLEAN: No blocking defect found in the changed cache locking or checkpoint resolution paths.

Checked

Duplicates: 18 new functions checked; no duplicate owner found. Dryer reported zero pairs; the changed gpu-gates code was checked manually.

Complexity: resolve_drafts 20→25 adds manifest orchestration; fetch_to 28→32 adds publication guards; verified_entries 13→16 adds disappearing-file guards. Manifest selection and locking remain shared decisions.

Silent failure: Six cache absence/error sites followed. Unverified files remain unavailable, malformed manifests return named errors, and missing required shards prevent resolution.

Tests (static mutation inspection): In crates/hf/src/fetch.rs:436, dropping locks at 436/897/961 fails download-count or wait assertions; dropping the suffix check at 1034 or read-only fallback at 1061 fails refusal or cached-read assertions. Reversing publication at 525/526 or replacing atomic marker publication at 1024 with an in-place write fails file-presence or inode assertions.

Tests (continued): In crates/hf/src/lib.rs:482, dropping required-shard checks, selecting unused shards at 485, or dropping Jinja selection at 463 fails manifest assertions. Removing parsing/path guards at crates/safetensors/src/lib.rs:479 fails invalid-map assertions. Bypassing the cached manifest at crates/hf/src/catalog.rs:635 fails byte estimates. Flattening paths or bypassing validation at crates/model-hf/src/remote.rs:514 fails folder-open or path-error assertions. Changed fixtures retain selection and checkpoint-support assertions.

Gates: No threshold, tolerance, lint level or test was removed or relaxed.

Performance: Load-time fetching and planning only; no decode, prefill or kernel path changed.

Escape catalog: No C1–C4 pattern matched.

Notes

Tests were inspected, not executed. The body supplies host fetch tests and a load-free plan run, but no just gate-* card run with output.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/model_file/fetch.rs, crates/gpu-gates/src/model_file/pick.rs and carries no gate run on a card (CONTRIBUTING's evidence list); a maintainer adds gpu-verified after one.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

Filed outside this PR: #986

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 7f04223 into midagedev:main Oct 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu The card engine: crates/gpu*, crates/app model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja needs-gpu Changes code a card must check, and carries no gate run on a card

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant