Introduction to Hugging Face

What is Hugging Face?

Hugging Face is both a community and an open-source stack. Two pieces matter most:

  • Hub: a collaboration platform for models, datasets, and Spaces (hosted demos), with 1M+ public checkpoints.
  • Transformers: the model-definition framework for text, vision, audio, video, and multimodal models—inference and training. One definition can plug into PyTorch loops, Accelerate, and engines such as vLLM / TGI.

This course centers on Transformers + Hub, and introduces datasets, evaluate, accelerate, and peft.


Ecosystem

┌─────────────────────────────────────────────────────────────┐
│                   Hugging Face ecosystem                     │
└─────────────────────────────────────────────────────────────┘
   Hub (models / datasets / Spaces / model cards)
           │  from_pretrained / push_to_hub
   ┌───────┴────────┐
   │  Transformers  │  definitions + Pipeline + Trainer + generate()
   └───────┬────────┘
     datasets   evaluate   accelerate   peft   huggingface_hub
ComponentRoleTypical use
TransformersModel classes, tokenizers, Pipeline, TrainerLoad checkpoints, infer, fine-tune
Hub“App store” for weights and dataSearch, download, publish, Spaces
datasetsCached / streamed datasetsload_dataset, map
evaluateMetricsaccuracy, F1
accelerateDevices and distributed runsdevice_map="auto", multi-GPU
peftParameter-efficient fine-tuningLoRA / QLoRA
huggingface_hubHub client and hf CLIhf auth login, uploads

Three core classes

Officially a checkpoint is three objects, assembled with AutoClass + from_pretrained:

ClassJobOn disk
PreTrainedConfigDepth, hidden size, vocab, …config.json
PreTrainedModelWeights and forward passmodel.safetensors
PreprocessorTokenizer / ImageProcessor / Processortokenizer.json, …
from transformers import AutoConfig, AutoTokenizer, AutoModelForCausalLM

name = "distilbert/distilgpt2"
config = AutoConfig.from_pretrained(name)
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForCausalLM.from_pretrained(
    name, device_map="auto", dtype="auto"
)

device_map="auto" shards layers across GPU/CPU; dtype="auto" picks the checkpoint’s float type. You do not need to write attention from scratch.


Three official paths

GoalAPI
Fast inferencepipeline
Training / fine-tuningTrainer (or a custom loop + Accelerate)
LLM continuation / chatmodel.generate()

vs training from scratch and vs Ollama

This site’s PyTorch course teaches tensors, nn.Module, and handwritten loops—building networks from scratch. Transformers is load a pretrained checkpoint, then infer or fine-tune. They complement each other: if loss.backward() is unclear, go back to PyTorch; if you need BERT or Qwen from the Hub, stay here.

TransformersOllamaScratch PyTorch
On-rampThree-line PipelineOne command local chatYou write the loop
StrengthResearch, fine-tune, multimodal, custom headsLocal Chat UI / APIFully custom architectures
CostYou manage deps and VRAMGGUF (and similar) formatsYou own data and compute

Pick: chat only → Ollama; fine-tune, eval, publish, vision/audio → Transformers.

Coding agents (Cursor, Claude Code, …) can use Hugging Face Skills to run hf or TRL jobs. That is optional, not the main path of this tutorial.


Good fit / caveats

Good fit: text classification / NER, generation and chat, ASR, image classification, reproducing Hub paper checkpoints, LoRA on domain data.

Watch out: full-finetuning a 7B on a laptop GPU; shipping weights without reading the card; gated Llama-style repos you never accepted; committing tokens.

Readers who need strong Chinese support can prefer Qwen and DeepSeek (often ungated). Generation examples use distilgpt2 and Qwen/Qwen2.5-0.5B-Instruct.


Next steps

评论