Set up the environment
After this lesson you have a working Python environment with the libraries we'll use for the rest of the track, and you've verified PyTorch can see your GPU.
Tracks 0 and 1 built the understanding. Track 2 turns it into a working fine-tune you write yourself — no platform, nothing hidden. We'll use the standard open-source stack: PyTorch plus Hugging Face's libraries. This lesson gets that stack installed and verified so the next eight lessons just run.
What you need
- Python 3.10+ and pip.
- A GPU for the training lessons (a single consumer card is plenty for SmolLM2-135M). You can read along on CPU; training will just be slow.
- Disk space for the model (~0.3 GB) and a cache directory.
Create an isolated environment
Always work in a virtual environment (venv) so this project's packages don't collide with anything else on your machine.
# create and activate a venv
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install --upgrade pip
Install the libraries
Each library has one job: torch is the tensor/autograd engine; transformers gives us models, tokenizers, and the Trainer; peft provides LoRA; trl adds SFT helpers; datasets handles data; accelerate manages device placement under the hood.
# install torch first, matching your CUDA (see pytorch.org for the exact command)
pip install torch
# then the Hugging Face stack
pip install transformers peft trl datasets accelerate
A note on versions
This track is written against recent releases (transformers 5.x, peft 0.18.x, trl 1.x). APIs drift between major versions — where a call has changed recently, the lessons flag it. If something errors on a name, check the library's changelog for a rename.
Verify your setup
Run this once. It confirms the imports work and tells you whether PyTorch can see a GPU.
import torch, transformers, peft
print("torch:", torch.__version__)
print("transformers:", transformers.__version__)
print("peft:", peft.__version__)
print("CUDA available:", torch.cuda.is_available())
if torch.cuda.is_available():
print("GPU:", torch.cuda.get_device_name(0))
If CUDA available prints True and you see your GPU's name, you're ready. If it prints False but you have a GPU, your torch build is CPU-only — reinstall torch with the CUDA build from pytorch.org. With the environment working, the next lesson loads the model we'll fine-tune.
Key idea
The whole by-hand workflow is: torch (math) + transformers (model & Trainer) + peft (LoRA) + datasets (data). Everything in this track is a few dozen lines on top of these.
Key terms
- venv
- An isolated Python environment so project packages don't collide with the system.
- torch (PyTorch)
- The tensor and autograd engine that runs the model and backprop.
- transformers
- Hugging Face library providing models, tokenizers, and the Trainer.
- peft
- Parameter-Efficient Fine-Tuning library; provides LoRA.
- trl
- Hugging Face library with SFT/preference training helpers.
- accelerate
- Handles device placement and distributed details under Trainer.
- CUDA availability
- Whether PyTorch can use the GPU (torch.cuda.is_available()).
Check yourself
Answers are saved to this browser.