skip to content

With the Hugging Face Hub cache, how do you stop batch containers re-downloading a 15 GB model at every start and run them offline?

level: seniorimportance: nice to knowfreq 9%

answer

  1. where do the downloaded bytes land?
  2. HF_HOME versus HF_HUB_CACHE
  3. refs, blobs and snapshots
  4. warm once, then HF_HUB_OFFLINE=1
  5. hf cache ls and hf cache prune

basics

~20 s

Mount one volume as HF_HUB_CACHE in every container, fill it once with snapshot_download or hf download at a pinned commit, then run the workers with HF_HUB_OFFLINE=1 so they read only the cache and never call the Hub.

solid answer

~40 s

`huggingface_hub` caches downloads under `HF_HUB_CACHE`, which defaults to `$HF_HOME/hub` (`~/.cache/huggingface/hub`); in a fresh container that path is empty, so every start downloads again. Mount one volume as `HF_HUB_CACHE` (not the whole `HF_HOME`, which also holds the token file) and warm it in a single job with `snapshot_download` or `hf download` at a pinned commit. Run the workers with `HF_HUB_OFFLINE=1`: no HTTP calls are made, not even the freshness check an online `hf_hub_download` performs for a cached file on a branch, and a missing file raises an error. Set these variables before `huggingface_hub` is imported, because it reads them at import time. Current huggingface_hub (2.1) raises `IncompleteSnapshotError` when an offline snapshot is known to be missing files, instead of returning a partial folder. The cache never evicts on its own, so schedule `hf cache prune`.

code

bash · 10 lines
bash
# Warm-up job: network access and a token, run once per model version
export HF_HUB_CACHE=/mnt/hf-cache/hub
hf download your-org/your-model \
  --revision "$MODEL_SHA" \
  --include "*.json" --include "*.safetensors"

# Worker containers: the same volume, no calls to the Hub
export HF_HUB_CACHE=/mnt/hf-cache/hub
export HF_HUB_OFFLINE=1
python run_batch.py --model your-org/your-model --revision "$MODEL_SHA"

go deeper

for a junior

Recall that downloads are cached under HF_HOME, by default in ~/.cache/huggingface/hub, and that HF_HUB_OFFLINE=1 makes the libraries use only cached files.

for a middle

Explain the cache layout of refs, blobs and snapshots, the HF_HUB_CACHE default, and why environment variables must be set before import.

for a senior

Design the warm-once, run-offline pattern for containers at a pinned commit, predict which errors appear when the warm-up missed a file, and keep the volume from filling up.

for a principal

Choose where model bytes live for a fleet: a shared volume, a baked image or per-node caches, trading start-up time, disk, token exposure and how updates roll out.

## Where the cache lives `huggingface_hub` is the download layer under Transformers, Datasets and the other Hugging Face Python libraries, so their downloads from the Hub share one cache. Its location comes from environment variables: | Variable | Default | Holds | |---|---|---| | `HF_HOME` | `~/.cache/huggingface` (or `$XDG_CACHE_HOME/huggingface` when that is set) | The root: hub cache, token, Xet data, assets | | `HF_HUB_CACHE` | `$HF_HOME/hub` | Model, dataset and Space repos | | `HF_TOKEN_PATH` | `$HF_HOME/token` | The token saved by `hf auth login` | | `HF_XET_CACHE` | `$HF_HOME/xet` | Xet shard cache and upload staging | A `cache_dir` argument on a single call overrides the location for that call. All of these variables are **read when `huggingface_hub` is imported**; changing `os.environ` later in the same process has no effect. ## What is inside a cached repo Each repo gets a folder such as `models--your-org--your-model` with: - **`refs/`**: one file per branch or tag you used, holding the commit it last resolved to. - **`blobs/`**: the actual file contents, named by hash. - **`snapshots/<commit>/`**: one folder per commit, whose entries are symlinks into `blobs/`, so an unchanged file is stored once across revisions. - **`trees/`**: the cached file list for each commit, which lets a re-run of `snapshot_download` on a fully cached commit cost a single network call, the one that resolves a branch or tag to a commit. The paths the download functions return point into `snapshots/`. Never edit those files; that corrupts the cache. ## Sharing one cache across containers 1. **Mount a volume** at the same path in every container and set `HF_HUB_CACHE` to it. Share the hub cache, not all of `HF_HOME`, unless you mean to share the token file too. 2. **Warm it once** in a job that has network access and, for gated or private repos, a token: `snapshot_download` or `hf download` at a pinned commit hash, filtered to the files the workers load. 3. **Run workers offline** with `HF_HUB_OFFLINE=1`. The libraries then make no HTTP calls and use only cached files. `huggingface_hub` also honours the older `TRANSFORMERS_OFFLINE` variable, and Transformers accepts `local_files_only=True` per call. 4. **Load by the same commit** the warm-up used. An unpinned offline load resolves `main` through `refs/main`, which is whatever the warm-up recorded. Downloads take per-file locks under the cache's `.locks` folder, so two processes filling the same cache do not write one blob at the same time. If the volume is a network share used from different operating systems, symlinks created on one may not work on another; `HF_HUB_DISABLE_SYMLINKS=1` stores real copies instead, at the cost of duplicating large files. ## What fails, loudly or quietly | Symptom | Likely cause | |---|---| | Every container start downloads the model | The cache path sits inside the container's own ephemeral filesystem | | A worker raises an error for a file offline | The warm-up's patterns excluded it, or the warm-up used another commit | | `IncompleteSnapshotError` offline | The cached file list for that commit names files missing from the snapshot | | Offline mode seems ignored | The variable was set after `huggingface_hub` was imported | | The volume fills up | Old revisions are never evicted automatically | The `IncompleteSnapshotError` behaviour is recent: older clients returned the partial folder silently, and the error relies on the cached file list for that commit. Files excluded by `allow_patterns` or `ignore_patterns` are not counted as missing. ## Housekeeping - `hf cache ls` lists cached repos; `--revisions` lists each snapshot, and filters such as `size>1GB` or `accessed>30d` narrow it. - `hf cache prune` deletes revisions no branch or tag points to any more, leftover `.incomplete` files from interrupted downloads, and shared blobs no repo uses. A snapshot warmed by commit hash may have no branch or tag pointing at it, so check `hf cache ls --revisions` before pruning a cache your workers depend on. - `hf cache rm` deletes chosen repos or revisions; `--dry-run` previews and `--yes` skips the prompt for scripts. - `hf cache verify` checks cached files against the Hub's checksums for a revision. `hf cache ls`, `rm` and `prune` replaced the older `hf cache scan` and `hf cache delete` commands in `huggingface_hub` v1.0.

  • A worker sets os.environ["HF_HUB_OFFLINE"] = "1" at the top of its script, after its imports, yet still calls the Hub. Why?
    `huggingface_hub` reads its environment variables once, at import time. If Transformers or `huggingface_hub` was imported before the assignment, offline mode was already decided as off. Set the variable in the container's environment, or before the first import, or pass `local_files_only=True` to each load.
  • How do you reclaim space on a shared cache after several model updates without breaking the current version?
    Run `hf cache ls --revisions` to see snapshots and their refs, then `hf cache prune`, which removes revisions no branch or tag points to plus leftover `.incomplete` downloads. Be aware that a revision you pinned by commit hash may have no ref; check it before pruning, or remove specific old revisions with `hf cache rm` and `--dry-run` first.

  • Sharing the whole HF_HOME between jobs is harmless because it only holds model files.
  • Setting os.environ['HF_HUB_OFFLINE'] after importing huggingface_hub turns offline mode on.
  • The Hub cache evicts old revisions automatically when the disk fills up.
  • With HF_HUB_OFFLINE=1, a file missing from the cache is fetched from the Hub as a fallback.
  • Pointing several containers at one cache volume corrupts it, because concurrent downloads are never coordinated.

Assumes huggingface_hub 2.1 under Transformers v5: hf cache ls, rm and prune replaced the older cache commands in huggingface_hub v1.0, and an incomplete offline snapshot now raises IncompleteSnapshotError where older clients returned it silently.