AI Hints
Home›Fine-Tuning LLMs Tutorial›Sharing and Serving Your Model
Fine-Tuning LLMs · Lesson 97 of 120

Push to the Hub

Share your model.

⏱ 2 min read▶ Video at 2:27:52🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

Our model lives in a folder on one laptop. To use it on a server, or share it with a team, we upload it to the Hugging Face Hub.

  • A folder on one laptop
  • Use it on a server
  • Share it on the Hub

Adapter or merged?

First, what exactly would we upload? We save the adapter alone. Then we merge it into the base model, and save the full model. Then we list the files, with their sizes. Run it. The adapter is under twenty megabytes. The merged model is over five hundred. That's a big difference.

app.pypython
import os
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel

NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
tok = AutoTokenizer.from_pretrained(NAME)
model = PeftModel.from_pretrained(AutoModelForCausalLM.from_pretrained(NAME), "ticket-lora")
model.save_pretrained("upload/adapter")                      # the small adapter
model.merge_and_unload().save_pretrained("upload/merged")    # a full, merged model
tok.save_pretrained("upload/merged")
for folder in ["upload/adapter", "upload/merged"]:
    print(folder)
    for name in sorted(os.listdir(folder)):
        size = os.path.getsize(f"{folder}/{name}") / 1e6
        print(f"   {name:26} {size:8.2f} MB")
Output
upload/adapter
   README.md                      0.01 MB
   adapter_config.json            0.00 MB
   adapter_model.safetensors     19.59 MB
upload/merged
   chat_template.jinja            0.00 MB
   config.json                    0.00 MB
   generation_config.json         0.00 MB
   model.safetensors            538.09 MB
   tokenizer.json                 3.52 MB
   tokenizer_config.json          0.00 MB
Also uses _setup.py
import shutil
shutil.copytree('/Users/hussain/Docker Complete Course/shared-data/ft_cache/ticket-lora', 'ticket-lora')

Pushing to the Hub

Here's how to upload. First, HF auth login stores your access token. Then push to hub uploads the adapter, to a repository under your name. Private equals true keeps it hidden. We push the tokenizer too, so the repository has everything it needs.

push.shbash
hf auth login                       # paste a token from huggingface.co/settings/tokens

python - <<'PY'
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel

NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
model = PeftModel.from_pretrained(AutoModelForCausalLM.from_pretrained(NAME), "ticket-lora")
model.push_to_hub("your-name/ticket-lora", private=True)        # adapter only, ~20 MB
AutoTokenizer.from_pretrained(NAME).push_to_hub("your-name/ticket-lora", private=True)
PY

Loading it anywhere

Now anyone with access can load it. They load the base model as usual. Then PEFT downloads our adapter from the Hub, and attaches it. Two lines, on any machine.

load.pypython
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel

base = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")
model = PeftModel.from_pretrained(base, "your-name/ticket-lora")   # downloads the adapter
tok = AutoTokenizer.from_pretrained("your-name/ticket-lora")

Summary

Let's recap. An adapter is small, a merged model is big. Push to hub uploads either one. Use private equals true, until you're ready to share. And PEFT loads adapters straight from the Hub. Next, serving with v LLM.

Key takeaways
  • Adapter: ~20 MB
  • push_to_hub(private=True)
  • Load from the Hub
Next up: Serving with vLLM — a fast model server