PushToHub#
- class PushToHub(repo_id, checkpoint='model_best', weights_only=True, private=True, token=None, revision=None, name=None, on_train_end=True, on_best=False, every_n_epochs=None, every_n_steps=None, background=True, squash_history=True, squash_every_n_pushes=None)[source]#
Bases:
HookBasePush model checkpoints to the Hugging Face Hub during and/or after training.
Entirely opt-in. By default it uploads the raw
model_bestcheckpoint once at the end of training (weights_only=True), so it loads back byte-identically viaweight=hf://<repo>/model_best.pth; setweights_only=Falseto push a consolidatedpimm exportartifact instead. For cross-cluster sync it can also push periodically: withon_best=Trueit pushesmodel_bestinafter_stepwhenever the best metric improves, and withevery_n_epochsset it pushes the rolling checkpoint inafter_epochevery N epochs. The final end-of-training push happens inafter_train, which also drains any in-flight background uploads. Registered asPushToHub.- Parameters:
repo_id (str) – Target Hub repo, e.g.
"youngsm/sonata-pilarnet-L".checkpoint (str) – Which checkpoint to push at end of training, e.g.
"model_best"or"last"/"model_last"(the latter resolve to the newest rolling checkpoint). Defaults to"model_best".weights_only (bool) – If
True, upload the raw checkpoint file; ifFalse, build and push a consolidatedpimm exportartifact. Defaults toTrue.private (bool) – Create the repo as private if it does not exist. Defaults to
True.token (str, optional) – Hugging Face token; falls back to the ambient credential when
None. Defaults toNone.revision (str, optional) – Target branch/revision for uploads. Defaults to
None.name (str, optional) – Destination filename in the repo for raw uploads; defaults to the source file’s basename when
None. Defaults toNone.on_train_end (bool) – Push
checkpointinafter_train. Defaults toTrue.on_best (bool) – Push
model_bestinafter_stepwheneverbest_metric_valueimproves. Defaults toFalse.every_n_epochs (int, optional) – Push the rolling checkpoint in
after_epochevery N epochs.Nonedisables. Defaults toNone.every_n_steps (int, optional) – Push the rolling checkpoint in
after_stepevery N global steps – use this to match an iteration-oriented saver likeCheckpointSaverIteration(set it to the saver’ssave_freq). The step counter is seeded from resumed progress inbefore_train.Nonedisables. Defaults toNone.background (bool) – Run periodic raw uploads in a background thread so they never block the training step (uploads to the same path are serialized). The final
after_trainpush is always blocking. Defaults toTrue.squash_history (bool) – Squash the repo’s git/LFS history once in
after_train(after the final push) to reclaim orphaned LFS blobs. Cheap server-side metadata op; irreversible (drops history). Defaults toTrue.squash_every_n_pushes (int, optional) – Also squash after every N successful periodic pushes (
on_best/every_n_epochs) so a long run does not accumulate unbounded LFS storage before it ends.Nonedisables mid-run squashing. Defaults toNone.
Note
Uploads run on rank 0 only, and failures are logged but never crash the run. Place
PushToHubafter the checkpoint saver in thehookslist so the files it reads are already written. Repeatedly uploading large checkpoints to the same path accumulates orphaned LFS blobs in the repo’s history;squash_history(end of run) andsquash_every_n_pushes(mid run) reclaim that storage viasuper_squash_history.Example
Add to
cfg.hooksafter the checkpoint saver; by default it uploadsmodel_bestto the Hub once at the end of training:hooks = [ dict(type="CheckpointSaver"), dict(type="PushToHub", repo_id="youngsm/sonata-pilarnet-L", private=True), ] # → in after_train (rank 0, blocking) uploads # <save_path>/model/model_best.pth to # hf://youngsm/sonata-pilarnet-L/model_best.pth; with on_best=True it # also pushes model_best in after_step whenever best_metric_value # improves, and every_n_epochs=N pushes the rolling ckpt in after_epoch