Saving, loading and pushing to the Hub

Hugging Face Transformers · Practical · 10 min

A model you cannot reload or share reproducibly is not finished.

Every model, tokenizer and config has save_pretrained(dir) and from_pretrained(dir). A saved directory contains config.json, model.safetensors (sharded with an index above a size threshold), tokenizer files and the generation config. Always save the tokenizer with the model; a model directory without its tokenizer is a common deployment bug.

With Trainer:

  • trainer.save_model("final") writes the model plus the processing_class.
  • Intermediate checkpoints (checkpoint-500/) also include optimizer, scheduler and RNG state for resuming; save_total_limit=2 keeps disk usage bounded.

Sharing on the Hub:

  • Log in with hf auth login (or set HF_TOKEN).
  • model.push_to_hub("you/my-model") and tok.push_to_hub("you/my-model") upload to a repo, creating it if needed; private=True keeps it private.
  • TrainingArguments(push_to_hub=True, hub_model_id=...) pushes checkpoints during training, and trainer.push_to_hub() uploads the final model with an auto-generated model card listing hyperparameters and metrics.

Edit the model card (README.md) to describe the training data, intended use and limitations. Set id2label in the config before saving so pipelines show readable labels.

To reload, point any from_pretrained or pipeline(model=...) at the local path or repo id.

Example

from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline

name = "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(name)
tok = AutoTokenizer.from_pretrained(name)

model.save_pretrained("my-sentiment")
tok.save_pretrained("my-sentiment")       # keep tokenizer next to weights
# my-sentiment/: config.json, model.safetensors, tokenizer.json, ...

clf = pipeline("text-classification", model="my-sentiment")
print(clf("Reloaded fine.")[0]["label"])  # POSITIVE

# after `hf auth login`:
# model.push_to_hub("your-name/my-sentiment", private=True)
# tok.push_to_hub("your-name/my-sentiment", private=True)

Remember

  • save_pretrained/from_pretrained round-trip models, tokenizers and configs
  • Checkpoints include optimizer state for resuming; final saves need only the model and tokenizer
  • push_to_hub publishes to a repo; document it in the model card

Watch out for

  • Saving only the model and later loading a different tokenizer, which silently produces wrong token IDs.

Check yourself

  1. What does a Trainer checkpoint-1000/ folder contain that a plain save_pretrained folder does not?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: huggingface.co/docs/transformers/model_sharing