skip to content

A Hugging Face training job loaded different weights on Tuesday than Monday from the same repo id — why, and how do you pin it?

level: middleimportance: must knowfreq 24%

answer

  1. a repo id is not a version
  2. revision defaults to the main branch
  3. branch, tag, refs/pr/N or commit
  4. only a full commit hash is immutable
  5. record the resolved sha with the run

basics

~20 s

A Hub repo is a git repo, and an unpinned load follows the main branch, which moves whenever the author pushes. Pin revision to a full 40-character commit hash and record that hash with the run's configuration.

solid answer

~40 s

Every model on the Hugging Face Hub is a git repository, and `from_pretrained`, `hf_hub_download` and `snapshot_download` all take a `revision` that defaults to `"main"`. A branch is a moving pointer, so Monday's run and Tuesday's run resolved `main` to different commits after the author pushed new weights. `revision` also accepts a tag, a pull-request ref such as `refs/pr/3`, or a commit hash, and only the full commit hash is immutable: a tag can be deleted and re-created, and the download guide requires the full-length hash rather than a 7-character short one. So resolve the commit once (for example `HfApi().model_info(repo_id, revision="main").sha`), pass that hash as `revision` to the tokenizer and the model alike, and store it with the run's parameters so the run can be repeated.

code

python · 13 lines
python
from huggingface_hub import HfApi
from transformers import AutoModelForCausalLM, AutoTokenizer

REPO = "your-org/your-model"

# Resolve the moving branch once and keep the full commit hash
sha = HfApi().model_info(REPO, revision="main").sha

# Every component loads from the same commit
tokenizer = AutoTokenizer.from_pretrained(REPO, revision=sha)
model = AutoModelForCausalLM.from_pretrained(REPO, revision=sha)

print(f"loaded {REPO} at {sha}")  # write this into the run's config

go deeper

for a junior

Recall that a Hub repo is a git repo and that loads default to the main branch. Know the four revision kinds: branch, tag, pull-request ref and commit hash.

for a middle

Explain why main moves, why only a full commit hash is immutable, and how to resolve and pass one hash to every component of a load.

for a senior

Show how unpinned loads drift silently, including offline machines resolving main from their own cache, and make the resolved hash part of every run's recorded parameters.

for a principal

Weigh convenience against reproducibility across a team: when following main is acceptable for experiments, and how pinned hashes make a model update a deliberate, reviewable change.

## Why the weights changed A repository on the Hugging Face Hub is a **git repository** with large files stored behind it. There are model repos, dataset repos and **Spaces** (a third repo type holding a demo app built with Gradio, Docker or static HTML, which rebuilds on every push); all of them version their files with commits, branches and tags. The loading calls (`from_pretrained` in Transformers, `hf_hub_download` and `snapshot_download` in `huggingface_hub`) take a `revision` argument. When you leave it out, they use the **`main` branch**. A branch is a pointer that moves forward every time the repo's author pushes a commit. So the same repo id on two days can mean two different commits: - On Monday, `main` pointed at commit `aaaa…`, and your job cached and loaded those weights. - On Tuesday morning the author pushed retrained weights, `main` now points at `bbbb…`, and an online load followed it. - Nothing in your code changed, and no error was raised. The cache simply gained a second snapshot folder. ## What `revision` accepts | Revision kind | Example | Can it change under you? | |---|---|---| | Branch | `main`, `test-branch` | Yes, on every push | | Tag | `v1.0` | Not by itself, but the owner can delete it and create it again on another commit | | Pull-request ref | `refs/pr/3` | Yes, while the pull request receives commits | | Commit hash | the full 40-character hash | **No**, a commit's content is fixed | Two details trip people up. First, `huggingface_hub`'s download guide says a commit hash **must be the full-length hash**, not the 7-character short form git prints. Second, a tag is a convenience label, not a guarantee: `HfApi` has both `create_tag` and `delete_tag`, so a tag can be moved by recreating it. ## Pinning in practice 1. **Resolve once.** Ask the Hub which commit a branch or tag points to right now, for example `HfApi().model_info(repo_id, revision="main").sha`, or browse the repo's commit history. 2. **Pass the hash everywhere.** A tokenizer load and a model load are separate calls, and each resolves `main` on its own; a push between them can mix files from two commits. One hash passed to both closes that gap. 3. **Record it.** Store the repo id and the hash with the run's other parameters, so a rerun months later pulls the same bytes. 4. **Move the pin on purpose.** Updating to a newer commit becomes a reviewed change in your code, not something that happens to you overnight. ## What the cache does with an unpinned load `huggingface_hub` keeps a `refs/` folder inside each cached repo. The file `refs/main` holds the commit that `main` resolved to the last time this machine was online. That has consequences: - **Online and unpinned:** each download call contacts the Hub to check whether the file is still current, and follows `main` to its newest commit. - **Offline and unpinned** (`HF_HUB_OFFLINE=1`): no HTTP call is made, so `main` resolves from that machine's own `refs/main`. Two machines whose caches were filled on different days load different commits, silently. - **Pinned to a hash:** the snapshot folder is named after the commit, so the cached files are found directly and every machine agrees. ## Pull-request refs and remote code - To try a change someone proposed before it is merged, pass `revision="refs/pr/<number>"`. The Hub stores pull requests as refs on the source repo rather than as forks, and the `from_pretrained` docstring suggests exactly this to test one. - When a repo ships its own modeling code and you load it with `trust_remote_code=True`, the Transformers documentation recommends pinning a commit for that reason too: the code you reviewed is the code that runs. Why executing downloaded code or pickled weights is dangerous is a supply-chain question and is not covered here. ## A short checklist - Never ship a job whose model reference is only a repo id. - Prefer a full commit hash over a tag when reproducibility matters. - Pin the tokenizer, the config and any adapter to the hash you pinned the weights to. - Log the resolved hash at startup so a surprising result can be traced to the exact files.

  • How do you test a fix that someone opened as a pull request on a Hub model repo, before it is merged?
    Pass `revision="refs/pr/<number>"` to `from_pretrained` or to the `huggingface_hub` download calls. The Hub stores each pull request as a ref on the source repo, so you get the proposed files without the change touching `main`. Once you are happy with it, pin the resulting commit hash rather than the PR ref, because the ref moves as more commits land.
  • Why can two machines running the same unpinned job with HF_HUB_OFFLINE=1 still load different weights?
    Offline mode makes no HTTP calls, so `main` is resolved from each machine's own cache entry `refs/main`, which records whatever commit that machine last fetched online. Caches filled on different days point at different commits. Pinning a full commit hash removes the ambiguity, because the snapshot folder is named after that commit.
  • Is pinning a tag such as v1.0 good enough for a reproducible training run?
    It is better than `main`, but not airtight. A tag does not move on its own, yet the repo owner can delete it and create it again on another commit, since `HfApi` exposes both `delete_tag` and `create_tag`. For a run that must be repeatable, resolve the tag to its full commit hash and record that.

A repo id with no revision is like a dependency declared as "latest": every install can resolve to something new. A full commit hash is the lockfile entry; a tag is a friendly name someone can repoint.

  • The same repo id always means the same weights, because the Hub does not version models.
  • Pinning a tag such as v1.0 is exactly as safe as pinning a commit hash.
  • A 7-character short commit hash works as a revision, just as it does in git.
  • Setting HF_HUB_OFFLINE=1 freezes main at the same commit on every machine.
  • Only the model weights need a pinned revision; the tokenizer can follow main.