Installation
Requirements
- Python 3.10+ (3.11 / 3.12 recommended)
- pip (or uv / conda)
- Disk: the Hub cache grows—leave several GB
- GPU optional: classification and tiny generation run on CPU; full fine-tunes prefer CUDA / MPS
Tensors and torch.device are covered in this site’s PyTorch tutorial.
Virtual environment
Install PyTorch
Install a PyTorch build that matches your machine before Transformers, so pip does not pull the wrong CUDA wheel:
Install the Transformers stack
The official starter set:
Add as needed:
Verify:
Log in: hf auth login
huggingface_hub ships the hf CLI. Public ungated models download without login. Uploads, private repos, and some datasets need a token.
Authorize in the browser or paste an Access Token (write scope only if you push_to_hub). Check identity:
Or use an env var (CI / scripts; never commit it):
huggingface-cli login may still work; current docs use hf auth login.
GPU and load tips
On a consumer GPU, large models typically load like this:
You can finish this course without a GPU: use DistilBERT / DistilGPT2 / Whisper-tiny and omit device_map or set it to cpu. Full-parameter 7B fine-tuning is not a beginner exercise.
Cache and networks
The default cache is ~/.cache/huggingface (override with HF_HOME). Repeated from_pretrained calls hit disk.
If Hub downloads stall, use a proxy or a documented mirror endpoint. Never paste a token into a notebook you will publish.
Suggested layout
Minimum requirements.txt: torch, transformers, datasets, evaluate, accelerate.
Troubleshooting
hf is not recognized?
pip install huggingface_hub, then reactivate the venv.
Downloads hang?
Check the network; switch to this course’s ungated tiny models; HF_HUB_ENABLE_HF_TRANSFER=1 sometimes helps.
CUDA out of memory?
Lower per_device_train_batch_size, use LoRA, or switch to 0.5B / DistilBERT. For casual local chat, try Ollama.
Windows login wants Git?
Pass --no-add-to-git-credential, or install Git for Windows.