C.W.K.
Stream
Lesson 06 of 06 · published

Saving, Loading, and push_to_hub

~25 min · transformers, hub, save

Level 0Scout
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Symmetry: save_pretrained / from_pretrained

Every PreTrainedModel, PreTrainedTokenizer, and processor in the library has the same pair: save_pretrained(path) writes config + weights + tokenizer files to a directory, and from_pretrained(path or repo_id) loads them. Local paths and Hub repo ids are interchangeable; the loader sniffs which one you passed.

This symmetry is the reason the entire ecosystem composes. A LoRA trainer outputs a directory; you pass that directory to AutoPeftModelForCausalLM; you pass the merged directory to push_to_hub(); the next person calls from_pretrained on your repo id. End-to-end: same two methods.

push_to_hub: the underrated half

Both model.push_to_hub("yourname/your-repo") and tokenizer.push_to_hub(...) create or update a Hub repo. The first call creates with a default README; subsequent calls update. create_repo() from huggingface_hub gives you the explicit control (private, exist_ok, organization, etc.).

For weight uploads, prefer the safetensors format (default since transformers 4.34). It's safer (no pickle), faster (memory-mapped), and smaller (no Python overhead).

Code

Save locally then push to Hub·python
from transformers import AutoModelForCausalLM, AutoTokenizer

repo = "Qwen/Qwen2.5-1.5B-Instruct"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo)

# Local save (round-trippable)
local = "./qwen-1.5b-local"
tok.save_pretrained(local)
model.save_pretrained(local, safe_serialization=True)

# Round-trip
tok2 = AutoTokenizer.from_pretrained(local)
model2 = AutoModelForCausalLM.from_pretrained(local)

# Push to Hub (need a write token; private=True for private repo)
# tok.push_to_hub("yourname/qwen-mirror", private=True)
# model.push_to_hub("yourname/qwen-mirror", private=True)
Explicit repo creation + selective upload·python
from huggingface_hub import HfApi, create_repo

repo_id = "yourname/qwen-mirror"

# Idempotent
create_repo(repo_id, repo_type="model", private=True, exist_ok=True)

api = HfApi()
api.upload_folder(
    folder_path="./qwen-1.5b-local",
    repo_id=repo_id,
    repo_type="model",
    commit_message="Initial mirror",
    ignore_patterns=["*.bin"],  # we have safetensors; skip duplicates
)

External links

Exercise

Take a small public model, save it locally, then push it to a private repo on your account using upload_folder. Then load the model back from the Hub repo id (not the local path) to verify the round-trip. Inspect the repo on the web: which files exist, what shows on the model card.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.